|
| 1 | +// SPDX-License-Identifier: AGPL-3.0 |
| 2 | +pragma solidity ^0.8.10; |
| 3 | + |
| 4 | +import {ERC20} from "solmate/tokens/ERC20.sol"; |
| 5 | +import {ERC4626} from "solmate/mixins/ERC4626.sol"; |
| 6 | +import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol"; |
| 7 | +import {ReentrancyGuard} from "openzeppelin-contracts/security/ReentrancyGuard.sol"; |
| 8 | + |
| 9 | +abstract contract BonusTracker is ERC4626, ReentrancyGuard { |
| 10 | + using FixedPointMathLib for uint256; |
| 11 | + |
| 12 | + event BonusPaid(address indexed user, uint256 bonus); |
| 13 | + event BonusBurned(address indexed user, uint256 bonus); |
| 14 | + |
| 15 | + uint256 internal constant PRECISION = 1e30; |
| 16 | + |
| 17 | + /// @notice The last Unix timestamp (in seconds) when bonusPerTokenStored was updated |
| 18 | + uint64 public lastBonusUpdateTime; |
| 19 | + |
| 20 | + /// @notice The last stored bonusPerToken value |
| 21 | + uint256 public bonusPerTokenStored; |
| 22 | + |
| 23 | + /// @notice The total bonus amount currently held by users |
| 24 | + uint256 public totalBonus; |
| 25 | + |
| 26 | + /// @notice The bonusPerToken value when an account last compounded/withdrew bonus |
| 27 | + mapping(address => uint256) public userBonusPerTokenPaid; |
| 28 | + |
| 29 | + /// @notice The number of multiplier points compounded for an account |
| 30 | + mapping(address => uint256) public multiplierPointsOf; |
| 31 | + |
| 32 | + /// @notice The bonusOf() value when an account last staked/withdrew bonus |
| 33 | + mapping(address => uint256) public bonus; |
| 34 | + |
| 35 | + constructor(ERC20 _asset, string memory _name, string memory _symbol) ERC4626(_asset, _name, _symbol) { |
| 36 | + lastBonusUpdateTime = uint64(block.timestamp); |
| 37 | + } |
| 38 | + |
| 39 | + /// @notice Claim bonus |
| 40 | + function boost() external nonReentrant returns (uint256 _bonus) { |
| 41 | + _updateReward(msg.sender); |
| 42 | + _updateBonus(msg.sender); |
| 43 | + _bonus = bonus[msg.sender]; |
| 44 | + |
| 45 | + if (_bonus > 0) { |
| 46 | + bonus[msg.sender] = 0; |
| 47 | + multiplierPointsOf[msg.sender] += _bonus; |
| 48 | + totalBonus += _bonus; |
| 49 | + emit BonusPaid(msg.sender, _bonus); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// @notice The latest time at which stakers are earning bonus. |
| 54 | + function lastTimeBonusApplicable() public view returns (uint64) { |
| 55 | + return uint64(block.timestamp); |
| 56 | + } |
| 57 | + |
| 58 | + /// @notice The amount of bonus tokens each staked token has earned so far |
| 59 | + function bonusPerToken() external view returns (uint256) { |
| 60 | + return _bonusPerToken(lastTimeBonusApplicable()); |
| 61 | + } |
| 62 | + |
| 63 | + /// @notice The amount of bonus tokens an account has accrued so far. |
| 64 | + function bonusOf(address _account) external view returns (uint256) { |
| 65 | + return _earnedBonus(_account, balanceOf[_account], _bonusPerToken(lastTimeBonusApplicable()), bonus[_account]); |
| 66 | + } |
| 67 | + |
| 68 | + function _earnedBonus(address account, uint256 accountBalance, uint256 bonusPerToken_, uint256 accountBonus) |
| 69 | + internal |
| 70 | + view |
| 71 | + returns (uint256) |
| 72 | + { |
| 73 | + return accountBalance.mulDivDown(bonusPerToken_ - userBonusPerTokenPaid[account], PRECISION) + accountBonus; |
| 74 | + } |
| 75 | + |
| 76 | + function _bonusPerToken(uint256 lastTimeBonusApplicable_) internal view returns (uint256) { |
| 77 | + return bonusPerTokenStored + (lastTimeBonusApplicable_ - lastBonusUpdateTime).mulDivDown(PRECISION, 365 days); |
| 78 | + } |
| 79 | + |
| 80 | + function _updateBonus(address _account) internal { |
| 81 | + // storage loads |
| 82 | + uint256 accountBalance = balanceOf[_account]; |
| 83 | + uint64 lastTimeBonusApplicable_ = lastTimeBonusApplicable(); |
| 84 | + uint256 bonusPerToken_ = _bonusPerToken(lastTimeBonusApplicable_); |
| 85 | + |
| 86 | + // accrue bonus |
| 87 | + bonusPerTokenStored = bonusPerToken_; |
| 88 | + lastBonusUpdateTime = lastTimeBonusApplicable_; |
| 89 | + bonus[_account] = _earnedBonus(_account, accountBalance, bonusPerToken_, bonus[_account]); |
| 90 | + userBonusPerTokenPaid[_account] = bonusPerToken_; |
| 91 | + } |
| 92 | + |
| 93 | + function _updateReward(address) internal virtual {} |
| 94 | +} |
0 commit comments