-
Notifications
You must be signed in to change notification settings - Fork 5
/
VirtualBalanceRewardPool.sol
254 lines (223 loc) · 8.58 KB
/
VirtualBalanceRewardPool.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
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
/**
*Submitted for verification at Etherscan.io on 2020-07-17
*/
/*
____ __ __ __ _
/ __/__ __ ___ / /_ / / ___ / /_ (_)__ __
_\ \ / // // _ \/ __// _ \/ -_)/ __// / \ \ /
/___/ \_, //_//_/\__//_//_/\__/ \__//_/ /_\_\
/___/
* Synthetix: VirtualBalanceRewardPool.sol
*
* Docs: https://docs.synthetix.io/
*
*
* MIT License
* ===========
*
* Copyright (c) 2020 Synthetix
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*/
import "./Interfaces.sol";
import "./interfaces/MathUtil.sol";
import "@openzeppelin/contracts-0.6/math/SafeMath.sol";
import "@openzeppelin/contracts-0.6/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts-0.6/utils/Address.sol";
import "@openzeppelin/contracts-0.6/token/ERC20/SafeERC20.sol";
abstract contract VirtualBalanceWrapper {
using SafeMath for uint256;
using SafeERC20 for IERC20;
IDeposit public immutable deposits;
constructor(address deposit_) internal {
deposits = IDeposit(deposit_);
}
function totalSupply() public view returns (uint256) {
return deposits.totalSupply();
}
function balanceOf(address account) public view returns (uint256) {
return deposits.balanceOf(account);
}
}
/**
* @title VirtualBalanceRewardPool
* @author ConvexFinance
* @notice Reward pool used for ExtraRewards in Booster lockFees (3crv) and
* Extra reward stashes
* @dev The rewards are sent to this contract for distribution to stakers. This
* contract does not hold any of the staking tokens it just maintains a virtual
* balance of what a user has staked in the staking pool (BaseRewardPool).
* For example the Booster sends veCRV fees (3Crv) to a VirtualBalanceRewardPool
* which tracks the virtual balance of cxvCRV stakers and distributes their share
* of 3Crv rewards
*/
contract VirtualBalanceRewardPool is VirtualBalanceWrapper {
using SafeERC20 for IERC20;
IERC20 public immutable rewardToken;
uint256 public constant duration = 7 days;
address public immutable operator;
uint256 public periodFinish = 0;
uint256 public rewardRate = 0;
uint256 public lastUpdateTime;
uint256 public rewardPerTokenStored;
uint256 public queuedRewards = 0;
uint256 public currentRewards = 0;
uint256 public historicalRewards = 0;
uint256 public constant newRewardRatio = 830;
mapping(address => uint256) public userRewardPerTokenPaid;
mapping(address => uint256) public rewards;
event RewardAdded(uint256 reward);
event Staked(address indexed user, uint256 amount);
event Withdrawn(address indexed user, uint256 amount);
event RewardPaid(address indexed user, uint256 reward);
/**
* @param deposit_ Parent deposit pool e.g cvxCRV staking in BaseRewardPool
* @param reward_ The rewards token e.g 3Crv
* @param op_ Operator contract (Booster)
*/
constructor(
address deposit_,
address reward_,
address op_
) public VirtualBalanceWrapper(deposit_) {
rewardToken = IERC20(reward_);
operator = op_;
}
/**
* @notice Update rewards earned by this account
*/
modifier updateReward(address account) {
rewardPerTokenStored = rewardPerToken();
lastUpdateTime = lastTimeRewardApplicable();
if (account != address(0)) {
rewards[account] = earned(account);
userRewardPerTokenPaid[account] = rewardPerTokenStored;
}
_;
}
function lastTimeRewardApplicable() public view returns (uint256) {
return MathUtil.min(block.timestamp, periodFinish);
}
function rewardPerToken() public view returns (uint256) {
if (totalSupply() == 0) {
return rewardPerTokenStored;
}
return
rewardPerTokenStored.add(
lastTimeRewardApplicable()
.sub(lastUpdateTime)
.mul(rewardRate)
.mul(1e18)
.div(totalSupply())
);
}
function earned(address account) public view returns (uint256) {
return
balanceOf(account)
.mul(rewardPerToken().sub(userRewardPerTokenPaid[account]))
.div(1e18)
.add(rewards[account]);
}
/**
* @notice Update reward, emit, call linked reward's stake
* @dev Callable by the deposits address which is the BaseRewardPool
* this updates the virtual balance of this user as this contract doesn't
* actually hold any staked tokens it just diributes reward tokens
*/
function stake(address _account, uint256 amount)
external
updateReward(_account)
{
require(msg.sender == address(deposits), "!authorized");
// require(amount > 0, 'VirtualDepositRewardPool: Cannot stake 0');
emit Staked(_account, amount);
}
/**
* @notice Withdraw stake and update reward, emit, call linked reward's stake
* @dev See stake
*/
function withdraw(address _account, uint256 amount)
public
updateReward(_account)
{
require(msg.sender == address(deposits), "!authorized");
//require(amount > 0, 'VirtualDepositRewardPool : Cannot withdraw 0');
emit Withdrawn(_account, amount);
}
/**
* @notice Get rewards for this account
* @dev This can be called directly but it is usually called by the
* BaseRewardPool getReward when the BaseRewardPool loops through
* it's extraRewards array calling getReward on all of them
*/
function getReward(address _account) public updateReward(_account){
uint256 reward = earned(_account);
if (reward > 0) {
rewards[_account] = 0;
rewardToken.safeTransfer(_account, reward);
emit RewardPaid(_account, reward);
}
}
function getReward() external{
getReward(msg.sender);
}
function donate(uint256 _amount) external returns(bool){
IERC20(rewardToken).safeTransferFrom(msg.sender, address(this), _amount);
queuedRewards = queuedRewards.add(_amount);
}
function queueNewRewards(uint256 _rewards) external{
require(msg.sender == operator, "!authorized");
_rewards = _rewards.add(queuedRewards);
if (block.timestamp >= periodFinish) {
notifyRewardAmount(_rewards);
queuedRewards = 0;
return;
}
//et = now - (finish-duration)
uint256 elapsedTime = block.timestamp.sub(periodFinish.sub(duration));
//current at now: rewardRate * elapsedTime
uint256 currentAtNow = rewardRate * elapsedTime;
uint256 queuedRatio = currentAtNow.mul(1000).div(_rewards);
if(queuedRatio < newRewardRatio){
notifyRewardAmount(_rewards);
queuedRewards = 0;
}else{
queuedRewards = _rewards;
}
}
function notifyRewardAmount(uint256 reward)
internal
updateReward(address(0))
{
historicalRewards = historicalRewards.add(reward);
if (block.timestamp >= periodFinish) {
rewardRate = reward.div(duration);
} else {
uint256 remaining = periodFinish.sub(block.timestamp);
uint256 leftover = remaining.mul(rewardRate);
reward = reward.add(leftover);
rewardRate = reward.div(duration);
}
currentRewards = reward;
lastUpdateTime = block.timestamp;
periodFinish = block.timestamp.add(duration);
emit RewardAdded(reward);
}
}