|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8; |
| 3 | + |
| 4 | +contract StakingRewards { |
| 5 | + IERC20 public immutable stakingToken; |
| 6 | + IERC20 public immutable rewardsToken; |
| 7 | + |
| 8 | + address public owner; |
| 9 | + |
| 10 | + /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 11 | + staking-contract = 0x81908580A50517432d180e6e5eE3C05B06505314 |
| 12 | + staking-token = 0x06CF90BdcA174e02d960BF59483c7e09b6393d58 |
| 13 | + reward-token = 0xe0558beBC4D9c53333dACFc395e7a2FbfcAEeE32 |
| 14 | + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
| 15 | + |
| 16 | + // Duration of rewards to be paid out (in seconds) |
| 17 | + uint public duration; |
| 18 | + // Timestamp of when the rewards finish |
| 19 | + uint public finishAt; |
| 20 | + // Minimum of last updated time and reward finish time |
| 21 | + uint public updatedAt; |
| 22 | + // Reward to be paid out per second |
| 23 | + |
| 24 | + uint public rewardRate; |
| 25 | + // Sum of (reward rate * dt * 1e18 / total supply) |
| 26 | + uint public rewardPerTokenStored; |
| 27 | + // User address => rewardPerTokenStored |
| 28 | + mapping(address => uint) public userRewardPerTokenPaid; |
| 29 | + // User address => rewards to be claimed |
| 30 | + mapping(address => uint) public rewards; |
| 31 | + |
| 32 | + // Total staked |
| 33 | + uint public totalSupply; |
| 34 | + // User address => staked amount |
| 35 | + mapping(address => uint) public balanceOf; |
| 36 | + |
| 37 | + constructor(address _stakingToken, address _rewardToken) { |
| 38 | + owner = msg.sender; |
| 39 | + stakingToken = IERC20(_stakingToken); |
| 40 | + rewardsToken = IERC20(_rewardToken); |
| 41 | + } |
| 42 | + |
| 43 | + modifier onlyOwner() { |
| 44 | + require(msg.sender == owner, "not authorized"); |
| 45 | + _; |
| 46 | + } |
| 47 | + |
| 48 | + modifier updateReward(address _account) { |
| 49 | + rewardPerTokenStored = rewardPerToken(); |
| 50 | + updatedAt = lastTimeRewardApplicable(); |
| 51 | + |
| 52 | + if (_account != address(0)) { |
| 53 | + rewards[_account] = earned(_account); |
| 54 | + userRewardPerTokenPaid[_account] = rewardPerTokenStored; |
| 55 | + } |
| 56 | + |
| 57 | + _; |
| 58 | + } |
| 59 | + |
| 60 | + function lastTimeRewardApplicable() public view returns (uint) { |
| 61 | + return _min(finishAt, block.timestamp); |
| 62 | + } |
| 63 | + |
| 64 | + function rewardPerToken() public view returns (uint) { |
| 65 | + if (totalSupply == 0) { |
| 66 | + return rewardPerTokenStored; |
| 67 | + } |
| 68 | + |
| 69 | + return |
| 70 | + rewardPerTokenStored + |
| 71 | + (rewardRate * (lastTimeRewardApplicable() - updatedAt) * 1e18) / |
| 72 | + totalSupply; |
| 73 | + } |
| 74 | + |
| 75 | + function stake(uint _amount) external updateReward(msg.sender) { |
| 76 | + require(_amount > 0, "amount = 0"); |
| 77 | + stakingToken.transferFrom(msg.sender, address(this), _amount); |
| 78 | + balanceOf[msg.sender] += _amount; |
| 79 | + totalSupply += _amount; |
| 80 | + } |
| 81 | + |
| 82 | + function withdraw(uint _amount) external updateReward(msg.sender) { |
| 83 | + require(_amount > 0, "amount = 0"); |
| 84 | + balanceOf[msg.sender] -= _amount; |
| 85 | + totalSupply -= _amount; |
| 86 | + stakingToken.transfer(msg.sender, _amount); |
| 87 | + } |
| 88 | + |
| 89 | + function earned(address _account) public view returns (uint) { |
| 90 | + return |
| 91 | + ((balanceOf[_account] * |
| 92 | + (rewardPerToken() - userRewardPerTokenPaid[_account])) / 1e18) + |
| 93 | + rewards[_account]; |
| 94 | + } |
| 95 | + |
| 96 | + function getReward() external updateReward(msg.sender) { |
| 97 | + uint reward = rewards[msg.sender]; |
| 98 | + if (reward > 0) { |
| 99 | + rewards[msg.sender] = 0; |
| 100 | + rewardsToken.transfer(msg.sender, reward); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + function setRewardsDuration(uint _duration) external onlyOwner { |
| 105 | + require(finishAt < block.timestamp, "reward duration not finished"); |
| 106 | + duration = _duration; |
| 107 | + } |
| 108 | + |
| 109 | + function notifyRewardAmount( |
| 110 | + uint _amount |
| 111 | + ) external onlyOwner updateReward(address(0)) { |
| 112 | + if (block.timestamp >= finishAt) { |
| 113 | + rewardRate = _amount / duration; |
| 114 | + } else { |
| 115 | + uint remainingRewards = (finishAt - block.timestamp) * rewardRate; |
| 116 | + rewardRate = (_amount + remainingRewards) / duration; |
| 117 | + } |
| 118 | + |
| 119 | + require(rewardRate > 0, "reward rate = 0"); |
| 120 | + require( |
| 121 | + rewardRate * duration <= rewardsToken.balanceOf(address(this)), |
| 122 | + "reward amount > balance" |
| 123 | + ); |
| 124 | + |
| 125 | + finishAt = block.timestamp + duration; |
| 126 | + updatedAt = block.timestamp; |
| 127 | + } |
| 128 | + |
| 129 | + function _min(uint x, uint y) private pure returns (uint) { |
| 130 | + return x <= y ? x : y; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +interface IERC20 { |
| 135 | + function totalSupply() external view returns (uint); |
| 136 | + |
| 137 | + function balanceOf(address account) external view returns (uint); |
| 138 | + |
| 139 | + function transfer(address recipient, uint amount) external returns (bool); |
| 140 | + |
| 141 | + function allowance(address owner, address spender) external view returns (uint); |
| 142 | + |
| 143 | + function approve(address spender, uint amount) external returns (bool); |
| 144 | + |
| 145 | + function transferFrom( |
| 146 | + address sender, |
| 147 | + address recipient, |
| 148 | + uint amount |
| 149 | + ) external returns (bool); |
| 150 | + |
| 151 | + event Transfer(address indexed from, address indexed to, uint value); |
| 152 | + event Approval(address indexed owner, address indexed spender, uint value); |
| 153 | +} |
0 commit comments