-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStaking.sol
370 lines (297 loc) · 10.9 KB
/
Staking.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
// SPDX-License-Identifier: MIT
/**
* Copyright (C) SettleMint NV - All Rights Reserved
*
* Use of this file is strictly prohibited without an active license agreement.
* Distribution of this file, via any medium, is strictly prohibited.
*
* For license inquiries, contact [email protected]
*/
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
// https://docs.openzeppelin.com/contracts/4.x/api/proxy#transparent-vs-uups
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
contract Staking is Initializable, UUPSUpgradeable, AccessControlUpgradeable, PausableUpgradeable {
using Counters for Counters.Counter;
using EnumerableSet for EnumerableSet.AddressSet;
IERC20 dtxToken;
Counters.Counter private payoutCounter;
EnumerableSet.AddressSet private stakeholders;
struct Stake {
uint256 lockedDTX;
uint256 startTimestamp;
}
bytes32 private SUPER_ADMIN_ROLE;
bytes32 private ADMIN_ROLE;
uint256 private lastPayoutTimestamp;
uint256 private totalStakes;
mapping(address => Stake) private stakeholderToStake;
mapping(address => uint256) private claimRewards;
mapping(uint256 => mapping(address => uint256)) private payoutCredits;
event StakeTransactions(
address indexed stakeholder,
uint256 amount,
uint8 txType,
uint256 txTimestamp
);
event Rewards(address indexed stakeholder, uint256 amount, uint256 timestamp);
modifier hasSuperAdminRole() {
require(
hasRole(SUPER_ADMIN_ROLE, msg.sender),
"Caller is not a super admin"
);
_;
}
modifier hasAdminRole() {
require(hasRole(ADMIN_ROLE, msg.sender), "Caller is not an admin");
_;
}
function initialize(
address superAdmin,
address admin,
address _dtxToken
) public initializer {
AccessControlUpgradeable.__AccessControl_init();
PausableUpgradeable.__Pausable_init();
SUPER_ADMIN_ROLE = keccak256("SUPER_ADMIN_ROLE");
ADMIN_ROLE = keccak256("ADMIN_ROLE");
// Set up roles
_setupRole(SUPER_ADMIN_ROLE, superAdmin);
_setupRole(ADMIN_ROLE, superAdmin);
_setupRole(ADMIN_ROLE, admin);
dtxToken = IERC20(_dtxToken);
}
function _authorizeUpgrade(address) internal override hasSuperAdminRole {}
function isStakeholder(address stakeholder) public view returns (bool) {
return stakeholders.contains(stakeholder);
}
function addStakeholder(address stakeholder) internal whenNotPaused() {
bool _isStakeholder = stakeholders.contains(stakeholder);
if (!_isStakeholder) stakeholders.add(stakeholder);
}
function getLockedStakeDetails(address stakeholder)
public
view
returns (Stake memory)
{
return
stakeholderToStake[stakeholder];
}
function getPayoutCredits(uint256 payoutCycle, address stakeholder)
public
view
returns (uint256)
{
return payoutCredits[payoutCycle][stakeholder];
}
function createStake(address stakeholder, uint256 stake) public whenNotPaused() {
bool transferResult = dtxToken.transferFrom(
stakeholder,
address(this),
stake
);
require(transferResult, "DTX transfer failed");
totalStakes += stake;
bool _isStakeholder = stakeholders.contains(stakeholder);
uint256 currTimestamp = block.timestamp;
uint256 stakeStartTime;
if (lastPayoutTimestamp > stakeholderToStake[stakeholder].startTimestamp) {
stakeStartTime = lastPayoutTimestamp;
} else {
stakeStartTime = stakeholderToStake[stakeholder].startTimestamp;
}
if (_isStakeholder) {
// update the payout credit for existing stake and stake duration
payoutCredits[payoutCounter.current()][stakeholder] =
payoutCredits[payoutCounter.current()][stakeholder] +
((stakeholderToStake[stakeholder].lockedDTX) *
((currTimestamp) - (stakeStartTime)));
// update the stake
stakeholderToStake[stakeholder].lockedDTX =
(stakeholderToStake[stakeholder].lockedDTX) +
(stake);
stakeholderToStake[stakeholder].startTimestamp = currTimestamp;
} else {
addStakeholder(stakeholder);
stakeholderToStake[stakeholder] = Stake({
lockedDTX: stake,
startTimestamp: currTimestamp
});
}
emit StakeTransactions(stakeholder, stake, 0, currTimestamp);
}
function removeStake(uint256 stake) public whenNotPaused() {
require(
stakeholderToStake[msg.sender].lockedDTX >= stake,
"Not enough staked!"
);
bool transferResult = dtxToken.transfer(msg.sender, stake);
require(transferResult, "DTX transfer failed");
uint256 currTimestamp = block.timestamp;
uint256 stakeStartTime;
if (lastPayoutTimestamp > stakeholderToStake[msg.sender].startTimestamp) {
stakeStartTime = lastPayoutTimestamp;
} else {
stakeStartTime = stakeholderToStake[msg.sender].startTimestamp;
}
// update the payout credit for existing stake and stake duration
payoutCredits[payoutCounter.current()][msg.sender] =
payoutCredits[payoutCounter.current()][msg.sender] +
((stakeholderToStake[msg.sender].lockedDTX) *
((currTimestamp) - (stakeStartTime)));
// update the stake
stakeholderToStake[msg.sender].lockedDTX =
(stakeholderToStake[msg.sender].lockedDTX) -
(stake);
stakeholderToStake[msg.sender].startTimestamp = currTimestamp;
totalStakes -= stake;
emit StakeTransactions(msg.sender, stake, 1, currTimestamp);
}
function stakeOf(address stakeholder) public view returns (uint256) {
return stakeholderToStake[stakeholder].lockedDTX;
}
function getTotalStakes() public view returns (uint256) {
return totalStakes;
}
function totalCredits(uint256 payoutCycle) public view returns (uint256) {
uint256 totalCredits = 0;
for (uint256 s = 0; s < stakeholders.length(); s += 1) {
totalCredits =
totalCredits +
(payoutCredits[payoutCycle][stakeholders.at(s)]);
}
return totalCredits;
}
function rewardOf(address stakeholder) public view returns (uint256) {
return claimRewards[stakeholder];
}
function totalRewards() public view returns (uint256) {
uint256 totalRewards = 0;
for (uint256 s = 0; s < stakeholders.length(); s += 1) {
totalRewards = totalRewards + (claimRewards[stakeholders.at(s)]);
}
return totalRewards;
}
function monthlyReward() public view returns (uint256) {
uint256 _totalRewards = totalRewards();
require(
dtxToken.balanceOf(address(this)) >= totalStakes + (_totalRewards),
"Rewards are not available yet"
);
uint256 monthReward = dtxToken.balanceOf(address(this)) -
(totalStakes) -
(_totalRewards);
return monthReward;
}
function calculatePayoutCredit(address stakeholder, uint256 currTimestamp)
internal
returns (address)
{
uint256 stakeStartTime;
if (
payoutCredits[payoutCounter.current()][stakeholder] == 0 &&
stakeholderToStake[stakeholder].lockedDTX == 0
) {
return stakeholder;
}
if (lastPayoutTimestamp > stakeholderToStake[stakeholder].startTimestamp) {
stakeStartTime = lastPayoutTimestamp;
} else {
stakeStartTime = stakeholderToStake[stakeholder].startTimestamp;
}
payoutCredits[payoutCounter.current()][stakeholder] =
payoutCredits[payoutCounter.current()][stakeholder] +
((stakeholderToStake[stakeholder].lockedDTX) *
((currTimestamp) - (stakeStartTime)));
return address(0);
}
function calculateReward(
address stakeholder,
uint256 monthlyReward,
uint256 totalCredits
) internal returns (uint256) {
uint256 stakeholderRatio = ((
payoutCredits[payoutCounter.current()][stakeholder]
) * (1000000000000000000)) / (totalCredits);
return (monthlyReward * (stakeholderRatio)) / (1000000000000000000);
}
function distributeRewards() public hasAdminRole {
uint256 monthlyReward = monthlyReward();
uint256 currTimestamp = block.timestamp;
address[] memory stakeholdersToRemove = new address[](
stakeholders.length()
);
uint256 stakeholdersToRemoveIndex = 0;
require(monthlyReward > 0, "Not enough rewards to distribute");
// update payout credits
for (uint256 s = 0; s < stakeholders.length(); s += 1) {
address stakeholder = stakeholders.at(s);
address staker = calculatePayoutCredit(stakeholder, currTimestamp);
if (stakeholder != address(0)) {
stakeholdersToRemove[stakeholdersToRemoveIndex] = staker;
stakeholdersToRemoveIndex += 1;
}
}
// Remove non-stakeholders
for (uint256 s = 0; s < stakeholdersToRemove.length; s += 1) {
stakeholders.remove(stakeholdersToRemove[s]);
}
uint256 totalCredits = totalCredits(payoutCounter.current());
require(totalCredits > 0, "Total credits is 0");
// Calculate rewards
for (uint256 s = 0; s < stakeholders.length(); s += 1) {
address stakeholder = stakeholders.at(s);
uint256 rewards = calculateReward(
stakeholder,
monthlyReward,
totalCredits
);
claimRewards[stakeholder] = claimRewards[stakeholder] + (rewards);
}
payoutCounter.increment();
lastPayoutTimestamp = block.timestamp;
}
function withdrawAllReward() public hasAdminRole {
for (uint256 s = 0; s < stakeholders.length(); s += 1) {
address stakeholder = stakeholders.at(s);
uint256 reward = claimRewards[stakeholder];
bool transferResult = dtxToken.transfer(stakeholder, reward);
require(transferResult, "DTX transfer failed");
claimRewards[stakeholder] = 0;
emit Rewards(stakeholder, reward, block.timestamp);
}
}
function withdrawReward() public whenNotPaused() {
uint256 reward = claimRewards[msg.sender];
require(reward > 0, "No reward to withdraw");
claimRewards[msg.sender] = 0;
bool transferResult = dtxToken.transfer(msg.sender, reward);
require(transferResult, "DTX transfer failed");
emit Rewards(msg.sender, reward, block.timestamp);
}
function getTotalStakeholders() public view returns (uint256) {
return stakeholders.length();
}
function refundLockedDTX() public hasSuperAdminRole {
for (uint256 s = 0; s < stakeholders.length(); s += 1) {
dtxToken.transfer(
stakeholders.at(s),
stakeholderToStake[stakeholders.at(s)].lockedDTX
);
}
// Return remaining DTX to owner (platform commission)
uint256 balance = dtxToken.balanceOf(address(this));
dtxToken.transfer(msg.sender, balance);
}
function pauseContract() public hasSuperAdminRole {
_pause();
}
function unPauseContract() public hasSuperAdminRole {
_unpause();
}
}