|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import {Math} from "@openzeppelin/contracts/utils/math/Math.sol"; |
| 5 | +import {IReward} from "../interfaces/IReward.sol"; |
| 6 | +import {IGauge} from "../interfaces/IGauge.sol"; |
| 7 | +import {IVoter} from "../interfaces/IVoter.sol"; |
| 8 | +import {IVotingEscrow} from "../interfaces/IVotingEscrow.sol"; |
| 9 | +import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 10 | +import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; |
| 11 | +import {ERC2771Context} from "@openzeppelin/contracts/metatx/ERC2771Context.sol"; |
| 12 | +import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol"; |
| 13 | +import {ProtocolTimeLibrary} from "../libraries/ProtocolTimeLibrary.sol"; |
| 14 | + |
| 15 | +/// @title Protocol Gauge |
| 16 | +/// @author veldorome.finance, @figs999, @pegahcarter |
| 17 | +/// @notice Gauge contract for distribution of emissions by address |
| 18 | +contract Gauge is IGauge, ERC2771Context, ReentrancyGuard { |
| 19 | + using SafeERC20 for IERC20; |
| 20 | + /// @inheritdoc IGauge |
| 21 | + address public immutable stakingToken; |
| 22 | + /// @inheritdoc IGauge |
| 23 | + address public immutable voter; |
| 24 | + /// @inheritdoc IGauge |
| 25 | + address public immutable ve; |
| 26 | + |
| 27 | + uint256 internal constant DURATION = 7 days; // rewards are released over 7 days |
| 28 | + uint256 internal constant PRECISION = 10 ** 18; |
| 29 | + |
| 30 | + /// @inheritdoc IGauge |
| 31 | + uint256 public periodFinish; |
| 32 | + /// @inheritdoc IGauge |
| 33 | + uint256 public rewardRate; |
| 34 | + /// @inheritdoc IGauge |
| 35 | + uint256 public lastUpdateTime; |
| 36 | + /// @inheritdoc IGauge |
| 37 | + uint256 public rewardPerTokenStored; |
| 38 | + /// @inheritdoc IGauge |
| 39 | + uint256 public totalSupply; |
| 40 | + /// @inheritdoc IGauge |
| 41 | + mapping(address => uint256) public balanceOf; |
| 42 | + /// @inheritdoc IGauge |
| 43 | + mapping(address => uint256) public userRewardPerTokenPaid; |
| 44 | + /// @inheritdoc IGauge |
| 45 | + mapping(address => uint256) public rewards; |
| 46 | + /// @inheritdoc IGauge |
| 47 | + mapping(uint256 => uint256) public rewardRateByEpoch; |
| 48 | + |
| 49 | + constructor(address _forwarder, address _stakingToken, address _voter) ERC2771Context(_forwarder) { |
| 50 | + stakingToken = _stakingToken; |
| 51 | + voter = _voter; |
| 52 | + ve = IVoter(voter).ve(); |
| 53 | + } |
| 54 | + |
| 55 | + /// @inheritdoc IGauge |
| 56 | + function rewardPerToken() public view returns (uint256) { |
| 57 | + if (totalSupply == 0) { |
| 58 | + return rewardPerTokenStored; |
| 59 | + } |
| 60 | + return |
| 61 | + rewardPerTokenStored + ((lastTimeRewardApplicable() - lastUpdateTime) * rewardRate * PRECISION) / totalSupply; |
| 62 | + } |
| 63 | + |
| 64 | + /// @inheritdoc IGauge |
| 65 | + function lastTimeRewardApplicable() public view returns (uint256) { |
| 66 | + return Math.min(block.timestamp, periodFinish); |
| 67 | + } |
| 68 | + |
| 69 | + /// @inheritdoc IGauge |
| 70 | + function getReward(address _account) external nonReentrant { |
| 71 | + address sender = _msgSender(); |
| 72 | + if (sender != _account && sender != voter) revert NotAuthorized(); |
| 73 | + |
| 74 | + _updateRewards(_account); |
| 75 | + |
| 76 | + uint256 reward = rewards[_account]; |
| 77 | + if (reward > 0) { |
| 78 | + rewards[_account] = 0; |
| 79 | + payable(_account).transfer(reward); |
| 80 | + emit ClaimRewards(_account, reward); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /// @inheritdoc IGauge |
| 85 | + function earned(address _account) public view returns (uint256) { |
| 86 | + return |
| 87 | + (balanceOf[_account] * (rewardPerToken() - userRewardPerTokenPaid[_account])) / PRECISION + rewards[_account]; |
| 88 | + } |
| 89 | + |
| 90 | + /// @inheritdoc IGauge |
| 91 | + function deposit(uint256 _amount) external { |
| 92 | + _depositFor(_amount, _msgSender()); |
| 93 | + } |
| 94 | + |
| 95 | + /// @inheritdoc IGauge |
| 96 | + function deposit(uint256 _amount, address _recipient) external { |
| 97 | + _depositFor(_amount, _recipient); |
| 98 | + } |
| 99 | + |
| 100 | + function _depositFor(uint256 _amount, address _recipient) internal nonReentrant { |
| 101 | + if (_amount == 0) revert ZeroAmount(); |
| 102 | + if (!IVoter(voter).isAlive(address(this))) revert NotAlive(); |
| 103 | + |
| 104 | + address sender = _msgSender(); |
| 105 | + _updateRewards(_recipient); |
| 106 | + |
| 107 | + IERC20(stakingToken).safeTransferFrom(sender, address(this), _amount); |
| 108 | + totalSupply += _amount; |
| 109 | + balanceOf[_recipient] += _amount; |
| 110 | + |
| 111 | + emit Deposit(sender, _recipient, _amount); |
| 112 | + } |
| 113 | + |
| 114 | + /// @inheritdoc IGauge |
| 115 | + function withdraw(uint256 _amount) external nonReentrant { |
| 116 | + address sender = _msgSender(); |
| 117 | + |
| 118 | + _updateRewards(sender); |
| 119 | + |
| 120 | + totalSupply -= _amount; |
| 121 | + balanceOf[sender] -= _amount; |
| 122 | + IERC20(stakingToken).safeTransfer(sender, _amount); |
| 123 | + |
| 124 | + emit Withdraw(sender, _amount); |
| 125 | + } |
| 126 | + |
| 127 | + function _updateRewards(address _account) internal { |
| 128 | + rewardPerTokenStored = rewardPerToken(); |
| 129 | + lastUpdateTime = lastTimeRewardApplicable(); |
| 130 | + rewards[_account] = earned(_account); |
| 131 | + userRewardPerTokenPaid[_account] = rewardPerTokenStored; |
| 132 | + } |
| 133 | + |
| 134 | + /// @inheritdoc IGauge |
| 135 | + function left() external view returns (uint256) { |
| 136 | + if (block.timestamp >= periodFinish) return 0; |
| 137 | + uint256 _remaining = periodFinish - block.timestamp; |
| 138 | + return _remaining * rewardRate; |
| 139 | + } |
| 140 | + |
| 141 | + /// @inheritdoc IGauge |
| 142 | + function notifyRewardAmount() external payable nonReentrant { |
| 143 | + address sender = _msgSender(); |
| 144 | + uint256 _amount = msg.value; |
| 145 | + if (sender != voter) revert NotVoter(); |
| 146 | + if (_amount == 0) revert ZeroAmount(); |
| 147 | + _notifyRewardAmount(sender, _amount); |
| 148 | + } |
| 149 | + |
| 150 | + /// @inheritdoc IGauge |
| 151 | + function notifyRewardWithoutClaim() external payable nonReentrant { |
| 152 | + address sender = _msgSender(); |
| 153 | + uint256 _amount = msg.value; |
| 154 | + if (sender != IVotingEscrow(ve).team()) revert NotTeam(); |
| 155 | + if (_amount == 0) revert ZeroAmount(); |
| 156 | + _notifyRewardAmount(sender, _amount); |
| 157 | + } |
| 158 | + |
| 159 | + function _notifyRewardAmount(address sender, uint256 _amount) internal { |
| 160 | + rewardPerTokenStored = rewardPerToken(); |
| 161 | + uint256 timestamp = block.timestamp; |
| 162 | + uint256 timeUntilNext = ProtocolTimeLibrary.epochNext(timestamp) - timestamp; |
| 163 | + |
| 164 | + if (timestamp >= periodFinish) { |
| 165 | + rewardRate = _amount / timeUntilNext; |
| 166 | + } else { |
| 167 | + uint256 _remaining = periodFinish - timestamp; |
| 168 | + uint256 _leftover = _remaining * rewardRate; |
| 169 | + rewardRate = (_amount + _leftover) / timeUntilNext; |
| 170 | + } |
| 171 | + rewardRateByEpoch[ProtocolTimeLibrary.epochStart(timestamp)] = rewardRate; |
| 172 | + if (rewardRate == 0) revert ZeroRewardRate(); |
| 173 | + |
| 174 | + // Ensure the provided reward amount is not more than the balance in the contract. |
| 175 | + // This keeps the reward rate in the right range, preventing overflows due to |
| 176 | + // very high values of rewardRate in the earned and rewardsPerToken functions; |
| 177 | + // Reward + leftover must be less than 2^256 / 10^18 to avoid overflow. |
| 178 | + uint256 balance = address(this).balance; |
| 179 | + if (rewardRate > balance / timeUntilNext) revert RewardRateTooHigh(); |
| 180 | + |
| 181 | + lastUpdateTime = timestamp; |
| 182 | + periodFinish = timestamp + timeUntilNext; |
| 183 | + emit NotifyReward(sender, _amount); |
| 184 | + } |
| 185 | +} |
0 commit comments