/
GaugeV2.sol
390 lines (305 loc) · 13.9 KB
/
GaugeV2.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import './interfaces/IPair.sol';
import './interfaces/IBribe.sol';
import "./libraries/Math.sol";
interface IRewarder {
function onReward(
uint256 pid,
address user,
address recipient,
uint256 amount,
uint256 newLpAmount
) external;
}
contract GaugeV2 is ReentrancyGuard, Ownable {
using SafeMath for uint256;
using SafeERC20 for IERC20;
bool public isForPair;
bool public emergency;
IERC20 public rewardToken;
IERC20 public _VE;
IERC20 public TOKEN;
address public DISTRIBUTION;
address public gaugeRewarder;
address public internal_bribe;
address public external_bribe;
uint256 public rewarderPid;
uint256 public DURATION;
uint256 public periodFinish;
uint256 public rewardRate;
uint256 public lastUpdateTime;
uint256 public rewardPerTokenStored;
uint public fees0;
uint public fees1;
mapping(address => uint256) public userRewardPerTokenPaid;
mapping(address => uint256) public rewards;
uint256 public _totalSupply;
mapping(address => uint256) public _balances;
event RewardAdded(uint256 reward);
event Deposit(address indexed user, uint256 amount);
event Withdraw(address indexed user, uint256 amount);
event Harvest(address indexed user, uint256 reward);
event ClaimFees(address indexed from, uint claimed0, uint claimed1);
modifier updateReward(address account) {
rewardPerTokenStored = rewardPerToken();
lastUpdateTime = lastTimeRewardApplicable();
if (account != address(0)) {
rewards[account] = earned(account);
userRewardPerTokenPaid[account] = rewardPerTokenStored;
}
_;
}
modifier onlyDistribution() {
require(msg.sender == DISTRIBUTION, "Caller is not RewardsDistribution contract");
_;
}
modifier isNotEmergency() {
require(emergency == false);
_;
}
constructor(address _rewardToken,address _ve,address _token,address _distribution, address _internal_bribe, address _external_bribe, bool _isForPair) {
rewardToken = IERC20(_rewardToken); // main reward
_VE = IERC20(_ve); // vested
TOKEN = IERC20(_token); // underlying (LP)
DISTRIBUTION = _distribution; // distro address (voter)
DURATION = 7 * 86400; // distro time
internal_bribe = _internal_bribe; // lp fees goes here
external_bribe = _external_bribe; // bribe fees goes here
isForPair = _isForPair; // pair boolean, if false no claim_fees
emergency = false; // emergency flag
}
/* -----------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
ONLY OWNER
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
----------------------------------------------------------------------------- */
///@notice set distribution address (should be GaugeProxyL2)
function setDistribution(address _distribution) external onlyOwner {
require(_distribution != address(0), "zero addr");
require(_distribution != DISTRIBUTION, "same addr");
DISTRIBUTION = _distribution;
}
///@notice set gauge rewarder address
function setGaugeRewarder(address _gaugeRewarder) external onlyOwner {
require(_gaugeRewarder != address(0), "zero addr");
require(_gaugeRewarder != gaugeRewarder, "same addr");
gaugeRewarder = _gaugeRewarder;
}
///@notice set extra rewarder pid
function setRewarderPid(uint256 _pid) external onlyOwner {
require(_pid >= 0, "zero");
require(_pid != rewarderPid, "same pid");
rewarderPid = _pid;
}
///@notice set new internal bribe contract (where to send fees)
function setInternalBribe(address _int) external onlyOwner {
require(_int >= address(0), "zero");
internal_bribe = _int;
}
function activateEmergencyMode() external onlyOwner {
require(emergency == false);
emergency = true;
}
function stopEmergencyMode() external onlyOwner {
require(emergency == false);
emergency = false;
}
/* -----------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
VIEW FUNCTIONS
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
----------------------------------------------------------------------------- */
///@notice total supply held
function totalSupply() public view returns (uint256) {
return _totalSupply;
}
///@notice balance of a user
function balanceOf(address account) external view returns (uint256) {
return _balances[account];
}
///@notice last time reward
function lastTimeRewardApplicable() public view returns (uint256) {
return Math.min(block.timestamp, periodFinish);
}
///@notice reward for a sinle token
function rewardPerToken() public view returns (uint256) {
if (_totalSupply == 0) {
return rewardPerTokenStored;
} else {
return rewardPerTokenStored.add(lastTimeRewardApplicable().sub(lastUpdateTime).mul(rewardRate).mul(1e18).div(_totalSupply));
}
}
///@notice see earned rewards for user
function earned(address account) public view returns (uint256) {
return _balances[account].mul(rewardPerToken().sub(userRewardPerTokenPaid[account])).div(1e18).add(rewards[account]);
}
///@notice get total reward for the duration
function rewardForDuration() external view returns (uint256) {
return rewardRate.mul(DURATION);
}
function _periodFinish() external view returns (uint256) {
return periodFinish;
}
/* -----------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
USER INTERACTION
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
----------------------------------------------------------------------------- */
///@notice deposit all TOKEN of msg.sender
function depositAll() external {
_deposit(TOKEN.balanceOf(msg.sender), msg.sender);
}
///@notice deposit amount TOKEN
function deposit(uint256 amount) external {
_deposit(amount, msg.sender);
}
///@notice deposit internal
function _deposit(uint256 amount, address account) internal nonReentrant isNotEmergency updateReward(account) {
require(amount > 0, "deposit(Gauge): cannot stake 0");
_balances[account] = _balances[account].add(amount);
_totalSupply = _totalSupply.add(amount);
TOKEN.safeTransferFrom(account, address(this), amount);
if (address(gaugeRewarder) != address(0)) {
IRewarder(gaugeRewarder).onReward(rewarderPid, account, account, 0, _balances[account]);
}
emit Deposit(account, amount);
}
///@notice withdraw all token
function withdrawAll() external {
_withdraw(_balances[msg.sender]);
}
///@notice withdraw a certain amount of TOKEN
function withdraw(uint256 amount) external {
_withdraw(amount);
}
///@notice withdraw internal
function _withdraw(uint256 amount) internal nonReentrant isNotEmergency updateReward(msg.sender) {
require(amount > 0, "Cannot withdraw 0");
require(_totalSupply.sub(amount) >= 0, "supply < 0");
require(_balances[msg.sender] > 0, "no balances");
_totalSupply = _totalSupply.sub(amount);
_balances[msg.sender] = _balances[msg.sender].sub(amount);
if (address(gaugeRewarder) != address(0)) {
IRewarder(gaugeRewarder).onReward(rewarderPid, msg.sender, msg.sender, 0, _balances[msg.sender]);
}
TOKEN.safeTransfer(msg.sender, amount);
emit Withdraw(msg.sender, amount);
}
function emergencyWithdraw() external nonReentrant {
require(emergency);
require(_balances[msg.sender] > 0, "no balances");
uint256 _amount = _balances[msg.sender];
_totalSupply = _totalSupply.sub(_amount);
_balances[msg.sender] = 0;
TOKEN.safeTransfer(msg.sender, _amount);
emit Withdraw(msg.sender, _amount);
}
function emergencyWithdrawAmount(uint256 _amount) external nonReentrant {
require(emergency);
_totalSupply = _totalSupply.sub(_amount);
_balances[msg.sender] = _balances[msg.sender] - _amount;
TOKEN.safeTransfer(msg.sender, _amount);
emit Withdraw(msg.sender, _amount);
}
///@notice withdraw all TOKEN and harvest rewardToken
function withdrawAllAndHarvest() external {
_withdraw(_balances[msg.sender]);
getReward();
}
///@notice User harvest function called from distribution (voter allows harvest on multiple gauges)
function getReward(address _user) public nonReentrant onlyDistribution updateReward(_user) {
uint256 reward = rewards[_user];
if (reward > 0) {
rewards[_user] = 0;
rewardToken.safeTransfer(_user, reward);
emit Harvest(_user, reward);
}
if (gaugeRewarder != address(0)) {
IRewarder(gaugeRewarder).onReward(rewarderPid, _user, _user, reward, _balances[_user]);
}
}
///@notice User harvest function
function getReward() public nonReentrant updateReward(msg.sender) {
uint256 reward = rewards[msg.sender];
if (reward > 0) {
rewards[msg.sender] = 0;
rewardToken.safeTransfer(msg.sender, reward);
emit Harvest(msg.sender, reward);
}
if (gaugeRewarder != address(0)) {
IRewarder(gaugeRewarder).onReward(rewarderPid, msg.sender, msg.sender, reward, _balances[msg.sender]);
}
}
/* -----------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
DISTRIBUTION
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
----------------------------------------------------------------------------- */
/// @dev Receive rewards from distribution
function notifyRewardAmount(address token, uint reward) external nonReentrant isNotEmergency onlyDistribution updateReward(address(0)) {
require(token == address(rewardToken));
rewardToken.safeTransferFrom(DISTRIBUTION, address(this), reward);
if (block.timestamp >= periodFinish) {
rewardRate = reward.div(DURATION);
} else {
uint256 remaining = periodFinish.sub(block.timestamp);
uint256 leftover = remaining.mul(rewardRate);
rewardRate = reward.add(leftover).div(DURATION);
}
// Ensure the provided reward amount is not more than the balance in the contract.
// This keeps the reward rate in the right range, preventing overflows due to
// very high values of rewardRate in the earned and rewardsPerToken functions;
// Reward + leftover must be less than 2^256 / 10^18 to avoid overflow.
uint256 balance = rewardToken.balanceOf(address(this));
require(rewardRate <= balance.div(DURATION), "Provided reward too high");
lastUpdateTime = block.timestamp;
periodFinish = block.timestamp.add(DURATION);
emit RewardAdded(reward);
}
function claimFees() external nonReentrant returns (uint claimed0, uint claimed1) {
return _claimFees();
}
function _claimFees() internal returns (uint claimed0, uint claimed1) {
if (!isForPair) {
return (0, 0);
}
address _token = address(TOKEN);
(claimed0, claimed1) = IPair(_token).claimFees();
if (claimed0 > 0 || claimed1 > 0) {
uint _fees0 = fees0 + claimed0;
uint _fees1 = fees1 + claimed1;
(address _token0, address _token1) = IPair(_token).tokens();
if (_fees0 > 0) {
fees0 = 0;
IERC20(_token0).approve(internal_bribe, 0);
IERC20(_token0).approve(internal_bribe, _fees0);
IBribe(internal_bribe).notifyRewardAmount(_token0, _fees0);
} else {
fees0 = _fees0;
}
if (_fees1 > 0) {
fees1 = 0;
IERC20(_token1).approve(internal_bribe, 0);
IERC20(_token1).approve(internal_bribe, _fees1);
IBribe(internal_bribe).notifyRewardAmount(_token1, _fees1);
} else {
fees1 = _fees1;
}
emit ClaimFees(msg.sender, claimed0, claimed1);
}
}
}