Created
January 29, 2023 07:44
-
-
Save az0mb13/ec5d252b0798fb0c980f58069cbbbf51 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
| contract TheRewarderPool { | |
| // Minimum duration of each round of rewards in seconds | |
| uint256 private constant REWARDS_ROUND_MIN_DURATION = 5 days; | |
| ... | |
| function deposit(uint256 amountToDeposit) external { | |
| require(amountToDeposit > 0, "Must deposit tokens"); | |
| accToken.mint(msg.sender, amountToDeposit); | |
| distributeRewards(); | |
| require( | |
| liquidityToken.transferFrom(msg.sender, address(this), amountToDeposit) | |
| ); | |
| } | |
| function withdraw(uint256 amountToWithdraw) external { | |
| accToken.burn(msg.sender, amountToWithdraw); | |
| require(liquidityToken.transfer(msg.sender, amountToWithdraw)); | |
| } | |
| function distributeRewards() public returns (uint256) { | |
| uint256 rewards = 0; | |
| if(isNewRewardsRound()) { | |
| _recordSnapshot(); | |
| } | |
| uint256 totalDeposits = accToken.totalSupplyAt(lastSnapshotIdForRewards); | |
| uint256 amountDeposited = accToken.balanceOfAt(msg.sender, lastSnapshotIdForRewards); | |
| if (amountDeposited > 0 && totalDeposits > 0) { | |
| rewards = (amountDeposited * 100 * 10 ** 18) / totalDeposits; | |
| if(rewards > 0 && !_hasRetrievedReward(msg.sender)) { | |
| rewardToken.mint(msg.sender, rewards); | |
| lastRewardTimestamps[msg.sender] = block.timestamp; | |
| } | |
| } | |
| return rewards; | |
| } | |
| function _recordSnapshot() private { | |
| lastSnapshotIdForRewards = accToken.snapshot(); | |
| lastRecordedSnapshotTimestamp = block.timestamp; | |
| roundNumber++; | |
| } | |
| function _hasRetrievedReward(address account) private view returns (bool) { | |
| return ( | |
| lastRewardTimestamps[account] >= lastRecordedSnapshotTimestamp && | |
| lastRewardTimestamps[account] <= lastRecordedSnapshotTimestamp + REWARDS_ROUND_MIN_DURATION | |
| ); | |
| } | |
| function isNewRewardsRound() public view returns (bool) { | |
| return block.timestamp >= lastRecordedSnapshotTimestamp + REWARDS_ROUND_MIN_DURATION; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment