-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThreeYearsStakingContractFixedAPR.sol
More file actions
263 lines (237 loc) · 9.42 KB
/
Copy pathThreeYearsStakingContractFixedAPR.sol
File metadata and controls
263 lines (237 loc) · 9.42 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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
/**
* @author Fred DA ROS
* @title ThreeYearsStakingContractFixedAPR
* @notice Lock tokens for 3 years generating claimable rewards depending configured APR
*/
contract ThreeYearsStakingContractFixedAPR is Ownable, ReentrancyGuard {
address public constant STAKING_TOKEN_ADDRESS = 0x1d0Ac23F03870f768ca005c84cBb6FB82aa884fD; // galeon address
IERC20 private constant STAKING_TOKEN = IERC20(STAKING_TOKEN_ADDRESS);
uint public constant MAX_SUPPLY = 100 * 1e6 * 1e18; // 100M
uint public constant STAKING_DURATION = 1095 days; // 3 years
uint public constant APR = 10;
uint public constant STAK_COUNT_LIMIT_BY_ADDRESS = 200;
uint private constant SECONDS_IN_YEAR = 60 * 60 * 24 * 365;
uint public stakesCount;
uint public totalSupply; // Doesn't decrease on unstake
uint public totalStaked; // Decreases on unstake
bool public stakingAllowed;
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;
mapping(address => bool) private stakerAddressList;
struct Stake {
uint id;
uint amount;
uint endDate;
uint lastClaim;
}
struct Status {
bool stakingAllowed;
uint stakesCount;
uint totalSupply;
}
event Staked(uint _amount, uint _totalSupply);
event ReStaked(uint _amount);
event Unstaked(uint _amount);
event Claimed(uint _claimed);
event Withdrawed(uint _withdrawed);
event StakingAllowed(bool _allow);
event UpdatedStaker(address _staker, bool _allowed);
event LastClaim(address _staker, uint _lastClaim);
/**
* @dev function check if requirements to stake are ok
* @param _amount amount to stake
*/
function stakeRequirements(uint _amount) internal view {
require(stakingAllowed, "Staking is not enabled");
require(totalSupply + _amount <= MAX_SUPPLY, "Pool capacity exceeded");
require(userStakeIds[msg.sender].length < STAK_COUNT_LIMIT_BY_ADDRESS, "User stakings limit exceeded");
}
/**
* @dev enable or disable staking
* @param _allow true to enable, false to disable
*/
function allowStaking(bool _allow) external onlyOwner {
stakingAllowed = _allow;
emit StakingAllowed(_allow);
}
/**
* @dev external function to stake stake
* @param _amount amount to stake
*/
function stake(uint _amount) external {
stakeRequirements(_amount);
require(_amount <= STAKING_TOKEN.balanceOf(msg.sender), "Insuficient balance");
require(STAKING_TOKEN.transferFrom(msg.sender, address(this), _amount), "TransferFrom failed");
_stake(_amount);
}
/**
* @dev external function to stake stake
* @param _amount amount to stake
*/
function stakeMulti(uint _amount, uint count) external {
stakeRequirements(_amount);
require(_amount <= STAKING_TOKEN.balanceOf(msg.sender), "Insuficient balance");
require(STAKING_TOKEN.transferFrom(msg.sender, address(this), _amount), "TransferFrom failed");
for (uint i; i < count; i++) {
_stake(_amount/count);
}
}
/**
* @dev recompound unclaimed rewards, it create a new stake
*/
function recompound() external {
uint _amount = getClaimableRewards(msg.sender);
stakeRequirements(_amount);
require(STAKING_TOKEN.balanceOf(address(this)) > _amount + totalStaked, "Insuficient contract balance");
_updateLastClaim();
_stake(_amount);
emit ReStaked(_amount);
}
/**
* @dev claim claimable rewards
*/
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);
}
/**
* @dev withdraw available tokens (balance minus totalStaked) from the contract
* @param amount amount to withdraw
*/
function withdraw(uint amount) external onlyOwner nonReentrant {
require(amount > 0, "Nothing to withdraw");
require(STAKING_TOKEN.balanceOf(address(this)) > amount + totalStaked, "Insuficient contract balance");
require(STAKING_TOKEN.transfer(msg.sender, amount), "Transfer failed");
emit Withdrawed(amount);
}
/**
* @dev get withdrawable amount (balance minus totalStaked)
*/
function getWithdrawableAmount() external view returns(uint) {
return STAKING_TOKEN.balanceOf(address(this)) - totalStaked;
}
/**
* @dev unstake unlocked stakings
*/
function unstake() external nonReentrant returns(uint) {
uint toUnstake;
uint i;
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;
}
/**
* @dev get a list of stake ids of a user
* @param _user address of the user
* @return uint[] list of stake ids
*/
function getUserStakesIds(address _user) external view returns (uint[] memory) {
return userStakeIds[_user];
}
/**
* @dev return stakes details of the message sender call
* @return Stake[] list of stakes details
*/
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;
}
/**
* @dev return stakes details of an address
* @param _user address of the user
* @return Stake[] list of stakes details
*/
function getSpecificUserStakes(address _user) external view returns (Stake[] memory) {
Stake[] memory stakes = new Stake[](userStakeIds[_user].length);
uint stakeId;
for(uint i; i < userStakeIds[_user].length; i++) {
stakeId = userStakeIds[_user][i];
stakes[i] = Stake(stakeId, stakingAmount[stakeId], stakingEndDate[stakeId], stakingLastClaim[stakeId]);
}
return stakes;
}
/**
* @dev get contract details
* @return Status contract status details
*/
function getStatus() external view returns (Status memory) {
return Status(stakingAllowed,stakesCount,totalSupply);
}
/**
* @dev get claimable rewards
* @param _user address of the user
* @return uint amount claimable
*/
function getClaimableRewards(address _user) public view returns (uint) {
uint reward;
uint stakeId;
uint lastClaim;
uint until;
for (uint i; i < userStakeIds[_user].length; i++) {
stakeId = userStakeIds[_user][i];
lastClaim = stakingLastClaim[stakeId];
until = stakingEndDate[stakeId] < block.timestamp ? stakingEndDate[stakeId] : block.timestamp;
reward += until > lastClaim ? stakingAmount[stakeId] * (until - lastClaim) * APR / 100 / SECONDS_IN_YEAR : 0;
}
return reward;
}
/**
* @dev update last claim of a user
*/
function _updateLastClaim() internal {
for (uint i ; i < userStakeIds[msg.sender].length; i++) {
stakingLastClaim[userStakeIds[msg.sender][i]] = block.timestamp;
}
emit LastClaim(msg.sender,block.timestamp);
}
/**
* @dev internal stake
* @param _amount amount to stake
*/
function _stake(uint _amount) internal nonReentrant {
stakingUser[stakesCount] = msg.sender;
stakingAmount[stakesCount] = _amount;
stakingEndDate[stakesCount] = block.timestamp + STAKING_DURATION;
stakingLastClaim[stakesCount] = block.timestamp;
userStakeIds[msg.sender].push(stakesCount);
totalSupply += _amount;
totalStaked += _amount;
stakesCount += 1;
emit Staked(_amount, totalSupply);
}
}