forked from hoprnet/hoprnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHoprDistributor.sol
307 lines (253 loc) · 10.9 KB
/
HoprDistributor.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.6.0;
import "@openzeppelin/contracts/access/Ownable.sol";
import "./HoprToken.sol";
contract HoprDistributor is Ownable {
// A {Schedule} that defined when and how much will be claimed
// from an {Allocation}.
// The primary reason we decided to use uint128 is because the allocation
// may be used potentially thousands of times, this helps us reduce
// casting thus lower gas costs.
struct Schedule {
uint128[] durations;
uint128[] percents;
}
// An {Allocation} represents how much a account can claim, claimed,
// and when last claim occured.
// The primary reason we decided to use uint128 is so we can reduce
// our gas costs, since this struct will be stored potentially
// thousands of times.
struct Allocation {
uint128 amount;
uint128 claimed;
uint128 lastClaim;
bool revoked; // account can no longer claim
}
// helps us create more accurate calculations
uint128 public constant MULTIPLIER = 10 ** 6;
// total amount minted
uint128 public totalMinted = 0;
// how many tokens will be minted (the sum of all allocations)
uint128 public totalToBeMinted = 0;
// time where the contract will consider as starting time
uint128 public startTime;
// token which will be used
HoprToken public token;
// maximum tokens allowed to be minted
uint128 public maxMintAmount;
// schedule name -> Schedule
mapping(string => Schedule) internal schedules;
// account -> schedule name -> Allocation
// allows for an account to have more than one type of Schedule
mapping(address => mapping(string => Allocation)) public allocations;
event ScheduleAdded(uint128[] durations, uint128[] percents, string name);
event AllocationAdded(address indexed account, uint128 amount, string scheduleName);
event Claimed(address indexed account, uint128 amount, string scheduleName);
/**
* @param _startTime the timestamp to start counting
* @param _token the token which we will mint
*/
constructor(HoprToken _token, uint128 _startTime, uint128 _maxMintAmount) public {
startTime = _startTime;
token = _token;
maxMintAmount = _maxMintAmount;
}
/**
* @param name the schedule name
* @return the schedule
*/
function getSchedule(string calldata name) external view returns (uint128[] memory, uint128[] memory) {
return (
schedules[name].durations,
schedules[name].percents
);
}
/**
* @dev Allows the owner to update the start time,
* in case there are unforeseen issues in the long schedule.
* @param _startTime the new timestamp to start counting
*/
function updateStartTime(uint128 _startTime) external onlyOwner {
require(startTime > _currentBlockTimestamp(), "Previous start time must not be reached");
startTime = _startTime;
}
/**
* @dev Revokes the ability for an account to claim on the
* specified schedule.
* @param account the account to crevoke
* @param scheduleName the schedule name
*/
function revokeAccount(
address account,
string calldata scheduleName
) external onlyOwner {
Allocation storage allocation = allocations[account][scheduleName];
require(allocation.amount != 0, "Allocation must exist");
require(!allocation.revoked, "Allocation must not be already revoked");
allocation.revoked = true;
totalToBeMinted = _subUint128(totalToBeMinted, _subUint128(allocation.amount, allocation.claimed));
}
/**
* @dev Adds a schedule, the schedule must not already exist.
* Owner is expected to insert values in ascending order,
* each element in arrays {durations} and {percents} is meant to be
* related.
* @param durations the durations for each schedule period in seconds (6 months, 1 year)
* @param percents the percent of how much can be allocated during that period,
* instead of using 100 we scale the value up to {MULTIPLIER} so we can have more accurate
* "percentages".
*/
function addSchedule(
uint128[] calldata durations,
uint128[] calldata percents,
string calldata name
) external onlyOwner {
require(schedules[name].durations.length == 0, "Schedule must not exist");
require(durations.length == percents.length, "Durations and percents must have equal length");
uint128 lastDuration = 0;
uint128 totalPercent = 0;
for (uint256 i = 0; i < durations.length; i++) {
require(lastDuration < durations[i], "Durations must be added in ascending order");
lastDuration = durations[i];
require(percents[i] <= MULTIPLIER, "Percent provided must be smaller or equal to MULTIPLIER");
totalPercent = _addUint128(totalPercent, percents[i]);
}
require(totalPercent == MULTIPLIER, "Percents must sum to MULTIPLIER amount");
schedules[name] = Schedule(durations, percents);
emit ScheduleAdded(durations, percents, name);
}
/**
* @dev Adds allocations, all allocations will use the schedule specified,
* schedule must be created before and account must not have an allocation
* in the specific schedule.
* @param accounts accounts to create allocations for
* @param amounts total amount to be allocated
* @param scheduleName the schedule name
*/
function addAllocations(
address[] calldata accounts,
uint128[] calldata amounts,
string calldata scheduleName
) external onlyOwner {
require(schedules[scheduleName].durations.length != 0, "Schedule must exist");
require(accounts.length == amounts.length, "Accounts and amounts must have equal length");
// gas optimization
uint128 _totalToBeMinted = totalToBeMinted;
for (uint256 i = 0; i < accounts.length; i++) {
require(allocations[accounts[i]][scheduleName].amount == 0, "Allocation must not exist");
allocations[accounts[i]][scheduleName].amount = amounts[i];
_totalToBeMinted = _addUint128(_totalToBeMinted, amounts[i]);
assert(_totalToBeMinted <= maxMintAmount);
emit AllocationAdded(accounts[i], amounts[i], scheduleName);
}
totalToBeMinted = _totalToBeMinted;
}
/**
* @dev Claim tokens by specified a schedule.
* @param scheduleName the schedule name
*/
function claim(string calldata scheduleName) external {
return _claim(msg.sender, scheduleName);
}
/**
* @dev Claim tokens for a specific account by specified a schedule.
* @param account the account to claim for
* @param scheduleName the schedule name
*/
function claimFor(address account, string calldata scheduleName) external {
return _claim(account, scheduleName);
}
/**
* @param account the account to get claimable for
* @param scheduleName the schedule name
* @return claimable amount
*/
function getClaimable(address account, string calldata scheduleName) external view returns (uint128) {
return _getClaimable(schedules[scheduleName], allocations[account][scheduleName]);
}
/**
* @dev Claim claimable tokens, this will mint tokens.
* @param account the account to claim for
* @param scheduleName the schedule name
*/
function _claim(address account, string memory scheduleName) internal {
Allocation storage allocation = allocations[account][scheduleName];
require(allocation.amount > 0, "There is nothing to claim");
require(!allocation.revoked, "Account is revoked");
Schedule storage schedule = schedules[scheduleName];
uint128 claimable = _getClaimable(schedule, allocation);
// Trying to claim more than allocated
assert(claimable <= allocation.amount);
uint128 newClaimed = _addUint128(allocation.claimed, claimable);
// Trying to claim more than allocated
assert(newClaimed <= allocation.amount);
uint128 newTotalMinted = _addUint128(totalMinted, claimable);
// Total amount minted should be less or equal than specified
// we only check this when a user claims, not when allocations
// are added
assert(newTotalMinted <= maxMintAmount);
totalMinted = newTotalMinted;
allocation.claimed = newClaimed;
allocation.lastClaim = _currentBlockTimestamp();
// mint tokens
token.mint(account, claimable, "", "");
emit Claimed(account, claimable, scheduleName);
}
/**
* @dev Calculates claimable tokens.
* This function expects that the owner has added the schedule
* periods in ascending order.
*/
function _getClaimable(
Schedule storage schedule,
Allocation storage allocation
) internal view returns (uint128) {
// first unlock hasn't passed yet
if (_addUint128(startTime, schedule.durations[0]) > _currentBlockTimestamp()) {
return 0;
}
// last unlock has passed
if (_addUint128(startTime, schedule.durations[schedule.durations.length - 1]) < _currentBlockTimestamp()) {
// make sure to exclude already claimed amount
return _subUint128(allocation.amount, allocation.claimed);
}
uint128 claimable = 0;
for (uint256 i = 0; i < schedule.durations.length; i++) {
uint128 scheduleDeadline = _addUint128(startTime, schedule.durations[i]);
// schedule deadline not passed, exiting
if (scheduleDeadline > _currentBlockTimestamp()) break;
// already claimed during this period, skipping
if (allocation.lastClaim >= scheduleDeadline) continue;
claimable = _addUint128(claimable, _divUint128(_mulUint128(allocation.amount, schedule.percents[i]), MULTIPLIER));
}
return claimable;
}
function _currentBlockTimestamp() internal view returns (uint128) {
// solhint-disable-next-line
return uint128(block.timestamp);
}
// SafeMath variations
function _addUint128(uint128 a, uint128 b) internal pure returns (uint128) {
uint128 c = a + b;
require(c >= a, "uint128 addition overflow");
return c;
}
function _subUint128(uint128 a, uint128 b) internal pure returns (uint128) {
require(b <= a, "uint128 subtraction overflow");
uint128 c = a - b;
return c;
}
function _mulUint128(uint128 a, uint128 b) internal pure returns (uint128) {
if (a == 0) {
return 0;
}
uint128 c = a * b;
require(c / a == b, "uint128 multiplication overflow");
return c;
}
function _divUint128(uint128 a, uint128 b) internal pure returns (uint128) {
require(b > 0, "uint128 division by zero");
uint128 c = a / b;
return c;
}
}