-
Notifications
You must be signed in to change notification settings - Fork 0
/
TOKEN_staking_BSC.sol
202 lines (182 loc) · 8.04 KB
/
TOKEN_staking_BSC.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
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.7.5;
//MATH OPERATIONS -- designed to avoid possibility of errors with built-in math functions
library SafeMath {
//@dev Multiplies two numbers, throws on overflow.
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
assert(c / a == b);
return c;
}
//@dev Integer division of two numbers, truncating the quotient (i.e. rounds down).
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// assert(b > 0); Solidity automatically throws when dividing by 0
uint256 c = a / b;
return c;
}
//@dev Substracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
uint256 c = a - b;
return c;
}
//@dev Adds two numbers, throws on overflow.
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
//end library
}
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
abstract contract Ownable {
address internal _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor () {
_owner = msg.sender;
emit OwnershipTransferred(address(0), msg.sender);
}
function owner() public view returns (address) {
return _owner;
}
modifier onlyOwner() {
require(_owner == msg.sender, "Ownable: caller is not the owner");
_;
}
function renounceOwnership() public virtual onlyOwner {
emit OwnershipTransferred(_owner, address(0));
_owner = address(0);
}
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}
contract TOKEN_Staking is Ownable {
using SafeMath for uint256;
address public constant TOKEN = ""; //token to stake
//STAKING PARAMETERS
uint256 public constant stakingPeriod = 30 days; //period over which tokens are locked after staking
uint256 public stakingEnd; //point after which staking rewards cease to accumulate
uint256 public rewardRate = 14; //14% linear return per staking period
uint256 public totalStaked; //sum of all user stakes
uint256 public maxTotalStaked = 58e23; //5.8 million tokens
uint256 public minStaked = 1e21; //1000 tokens. min staked per user
//STAKING MAPPINGS
mapping (address => uint256) public stakedTokens; //amount of tokens that address has staked
mapping (address => uint256) public lastStaked; //last time at which address staked, deposited, or "rolled over" their position by calling updateStake directly
mapping (address => uint256) public totalEarnedTokens; //total tokens earned through staking by each user
constructor(){
stakingEnd = (block.timestamp + 180 days);
}
//STAKING FUNCTIONS
function deposit(uint256 amountTokens) external {
require( (stakedTokens[msg.sender] >= minStaked || amountTokens >= minStaked), "deposit: must exceed minimum stake" );
require(totalStaked + amountTokens <= maxTotalStaked, "deposit: amount would exceed max stake. call updateStake to claim dividends");
updateStake();
IERC20(TOKEN).transferFrom(msg.sender, address(this), amountTokens);
stakedTokens[msg.sender] += amountTokens;
totalStaked += amountTokens;
}
function updateStake() public {
uint256 stakedUntil = min(block.timestamp, stakingEnd);
uint256 periodStaked = stakedUntil.sub(lastStaked[msg.sender]);
uint256 dividends;
//linear rewards up to stakingPeriod
if(periodStaked < stakingPeriod) {
dividends = periodStaked.mul(stakedTokens[msg.sender]).mul(rewardRate).div(stakingPeriod).div(100);
} else {
dividends = stakedTokens[msg.sender].mul(rewardRate).div(100);
}
//update lastStaked time for msg.sender -- user cannot unstake until end of another stakingPeriod
lastStaked[msg.sender] = stakedUntil;
//withdraw dividends for user if rolling over dividends would exceed staking cap, else stake the dividends automatically
if(totalStaked + dividends > maxTotalStaked) {
IERC20(TOKEN).transfer(msg.sender, dividends);
totalEarnedTokens[msg.sender] += dividends;
} else {
stakedTokens[msg.sender] += dividends;
totalStaked += dividends;
totalEarnedTokens[msg.sender] += dividends;
}
}
function withdrawDividends() external {
uint256 stakedUntil = min(block.timestamp, stakingEnd);
uint256 periodStaked = stakedUntil.sub(lastStaked[msg.sender]);
uint256 dividends;
//linear rewards up to stakingPeriod
if(periodStaked < stakingPeriod) {
dividends = periodStaked.mul(stakedTokens[msg.sender]).mul(rewardRate).div(stakingPeriod).div(100);
} else {
dividends = stakedTokens[msg.sender].mul(rewardRate).div(100);
}
//update lastStaked time for msg.sender -- user cannot unstake until end of another stakingPeriod
lastStaked[msg.sender] = stakedUntil;
//withdraw dividends for user
IERC20(TOKEN).transfer(msg.sender, dividends);
totalEarnedTokens[msg.sender] += dividends;
}
function min(uint256 a, uint256 b) internal pure returns(uint256) {
return a < b ? a : b;
}
function unstake() external {
uint256 timeSinceStake = (block.timestamp).sub(lastStaked[msg.sender]);
require(timeSinceStake >= stakingPeriod || block.timestamp > stakingEnd, "unstake: staking period for user still ongoing");
updateStake();
uint256 toTransfer = stakedTokens[msg.sender];
stakedTokens[msg.sender] = 0;
IERC20(TOKEN).transfer(msg.sender, toTransfer);
totalStaked = totalStaked.sub(toTransfer);
}
function getPendingDivs(address user) external view returns(uint256) {
uint256 stakedUntil = min(block.timestamp, stakingEnd);
uint256 periodStaked = stakedUntil.sub(lastStaked[user]);
uint256 dividends;
//linear rewards up to stakingPeriod
if(periodStaked < stakingPeriod) {
dividends = periodStaked.mul(stakedTokens[user]).mul(rewardRate).div(stakingPeriod).div(100);
} else {
dividends = stakedTokens[user].mul(rewardRate).div(100);
}
return(dividends);
}
//OWNER ONLY FUNCTIONS
function updateMinStake(uint256 newMinStake) external onlyOwner() {
minStaked = newMinStake;
}
function updateStakingEnd(uint256 newStakingEnd) external onlyOwner() {
require(newStakingEnd >= block.timestamp, "updateStakingEnd: newStakingEnd must be in future");
stakingEnd = newStakingEnd;
}
function updateRewardRate(uint256 newRewardRate) external onlyOwner() {
require(newRewardRate <= 100, "what are you, crazy?");
rewardRate = newRewardRate;
}
function updateMaxTotalStaked(uint256 newMaxTotalStaked) external onlyOwner() {
maxTotalStaked = newMaxTotalStaked;
}
//allows owner to recover ERC20 tokens for users when they are mistakenly sent to contract
function recoverTokens(address tokenAddress, address dest, uint256 amountTokens) external onlyOwner() {
require(tokenAddress != TOKEN, "recoverTokens: cannot move staked token");
IERC20(tokenAddress).transfer(dest, amountTokens);
}
//allows owner to reclaim any tokens not distributed during staking
function recoverTOKEN() external onlyOwner() {
require(block.timestamp >= (stakingEnd + 30 days), "recoverTOKEN: too early");
uint256 amountToSend = IERC20(TOKEN).balanceOf(address(this));
IERC20(TOKEN).transfer(msg.sender, amountToSend);
}
}