Created
March 24, 2022 09:51
-
-
Save naik899/a34f8b8c562c341a2f25f0e7fbb30f7d to your computer and use it in GitHub Desktop.
Spock Staking contract
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.8; | |
import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | |
contract StakingRewards { | |
IERC20 public rewardsToken; | |
IERC20 public stakingToken; | |
uint public rewardRate = 100; | |
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 = IERC20(_stakingToken); | |
rewardsToken = IERC20(_rewardsToken); | |
} | |
function rewardPerToken() public view returns (uint) { | |
if (_totalSupply == 0) { | |
return 0; | |
} | |
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment