-
Notifications
You must be signed in to change notification settings - Fork 5
/
SunStakerStandAlone.sol
184 lines (140 loc) · 5.26 KB
/
SunStakerStandAlone.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
import "./SunStakerInterface.sol";
pragma solidity ^0.5.8;
interface ITRC20 {
function transfer(address recipient, uint256 amount) external returns (bool);
function balanceOf(address account) external view returns (uint256);
}
contract SunStakerStandAlone is SunStakerInterface {
bool public safeMode;
function setSafeMode()
external
{
require(msg.sender == gov, "Caller must be gov");
safeMode = true;
}
modifier checkStart() {
require(block.timestamp >= starttime && block.timestamp < periodFinish,"only in start period");
_;
}
modifier checkEnd() {
require(block.timestamp >= periodFinish,"not end");
_;
}
modifier updateReward(address account) {
rewardOneSunStored = rewardOneSun();
lastUpdateTime = lastTimeRewardApplicable();
if (account != address(0)) {
rewards[account] = earned(account);
userRewardOneSunPaid[account] = rewardOneSunStored;
}
_;
}
using SafeMath for uint256;
event Deposit(address indexed dst, uint sad);
event Withdrawal(address indexed src, uint sad);
event RewardPaid(address indexed user, uint256 reward);
constructor(uint256 _starttime, uint256 _periodFinish) public{
starttime = _starttime;
periodFinish = _periodFinish;
gov = msg.sender;
withdrawOn = false;
safeMode = false;
}
function lastTimeRewardApplicable() public view returns (uint256) {
return min(block.timestamp, periodFinish);
}
function rewardOneSun() public view returns (uint256) {
if (totalSupply() == 0 || lastTimeRewardApplicable() == lastUpdateTime) {
return rewardOneSunStored;
}
return
rewardOneSunStored.add(
lastTimeRewardApplicable()
.sub(lastUpdateTime)
.mul(rewardRate)
.mul(1e18)
.div(totalSupply())
);
}
function earned(address account) public view returns (uint256) {
return
balanceOf(account)
.mul(rewardOneSun().sub(userRewardOneSunPaid[account]))
.div(1e18)
.add(rewards[account]);
}
function earned(address account,address token) public view returns (uint256) {
uint256 tokenAll = ITRC20(token).balanceOf(address(this));
uint256 rawReward = earned(account);
return tokenAll.mul(rawReward).div(rawAll);
}
function getReward(address token) internal updateReward(msg.sender) {
uint256 rawReward = rewards[msg.sender];
uint256 tokenAll = ITRC20(token).balanceOf(address(this));
uint256 trueReward = tokenAll.mul(rawReward).div(rawAll);
rewards[msg.sender] = 0;
rawAll = rawAll.sub(rawReward);
if (trueReward > 0) {
require(ITRC20(token).transfer(msg.sender, trueReward),"transfer failed");
}
emit RewardPaid(msg.sender, trueReward);
}
function deposit() updateReward(msg.sender) checkStart public payable {
require( msg.value > 0,"deposit must gt 0");
balanceOf_[msg.sender] = balanceOf_[msg.sender].add(msg.value);
totalSupply_ = totalSupply_.add(msg.value);
if(rawAll == 0){
uint256 firstDepositTime = block.timestamp;
rawAll = periodFinish.sub(firstDepositTime).mul(rewardRate);
}
emit Deposit(msg.sender, msg.value);
}
function depositOnly() checkStart public payable {
require(safeMode, "can not depositOnly yet");
require(msg.value > 0, "deposit must gt 0");
balanceOf_[msg.sender] = balanceOf_[msg.sender].add(msg.value);
totalSupply_ = totalSupply_.add(msg.value);
emit Deposit(msg.sender, msg.value);
}
function withdrawOnly() checkEnd public {
require(safeMode, "can not withdrawOnly yet");
uint256 sad = balanceOf_[msg.sender];
require(sad > 0, "balance must gt 0 ");
balanceOf_[msg.sender] = 0;
msg.sender.transfer(sad);
totalSupply_ = totalSupply_.sub(sad);
emit Withdrawal(msg.sender, sad);
}
function withdraw(address token) updateReward(msg.sender) checkEnd public {
if (!withdrawOn) {
if (msg.sender == gov || block.timestamp >= periodFinish + 3 days) {
withdrawOn = true;
}
}
require(withdrawOn, "can not withdraw yet");
uint256 sad = balanceOf_[msg.sender];
require(sad > 0, "balance must gt 0 ");
balanceOf_[msg.sender] = 0;
msg.sender.transfer(sad);
totalSupply_ = totalSupply_.sub(sad);
getReward(token);
emit Withdrawal(msg.sender, sad);
}
function totalSupply() public view returns (uint256) {
return totalSupply_;
}
function balanceOf(address guy) public view returns (uint256){
return balanceOf_[guy];
}
function getInfo(address _user) public view returns(uint256 _balanceTRX, uint256 _balance, uint256 _totalSupply){
_balanceTRX = _user.balance;
_balance = balanceOf_[_user];
_totalSupply = totalSupply_;
}
/**
* @dev Returns the smallest of two numbers.
*/
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
}