Skip to content

Instantly share code, notes, and snippets.

@knek-little-projects
Created July 13, 2023 09:56
Show Gist options
  • Save knek-little-projects/51e59a52f15e21cf83893c03f58c27b4 to your computer and use it in GitHub Desktop.
Save knek-little-projects/51e59a52f15e21cf83893c03f58c27b4 to your computer and use it in GitHub Desktop.
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract Staking {
mapping(address => uint) timestamp;
mapping(address => uint) amount;
IERC20 rewardToken;
IERC20 stakedToken;
constructor(IERC20 _rewardToken, IERC20 _stakedToken) {
rewardToken = _rewardToken;
stakedToken = _stakedToken;
}
function stake(uint _amount) external {
timestamp[msg.sender] = block.timestamp;
amount[msg.sender] = _amount;
require(stakedToken.transferFrom(msg.sender, address(this), _amount), "F");
}
function unstake() external {
uint duration = block.timestamp - timestamp[msg.sender];
uint _reward = duration * amount[msg.sender] / 365 days;
uint _amount = amount[msg.sender];
amount[msg.sender] = timestamp[msg.sender] = 0;
require(stakedToken.transfer(msg.sender, _reward), "F");
require(rewardToken.transfer(msg.sender, _amount), "F");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment