Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Last active May 9, 2021 19:13
Show Gist options
  • Save percybolmer/8da048f15c551583971a7cc26c914433 to your computer and use it in GitHub Desktop.
Save percybolmer/8da048f15c551583971a7cc26c914433 to your computer and use it in GitHub Desktop.
/**
* @notice
* Stake is used to make a stake for an sender. It will remove the amount staked from the stakers account and place those tokens inside a stake container
* StakeID
*/
function stake(uint256 _stake) public{
// Simple check so that user does not stake 0
require(_stake > 0, "Cannot stake nothing");
// Make sure staker actually is good for it
require(_stake < balanceOf(msg.sender), "Cannot stake more than you own");
// Burn the amount of tokens on the sender
_burn(msg.sender, _stake);
// Use the msg.sender address to find the array, then push a newly created Stake with the current block timestamp.
// block.timestamp = timestamp of the current block in seconds since the epoch
// we use the Storage modifier here since we want that to persist
Stake[] storage user_stakes = stakes[msg.sender];
uint256 timestamp = block.timestamp;
user_stakes.push(Stake(msg.sender, _stake, timestamp, 0));
// Emit an event that the stake has occured
emit Staked(msg.sender, _stake, user_stakes.length-1,timestamp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment