-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathConvexDepositToken.sol
303 lines (245 loc) · 10.8 KB
/
ConvexDepositToken.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import "../../interfaces/ICurveProxy.sol";
import "../../interfaces/ITreasury.sol";
import "../../dependencies/PrismaOwnable.sol";
interface IBooster {
function deposit(uint256 _pid, uint256 _amount, bool _stake) external returns (bool);
function poolInfo(
uint256 _pid
)
external
view
returns (address lpToken, address token, address gauge, address crvRewards, address stash, bool shutdown);
}
interface IBaseRewardPool {
function withdrawAndUnwrap(uint256 amount, bool claim) external returns (bool);
function getReward(address _account, bool _claimExtras) external returns (bool);
}
/**
@title Prisma Convex Deposit Wrapper
@notice Standard ERC20 interface around a deposit of a Curve LP token into Convex.
Tokens are minted by depositing Curve LP tokens, and burned to receive the LP
tokens back. Holders may claim PRISMA emissions on top of the earned CRV and CVX.
*/
contract ConvexDepositToken {
IERC20 public immutable PRISMA;
IERC20 public immutable CRV;
IERC20 public immutable CVX;
IBooster public immutable booster;
ICurveProxy public immutable curveProxy;
IPrismaTreasury public immutable treasury;
IERC20 public lpToken;
uint256 public depositPid;
IBaseRewardPool public crvRewards;
uint256 public emissionId;
string public symbol;
string public name;
uint256 public constant decimals = 18;
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
// each array relates to [PRISMA, CRV, CVX]
uint256[3] public rewardIntegral;
uint128[3] public rewardRate;
// last known balances for CRV, CVX
// must track because anyone can trigger a claim for any address
uint128 public lastCrvBalance;
uint128 public lastCvxBalance;
uint32 public lastUpdate;
uint32 public periodFinish;
mapping(address => uint256[3]) public rewardIntegralFor;
mapping(address => uint128[3]) private pendingRewardFor;
uint256 constant REWARD_DURATION = 1 weeks;
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
event LPTokenDeposited(address indexed lpToken, address indexed receiver, uint256 amount);
event LPTokenWithdrawn(address indexed lpToken, address indexed receiver, uint256 amount);
event RewardClaimed(address indexed receiver, uint256 prismaAmount, uint256 crvAmount, uint256 cvxAmount);
constructor(
IERC20 _prisma,
IERC20 _CRV,
IERC20 _CVX,
IBooster _booster,
ICurveProxy _proxy,
IPrismaTreasury _treasury
) {
PRISMA = _prisma;
CRV = _CRV;
CVX = _CVX;
booster = _booster;
curveProxy = _proxy;
treasury = _treasury;
}
function initialize(uint256 pid) external {
require(address(lpToken) == address(0), "Already initialized");
address _lpToken;
address _crvRewards;
(_lpToken, , , _crvRewards, , ) = booster.poolInfo(pid);
depositPid = pid;
lpToken = IERC20(_lpToken);
crvRewards = IBaseRewardPool(_crvRewards);
IERC20(_lpToken).approve(address(booster), type(uint256).max);
string memory _symbol = IERC20Metadata(_lpToken).symbol();
name = string.concat("Prisma ", _symbol, " Convex Deposit");
symbol = string.concat("prisma-", _symbol);
periodFinish = uint32(block.timestamp - 1);
}
function notifyRegisteredId(uint256[] memory assignedIds) external returns (bool) {
require(msg.sender == address(treasury));
require(emissionId == 0, "Already registered");
require(assignedIds.length == 1, "Incorrect ID count");
emissionId = assignedIds[0];
return true;
}
function deposit(address receiver, uint256 amount) external returns (bool) {
require(amount > 0, "Cannot deposit zero");
lpToken.transferFrom(msg.sender, address(this), amount);
booster.deposit(depositPid, amount, true);
uint256 balance = balanceOf[receiver];
uint256 supply = totalSupply;
balanceOf[receiver] = balance + amount;
totalSupply = supply + amount;
_updateIntegrals(receiver, balance, supply);
if (block.timestamp / 1 weeks >= periodFinish / 1 weeks) _fetchRewards(true);
emit LPTokenDeposited(address(lpToken), receiver, amount);
return true;
}
function withdraw(address receiver, uint256 amount) external returns (bool) {
require(amount > 0, "Cannot withdraw zero");
uint256 balance = balanceOf[msg.sender];
uint256 supply = totalSupply;
balanceOf[msg.sender] = balance - amount;
totalSupply = supply - amount;
bool claimRewards = block.timestamp / 1 weeks >= periodFinish / 1 weeks;
crvRewards.withdrawAndUnwrap(amount, claimRewards);
lpToken.transfer(receiver, amount);
_updateIntegrals(msg.sender, balance, supply);
if (claimRewards) _fetchRewards(false);
emit LPTokenWithdrawn(address(lpToken), receiver, amount);
return true;
}
function _claimReward(address claimant, address receiver) internal returns (uint128[3] memory amounts) {
_updateIntegrals(claimant, balanceOf[claimant], totalSupply);
amounts = pendingRewardFor[claimant];
delete pendingRewardFor[claimant];
lastCrvBalance -= amounts[1];
lastCvxBalance -= amounts[2];
CRV.transfer(receiver, amounts[1]);
CVX.transfer(receiver, amounts[2]);
return amounts;
}
function claimReward(
address receiver
) external returns (uint256 prismaAmount, uint256 crvAmount, uint256 cvxAmount) {
uint128[3] memory amounts = _claimReward(msg.sender, receiver);
treasury.transferAllocatedTokens(msg.sender, receiver, amounts[0]);
emit RewardClaimed(receiver, amounts[0], amounts[1], amounts[2]);
return (amounts[0], amounts[1], amounts[2]);
}
function treasuryClaimReward(address claimant, address receiver) external returns (uint256) {
require(msg.sender == address(treasury));
uint128[3] memory amounts = _claimReward(claimant, receiver);
emit RewardClaimed(claimant, 0, amounts[1], amounts[2]);
return amounts[0];
}
function claimableReward(
address account
) external view returns (uint256 prismaAmount, uint256 crvAmount, uint256 cvxAmount) {
uint256 updated = periodFinish;
if (updated > block.timestamp) updated = block.timestamp;
uint256 duration = updated - lastUpdate;
uint256 balance = balanceOf[account];
uint256 supply = totalSupply;
uint256[3] memory amounts;
for (uint256 i = 0; i < 3; i++) {
uint256 integral = rewardIntegral[i];
if (supply > 0) {
integral += (duration * rewardRate[i] * 1e18) / supply;
}
uint256 integralFor = rewardIntegralFor[account][i];
amounts[i] = pendingRewardFor[account][i] + ((balance * (integral - integralFor)) / 1e18);
}
return (amounts[0], amounts[1], amounts[2]);
}
function approve(address _spender, uint256 _value) public returns (bool) {
allowance[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}
function _transfer(address _from, address _to, uint256 _value) internal {
uint256 supply = totalSupply;
uint256 balance = balanceOf[_from];
balanceOf[_from] = balance - _value;
_updateIntegrals(_from, balance, supply);
balance = balanceOf[_to];
balanceOf[_to] = balance + _value;
_updateIntegrals(_to, balance, supply);
emit Transfer(_from, _to, _value);
}
function transfer(address _to, uint256 _value) public returns (bool) {
_transfer(msg.sender, _to, _value);
return true;
}
function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
uint256 allowed = allowance[_from][msg.sender];
if (allowed != type(uint256).max) {
allowance[_from][msg.sender] = allowed - _value;
}
_transfer(_from, _to, _value);
return true;
}
function _updateIntegrals(address account, uint256 balance, uint256 supply) internal {
uint256 updated = periodFinish;
if (updated > block.timestamp) updated = block.timestamp;
uint256 duration = updated - lastUpdate;
if (duration > 0) lastUpdate = uint32(updated);
for (uint256 i = 0; i < 3; i++) {
uint256 integral = rewardIntegral[i];
if (duration > 0 && supply > 0) {
integral += (duration * rewardRate[i] * 1e18) / supply;
rewardIntegral[i] = integral;
}
uint256 integralFor = rewardIntegralFor[account][i];
if (integral > integralFor) {
pendingRewardFor[account][i] += uint128((balance * (integral - integralFor)) / 1e18);
rewardIntegralFor[account][i] = integral;
}
}
}
function fetchRewards() external {
require(block.timestamp / 1 weeks > periodFinish / 1 weeks, "Can only fetch once per week");
_fetchRewards(true);
}
function _fetchRewards(bool claim) internal {
uint256 prismaAmount;
uint256 id = emissionId;
if (id > 0) prismaAmount = treasury.allocateNewEmissions(id);
if (claim) crvRewards.getReward(address(this), false);
uint256 last = lastCrvBalance;
uint256 crvAmount = CRV.balanceOf(address(this)) - last;
// apply CRV fee and send fee tokens to curveProxy
uint256 fee = (crvAmount * curveProxy.crvFeePct()) / 10000;
if (fee > 0) {
crvAmount -= fee;
CRV.transfer(address(curveProxy), fee);
}
lastCrvBalance = uint128(last + crvAmount);
last = lastCvxBalance;
uint256 cvxAmount = CVX.balanceOf(address(this)) - last;
lastCvxBalance = uint128(cvxAmount + last);
uint256 _periodFinish = periodFinish;
if (block.timestamp < _periodFinish) {
uint256 remaining = _periodFinish - block.timestamp;
prismaAmount += remaining * rewardRate[0];
crvAmount += remaining * rewardRate[1];
cvxAmount += remaining * rewardRate[2];
}
rewardRate[0] = uint128(prismaAmount / REWARD_DURATION);
rewardRate[1] = uint128(crvAmount / REWARD_DURATION);
rewardRate[2] = uint128(cvxAmount / REWARD_DURATION);
lastUpdate = uint32(block.timestamp);
periodFinish = uint32(block.timestamp + REWARD_DURATION);
}
}