|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.4; |
| 3 | + |
| 4 | +import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 5 | +import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; |
| 6 | +import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; |
| 7 | + |
| 8 | +import {AbstractValueDistributor} from "./AbstractValueDistributor.sol"; |
| 9 | + |
| 10 | +/** |
| 11 | + * @notice The AbstractStaking module |
| 12 | + * |
| 13 | + * Contract module for staking tokens and earning rewards based on shares. |
| 14 | + */ |
| 15 | +abstract contract AbstractStaking is AbstractValueDistributor, Initializable { |
| 16 | + using SafeERC20 for IERC20; |
| 17 | + |
| 18 | + address private _sharesToken; |
| 19 | + address private _rewardsToken; |
| 20 | + |
| 21 | + /** |
| 22 | + * @dev The rate of rewards distribution per second. |
| 23 | + * |
| 24 | + * It determines the rate at which rewards are earned and distributed |
| 25 | + * to stakers based on their shares. |
| 26 | + * |
| 27 | + * Note: Ensure that the `_rate` value is set correctly to match |
| 28 | + * the decimal precision of the `_rewardsToken` to ensure accurate rewards distribution. |
| 29 | + */ |
| 30 | + uint256 private _rate; |
| 31 | + |
| 32 | + uint256 private _stakingStartTime; |
| 33 | + |
| 34 | + /** |
| 35 | + * @dev Throws if the staking has not started yet. |
| 36 | + */ |
| 37 | + modifier stakingStarted() { |
| 38 | + _checkStakingStarted(); |
| 39 | + _; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @notice Initializes the contract setting the values provided as shares token, rewards token, reward rate and staking start time. |
| 44 | + * |
| 45 | + * Warning: when shares and rewards tokens are the same, users may accidentally withdraw |
| 46 | + * other users' shares as a reward if the rewards token balance is improperly handled. |
| 47 | + * |
| 48 | + * @param sharesToken_ The address of the shares token. |
| 49 | + * @param rewardsToken_ The address of the rewards token. |
| 50 | + * @param rate_ The reward rate. |
| 51 | + * @param stakingStartTime_ The staking start time |
| 52 | + */ |
| 53 | + function __AbstractStaking_init( |
| 54 | + address sharesToken_, |
| 55 | + address rewardsToken_, |
| 56 | + uint256 rate_, |
| 57 | + uint256 stakingStartTime_ |
| 58 | + ) internal onlyInitializing { |
| 59 | + require(sharesToken_ != address(0), "Staking: zero address cannot be the Shares Token"); |
| 60 | + require(rewardsToken_ != address(0), "Staking: zero address cannot be the Rewards Token"); |
| 61 | + |
| 62 | + _sharesToken = sharesToken_; |
| 63 | + _rewardsToken = rewardsToken_; |
| 64 | + _setRate(rate_); |
| 65 | + _setStakingStartTime(stakingStartTime_); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @notice Stakes the specified amount of tokens. |
| 70 | + * @param amount_ The amount of tokens to stake. |
| 71 | + */ |
| 72 | + function stake(uint256 amount_) public stakingStarted { |
| 73 | + _addShares(msg.sender, amount_); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @notice Unstakes the specified amount of tokens. |
| 78 | + * @param amount_ The amount of tokens to unstake. |
| 79 | + */ |
| 80 | + function unstake(uint256 amount_) public stakingStarted { |
| 81 | + _removeShares(msg.sender, amount_); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @notice Claims the specified amount of rewards. |
| 86 | + * @param amount_ The amount of rewards to claim. |
| 87 | + */ |
| 88 | + function claim(uint256 amount_) public stakingStarted { |
| 89 | + _distributeValue(msg.sender, amount_); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @notice Withdraws all the staked tokens together with rewards. |
| 94 | + * |
| 95 | + * Note: All the rewards are claimed after the shares are removed. |
| 96 | + * |
| 97 | + * @return shares_ The amount of shares being withdrawn. |
| 98 | + * @return owedValue_ The total value of the rewards owed to the user. |
| 99 | + */ |
| 100 | + function withdraw() public stakingStarted returns (uint256 shares_, uint256 owedValue_) { |
| 101 | + shares_ = userDistribution(msg.sender).shares; |
| 102 | + owedValue_ = getOwedValue(msg.sender); |
| 103 | + |
| 104 | + unstake(shares_); |
| 105 | + claim(owedValue_); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @notice Returns the shares token. |
| 110 | + * @return The address of the shares token contract. |
| 111 | + */ |
| 112 | + function sharesToken() public view returns (address) { |
| 113 | + return _sharesToken; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @notice Returns the rewards token. |
| 118 | + * @return The address of the rewards token contract. |
| 119 | + */ |
| 120 | + function rewardsToken() public view returns (address) { |
| 121 | + return _rewardsToken; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @notice Returns the staking start time. |
| 126 | + * @return The timestamp when staking starts. |
| 127 | + */ |
| 128 | + function stakingStartTime() public view returns (uint256) { |
| 129 | + return _stakingStartTime; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * @notice Returns the rate of rewards distribution. |
| 134 | + * @return The rate of rewards distribution per second. |
| 135 | + */ |
| 136 | + function rate() public view returns (uint256) { |
| 137 | + return _rate; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @notice Sets the staking start time. |
| 142 | + * @param stakingStartTime_ The timestamp when staking will start. |
| 143 | + */ |
| 144 | + function _setStakingStartTime(uint256 stakingStartTime_) internal { |
| 145 | + _stakingStartTime = stakingStartTime_; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @notice Sets the rate of rewards distribution per second. |
| 150 | + * @param newRate_ The new rate of rewards distribution. |
| 151 | + */ |
| 152 | + function _setRate(uint256 newRate_) internal { |
| 153 | + _update(address(0)); |
| 154 | + |
| 155 | + _rate = newRate_; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * @notice Hook function that is called after shares have been added to a user's distribution. |
| 160 | + * @param user_ The address of the user. |
| 161 | + * @param amount_ The amount of shares added. |
| 162 | + */ |
| 163 | + function _afterAddShares(address user_, uint256 amount_) internal virtual override { |
| 164 | + IERC20(_sharesToken).safeTransferFrom(user_, address(this), amount_); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * @notice Hook function that is called after shares have been removed from a user's distribution. |
| 169 | + * @param user_ The address of the user. |
| 170 | + * @param amount_ The amount of shares removed. |
| 171 | + */ |
| 172 | + function _afterRemoveShares(address user_, uint256 amount_) internal virtual override { |
| 173 | + IERC20(_sharesToken).safeTransfer(user_, amount_); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * @notice Hook function that is called after value has been distributed to a user. |
| 178 | + * @param user_ The address of the user. |
| 179 | + * @param amount_ The amount of value distributed. |
| 180 | + */ |
| 181 | + function _afterDistributeValue(address user_, uint256 amount_) internal virtual override { |
| 182 | + IERC20(_rewardsToken).safeTransfer(user_, amount_); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * @dev Throws if the staking has not started yet. |
| 187 | + */ |
| 188 | + function _checkStakingStarted() internal view { |
| 189 | + require(block.timestamp >= _stakingStartTime, "Staking: staking has not started yet"); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * @notice Gets the value to be distributed for a given time period. |
| 194 | + * @param timeUpTo_ The end timestamp of the period. |
| 195 | + * @param timeLastUpdate_ The start timestamp of the period. |
| 196 | + * @return The value to be distributed for the period. |
| 197 | + */ |
| 198 | + function _getValueToDistribute( |
| 199 | + uint256 timeUpTo_, |
| 200 | + uint256 timeLastUpdate_ |
| 201 | + ) internal view virtual override returns (uint256) { |
| 202 | + return _rate * (timeUpTo_ - timeLastUpdate_); |
| 203 | + } |
| 204 | +} |
0 commit comments