Created
July 23, 2021 07:17
-
-
Save percybolmer/33c4d89176adff90b36dd2c1a5f48f6b to your computer and use it in GitHub Desktop.
A example Staking Function
This file contains hidden or 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
/** | |
* @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 _amount) internal{ | |
// Simple check so that user does not stake 0 | |
require(_amount > 0, "Cannot stake nothing"); | |
// Mappings in solidity creates all values, but empty, so we can just check the address | |
uint256 index = stakes[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 returned will be the index of the stakeholder in the stakeholders array | |
index = _addStakeholder(msg.sender); | |
} | |
// Use the index to push a new Stake | |
// push a newly created Stake with the current block timestamp. | |
stakeholders[index].address_stakes.push(Stake(msg.sender, _amount, timestamp)); | |
// Emit an event that the stake has occured | |
emit Staked(msg.sender, _amount, index,timestamp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment