Last active
May 10, 2021 18:57
-
-
Save percybolmer/e6aac19b6161bf01cdf5c6c5325d56ce 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 | |
* calculateStakeReward is used to calculate how much a user should be rewarded for their stakes | |
* and the duration the stake has been active | |
*/ | |
function calculateStakeReward(Stake memory _stake) internal view returns(uint256){ | |
// First calculate how long the stake has been active | |
// Use current seconds since epoch - the seconds since epoch the stake was made | |
// The output will be duration in SECONDS , | |
// We will reward the user 0.1% per Hour So thats 0.1% per 3600 seconds | |
// the alghoritm is seconds = block.timestamp - stake seconds (block.timestap - _stake.since) | |
// hours = Seconds / 3600 (seconds /3600) 3600 is an variable in Solidity names hours | |
// we then multiply each token by the hours staked , then divide by the rewardPerHour rate | |
return (((block.timestamp - _stake.since) / 1 hours) * _stake.amount) / rewardPerHour; | |
} | |
/** | |
* @notice | |
* calculateTotalAvailableRewards will iterate all stakes made by a account | |
* and summerize the total unclaimed rewards | |
* | |
*/ | |
function calculateTotalAvailableRewards(address _staker) public view returns(uint256){ | |
uint256 total_reward; | |
// Itterate all stakes from the address and calculate their reward for each stake | |
Stake[] memory user_stakes = stakes[_staker]; | |
for(uint256 i = 0; i<user_stakes.length; i++) { | |
// Skip any stake that is actually stopped or empty | |
if(user_stakes[i].amount == 0 || user_stakes[i].until != 0){ | |
continue; | |
} | |
total_reward = total_reward + calculateStakeReward(user_stakes[i]); | |
} | |
return total_reward; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment