-
Notifications
You must be signed in to change notification settings - Fork 0
/
LiquidityGaugePool.sol
247 lines (190 loc) · 8.49 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// 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/PausableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol";
import "../util/TokenRecovery.sol";
import "./LiquidityGaugePoolReward.sol";
import "../util/interfaces/IAccessControlUtil.sol";
contract LiquidityGaugePool is IAccessControlUtil, AccessControlUpgradeable, PausableUpgradeable, ReentrancyGuardUpgradeable, TokenRecovery, LiquidityGaugePoolReward {
using SafeERC20Upgradeable for IERC20Upgradeable;
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
function initialize(InitializationArgs 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]);
}
if (args.key == 0) {
revert InvalidArgumentError("args.key");
}
if (args.registry == address(0)) {
revert InvalidArgumentError("args.registry");
}
if (args.rewardToken == address(0)) {
revert InvalidArgumentError("args.rewardToken");
}
if (args.stakingToken == address(0)) {
revert InvalidArgumentError("args.stakingToken");
}
if (args.veToken == address(0)) {
revert InvalidArgumentError("args.veToken");
}
_key = args.key;
_registry = args.registry;
_rewardToken = args.rewardToken;
_stakingToken = args.stakingToken;
_veToken = args.veToken;
_setPool(args.poolInfo);
emit LiquidityGaugePoolInitialized(args.key, _msgSender(), args);
}
function setPool(PoolInfo calldata args) external override onlyRole(DEFAULT_ADMIN_ROLE) {
if (block.timestamp <= _epochEndTimestamp) {
revert EpochUnavailableError();
}
_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();
}
IERC20Upgradeable(_stakingToken).safeTransferFrom(_msgSender(), address(this), amount);
_updateReward(_msgSender(), false);
_lockedByEveryone += amount;
_lockedByMe[_msgSender()] += amount;
_lastDepositHeights[_msgSender()] = block.number;
emit LiquidityGaugeDeposited(_key, _msgSender(), _stakingToken, amount);
}
function _withdraw(uint256 amount, bool emergency) private {
if (amount == 0) {
revert ZeroAmountError("amount");
}
if (amount > _lockedByMe[_msgSender()]) {
revert WithdrawalTooHighError(_lockedByMe[_msgSender()], amount);
}
if (block.number < _lastDepositHeights[_msgSender()] + _LOCKUP_PERIOD_IN_BLOCKS) {
revert WithdrawalLockedError(_lastDepositHeights[_msgSender()] + _LOCKUP_PERIOD_IN_BLOCKS);
}
_updateReward(_msgSender(), emergency);
_lockedByEveryone -= amount;
_lockedByMe[_msgSender()] -= amount;
IERC20Upgradeable(_stakingToken).safeTransfer(_msgSender(), amount);
emit LiquidityGaugeWithdrawn(_key, _msgSender(), _stakingToken, amount);
}
function withdraw(uint256 amount) external override nonReentrant whenNotPaused {
_withdraw(amount, false);
}
function _withdrawRewards() private {
_updateReward(_msgSender(), false);
uint256 rewards = _pendingRewardToDistribute[_msgSender()];
if (rewards > 0) {
uint256 platformFee = (rewards * _poolInfo.platformFee) / _denominator();
_pendingRewardToDistribute[_msgSender()] = 0;
IERC20Upgradeable(_rewardToken).safeTransfer(_msgSender(), rewards - platformFee);
if (platformFee > 0) {
IERC20Upgradeable(_rewardToken).safeTransfer(_poolInfo.treasury, platformFee);
}
emit LiquidityGaugeRewardsWithdrawn(_key, _msgSender(), _poolInfo.treasury, rewards, platformFee);
}
}
function withdrawRewards() external override nonReentrant whenNotPaused {
_withdrawRewards();
}
function exit() external override nonReentrant whenNotPaused {
_withdraw(_lockedByMe[_msgSender()], false);
_withdrawRewards();
}
function emergencyWithdraw() external override nonReentrant {
_withdraw(_lockedByMe[_msgSender()], true);
}
// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Gauge Controller Registry Only
// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function setEpoch(uint256 epoch, uint256 epochDuration, uint256 rewards) external override nonReentrant onlyRegistry {
_updateReward(address(0), false);
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(_rewardToken).balanceOf(address(this))) {
revert InsufficientDepositError(_poolInfo.epochDuration * _rewardPerSecond, IERC20Upgradeable(_rewardToken).balanceOf(address(this)));
}
_lastRewardTimestamp = block.timestamp;
_epochEndTimestamp = block.timestamp + _poolInfo.epochDuration;
emit EpochRewardSet(_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) {
if (address(malicious) == _stakingToken || address(malicious) == _rewardToken) {
revert InvalidArgumentError("malicious");
}
_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 _key;
}
}