/
TokenDistro.sol
444 lines (385 loc) · 14.7 KB
/
TokenDistro.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// SPDX-License-Identifier: GPL-3.0
pragma solidity =0.8.6;
import "openzeppelin-contracts-upgradable-v4/token/ERC20/utils/SafeERC20Upgradeable.sol";
import "openzeppelin-contracts-upgradable-v4/token/ERC20/IERC20Upgradeable.sol";
import "openzeppelin-contracts-upgradable-v4/access/AccessControlEnumerableUpgradeable.sol";
import "openzeppelin-contracts-upgradable-v4/proxy/utils/Initializable.sol";
import "../Interfaces/IDistro.sol";
/**
* Contract responsible for managing the release of tokens over time.
* The distributor is in charge of releasing the corresponding amounts to its recipients.
* This distributor is expected to be another smart contract, such as a merkledrop or the liquidity mining smart contract
*/
contract TokenDistro is
Initializable,
IDistro,
AccessControlEnumerableUpgradeable
{
using SafeERC20Upgradeable for IERC20Upgradeable;
// bytes32 public constant DISTRIBUTOR_ROLE = keccak256("DISTRIBUTOR_ROLE");
bytes32 public constant DISTRIBUTOR_ROLE =
0xfbd454f36a7e1a388bd6fc3ab10d434aa4578f811acbbcf33afb1c697486313c;
// Structure to of the accounting for each account
struct accountStatus {
uint256 allocatedTokens;
uint256 claimed;
}
mapping(address => accountStatus) public balances; // Mapping with all accounts that have received an allocation
uint256 public override totalTokens; // total tokens to be distribute
uint256 public startTime; // Instant of time in which distribution begins
uint256 public cliffTime; // Instant of time in which tokens will begin to be released
uint256 public duration;
uint256 public initialAmount; // Initial amount that will be available from startTime
uint256 public lockedAmount; // Amount that will be released over time from cliffTime
IERC20Upgradeable public token; // Token to be distribute
bool public cancelable; // Variable that allows the ADMIN_ROLE to cancel an allocation
/**
* @dev Emitted when the DISTRIBUTOR allocate an amount of givBack to a recipient
*/
event GivBackPaid(address distributor);
/**
* @dev Emitted when the duration is changed
*/
event DurationChanged(uint256 newDuration);
modifier onlyDistributor() {
require(
hasRole(DISTRIBUTOR_ROLE, msg.sender),
"TokenDistro::onlyDistributor: ONLY_DISTRIBUTOR_ROLE"
);
require(
balances[msg.sender].claimed == 0,
"TokenDistro::onlyDistributor: DISTRIBUTOR_CANNOT_CLAIM"
);
_;
}
/**
* @dev Initially the deployer of the contract will be able to assign the tokens to one or several addresses,
* these addresses (EOA or Smart Contracts) are responsible to allocate tokens to specific addresses which can
* later claim them
* @param _totalTokens Total amount of tokens to distribute
* @param _startTime Unix time that the distribution begins
* @param _cliffPeriod Number of seconds to delay the claiming period for the tokens not initially released
* @param _duration Time it will take for all tokens to be distributed
* @param _initialPercentage Percentage of tokens initially released (2 decimals, 1/10000)
* @param _token Address of the token to distribute
* @param _cancelable In case the owner wants to have the power to cancel an assignment
*/
function initialize(
uint256 _totalTokens,
uint256 _startTime,
uint256 _cliffPeriod,
uint256 _duration,
uint256 _initialPercentage,
IERC20Upgradeable _token,
bool _cancelable
) public initializer {
require(
_duration >= _cliffPeriod,
"TokenDistro::constructor: DURATION_LESS_THAN_CLIFF"
);
require(
_initialPercentage <= 10000,
"TokenDistro::constructor: INITIALPERCENTAGE_GREATER_THAN_100"
);
__AccessControlEnumerable_init();
_setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
uint256 _initialAmount = (_totalTokens * _initialPercentage) / 10000;
token = _token;
duration = _duration;
startTime = _startTime;
totalTokens = _totalTokens;
initialAmount = _initialAmount;
cliffTime = _startTime + _cliffPeriod;
lockedAmount = _totalTokens - _initialAmount;
balances[address(this)].allocatedTokens = _totalTokens;
cancelable = _cancelable;
}
/**
* Function that allows the DEFAULT_ADMIN_ROLE to assign set a new startTime if it hasn't started yet
* @param newStartTime new startTime
*
* Emits a {StartTimeChanged} event.
*
*/
function setStartTime(uint256 newStartTime) external override {
require(
hasRole(DEFAULT_ADMIN_ROLE, msg.sender),
"TokenDistro::setStartTime: ONLY_ADMIN_ROLE"
);
require(
startTime > getTimestamp() && newStartTime > getTimestamp(),
"TokenDistro::setStartTime: IF_HAS_NOT_STARTED_YET"
);
uint256 _cliffPeriod = cliffTime - startTime;
startTime = newStartTime;
cliffTime = newStartTime + _cliffPeriod;
emit StartTimeChanged(startTime, cliffTime);
}
/**
* Function that allows the DEFAULT_ADMIN_ROLE to assign tokens to an address who later can distribute them.
* @dev It is required that the DISTRIBUTOR_ROLE is already held by the address to which an amount will be assigned
* @param distributor the address, generally a smart contract, that will determine who gets how many tokens
* @param amount Total amount of tokens to assign to that address for distributing
*
* Emits a {Assign} event.
*
*/
function assign(address distributor, uint256 amount) external override {
require(
hasRole(DEFAULT_ADMIN_ROLE, msg.sender),
"TokenDistro::assign: ONLY_ADMIN_ROLE"
);
require(
hasRole(DISTRIBUTOR_ROLE, distributor),
"TokenDistro::assign: ONLY_TO_DISTRIBUTOR_ROLE"
);
balances[address(this)].allocatedTokens =
balances[address(this)].allocatedTokens -
amount;
balances[distributor].allocatedTokens =
balances[distributor].allocatedTokens +
amount;
emit Assign(msg.sender, distributor, amount);
}
/**
* Function to claim tokens for a specific address. It uses the current timestamp
*
* Emits a {claim} event.
*
*/
function claimTo(address account) external {
// This check is not necessary as it does not break anything, just changes the claimed value
// for this contract
//require(address(this) != account, "TokenDistro::claimTo: CANNOT_CLAIM_FOR_CONTRACT_ITSELF");
_claim(account);
}
/**
* Function to claim tokens for a specific address. It uses the current timestamp
*
* Emits a {claim} event.
*
*/
function claim() external override {
_claim(msg.sender);
}
/**
* Function that allows to the distributor address to allocate some amount of tokens to a specific recipient
* @param recipient of token allocation
* @param amount allocated amount
* @param claim whether claim after allocate
*
* Emits a {Allocate} event.
*
*/
function _allocate(
address recipient,
uint256 amount,
bool claim
) internal {
require(
!hasRole(DISTRIBUTOR_ROLE, recipient),
"TokenDistro::allocate: DISTRIBUTOR_NOT_VALID_RECIPIENT"
);
balances[msg.sender].allocatedTokens =
balances[msg.sender].allocatedTokens -
amount;
balances[recipient].allocatedTokens =
balances[recipient].allocatedTokens +
amount;
if (claim && claimableNow(recipient) > 0) {
_claim(recipient);
}
emit Allocate(msg.sender, recipient, amount);
}
function allocate(
address recipient,
uint256 amount,
bool claim
) external override onlyDistributor {
_allocate(recipient, amount, claim);
}
/**
* Function that allows to the distributor address to allocate some amounts of tokens to specific recipients
* @dev Needs to be initialized: Nobody has the DEFAULT_ADMIN_ROLE and all available tokens have been assigned
* @param recipients of token allocation
* @param amounts allocated amount
*
* Unlike allocate method it doesn't claim recipients available balance
*/
function _allocateMany(
address[] memory recipients,
uint256[] memory amounts
) internal onlyDistributor {
require(
recipients.length == amounts.length,
"TokenDistro::allocateMany: INPUT_LENGTH_NOT_MATCH"
);
for (uint256 i = 0; i < recipients.length; i++) {
_allocate(recipients[i], amounts[i], false);
}
}
function allocateMany(address[] memory recipients, uint256[] memory amounts)
external
override
{
_allocateMany(recipients, amounts);
}
function sendGIVbacks(address[] memory recipients, uint256[] memory amounts)
external
override
{
_allocateMany(recipients, amounts);
emit GivBackPaid(msg.sender);
}
/**
* Function that allows a recipient to change its address
* @dev The change can only be made to an address that has not previously received an allocation &
* the distributor cannot change its address
*
* Emits a {ChangeAddress} event.
*
*/
function changeAddress(address newAddress) external override {
require(
balances[newAddress].allocatedTokens == 0 &&
balances[newAddress].claimed == 0,
"TokenDistro::changeAddress: ADDRESS_ALREADY_IN_USE"
);
require(
!hasRole(DISTRIBUTOR_ROLE, msg.sender) &&
!hasRole(DISTRIBUTOR_ROLE, newAddress),
"TokenDistro::changeAddress: DISTRIBUTOR_ROLE_NOT_A_VALID_ADDRESS"
);
balances[newAddress].allocatedTokens = balances[msg.sender]
.allocatedTokens;
balances[msg.sender].allocatedTokens = 0;
balances[newAddress].claimed = balances[msg.sender].claimed;
balances[msg.sender].claimed = 0;
emit ChangeAddress(msg.sender, newAddress);
}
/**
* Function to get the current timestamp from the block
*/
function getTimestamp() public view virtual override returns (uint256) {
return block.timestamp;
}
/**
* Function to get the total claimable tokens at some moment
* @param timestamp Unix time to check the number of tokens claimable
* @return Number of tokens claimable at that timestamp
*/
function globallyClaimableAt(uint256 timestamp)
public
view
override
returns (uint256)
{
if (timestamp < startTime) return 0;
if (timestamp < cliffTime) return initialAmount;
if (timestamp > startTime + duration) return totalTokens;
uint256 deltaTime = timestamp - startTime;
return initialAmount + (deltaTime * lockedAmount) / duration;
}
/**
* Function to get the unlocked tokes at some moment for a specific address
* @param recipient account to query
* @param timestamp Instant of time in which the calculation is made
*/
function claimableAt(address recipient, uint256 timestamp)
public
view
override
returns (uint256)
{
require(
!hasRole(DISTRIBUTOR_ROLE, recipient),
"TokenDistro::claimableAt: DISTRIBUTOR_ROLE_CANNOT_CLAIM"
);
require(
timestamp >= getTimestamp(),
"TokenDistro::claimableAt: NOT_VALID_PAST_TIMESTAMP"
);
uint256 unlockedAmount = (globallyClaimableAt(timestamp) *
balances[recipient].allocatedTokens) / totalTokens;
return unlockedAmount - balances[recipient].claimed;
}
/**
* Function to get the unlocked tokens for a specific address. It uses the current timestamp
* @param recipient account to query
*/
function claimableNow(address recipient)
public
view
override
returns (uint256)
{
return claimableAt(recipient, getTimestamp());
}
/**
* Function that allows the DEFAULT_ADMIN_ROLE to change a recipient in case it wants to cancel an allocation
* @dev The change can only be made when cancelable is true and to an address that has not previously received
* an allocation and the distributor cannot change its address
*
* Emits a {ChangeAddress} event.
*
*/
function cancelAllocation(address prevRecipient, address newRecipient)
external
override
{
require(cancelable, "TokenDistro::cancelAllocation: NOT_CANCELABLE");
require(
hasRole(DEFAULT_ADMIN_ROLE, msg.sender),
"TokenDistro::cancelAllocation: ONLY_ADMIN_ROLE"
);
require(
balances[newRecipient].allocatedTokens == 0 &&
balances[newRecipient].claimed == 0,
"TokenDistro::cancelAllocation: ADDRESS_ALREADY_IN_USE"
);
require(
!hasRole(DISTRIBUTOR_ROLE, prevRecipient) &&
!hasRole(DISTRIBUTOR_ROLE, newRecipient),
"TokenDistro::cancelAllocation: DISTRIBUTOR_ROLE_NOT_A_VALID_ADDRESS"
);
balances[newRecipient].allocatedTokens = balances[prevRecipient]
.allocatedTokens;
balances[prevRecipient].allocatedTokens = 0;
balances[newRecipient].claimed = balances[prevRecipient].claimed;
balances[prevRecipient].claimed = 0;
emit ChangeAddress(prevRecipient, newRecipient);
}
/**
* Function to claim tokens for a specific address. It uses the current timestamp
*
* Emits a {claim} event.
*
*/
function _claim(address recipient) private {
uint256 remainingToClaim = claimableNow(recipient);
require(
remainingToClaim > 0,
"TokenDistro::claim: NOT_ENOUGH_TOKENS_TO_CLAIM"
);
balances[recipient].claimed =
balances[recipient].claimed +
remainingToClaim;
token.safeTransfer(recipient, remainingToClaim);
emit Claim(recipient, remainingToClaim);
}
/**
* Function to change the duration
*/
function setDuration(uint256 newDuration) public {
require(
hasRole(DEFAULT_ADMIN_ROLE, msg.sender),
"TokenDistro::setDuration: ONLY_ADMIN_ROLE"
);
require(
startTime > getTimestamp(),
"TokenDistro::setDuration: IF_HAS_NOT_STARTED_YET"
);
duration = newDuration;
emit DurationChanged(newDuration);
}
}