-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstakeThreeYears.sol
More file actions
267 lines (226 loc) · 9.67 KB
/
Copy pathstakeThreeYears.sol
File metadata and controls
267 lines (226 loc) · 9.67 KB
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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
contract ThreeYearsStakingContract is Ownable, ReentrancyGuard {
address public constant STAKING_TOKEN_ADDRESS = 0x1d0Ac23F03870f768ca005c84cBb6FB82aa884fD; // galeon address
IERC20 private constant STAKING_TOKEN = IERC20(STAKING_TOKEN_ADDRESS);
uint public constant POOL_SIZE = 180 * 1e6 * 1e18;
uint public constant MAX_SUPPLY = 200 * 1e6 * 1e18;
uint public constant STAKING_DURATION = 1095 days; // 3 years
uint public constant MIN_APR = 30; // 30 % = 10000 * POOL_SIZE / MAX_SUPPLY / STAKING_YEARS_PERCENT
uint public constant MINIMUM_AMOUNT = 500 * 1e18; // TODO 500
uint private constant SECONDS_IN_YEAR = 60 * 60 * 24 * 365;
uint private constant STAKING_YEARS_PERCENT = 300;
mapping(address => uint[]) private userStakeIds;
mapping(uint => address) public stakingUser;
mapping(uint => uint) public stakingAmount;
mapping(uint => uint) public stakingEndDate;
mapping(uint => uint) public stakingLastClaim;
uint public stakesCount;
uint public totalSupply; // Doesn't decrease on unstake
uint public totalStaked; // Decreases on unstake
bool public stakingAllowed;
uint public lastUpdatePoolSizePercent;
uint public maxApr;
uint public percentAutoUpdatePool;
struct Struct {
uint timestamp;
uint apr;
}
struct Stake {
uint id;
uint amount;
uint endDate;
uint lastClaim;
}
struct Status {
bool stakingAllowed;
uint currentApr;
uint stakesCount;
uint totalSupply;
}
Struct[] public aprHistory;
mapping(address => bool) private stakerAddressList;
constructor() {
totalSupply = 0;
lastUpdatePoolSizePercent = 0;
stakingAllowed = true; // TODO false
maxApr = 500;
stakesCount = 0;
percentAutoUpdatePool = 5;
aprHistory.push(Struct(block.timestamp, maxApr));
}
event Staked(uint _amount, uint _totalSupply);
event ReStaked(uint _amount);
event Unstaked(uint _amount);
event Claimed(uint _claimed);
event StakingAllowed(bool _allow);
event AprUpdated(uint _lastupdate, uint _apr);
event AdjustMaxApr(uint _maxApr);
event AdjustpercentAutoUpdatePool(uint _percentAutoUpdatePool);
event UpdatedStaker(address _staker, bool _allowed);
event LastClaim(address _staker, uint _lastClaim);
function addStakerAddress(address _addr) external onlyOwner {
stakerAddressList[_addr] = true;
emit UpdatedStaker(_addr, true);
}
function delStakerAddress(address _addr) external onlyOwner {
stakerAddressList[_addr] = false;
emit UpdatedStaker(_addr, false);
}
function isStakerAddress(address check) public view returns(bool isIndeed) {
return stakerAddressList[check];
}
function adjustMaxApr(uint _maxApr) external onlyOwner {
maxApr = _maxApr;
emit AdjustMaxApr(maxApr);
}
function adjustpercentAutoUpdatePool(uint _percentAutoUpdatePool) external onlyOwner {
percentAutoUpdatePool = _percentAutoUpdatePool;
emit AdjustMaxApr(percentAutoUpdatePool);
}
function updateApr() external onlyOwner {
_updateApr();
lastUpdatePoolSizePercent = totalSupply * 100 / MAX_SUPPLY;
}
function allowStaking(bool _allow) external onlyOwner {
stakingAllowed = _allow;
emit StakingAllowed(_allow);
}
function stake(uint _amount) external {
_stake(_amount, msg.sender);
}
function stakeForSomeoneElse(uint _amount, address _user) external {
require(_user != address(0), "0x address not allowed");
require(isStakerAddress(msg.sender), "Stakers allowed only");
_stake(_amount, _user);
}
function recompound() external {
uint toClaim = getClaimableRewards(msg.sender);
require(toClaim >= MINIMUM_AMOUNT, "Insuficient amount");
_stake(toClaim, msg.sender);
_updateLastClaim();
emit ReStaked(toClaim);
}
function claim() external nonReentrant {
uint toClaim = getClaimableRewards(msg.sender);
require(toClaim > 0, "Nothing to claim");
require(STAKING_TOKEN.balanceOf(address(this)) > toClaim + totalStaked, "Insuficient contract balance");
require(STAKING_TOKEN.transfer(msg.sender, toClaim), "Transfer failed");
_updateLastClaim();
emit Claimed(toClaim);
}
function unstake() external nonReentrant returns(uint) {
uint toUnstake = 0;
uint i = 0;
uint stakeId;
uint toClaim = getClaimableRewards(msg.sender);
while (i < userStakeIds[msg.sender].length) {
stakeId = userStakeIds[msg.sender][i];
if (stakingEndDate[stakeId] < block.timestamp) {
toUnstake += stakingAmount[stakeId];
stakingAmount[stakeId] = 0;
userStakeIds[msg.sender][i] = userStakeIds[msg.sender][userStakeIds[msg.sender].length - 1];
userStakeIds[msg.sender].pop();
} else {
i++;
}
}
require(toUnstake > 0, "Nothing to unstake");
require(STAKING_TOKEN.balanceOf(address(this)) > toUnstake + toClaim, "Insuficient contract balance");
require(STAKING_TOKEN.transfer(msg.sender, toUnstake + toClaim), "Transfer failed");
totalStaked -= toUnstake;
_updateLastClaim();
emit Unstaked(toUnstake);
emit Claimed(toClaim);
return toUnstake + toClaim;
}
function getUserStakesIds(address _user) external view returns (uint[] memory) {
return userStakeIds[_user];
}
function getUserStakes() external view returns (Stake[] memory) {
Stake[] memory stakes = new Stake[](userStakeIds[msg.sender].length);
uint stakeId;
for(uint i; i < userStakeIds[msg.sender].length; i++) {
stakeId = userStakeIds[msg.sender][i];
stakes[i] = Stake(stakeId, stakingAmount[stakeId], stakingEndDate[stakeId], stakingLastClaim[stakeId]);
}
return stakes;
}
function getCurrentApr() external view returns (uint) {
return aprHistory[aprHistory.length - 1].apr;
}
function getStatus() external view returns (Status memory) {
return Status(stakingAllowed,aprHistory[aprHistory.length - 1].apr,stakesCount,totalSupply);
}
function getClaimableRewards(address _user) public view returns (uint) {
uint reward = 0;
uint stakeId;
for (uint i = 0; i < userStakeIds[_user].length; i++) {
stakeId = userStakeIds[_user][i];
uint lastClaim = stakingLastClaim[stakeId];
uint endDate = stakingEndDate[stakeId];
uint until = block.timestamp;
if (endDate < block.timestamp) {
until = endDate;
}
uint j;
for (j = 1; j < aprHistory.length; j++) {
if (aprHistory[j].timestamp > lastClaim) {
uint interval = aprHistory[j].timestamp - lastClaim;
if (until < aprHistory[j].timestamp) {
interval = until > lastClaim ? until - lastClaim : 0;
}
reward += stakingAmount[stakeId] * interval * aprHistory[j-1].apr / 100 / SECONDS_IN_YEAR;
lastClaim = until < aprHistory[j].timestamp ? until : aprHistory[j].timestamp;
}
}
if (until > lastClaim) {
uint interval = until > lastClaim ? until - lastClaim : 0;
reward += stakingAmount[stakeId] * interval * aprHistory[aprHistory.length - 1].apr / 100 / SECONDS_IN_YEAR;
}
}
return reward;
}
function _updateLastClaim() internal {
for (uint i = 0; i < userStakeIds[msg.sender].length; i++) {
stakingLastClaim[userStakeIds[msg.sender][i]] = block.timestamp;
}
emit LastClaim(msg.sender,block.timestamp);
}
function _stake(uint _amount, address _user) internal nonReentrant {
require(stakingAllowed, "Staking is not enabled");
require(_amount >= MINIMUM_AMOUNT, "Insuficient stake amount");
require(totalSupply + _amount <= MAX_SUPPLY, "Pool capacity exceeded");
require(_amount <= STAKING_TOKEN.balanceOf(msg.sender), "Insuficient balance");
require(STAKING_TOKEN.transferFrom(msg.sender, address(this), _amount), "TransferFrom failed");
require(userStakeIds[_user].length < 100, "User stakings limit exceeded");
stakingUser[stakesCount] = _user;
stakingAmount[stakesCount] = _amount;
stakingEndDate[stakesCount] = block.timestamp + STAKING_DURATION;
stakingLastClaim[stakesCount] = block.timestamp;
userStakeIds[_user].push(stakesCount);
totalSupply += _amount;
totalStaked += _amount;
stakesCount += 1;
uint poolSizePercent = totalSupply * 100 / MAX_SUPPLY;
if (poolSizePercent > lastUpdatePoolSizePercent + percentAutoUpdatePool) {
_updateApr();
lastUpdatePoolSizePercent = poolSizePercent;
}
emit Staked(_amount, totalSupply);
}
function _updateApr() internal {
require(totalSupply > 0, "0 division protection");
uint apr = 10000 * POOL_SIZE / totalSupply / STAKING_YEARS_PERCENT;
if (apr < MIN_APR) {
apr = MIN_APR;
} else if (apr > maxApr) {
apr = maxApr;
}
aprHistory.push(Struct(block.timestamp, apr));
emit AprUpdated(block.timestamp, apr);
}
}