Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Last active May 10, 2021 20:41
Show Gist options
  • Save percybolmer/34ebd475cb6976e0815346d71ae2555b to your computer and use it in GitHub Desktop.
Save percybolmer/34ebd475cb6976e0815346d71ae2555b 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");
// Mappings in solidity creates all values, but empty, so we can just check the address
uint256 index = stakeHolders[msg.sender];
// block.timestamp = timestamp of the current block in seconds since the epoch
uint256 timestamp = block.timestamp;
// See if the staker already has a staked index or if its the first time
if(index == 0){
// This stakeholder stakes for the first time
// We need to add him to the stakeHolders and also map it into the Index of the stakes
// The index would be length of stakes +1
// Also make sure its not the FIRST staker, because we cant do stakes.length-1 on that
index = stakes.length-1;
}
// Add index to the stakeHolders
stakeHolders[msg.sender] = index;
// Burn the amount of tokens on the sender
_burn(msg.sender, _stake);
// Use the index to push a new Stake
// push a newly created Stake with the current block timestamp.
stakes[index].push(Stake(msg.sender, _stake, timestamp, 0));
// Emit an event that the stake has occured
emit Staked(msg.sender, _stake, stakes[index].length-1,timestamp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment