-
Notifications
You must be signed in to change notification settings - Fork 0
/
LiquidityGaugePool.sol
211 lines (163 loc) · 7.56 KB
/
LiquidityGaugePool.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Neptune Mutual Protocol (https://neptunemutual.com)
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.12;
import "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol";
import "../util/TokenRecovery.sol";
import "../util/WithPausability.sol";
import "./LiquidityGaugePoolReward.sol";
import "../util/interfaces/IAccessControlUtil.sol";
contract LiquidityGaugePool is IAccessControlUtil, AccessControlUpgradeable, ReentrancyGuardUpgradeable, TokenRecovery, WithPausability, LiquidityGaugePoolReward {
using SafeERC20Upgradeable for IERC20Upgradeable;
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
function initialize(PoolInfo calldata args, address admin, address[] calldata pausers) external initializer {
if (admin == address(0)) {
revert InvalidArgumentError("admin");
}
__AccessControl_init();
__Pausable_init();
__ReentrancyGuard_init();
_setRoleAdmin(_NS_ROLES_PAUSER, DEFAULT_ADMIN_ROLE);
_setRoleAdmin(_NS_ROLES_RECOVERY_AGENT, DEFAULT_ADMIN_ROLE);
_grantRole(DEFAULT_ADMIN_ROLE, admin);
_grantRole(_NS_ROLES_RECOVERY_AGENT, admin);
for (uint256 i = 0; i < pausers.length; i++) {
if (pausers[i] == address(0)) {
revert InvalidArgumentError("pausers");
}
_grantRole(_NS_ROLES_PAUSER, pausers[i]);
}
_setPool(args);
}
function setPool(PoolInfo calldata args) external override onlyRole(DEFAULT_ADMIN_ROLE) {
_setPool(args);
}
// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Access Control
// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function grantRoles(AccountWithRoles[] calldata detail) external override whenNotPaused {
if (detail.length == 0) {
revert InvalidArgumentError("detail");
}
for (uint256 i = 0; i < detail.length; i++) {
for (uint256 j = 0; j < detail[i].roles.length; j++) {
grantRole(detail[i].roles[j], detail[i].account);
}
}
}
// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Danger!!! External & Public Functions
// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function deposit(uint256 amount) external override nonReentrant whenNotPaused {
if (amount == 0) {
revert ZeroAmountError("amount");
}
if (_epoch == 0) {
revert EpochUnavailableError();
}
_updateReward(_msgSender());
_lockedByEveryone += amount;
_lockedByMe[_msgSender()] += amount;
_lastDepositHeights[_msgSender()] = block.number;
IERC20Upgradeable(_poolInfo.stakingToken).safeTransferFrom(_msgSender(), address(this), amount);
emit LiquidityGaugeDeposited(_poolInfo.key, _msgSender(), _poolInfo.stakingToken, amount);
}
function _withdraw(uint256 amount) private {
if (amount == 0) {
revert ZeroAmountError("amount");
}
if (amount > _lockedByMe[_msgSender()]) {
revert WithdrawalTooHighError(_lockedByMe[_msgSender()], amount);
}
if (block.number < _lastDepositHeights[_msgSender()] + _poolInfo.lockupPeriodInBlocks) {
revert WithdrawalLockedError(_lastDepositHeights[_msgSender()] + _poolInfo.lockupPeriodInBlocks);
}
_updateReward(_msgSender());
_lockedByEveryone -= amount;
_lockedByMe[_msgSender()] -= amount;
IERC20Upgradeable(_poolInfo.stakingToken).safeTransfer(_msgSender(), amount);
emit LiquidityGaugeWithdrawn(_poolInfo.key, _msgSender(), _poolInfo.stakingToken, amount);
}
function withdraw(uint256 amount) external override nonReentrant whenNotPaused {
_withdraw(amount);
}
function _withdrawRewards() private {
_updateReward(_msgSender());
uint256 rewards = _pendingRewardToDistribute[_msgSender()];
if (rewards > 0) {
uint256 platformFee = (rewards * _poolInfo.platformFee) / _denominator();
if (rewards <= platformFee) {
revert PlatformFeeTooHighError(_poolInfo.platformFee);
}
_pendingRewardToDistribute[_msgSender()] = 0;
IERC20Upgradeable(_poolInfo.rewardToken).safeTransfer(_msgSender(), rewards - platformFee);
if (platformFee > 0) {
IERC20Upgradeable(_poolInfo.rewardToken).safeTransfer(_poolInfo.treasury, platformFee);
}
emit LiquidityGaugeRewardsWithdrawn(_poolInfo.key, _msgSender(), _poolInfo.treasury, rewards, platformFee);
}
}
function withdrawRewards() external override nonReentrant whenNotPaused {
_withdrawRewards();
}
function exit() external override nonReentrant whenNotPaused {
_withdraw(_lockedByMe[_msgSender()]);
_withdrawRewards();
}
// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Gauge Controller Registry Only
// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function setEpoch(uint256 epoch, uint256 epochDuration, uint256 rewards) external override nonReentrant onlyRegistry {
_updateReward(address(0));
if (epochDuration > 0) {
_setEpochDuration(epochDuration);
}
if (block.timestamp >= _epochEndTimestamp) {
_rewardPerSecond = rewards / _poolInfo.epochDuration;
} else {
uint256 remaining = _epochEndTimestamp - block.timestamp;
uint256 leftover = remaining * _rewardPerSecond;
_rewardPerSecond = (rewards + leftover) / _poolInfo.epochDuration;
}
if (epoch <= _epoch) {
revert InvalidArgumentError("epoch");
}
_epoch = epoch;
if (_poolInfo.epochDuration * _rewardPerSecond > IERC20Upgradeable(_poolInfo.rewardToken).balanceOf(address(this))) {
revert InsufficientDepositError(_poolInfo.epochDuration * _rewardPerSecond, IERC20Upgradeable(_poolInfo.rewardToken).balanceOf(address(this)));
}
_lastRewardTimestamp = block.timestamp;
_epochEndTimestamp = block.timestamp + _poolInfo.epochDuration;
emit EpochRewardSet(_poolInfo.key, _msgSender(), rewards);
}
// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Recoverable
// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function recoverEther(address sendTo) external onlyRole(_NS_ROLES_RECOVERY_AGENT) {
_recoverEther(sendTo);
}
function recoverToken(IERC20Upgradeable malicious, address sendTo) external onlyRole(_NS_ROLES_RECOVERY_AGENT) {
_recoverToken(malicious, sendTo);
}
// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Pausable
// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function pause() external onlyRole(_NS_ROLES_PAUSER) {
_pause();
}
function unpause() external onlyRole(DEFAULT_ADMIN_ROLE) {
_unpause();
}
// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Getters
// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function calculateReward(address account) external view returns (uint256) {
return _getPendingRewards(account);
}
function getKey() external view override returns (bytes32) {
return _poolInfo.key;
}
}