-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstake.sol
More file actions
71 lines (58 loc) · 2.12 KB
/
stake.sol
File metadata and controls
71 lines (58 loc) · 2.12 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;
import "./main.sol";
import "./reward.sol";
contract StakingRewards {
string public name = "Reward Token Pool";
address public owner;
RMAGICIERC20 public rewardsToken;
MAGICIERC20 public stakingToken;
uint public rewardRate = 200;
uint public lastUpdateTime;
uint public rewardPerTokenStored;
mapping(address => uint) public userRewardPerTokenPaid;
mapping(address => uint) public rewards;
uint private _totalSupply;
mapping(address => uint) private _balances;
constructor(address _stakingToken, address _rewardsToken) {
stakingToken = MAGICIERC20(_stakingToken);
rewardsToken = RMAGICIERC20(_rewardsToken);
owner = msg.sender;
}
function rewardPerToken() public view returns (uint) {
if (_totalSupply == 0) {
return rewardPerTokenStored;
}
return
rewardPerTokenStored +
(((block.timestamp - lastUpdateTime) * rewardRate * 1e18) / _totalSupply);
}
function earned(address account) public view returns (uint) {
return
((_balances[account] *
(rewardPerToken() - userRewardPerTokenPaid[account])) / 1e18) +
rewards[account];
}
modifier updateReward(address account) {
rewardPerTokenStored = rewardPerToken();
lastUpdateTime = block.timestamp;
rewards[account] = earned(account);
userRewardPerTokenPaid[account] = rewardPerTokenStored;
_;
}
function stake(uint _amount) external updateReward(msg.sender) {
_totalSupply += _amount;
_balances[msg.sender] += _amount;
stakingToken.transferFrom(msg.sender, address(this), _amount);
}
function withdraw(uint _amount) external updateReward(msg.sender) {
_totalSupply -= _amount;
_balances[msg.sender] -= _amount;
stakingToken.transfer(msg.sender, _amount);
}
function getReward() external updateReward(msg.sender) {
uint reward = rewards[msg.sender];
rewards[msg.sender] = 0;
rewardsToken.transfer(msg.sender, reward);
}
}