-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVoEqb.sol
268 lines (219 loc) · 8.43 KB
/
VoEqb.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol";
contract VoEqb is OwnableUpgradeable, ReentrancyGuardUpgradeable {
using SafeERC20 for IERC20;
IERC20 public eqb;
uint256 public constant WEEK = 86400 * 7;
uint256 public constant MAX_LOCK_WEEKS = 52;
struct LockData {
mapping(uint256 => uint256) weeklyWeight;
mapping(uint256 => uint256) weeklyUnlock;
uint256 lastUnlockedWeek;
}
// user address => LockData
mapping(address => LockData) public userLockData;
mapping(uint256 => uint256) public weeklyTotalWeight;
// when set to true, other accounts cannot call `lock` on behalf of an account
mapping(address => bool) public blockThirdPartyActions;
event LockCreated(address indexed _user, uint256 _amount, uint256 _weeks);
event LockExtended(
address indexed _user,
uint256 _amount,
uint256 _oldWeeks,
uint256 _newWeeks
);
event Unlocked(
address indexed _user,
uint256 _amount,
uint256 _lastUnlockedWeek
);
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
function initialize(address _eqb) public initializer {
require(_eqb != address(0), "invalid _eqb!");
__Ownable_init();
__ReentrancyGuard_init_unchained();
eqb = IERC20(_eqb);
}
function name() external pure returns (string memory) {
return "vote-only EQB";
}
function symbol() external pure returns (string memory) {
return "voEQB";
}
function decimals() external pure returns (uint8) {
return 18;
}
function totalSupply() public view returns (uint256) {
return totalWeight();
}
function balanceOf(address _user) public view returns (uint256) {
return userWeight(_user);
}
function userWeight(address _user) public view returns (uint256) {
return userLockData[_user].weeklyWeight[_getCurWeek()];
}
function userWeightAt(
address _user,
uint256 _week
) public view returns (uint256) {
return userLockData[_user].weeklyWeight[_week];
}
function userUnlock(address _user) external view returns (uint256) {
return userLockData[_user].weeklyUnlock[_getCurWeek()];
}
function userUnlockAt(
address _user,
uint256 _week
) external view returns (uint256) {
return userLockData[_user].weeklyUnlock[_week];
}
function totalWeight() public view returns (uint256) {
return weeklyTotalWeight[_getCurWeek()];
}
function totalWeightAt(uint256 _week) public view returns (uint256) {
return weeklyTotalWeight[_week];
}
function getActiveLocks(
address _user
) external view returns (uint256[2][] memory) {
uint256 nextWeek = _getNextWeek();
uint256[] memory unlocks = new uint256[](MAX_LOCK_WEEKS + 1);
uint256 unlockNum = 0;
for (uint256 i = 0; i <= MAX_LOCK_WEEKS; i++) {
unlocks[i] = userLockData[_user].weeklyUnlock[
nextWeek + (i * WEEK)
];
if (unlocks[i] > 0) {
unlockNum++;
}
}
uint256[2][] memory lockData = new uint256[2][](unlockNum);
uint256 j = 0;
for (uint256 i = 0; i <= MAX_LOCK_WEEKS; i++) {
if (unlocks[i] > 0) {
lockData[j] = [nextWeek + (i * WEEK), unlocks[i]];
j++;
}
}
return lockData;
}
// Get the amount of eqb in expired locks that is eligible to be released
function getUnlockable(address _user) public view returns (uint256) {
uint256 finishedWeek = _getCurWeek();
LockData storage data = userLockData[_user];
// return 0 if user has never locked
if (data.lastUnlockedWeek == 0) {
return 0;
}
uint256 amount;
for (
uint256 cur = data.lastUnlockedWeek + WEEK;
cur <= finishedWeek;
cur = cur + WEEK
) {
amount = amount + data.weeklyUnlock[cur];
}
return amount;
}
// Allow or block third-party calls on behalf of the caller
function setBlockThirdPartyActions(bool _block) external {
blockThirdPartyActions[msg.sender] = _block;
}
function lock(
address _user,
uint256 _amount,
uint256 _weeks
) external nonReentrant {
require(_user != address(0), "invalid _user!");
require(
msg.sender == _user || !blockThirdPartyActions[_user],
"Cannot lock on behalf of this account"
);
require(_weeks > 0, "Min 1 week");
require(_weeks <= MAX_LOCK_WEEKS, "Exceeds MAX_LOCK_WEEKS");
require(_amount > 0, "Amount must be nonzero");
eqb.safeTransferFrom(msg.sender, address(this), _amount);
uint256 start = _getNextWeek();
_increaseAmount(_user, start, _amount, _weeks, 0);
uint256 end = start + (_weeks * WEEK);
userLockData[_user].weeklyUnlock[end] =
userLockData[_user].weeklyUnlock[end] +
_amount;
uint256 curWeek = _getCurWeek();
if (userLockData[_user].lastUnlockedWeek == 0) {
userLockData[_user].lastUnlockedWeek = curWeek;
}
emit LockCreated(_user, _amount, _weeks);
}
/**
@notice Extend the length of an existing lock.
@param _amount Amount of tokens to extend the lock for. When the value given equals
the total size of the existing lock, the entire lock is moved.
If the amount is less, then the lock is effectively split into
two locks, with a portion of the balance extended to the new length
and the remaining balance at the old length.
@param _weeks The number of weeks for the lock that is being extended.
@param _newWeeks The number of weeks to extend the lock until.
*/
function extendLock(
uint256 _amount,
uint256 _weeks,
uint256 _newWeeks
) external nonReentrant {
require(_weeks > 0, "Min 1 week");
require(_newWeeks <= MAX_LOCK_WEEKS, "Exceeds MAX_LOCK_WEEKS");
require(_weeks < _newWeeks, "newWeeks must be greater than weeks");
require(_amount > 0, "Amount must be nonzero");
LockData storage data = userLockData[msg.sender];
uint256 start = _getNextWeek();
uint256 oldEnd = start + (_weeks * WEEK);
require(_amount <= data.weeklyUnlock[oldEnd], "invalid amount");
data.weeklyUnlock[oldEnd] = data.weeklyUnlock[oldEnd] - _amount;
uint256 end = start + (_newWeeks * WEEK);
data.weeklyUnlock[end] = data.weeklyUnlock[end] + _amount;
_increaseAmount(msg.sender, start, _amount, _newWeeks, _weeks);
emit LockExtended(msg.sender, _amount, _weeks, _newWeeks);
}
function unlock() external nonReentrant {
uint256 amount = getUnlockable(msg.sender);
if (amount != 0) {
eqb.safeTransfer(msg.sender, amount);
}
uint256 lastUnlockedWeek = _getCurWeek();
userLockData[msg.sender].lastUnlockedWeek = lastUnlockedWeek;
emit Unlocked(msg.sender, amount, lastUnlockedWeek);
}
function _getCurWeek() internal view returns (uint256) {
return (block.timestamp / WEEK) * WEEK;
}
function _getNextWeek() internal view returns (uint256) {
return _getCurWeek() + WEEK;
}
/**
@dev Increase the amount within a lock weight array over a given time period
*/
function _increaseAmount(
address _user,
uint256 _start,
uint256 _amount,
uint256 _rounds,
uint256 _oldRounds
) internal {
LockData storage data = userLockData[_user];
for (uint256 i = 0; i < _rounds; i++) {
uint256 curWeek = _start + (i * WEEK);
uint256 amount = _amount * (_rounds - i);
if (i < _oldRounds) {
amount = amount - (_amount * (_oldRounds - i));
}
data.weeklyWeight[curWeek] = data.weeklyWeight[curWeek] + amount;
weeklyTotalWeight[curWeek] = weeklyTotalWeight[curWeek] + amount;
}
}
}