Skip to content

Instantly share code, notes, and snippets.

@bertil291utn
Last active November 8, 2022 23:02
Show Gist options
  • Save bertil291utn/e4b2f9da657cef9b0339929432638b18 to your computer and use it in GitHub Desktop.
Save bertil291utn/e4b2f9da657cef9b0339929432638b18 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "./IERC20.sol";
contract StakingToken is ReentrancyGuard {
mapping(address => uint256) internal stakeholders;
uint256 private _totalSupply;
// IERC20 public stakingToken;
// constructor( address _stakingToken) {
// stakingToken = IERC20(_stakingToken);
// }
/*========VIEWS==========*/
function totalSupply() external view returns (uint256) {
return _totalSupply;
}
function balanceOf(address account) external view returns (uint256) {
return stakeholders[account];
}
/*========FUNCTIONS==========*/
function stake(uint256 amount,IERC20 stakingToken ) external nonReentrant {
require(amount > 0, "Cannot stake 0");
require(stakingToken.balanceOf(msg.sender) > 0, "Empty balance");
_totalSupply += amount;
stakeholders[msg.sender] += amount;
// stakingToken.approve(address(this),amount);
stakingToken.transferFrom(msg.sender,address(this), amount);
emit Staked(msg.sender, amount);
}
function unstake(uint256 amount,IERC20 stakingToken) public nonReentrant {
require(amount > 0, "Cannot withdraw 0");
_totalSupply -= amount;
stakeholders[msg.sender] -= amount;
stakingToken.transfer(msg.sender, amount);
emit Unstake(msg.sender, amount);
}
/*========EVENTS==========*/
event Staked(address indexed user, uint256 amount);
event Unstake(address indexed user, uint256 amount);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment