-
Notifications
You must be signed in to change notification settings - Fork 5
/
SunStaker.sol
135 lines (102 loc) · 3.84 KB
/
SunStaker.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
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 SunStaker is SunStakerInterface {
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);
function initialize(uint256 _starttime, uint256 _periodFinish) public{
starttime = _starttime;
periodFinish = _periodFinish;
}
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);
if (trueReward > 0) {
rewards[msg.sender] = 0;
rawAll = rawAll.sub(rawReward);
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 withdraw(address token) updateReward(msg.sender) checkEnd public {
uint256 sad = balanceOf_[msg.sender];
balanceOf_[msg.sender] = 0;
msg.sender.transfer(sad);
totalSupply_ = totalSupply_.sub(sad);
getReward(token);
emit Withdrawal(msg.sender, sad);
}
function totalSupply() public view returns (uint) {
return totalSupply_;
}
function balanceOf(address guy) public view returns (uint){
return balanceOf_[guy];
}
/**
* @dev Returns the smallest of two numbers.
*/
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
}