-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBaseRewardPoolV2.sol
364 lines (295 loc) · 10.9 KB
/
BaseRewardPoolV2.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
import "@openzeppelin/contracts/utils/math/Math.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
import "./Dependencies/EqbConstants.sol";
import "./Interfaces/IBaseRewardPoolV2.sol";
import "./Interfaces/IPendleBooster.sol";
import "@shared/lib-contracts-v0.8/contracts/Dependencies/TransferHelper.sol";
contract BaseRewardPoolV2 is IBaseRewardPoolV2, AccessControlUpgradeable {
using SafeERC20 for IERC20;
using TransferHelper for address;
address public booster;
uint256 public pid;
IERC20 public stakingToken;
address[] public rewardTokens;
uint256 public constant duration = 7 days;
uint256 private _totalSupply;
mapping(address => uint256) private _balances;
struct Reward {
uint256 periodFinish;
uint256 rewardRate;
uint256 lastUpdateTime;
uint256 rewardPerTokenStored;
uint256 queuedRewards;
}
struct UserReward {
uint256 userRewardPerTokenPaid;
uint256 rewards;
}
mapping(address => Reward) public rewards;
mapping(address => bool) public isRewardToken;
mapping(address => mapping(address => UserReward)) public userRewards;
mapping(address => uint256) public userLastTime;
mapping(address => uint256) public userAmountTime;
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
function initialize(address _owner, address _booster) public initializer {
require(_booster != address(0), "invalid _booster!");
__AccessControl_init();
booster = _booster;
_grantRole(DEFAULT_ADMIN_ROLE, _owner);
_grantRole(EqbConstants.ADMIN_ROLE, _booster);
emit BoosterUpdated(_booster);
}
function setParams(
uint256 _pid,
address _stakingToken,
address _rewardToken
) public override {
require(
hasRole(DEFAULT_ADMIN_ROLE, msg.sender) || msg.sender == booster,
"!auth"
);
require(
address(stakingToken) == address(0),
"params have already been set"
);
require(_stakingToken != address(0), "invalid _stakingToken!");
require(_rewardToken != address(0), "invalid _rewardToken!");
pid = _pid;
stakingToken = IERC20(_stakingToken);
addRewardToken(_rewardToken);
}
function setParams(
uint256 _pid,
address _stakingToken,
address _rewardToken,
address _eqbZap
) external override {
require(_eqbZap != address(0), "invalid _eqbZap!");
setParams(_pid, _stakingToken, _rewardToken);
_grantRole(EqbConstants.ZAP_ROLE, _eqbZap);
}
function addRewardToken(address _rewardToken) internal {
require(_rewardToken != address(0), "invalid _rewardToken!");
if (isRewardToken[_rewardToken]) {
return;
}
rewardTokens.push(_rewardToken);
isRewardToken[_rewardToken] = true;
emit RewardTokenAdded(_rewardToken);
}
function totalSupply() public view override returns (uint256) {
return _totalSupply;
}
function balanceOf(address account) public view override returns (uint256) {
return _balances[account];
}
modifier updateReward(address _account) {
for (uint256 i = 0; i < rewardTokens.length; i++) {
address rewardToken = rewardTokens[i];
Reward storage reward = rewards[rewardToken];
reward.rewardPerTokenStored = rewardPerToken(rewardToken);
reward.lastUpdateTime = lastTimeRewardApplicable(rewardToken);
UserReward storage userReward = userRewards[_account][rewardToken];
userReward.rewards = earned(_account, rewardToken);
userReward.userRewardPerTokenPaid = rewards[rewardToken]
.rewardPerTokenStored;
}
userAmountTime[_account] = getUserAmountTime(_account);
userLastTime[_account] = block.timestamp;
_;
}
function getRewardTokens()
external
view
override
returns (address[] memory)
{
return rewardTokens;
}
function getRewardTokensLength() external view override returns (uint256) {
return rewardTokens.length;
}
function lastTimeRewardApplicable(
address _rewardToken
) public view returns (uint256) {
return Math.min(block.timestamp, rewards[_rewardToken].periodFinish);
}
function rewardPerToken(
address _rewardToken
) public view returns (uint256) {
Reward memory reward = rewards[_rewardToken];
if (totalSupply() == 0) {
return reward.rewardPerTokenStored;
}
return
reward.rewardPerTokenStored +
(((lastTimeRewardApplicable(_rewardToken) - reward.lastUpdateTime) *
reward.rewardRate *
1e18) / totalSupply());
}
function earned(
address _account,
address _rewardToken
) public view override returns (uint256) {
UserReward memory userReward = userRewards[_account][_rewardToken];
return
((balanceOf(_account) *
(rewardPerToken(_rewardToken) -
userReward.userRewardPerTokenPaid)) / 1e18) +
userReward.rewards;
}
function getUserAmountTime(
address _account
) public view override returns (uint256) {
uint256 lastTime = userLastTime[_account];
if (lastTime == 0) {
return 0;
}
uint256 userBalance = _balances[_account];
if (userBalance == 0) {
return userAmountTime[_account];
}
return
userAmountTime[_account] +
((block.timestamp - lastTime) * userBalance);
}
function stake(uint256 _amount) public override updateReward(msg.sender) {
require(_amount > 0, "RewardPool : Cannot stake 0");
_totalSupply = _totalSupply + _amount;
_balances[msg.sender] = _balances[msg.sender] + _amount;
stakingToken.safeTransferFrom(msg.sender, address(this), _amount);
emit Staked(msg.sender, _amount);
}
function stakeAll() external override {
uint256 balance = stakingToken.balanceOf(msg.sender);
stake(balance);
}
function stakeFor(
address _for,
uint256 _amount
) external override updateReward(_for) {
require(_for != address(0), "invalid _for!");
require(_amount > 0, "RewardPool : Cannot stake 0");
//give to _for
_totalSupply = _totalSupply + _amount;
_balances[_for] = _balances[_for] + _amount;
//take away from sender
stakingToken.safeTransferFrom(msg.sender, address(this), _amount);
emit Staked(_for, _amount);
}
function withdraw(uint256 amount) external override {
_withdraw(msg.sender, amount, true);
}
function withdrawAll() external override {
_withdraw(msg.sender, _balances[msg.sender], true);
}
function withdrawFor(
address _account,
uint256 _amount
) external override onlyRole(EqbConstants.ZAP_ROLE) {
_withdraw(_account, _amount, true);
}
// Withdraw without caring about rewards. EMERGENCY ONLY.
function emergencyWithdraw() external {
uint256 _amount = _balances[msg.sender];
_withdraw(msg.sender, _amount, false);
emit EmergencyWithdrawn(msg.sender, _amount);
}
function _withdraw(
address _account,
uint256 _amount,
bool _reward
) internal updateReward(_account) {
require(_amount > 0, "RewardPool : Cannot withdraw 0");
_totalSupply = _totalSupply - _amount;
_balances[_account] = _balances[_account] - _amount;
stakingToken.safeTransfer(_account, _amount);
emit Withdrawn(_account, _amount);
if (_reward) {
_getReward(_account);
}
}
function getReward(
address _account
) public override updateReward(_account) {
_getReward(_account);
}
function _getReward(address _account) internal {
for (uint256 i = 0; i < rewardTokens.length; i++) {
address rewardToken = rewardTokens[i];
uint256 reward = userRewards[_account][rewardToken].rewards;
if (reward > 0) {
userRewards[_account][rewardToken].rewards = 0;
rewardToken.safeTransferToken(_account, reward);
IPendleBooster(booster).rewardClaimed(
pid,
_account,
rewardToken,
reward
);
emit RewardPaid(_account, rewardToken, reward);
}
}
}
function donate(
address _rewardToken,
uint256 _amount
) external payable override {
require(isRewardToken[_rewardToken], "invalid token");
if (AddressLib.isPlatformToken(_rewardToken)) {
require(_amount == msg.value, "invalid amount");
} else {
require(msg.value == 0, "invalid msg.value");
IERC20(_rewardToken).safeTransferFrom(
msg.sender,
address(this),
_amount
);
}
rewards[_rewardToken].queuedRewards =
rewards[_rewardToken].queuedRewards +
_amount;
}
function queueNewRewards(
address _rewardToken,
uint256 _rewards
) external payable override onlyRole(EqbConstants.ADMIN_ROLE) {
addRewardToken(_rewardToken);
if (AddressLib.isPlatformToken(_rewardToken)) {
require(_rewards == msg.value, "invalid amount");
} else {
require(msg.value == 0, "invalid msg.value");
IERC20(_rewardToken).safeTransferFrom(
msg.sender,
address(this),
_rewards
);
}
Reward storage rewardInfo = rewards[_rewardToken];
if (totalSupply() == 0) {
rewardInfo.queuedRewards = rewardInfo.queuedRewards + _rewards;
return;
}
rewardInfo.rewardPerTokenStored = rewardPerToken(_rewardToken);
_rewards = _rewards + rewardInfo.queuedRewards;
rewardInfo.queuedRewards = 0;
if (block.timestamp >= rewardInfo.periodFinish) {
rewardInfo.rewardRate = _rewards / duration;
} else {
uint256 remaining = rewardInfo.periodFinish - block.timestamp;
uint256 leftover = remaining * rewardInfo.rewardRate;
_rewards = _rewards + leftover;
rewardInfo.rewardRate = _rewards / duration;
}
rewardInfo.lastUpdateTime = block.timestamp;
rewardInfo.periodFinish = block.timestamp + duration;
emit RewardAdded(_rewardToken, _rewards);
}
receive() external payable {}
}