/
Bribes.sol
444 lines (357 loc) · 16.6 KB
/
Bribes.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;
import "./interfaces/IMinter.sol";
import "./interfaces/IVoter.sol";
import "./interfaces/IVotingEscrow.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import './libraries/Math.sol';
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
interface IERC20Ext {
function name() external returns(string memory);
function symbol() external returns(string memory);
}
contract Bribe is ReentrancyGuard {
using SafeERC20 for IERC20;
uint256 public constant WEEK = 7 days; // rewards are released over 7 days
uint256 public firstBribeTimestamp;
/* ========== STATE VARIABLES ========== */
struct Reward {
uint256 periodFinish;
uint256 rewardsPerEpoch;
uint256 lastUpdateTime;
}
mapping(address => mapping(uint => Reward)) public rewardData; // token -> startTimestamp -> Reward
mapping(address => bool) public isRewardToken;
address[] public rewardTokens;
address public voter;
address public bribeFactory;
address public minter;
address public ve;
address public owner;
string public TYPE;
// owner -> reward token -> lastTime
mapping(address => mapping(address => uint256)) public userRewardPerTokenPaid;
mapping(address => mapping(address => uint256)) public userTimestamp;
//uint256 private _totalSupply;
mapping(uint256 => uint256) public _totalSupply;
mapping(address => mapping(uint256 => uint256)) private _balances; //owner -> timestamp -> amount
/* ========== CONSTRUCTOR ========== */
constructor(address _owner,address _voter,address _bribeFactory, string memory _type) {
require(_bribeFactory != address(0) && _voter != address(0) && _owner != address(0));
voter = _voter;
bribeFactory = _bribeFactory;
firstBribeTimestamp = 0;
ve = IVoter(_voter)._ve();
minter = IVoter(_voter).minter();
require(minter != address(0));
owner = _owner;
TYPE = _type;
}
/// @notice get the current epoch
function getEpochStart() public view returns(uint){
return IMinter(minter).active_period();
}
/// @notice get next epoch (where bribes are saved)
function getNextEpochStart() public view returns(uint){
return getEpochStart() + WEEK;
}
/* ========== VIEWS ========== */
/// @notice get the length of the reward tokens
function rewardsListLength() external view returns(uint256) {
return rewardTokens.length;
}
/// @notice get the last totalSupply (total votes for a pool)
function totalSupply() external view returns (uint256) {
uint256 _currentEpochStart = IMinter(minter).active_period(); // claim until current epoch
return _totalSupply[_currentEpochStart];
}
/// @notice get a totalSupply given a timestamp
function totalSupplyAt(uint256 _timestamp) external view returns (uint256) {
return _totalSupply[_timestamp];
}
/// @notice read the balanceOf the tokenId at a given timestamp
function balanceOfAt(uint256 tokenId, uint256 _timestamp) public view returns (uint256) {
address _owner = IVotingEscrow(ve).ownerOf(tokenId);
return _balances[_owner][_timestamp];
}
/// @notice get last deposit available given a tokenID
function balanceOf(uint256 tokenId) public view returns (uint256) {
uint256 _timestamp = getNextEpochStart();
address _owner = IVotingEscrow(ve).ownerOf(tokenId);
return _balances[_owner][_timestamp];
}
/// @notice get the balance of a owner in the current epoch
function balanceOfOwner(address _owner) public view returns (uint256) {
uint256 _timestamp = getNextEpochStart();
return _balances[_owner][_timestamp];
}
/// @notice get the balance of a owner given a timestamp
function balanceOfOwnerAt(address _owner, uint256 _timestamp) public view returns (uint256) {
return _balances[_owner][_timestamp];
}
/// @notice Read earned amount given a tokenID and _rewardToken
function earned(uint256 tokenId, address _rewardToken) public view returns(uint256){
uint k = 0;
uint reward = 0;
uint256 _endTimestamp = IMinter(minter).active_period(); // claim until current epoch
address _owner = IVotingEscrow(ve).ownerOf(tokenId);
uint256 _userLastTime = userTimestamp[_owner][_rewardToken];
if(_endTimestamp == _userLastTime){
return 0;
}
// if user first time then set it to first bribe - week to avoid any timestamp problem
if(_userLastTime < firstBribeTimestamp){
_userLastTime = firstBribeTimestamp - WEEK;
}
for(k; k < 50; k++){
if(_userLastTime == _endTimestamp){
// if we reach the current epoch, exit
break;
}
reward += _earned(_owner, _rewardToken, _userLastTime);
_userLastTime += WEEK;
}
return reward;
}
/// @notice read earned amounts given an address and the reward token
function earned(address _owner, address _rewardToken) public view returns(uint256){
uint k = 0;
uint reward = 0;
uint256 _endTimestamp = IMinter(minter).active_period(); // claim until current epoch
uint256 _userLastTime = userTimestamp[_owner][_rewardToken];
if(_endTimestamp == _userLastTime){
return 0;
}
// if user first time then set it to first bribe - week to avoid any timestamp problem
if(_userLastTime < firstBribeTimestamp){
_userLastTime = firstBribeTimestamp - WEEK;
}
for(k; k < 50; k++){
if(_userLastTime == _endTimestamp){
// if we reach the current epoch, exit
break;
}
reward += _earned(_owner, _rewardToken, _userLastTime);
_userLastTime += WEEK;
}
return reward;
}
/// @notice Read earned amount given address and reward token, returns the rewards and the last user timestamp (used in case user do not claim since 50+epochs)
function earnedWithTimestamp(address _owner, address _rewardToken) private view returns(uint256,uint256){
uint k = 0;
uint reward = 0;
uint256 _endTimestamp = IMinter(minter).active_period(); // claim until current epoch
uint256 _userLastTime = userTimestamp[_owner][_rewardToken];
if(_endTimestamp == _userLastTime){
return (0, _userLastTime);
}
// if user first time then set it to first bribe - week to avoid any timestamp problem
if(_userLastTime < firstBribeTimestamp){
_userLastTime = firstBribeTimestamp - WEEK;
}
for(k; k < 50; k++){
if(_userLastTime == _endTimestamp){
// if we reach the current epoch, exit
break;
}
reward += _earned(_owner, _rewardToken, _userLastTime);
_userLastTime += WEEK;
}
return (reward, _userLastTime);
}
/// @notice get the earned rewards
function _earned(address _owner, address _rewardToken, uint256 _timestamp) internal view returns (uint256) {
uint256 _balance = balanceOfOwnerAt(_owner, _timestamp);
if(_balance == 0){
return 0;
} else {
uint256 _rewardPerToken = rewardPerToken(_rewardToken, _timestamp);
uint256 _rewards = _rewardPerToken * _balance / 1e18;
return _rewards;
}
}
/// @notice get the rewards for token
function rewardPerToken(address _rewardsToken, uint256 _timestmap) public view returns (uint256) {
if (_totalSupply[_timestmap] == 0) {
return rewardData[_rewardsToken][_timestmap].rewardsPerEpoch;
}
return rewardData[_rewardsToken][_timestmap].rewardsPerEpoch * 1e18 / _totalSupply[_timestmap];
}
/* ========== MUTATIVE FUNCTIONS ========== */
/// @notice User votes deposit
/// @dev called on voter.vote() or voter.poke()
/// we save into owner "address" and not "tokenID".
/// Owner must reset before transferring token
function _deposit(uint256 amount, uint256 tokenId) external nonReentrant {
require(amount > 0, "Cannot stake 0");
require(msg.sender == voter);
uint256 _startTimestamp = IMinter(minter).active_period() + WEEK;
uint256 _oldSupply = _totalSupply[_startTimestamp];
address _owner = IVotingEscrow(ve).ownerOf(tokenId);
uint256 _lastBalance = _balances[_owner][_startTimestamp];
_totalSupply[_startTimestamp] = _oldSupply + amount;
_balances[_owner][_startTimestamp] = _lastBalance + amount;
emit Staked(tokenId, amount);
}
/// @notice User votes withdrawal
/// @dev called on voter.reset()
function _withdraw(uint256 amount, uint256 tokenId) public nonReentrant {
require(amount > 0, "Cannot withdraw 0");
require(msg.sender == voter);
uint256 _startTimestamp = IMinter(minter).active_period() + WEEK;
address _owner = IVotingEscrow(ve).ownerOf(tokenId);
// incase of bribe contract reset in gauge proxy
if (amount <= _balances[_owner][_startTimestamp]) {
uint256 _oldSupply = _totalSupply[_startTimestamp];
uint256 _oldBalance = _balances[_owner][_startTimestamp];
_totalSupply[_startTimestamp] = _oldSupply - amount;
_balances[_owner][_startTimestamp] = _oldBalance - amount;
emit Withdrawn(tokenId, amount);
}
}
/// @notice Claim the TOKENID rewards
function getReward(uint tokenId, address[] memory tokens) external nonReentrant {
require(IVotingEscrow(ve).isApprovedOrOwner(msg.sender, tokenId));
uint256 _userLastTime;
uint256 reward = 0;
address _owner = IVotingEscrow(ve).ownerOf(tokenId);
for (uint256 i = 0; i < tokens.length; i++) {
address _rewardToken = tokens[i];
(reward, _userLastTime) = earnedWithTimestamp(_owner, _rewardToken);
if (reward > 0) {
IERC20(_rewardToken).safeTransfer(_owner, reward);
emit RewardPaid(_owner, _rewardToken, reward);
}
userTimestamp[_owner][_rewardToken] = _userLastTime;
}
}
/// @notice Claim the rewards given msg.sender
function getReward(address[] memory tokens) external nonReentrant {
uint256 _userLastTime;
uint256 reward = 0;
address _owner = msg.sender;
for (uint256 i = 0; i < tokens.length; i++) {
address _rewardToken = tokens[i];
(reward, _userLastTime) = earnedWithTimestamp(_owner, _rewardToken);
if (reward > 0) {
IERC20(_rewardToken).safeTransfer(_owner, reward);
emit RewardPaid(_owner, _rewardToken, reward);
}
userTimestamp[_owner][_rewardToken] = _userLastTime;
}
}
/// @notice Claim rewards from voter
function getRewardForOwner(uint tokenId, address[] memory tokens) public nonReentrant {
require(msg.sender == voter);
uint256 _userLastTime;
uint256 reward = 0;
address _owner = IVotingEscrow(ve).ownerOf(tokenId);
for (uint256 i = 0; i < tokens.length; i++) {
address _rewardToken = tokens[i];
(reward, _userLastTime) = earnedWithTimestamp(_owner, _rewardToken);
if (reward > 0) {
IERC20(_rewardToken).safeTransfer(_owner, reward);
emit RewardPaid(_owner, _rewardToken, reward);
}
userTimestamp[_owner][_rewardToken] = _userLastTime;
}
}
/// @notice Claim rewards from voter
function getRewardForAddress(address _owner, address[] memory tokens) public nonReentrant {
require(msg.sender == voter);
uint256 _userLastTime;
uint256 reward = 0;
for (uint256 i = 0; i < tokens.length; i++) {
address _rewardToken = tokens[i];
(reward, _userLastTime) = earnedWithTimestamp(_owner, _rewardToken);
if (reward > 0) {
IERC20(_rewardToken).safeTransfer(_owner, reward);
emit RewardPaid(_owner, _rewardToken, reward);
}
userTimestamp[_owner][_rewardToken] = _userLastTime;
}
}
/// @notice Notify a bribe amount
/// @dev Rewards are saved into NEXT EPOCH mapping.
function notifyRewardAmount(address _rewardsToken, uint256 reward) external nonReentrant {
require(isRewardToken[_rewardsToken], "reward token not verified");
IERC20(_rewardsToken).safeTransferFrom(msg.sender,address(this),reward);
uint256 _startTimestamp = IMinter(minter).active_period() + WEEK; //period points to the current thursday. Bribes are distributed from next epoch (thursday)
if(firstBribeTimestamp == 0){
firstBribeTimestamp = _startTimestamp;
}
uint256 _lastReward = rewardData[_rewardsToken][_startTimestamp].rewardsPerEpoch;
rewardData[_rewardsToken][_startTimestamp].rewardsPerEpoch = _lastReward + reward;
rewardData[_rewardsToken][_startTimestamp].lastUpdateTime = block.timestamp;
rewardData[_rewardsToken][_startTimestamp].periodFinish = _startTimestamp + WEEK;
emit RewardAdded(_rewardsToken, reward, _startTimestamp);
}
/* ========== RESTRICTED FUNCTIONS ========== */
/// @notice add rewards tokens
function addRewards(address[] memory _rewardsToken) public onlyAllowed {
uint i = 0;
for(i; i < _rewardsToken.length; i++){
_addReward(_rewardsToken[i]);
}
}
/// @notice add a single reward token
function addReward(address _rewardsToken) public onlyAllowed {
_addReward(_rewardsToken);
}
function _addReward(address _rewardsToken) internal {
if(!isRewardToken[_rewardsToken]){
isRewardToken[_rewardsToken] = true;
rewardTokens.push(_rewardsToken);
}
}
/// @notice Recover some ERC20 from the contract and updated given bribe
function recoverERC20AndUpdateData(address tokenAddress, uint256 tokenAmount) external onlyAllowed {
require(tokenAmount <= IERC20(tokenAddress).balanceOf(address(this)));
uint256 _startTimestamp = IMinter(minter).active_period() + WEEK;
uint256 _lastReward = rewardData[tokenAddress][_startTimestamp].rewardsPerEpoch;
rewardData[tokenAddress][_startTimestamp].rewardsPerEpoch = _lastReward - tokenAmount;
rewardData[tokenAddress][_startTimestamp].lastUpdateTime = block.timestamp;
IERC20(tokenAddress).safeTransfer(owner, tokenAmount);
emit Recovered(tokenAddress, tokenAmount);
}
/// @notice Recover some ERC20 from the contract.
/// @dev Be careful --> if called then getReward() at last epoch will fail because some reward are missing!
/// Think about calling recoverERC20AndUpdateData()
function emergencyRecoverERC20(address tokenAddress, uint256 tokenAmount) external onlyAllowed {
require(tokenAmount <= IERC20(tokenAddress).balanceOf(address(this)));
IERC20(tokenAddress).safeTransfer(owner, tokenAmount);
emit Recovered(tokenAddress, tokenAmount);
}
/// @notice Set a new voter
function setVoter(address _Voter) external onlyAllowed {
require(_Voter != address(0));
voter = _Voter;
}
/// @notice Set a new minter
function setMinter(address _minter) external onlyAllowed {
require(_minter != address(0));
minter = _minter;
}
/// @notice Set a new Owner
function setOwner(address _owner) external onlyAllowed {
require(_owner != address(0));
owner = _owner;
}
/* ========== MODIFIERS ========== */
modifier onlyOwner() {
require(owner == msg.sender);
_;
}
modifier onlyAllowed() {
require( (msg.sender == owner || msg.sender == bribeFactory), "permission is denied!" );
_;
}
/* ========== EVENTS ========== */
event RewardAdded(address rewardToken, uint256 reward, uint256 startTimestamp);
event Staked(uint256 indexed tokenId, uint256 amount);
event Withdrawn(uint256 indexed tokenId, uint256 amount);
event RewardPaid(address indexed user,address indexed rewardsToken,uint256 reward);
event Recovered(address token, uint256 amount);
}