forked from stephenbyrne99/solidly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathve_dist.sol
355 lines (296 loc) · 12.3 KB
/
ve_dist.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
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.11;
/*
@title Curve Fee Distribution modified for ve(3,3) emissions
@author Curve Finance, andrecronje
@license MIT
*/
interface erc20 {
function totalSupply() external view returns (uint256);
function transfer(address recipient, uint amount) external returns (bool);
function decimals() external view returns (uint8);
function symbol() external view returns (string memory);
function balanceOf(address) external view returns (uint);
function transferFrom(address sender, address recipient, uint amount) external returns (bool);
function approve(address spender, uint value) external returns (bool);
}
library Math {
function min(uint a, uint b) internal pure returns (uint) {
return a < b ? a : b;
}
function max(uint a, uint b) internal pure returns (uint) {
return a >= b ? a : b;
}
}
interface VotingEscrow {
struct Point {
int128 bias;
int128 slope; // # -dweight / dt
uint256 ts;
uint256 blk; // block
}
function user_point_epoch(uint tokenId) external view returns (uint);
function epoch() external view returns (uint);
function user_point_history(uint tokenId, uint loc) external view returns (Point memory);
function point_history(uint loc) external view returns (Point memory);
function checkpoint() external;
function deposit_for(uint tokenId, uint value) external;
function token() external view returns (address);
}
contract ve_dist {
event CheckpointToken(
uint time,
uint tokens
);
event Claimed(
uint tokenId,
uint amount,
uint claim_epoch,
uint max_epoch
);
uint constant WEEK = 7 * 86400;
uint public start_time;
uint public time_cursor;
mapping(uint => uint) public time_cursor_of;
mapping(uint => uint) public user_epoch_of;
uint public last_token_time;
uint[1000000000000000] public tokens_per_week;
address public voting_escrow;
address public token;
uint public token_last_balance;
uint[1000000000000000] public ve_supply;
address public depositor;
constructor(address _voting_escrow) {
uint _t = block.timestamp / WEEK * WEEK;
start_time = _t;
last_token_time = _t;
time_cursor = _t;
address _token = VotingEscrow(_voting_escrow).token();
token = _token;
voting_escrow = _voting_escrow;
depositor = msg.sender;
erc20(_token).approve(_voting_escrow, type(uint).max);
}
function timestamp() external view returns (uint) {
return block.timestamp / WEEK * WEEK;
}
function _checkpoint_token() internal {
uint token_balance = erc20(token).balanceOf(address(this));
uint to_distribute = token_balance - token_last_balance;
token_last_balance = token_balance;
uint t = last_token_time;
uint since_last = block.timestamp - t;
last_token_time = block.timestamp;
uint this_week = t / WEEK * WEEK;
uint next_week = 0;
for (uint i = 0; i < 20; i++) {
next_week = this_week + WEEK;
if (block.timestamp < next_week) {
if (since_last == 0 && block.timestamp == t) {
tokens_per_week[this_week] += to_distribute;
} else {
tokens_per_week[this_week] += to_distribute * (block.timestamp - t) / since_last;
}
break;
} else {
if (since_last == 0 && next_week == t) {
tokens_per_week[this_week] += to_distribute;
} else {
tokens_per_week[this_week] += to_distribute * (next_week - t) / since_last;
}
}
t = next_week;
this_week = next_week;
}
emit CheckpointToken(block.timestamp, to_distribute);
}
function checkpoint_token() external {
assert(msg.sender == depositor);
_checkpoint_token();
}
function _find_timestamp_epoch(address ve, uint _timestamp) internal view returns (uint) {
uint _min = 0;
uint _max = VotingEscrow(ve).epoch();
for (uint i = 0; i < 128; i++) {
if (_min >= _max) break;
uint _mid = (_min + _max + 2) / 2;
VotingEscrow.Point memory pt = VotingEscrow(ve).point_history(_mid);
if (pt.ts <= _timestamp) {
_min = _mid;
} else {
_max = _mid - 1;
}
}
return _min;
}
function _find_timestamp_user_epoch(address ve, uint tokenId, uint _timestamp, uint max_user_epoch) internal view returns (uint) {
uint _min = 0;
uint _max = max_user_epoch;
for (uint i = 0; i < 128; i++) {
if (_min >= _max) break;
uint _mid = (_min + _max + 2) / 2;
VotingEscrow.Point memory pt = VotingEscrow(ve).user_point_history(tokenId, _mid);
if (pt.ts <= _timestamp) {
_min = _mid;
} else {
_max = _mid -1;
}
}
return _min;
}
function ve_for_at(uint _tokenId, uint _timestamp) external view returns (uint) {
address ve = voting_escrow;
uint max_user_epoch = VotingEscrow(ve).user_point_epoch(_tokenId);
uint epoch = _find_timestamp_user_epoch(ve, _tokenId, _timestamp, max_user_epoch);
VotingEscrow.Point memory pt = VotingEscrow(ve).user_point_history(_tokenId, epoch);
return Math.max(uint(int256(pt.bias - pt.slope * (int128(int256(_timestamp - pt.ts))))), 0);
}
function _checkpoint_total_supply() internal {
address ve = voting_escrow;
uint t = time_cursor;
uint rounded_timestamp = block.timestamp / WEEK * WEEK;
VotingEscrow(ve).checkpoint();
for (uint i = 0; i < 20; i++) {
if (t > rounded_timestamp) {
break;
} else {
uint epoch = _find_timestamp_epoch(ve, t);
VotingEscrow.Point memory pt = VotingEscrow(ve).point_history(epoch);
int128 dt = 0;
if (t > pt.ts) {
dt = int128(int256(t - pt.ts));
}
ve_supply[t] = Math.max(uint(int256(pt.bias - pt.slope * dt)), 0);
}
t += WEEK;
}
time_cursor = t;
}
function checkpoint_total_supply() external {
_checkpoint_total_supply();
}
function _claim(uint _tokenId, address ve, uint _last_token_time) internal returns (uint) {
uint user_epoch = 0;
uint to_distribute = 0;
uint max_user_epoch = VotingEscrow(ve).user_point_epoch(_tokenId);
uint _start_time = start_time;
if (max_user_epoch == 0) return 0;
uint week_cursor = time_cursor_of[_tokenId];
if (week_cursor == 0) {
user_epoch = _find_timestamp_user_epoch(ve, _tokenId, _start_time, max_user_epoch);
} else {
user_epoch = user_epoch_of[_tokenId];
}
if (user_epoch == 0) user_epoch = 1;
VotingEscrow.Point memory user_point = VotingEscrow(ve).user_point_history(_tokenId, user_epoch);
if (week_cursor == 0) week_cursor = (user_point.ts + WEEK - 1) / WEEK * WEEK;
if (week_cursor >= last_token_time) return 0;
if (week_cursor < _start_time) week_cursor = _start_time;
VotingEscrow.Point memory old_user_point;
for (uint i = 0; i < 50; i++) {
if (week_cursor >= _last_token_time) break;
if (week_cursor >= user_point.ts && user_epoch <= max_user_epoch) {
user_epoch += 1;
old_user_point = user_point;
if (user_epoch > max_user_epoch) {
user_point = VotingEscrow.Point(0,0,0,0);
} else {
user_point = VotingEscrow(ve).user_point_history(_tokenId, user_epoch);
}
} else {
int128 dt = int128(int256(week_cursor - old_user_point.ts));
uint balance_of = Math.max(uint(int256(old_user_point.bias - dt * old_user_point.slope)), 0);
if (balance_of == 0 && user_epoch > max_user_epoch) break;
if (balance_of > 0) {
to_distribute += balance_of * tokens_per_week[week_cursor] / ve_supply[week_cursor];
}
week_cursor += WEEK;
}
}
user_epoch = Math.min(max_user_epoch, user_epoch - 1);
user_epoch_of[_tokenId] = user_epoch;
time_cursor_of[_tokenId] = week_cursor;
emit Claimed(_tokenId, to_distribute, user_epoch, max_user_epoch);
return to_distribute;
}
function _claimable(uint _tokenId, address ve, uint _last_token_time) internal view returns (uint) {
uint user_epoch = 0;
uint to_distribute = 0;
uint max_user_epoch = VotingEscrow(ve).user_point_epoch(_tokenId);
uint _start_time = start_time;
if (max_user_epoch == 0) return 0;
uint week_cursor = time_cursor_of[_tokenId];
if (week_cursor == 0) {
user_epoch = _find_timestamp_user_epoch(ve, _tokenId, _start_time, max_user_epoch);
} else {
user_epoch = user_epoch_of[_tokenId];
}
if (user_epoch == 0) user_epoch = 1;
VotingEscrow.Point memory user_point = VotingEscrow(ve).user_point_history(_tokenId, user_epoch);
if (week_cursor == 0) week_cursor = (user_point.ts + WEEK - 1) / WEEK * WEEK;
if (week_cursor >= last_token_time) return 0;
if (week_cursor < _start_time) week_cursor = _start_time;
VotingEscrow.Point memory old_user_point;
for (uint i = 0; i < 50; i++) {
if (week_cursor >= _last_token_time) break;
if (week_cursor >= user_point.ts && user_epoch <= max_user_epoch) {
user_epoch += 1;
old_user_point = user_point;
if (user_epoch > max_user_epoch) {
user_point = VotingEscrow.Point(0,0,0,0);
} else {
user_point = VotingEscrow(ve).user_point_history(_tokenId, user_epoch);
}
} else {
int128 dt = int128(int256(week_cursor - old_user_point.ts));
uint balance_of = Math.max(uint(int256(old_user_point.bias - dt * old_user_point.slope)), 0);
if (balance_of == 0 && user_epoch > max_user_epoch) break;
if (balance_of > 0) {
to_distribute += balance_of * tokens_per_week[week_cursor] / ve_supply[week_cursor];
}
week_cursor += WEEK;
}
}
return to_distribute;
}
function claimable(uint _tokenId) external view returns (uint) {
uint _last_token_time = last_token_time / WEEK * WEEK;
return _claimable(_tokenId, voting_escrow, _last_token_time);
}
function claim(uint _tokenId) external returns (uint) {
if (block.timestamp >= time_cursor) _checkpoint_total_supply();
uint _last_token_time = last_token_time;
_last_token_time = _last_token_time / WEEK * WEEK;
uint amount = _claim(_tokenId, voting_escrow, _last_token_time);
if (amount != 0) {
VotingEscrow(voting_escrow).deposit_for(_tokenId, amount);
token_last_balance -= amount;
}
return amount;
}
function claim_many(uint[] memory _tokenIds) external returns (bool) {
if (block.timestamp >= time_cursor) _checkpoint_total_supply();
uint _last_token_time = last_token_time;
_last_token_time = _last_token_time / WEEK * WEEK;
address _voting_escrow = voting_escrow;
uint total = 0;
for (uint i = 0; i < _tokenIds.length; i++) {
uint _tokenId = _tokenIds[i];
if (_tokenId == 0) break;
uint amount = _claim(_tokenId, _voting_escrow, _last_token_time);
if (amount != 0) {
VotingEscrow(_voting_escrow).deposit_for(_tokenId, amount);
total += amount;
}
}
if (total != 0) {
token_last_balance -= total;
}
return true;
}
// Once off event on contract initialize
function setDepositor(address _depositor) external {
require(msg.sender == depositor);
depositor = _depositor;
}
}