Created
July 23, 2021 08:31
-
-
Save percybolmer/855e186eff734bcccb40d11b674cdfa8 to your computer and use it in GitHub Desktop.
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 | |
* withdrawStake takes in an amount and a index of the stake and will remove tokens from that stake | |
* Notice index of the stake is the users stake counter, starting at 0 for the first stake | |
* Will return the amount to MINT onto the acount | |
* Will also calculateStakeReward and reset timer | |
*/ | |
function _withdrawStake(uint256 amount, uint256 index) internal returns(uint256){ | |
// Grab user_index which is the index to use to grab the Stake[] | |
uint256 user_index = stakes[msg.sender]; | |
Stake memory current_stake = stakeholders[user_index].address_stakes[index]; | |
require(current_stake.amount >= amount, "Staking: Cannot withdraw more than you have staked"); | |
// Calculate available Reward first before we start modifying data | |
uint256 reward = calculateStakeReward(current_stake); | |
// Remove by subtracting the money unstaked | |
current_stake.amount = current_stake.amount - amount; | |
// If stake is empty, 0, then remove it from the array of stakes | |
if(current_stake.amount == 0){ | |
delete stakeholders[user_index].address_stakes[index]; | |
}else { | |
// If not empty then replace the value of it | |
stakeholders[user_index].address_stakes[index].amount = current_stake.amount; | |
// Reset timer of stake | |
stakeholders[user_index].address_stakes[index].since = block.timestamp; | |
} | |
return amount+reward; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment