-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLRTDepositPool.sol
437 lines (371 loc) · 16.2 KB
/
LRTDepositPool.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;
import { UtilLib } from "./utils/UtilLib.sol";
import { LRTConstants } from "./utils/LRTConstants.sol";
import { LRTConfigRoleChecker, ILRTConfig } from "./utils/LRTConfigRoleChecker.sol";
import { IRSETH } from "./interfaces/IRSETH.sol";
import { ILRTOracle } from "./interfaces/ILRTOracle.sol";
import { INodeDelegator } from "./interfaces/INodeDelegator.sol";
import { ILRTDepositPool } from "./interfaces/ILRTDepositPool.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { PausableUpgradeable } from "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";
import { ReentrancyGuardUpgradeable } from "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol";
/// @title LRTDepositPool - Deposit Pool Contract for LSTs
/// @notice Handles LST asset deposits
contract LRTDepositPool is ILRTDepositPool, LRTConfigRoleChecker, PausableUpgradeable, ReentrancyGuardUpgradeable {
uint256 public maxNodeDelegatorLimit;
uint256 public minAmountToDeposit;
mapping(address => uint256) public isNodeDelegator; // 0: not a node delegator, 1: is a node delegator
address[] public nodeDelegatorQueue;
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
/// @dev Initializes the contract
/// @param lrtConfigAddr LRT config address
function initialize(address lrtConfigAddr) external initializer {
UtilLib.checkNonZeroAddress(lrtConfigAddr);
__Pausable_init();
__ReentrancyGuard_init();
maxNodeDelegatorLimit = 10;
lrtConfig = ILRTConfig(lrtConfigAddr);
emit UpdatedLRTConfig(lrtConfigAddr);
}
/*//////////////////////////////////////////////////////////////
view functions
//////////////////////////////////////////////////////////////*/
/// @notice gets the total asset present in protocol
/// @param asset Asset address
/// @return totalAssetDeposit total asset present in protocol
function getTotalAssetDeposits(address asset) public view override returns (uint256 totalAssetDeposit) {
(uint256 assetLyingInDepositPool, uint256 assetLyingInNDCs, uint256 assetStakedInEigenLayer) =
getAssetDistributionData(asset);
return (assetLyingInDepositPool + assetLyingInNDCs + assetStakedInEigenLayer);
}
/// @notice gets the current limit of asset deposit
/// @param asset Asset address
/// @return currentLimit Current limit of asset deposit
function getAssetCurrentLimit(address asset) public view override returns (uint256) {
if (getTotalAssetDeposits(asset) > lrtConfig.depositLimitByAsset(asset)) {
return 0;
}
return lrtConfig.depositLimitByAsset(asset) - getTotalAssetDeposits(asset);
}
/// @dev get node delegator queue
/// @return nodeDelegatorQueue Array of node delegator contract addresses
function getNodeDelegatorQueue() external view override returns (address[] memory) {
return nodeDelegatorQueue;
}
/// @dev provides asset amount distribution data among depositPool, NDCs and eigenLayer
/// @param asset the asset to get the total amount of
/// @return assetLyingInDepositPool asset amount lying in this LRTDepositPool contract
/// @return assetLyingInNDCs asset amount sum lying in all NDC contract
/// @return assetStakedInEigenLayer asset amount deposited in eigen layer strategies through all NDCs
function getAssetDistributionData(address asset)
public
view
override
onlySupportedAsset(asset)
returns (uint256 assetLyingInDepositPool, uint256 assetLyingInNDCs, uint256 assetStakedInEigenLayer)
{
if (asset == LRTConstants.ETH_TOKEN) {
return getETHDistributionData();
}
assetLyingInDepositPool = IERC20(asset).balanceOf(address(this));
uint256 ndcsCount = nodeDelegatorQueue.length;
for (uint256 i; i < ndcsCount;) {
assetLyingInNDCs += IERC20(asset).balanceOf(nodeDelegatorQueue[i]);
assetStakedInEigenLayer += INodeDelegator(nodeDelegatorQueue[i]).getAssetBalance(asset);
unchecked {
++i;
}
}
}
/// @dev provides ETH amount distribution data among depositPool, NDCs and eigenLayer
function getETHDistributionData()
public
view
override
returns (uint256 ethLyingInDepositPool, uint256 ethLyingInNDCs, uint256 ethStakedInEigenLayer)
{
ethLyingInDepositPool = address(this).balance;
uint256 ndcsCount = nodeDelegatorQueue.length;
for (uint256 i; i < ndcsCount;) {
ethLyingInNDCs += nodeDelegatorQueue[i].balance;
ethStakedInEigenLayer += INodeDelegator(nodeDelegatorQueue[i]).getETHEigenPodBalance();
unchecked {
++i;
}
}
}
/// @notice View amount of rsETH to mint for given asset amount
/// @param asset Asset address
/// @param amount Asset amount
/// @return rsethAmountToMint Amount of rseth to mint
function getRsETHAmountToMint(
address asset,
uint256 amount
)
public
view
override
returns (uint256 rsethAmountToMint)
{
// setup oracle contract
address lrtOracleAddress = lrtConfig.getContract(LRTConstants.LRT_ORACLE);
ILRTOracle lrtOracle = ILRTOracle(lrtOracleAddress);
// calculate rseth amount to mint based on asset amount and asset exchange rate
rsethAmountToMint = (amount * lrtOracle.getAssetPrice(asset)) / lrtOracle.rsETHPrice();
}
/*//////////////////////////////////////////////////////////////
write functions
//////////////////////////////////////////////////////////////*/
/// @notice Allows user to deposit ETH to the protocol
/// @param minRSETHAmountExpected Minimum amount of rseth to receive
/// @param referralId referral id
function depositETH(
uint256 minRSETHAmountExpected,
string calldata referralId
)
external
payable
whenNotPaused
nonReentrant
{
// checks
uint256 rsethAmountToMint = _beforeDeposit(LRTConstants.ETH_TOKEN, msg.value, minRSETHAmountExpected);
// interactions
_mintRsETH(rsethAmountToMint);
emit ETHDeposit(msg.sender, msg.value, rsethAmountToMint, referralId);
}
/// @notice helps user stake LST to the protocol
/// @param asset LST asset address to stake
/// @param depositAmount LST asset amount to stake
/// @param minRSETHAmountExpected Minimum amount of rseth to receive
function depositAsset(
address asset,
uint256 depositAmount,
uint256 minRSETHAmountExpected,
string calldata referralId
)
external
whenNotPaused
nonReentrant
onlySupportedAsset(asset)
{
// checks
uint256 rsethAmountToMint = _beforeDeposit(asset, depositAmount, minRSETHAmountExpected);
// interactions
if (!IERC20(asset).transferFrom(msg.sender, address(this), depositAmount)) {
revert TokenTransferFailed();
}
_mintRsETH(rsethAmountToMint);
emit AssetDeposit(msg.sender, asset, depositAmount, rsethAmountToMint, referralId);
}
function _beforeDeposit(
address asset,
uint256 depositAmount,
uint256 minRSETHAmountExpected
)
private
view
returns (uint256 rsethAmountToMint)
{
if (depositAmount == 0 || depositAmount < minAmountToDeposit) {
revert InvalidAmountToDeposit();
}
if (depositAmount > getAssetCurrentLimit(asset)) {
revert MaximumDepositLimitReached();
}
rsethAmountToMint = getRsETHAmountToMint(asset, depositAmount);
if (rsethAmountToMint < minRSETHAmountExpected) {
revert MinimumAmountToReceiveNotMet();
}
}
/// @dev private function to mint rseth
/// @param rsethAmountToMint Amount of rseth minted
function _mintRsETH(uint256 rsethAmountToMint) private {
address rsethToken = lrtConfig.rsETH();
// mint rseth for user
IRSETH(rsethToken).mint(msg.sender, rsethAmountToMint);
}
/// @notice add new node delegator contract addresses
/// @dev only callable by LRT admin
/// @param nodeDelegatorContracts Array of NodeDelegator contract addresses
function addNodeDelegatorContractToQueue(address[] calldata nodeDelegatorContracts) external onlyLRTAdmin {
uint256 length = nodeDelegatorContracts.length;
if (nodeDelegatorQueue.length + length > maxNodeDelegatorLimit) {
revert MaximumNodeDelegatorLimitReached();
}
for (uint256 i; i < length;) {
UtilLib.checkNonZeroAddress(nodeDelegatorContracts[i]);
// check if node delegator contract is already added and add it if not
if (isNodeDelegator[nodeDelegatorContracts[i]] == 0) {
nodeDelegatorQueue.push(nodeDelegatorContracts[i]);
}
isNodeDelegator[nodeDelegatorContracts[i]] = 1;
unchecked {
++i;
}
}
emit NodeDelegatorAddedinQueue(nodeDelegatorContracts);
}
/// @notice remove node delegator contract address from queue
/// @dev only callable by LRT admin
/// @param nodeDelegatorAddress NodeDelegator contract address
function removeNodeDelegatorContractFromQueue(address nodeDelegatorAddress) public onlyLRTAdmin {
// 1. check if node delegator contract is in queue
uint256 length = nodeDelegatorQueue.length;
uint256 ndcIndex;
for (uint256 i; i < length;) {
if (nodeDelegatorQueue[i] == nodeDelegatorAddress) {
ndcIndex = i;
break;
}
// 1.1 If node delegator contract is not found in queue, revert
if (i == length - 1) {
revert NodeDelegatorNotFound();
}
unchecked {
++i;
}
}
// 2. revert if node delegator contract has any asset balances.
// 2.1 check if NDC has native ETH balance in eigen layer and in itself.
if (
INodeDelegator(nodeDelegatorAddress).getETHEigenPodBalance() > 0
|| address(nodeDelegatorAddress).balance > 0
) {
revert NodeDelegatorHasETH();
}
// 2.2 check if NDC has LST balance
address[] memory supportedAssets = lrtConfig.getSupportedAssetList();
uint256 supportedAssetsLength = supportedAssets.length;
uint256 assetBalance;
for (uint256 i; i < supportedAssetsLength; i++) {
if (supportedAssets[i] == LRTConstants.ETH_TOKEN) {
// ETH already checked above.
continue;
}
assetBalance = IERC20(supportedAssets[i]).balanceOf(nodeDelegatorAddress)
+ INodeDelegator(nodeDelegatorAddress).getAssetBalance(supportedAssets[i]);
if (assetBalance > 0) {
revert NodeDelegatorHasAssetBalance(supportedAssets[i], assetBalance);
}
}
// 3. remove node delegator contract from queue
// 3.1 remove from isNodeDelegator mapping
isNodeDelegator[nodeDelegatorAddress] = 0;
// 3.2 remove from nodeDelegatorQueue
nodeDelegatorQueue[ndcIndex] = nodeDelegatorQueue[length - 1];
nodeDelegatorQueue.pop();
emit NodeDelegatorRemovedFromQueue(nodeDelegatorAddress);
}
/// @notice remove many node delegator contracts from queue
/// @dev calls internally removeNodeDelegatorContractFromQueue which is only callable by LRT admin
/// @param nodeDelegatorContracts Array of NodeDelegator contract addresses
function removeManyNodeDelegatorContractsFromQueue(address[] calldata nodeDelegatorContracts) external {
uint256 length = nodeDelegatorContracts.length;
for (uint256 i; i < length;) {
removeNodeDelegatorContractFromQueue(nodeDelegatorContracts[i]);
unchecked {
++i;
}
}
}
/// @notice transfers asset lying in this DepositPool to node delegator contract
/// @dev only callable by LRT manager
/// @param ndcIndex Index of NodeDelegator contract address in nodeDelegatorQueue
/// @param asset Asset address
/// @param amount Asset amount to transfer
function transferAssetToNodeDelegator(
uint256 ndcIndex,
address asset,
uint256 amount
)
external
nonReentrant
onlyLRTManager
onlySupportedAsset(asset)
{
address nodeDelegator = nodeDelegatorQueue[ndcIndex];
if (!IERC20(asset).transfer(nodeDelegator, amount)) {
revert TokenTransferFailed();
}
}
/// @notice transfers ETH lying in this DepositPool to node delegator contract
/// @dev only callable by LRT manager
/// @param ndcIndex Index of NodeDelegator contract address in nodeDelegatorQueue
/// @param amount ETH amount to transfer
function transferETHToNodeDelegator(uint256 ndcIndex, uint256 amount) external nonReentrant onlyLRTManager {
address nodeDelegator = nodeDelegatorQueue[ndcIndex];
INodeDelegator(nodeDelegator).sendETHFromDepositPoolToNDC{ value: amount }();
}
/// @notice swap ETH for LST asset which is accepted by LRTDepositPool
/// @dev use LRTOracle to get price for toToken. Only callable by LRT manager
/// @param toAsset Asset address to swap to
/// @param minToAssetAmount Minimum asset amount to swap to
function swapETHForAssetWithinDepositPool(
address toAsset,
uint256 minToAssetAmount
)
external
payable
onlyLRTManager
onlySupportedAsset(toAsset)
{
// checks
uint256 ethAmountSent = msg.value;
uint256 returnAmount = getSwapETHToAssetReturnAmount(toAsset, ethAmountSent);
if (returnAmount < minToAssetAmount || IERC20(toAsset).balanceOf(address(this)) < returnAmount) {
revert NotEnoughAssetToTransfer();
}
// interactions
IERC20(toAsset).transfer(msg.sender, returnAmount);
emit ETHSwappedForLST(ethAmountSent, toAsset, returnAmount);
}
/// @notice get return amount for swapping ETH to asset that is accepted by LRTDepositPool
/// @dev use LRTOracle to get price for toToken
/// @param toAsset Asset address to swap to
/// @param ethAmountToSend Eth amount to swap from
/// @return returnAmount Return amount of toAsset
function getSwapETHToAssetReturnAmount(
address toAsset,
uint256 ethAmountToSend
)
public
view
returns (uint256 returnAmount)
{
address lrtOracleAddress = lrtConfig.getContract(LRTConstants.LRT_ORACLE);
ILRTOracle lrtOracle = ILRTOracle(lrtOracleAddress);
uint256 ethPricePerUint = 1e18;
return ethPricePerUint * ethAmountToSend / lrtOracle.getAssetPrice(toAsset);
}
/// @notice update max node delegator count
/// @dev only callable by LRT admin
/// @param maxNodeDelegatorLimit_ Maximum count of node delegator
function updateMaxNodeDelegatorLimit(uint256 maxNodeDelegatorLimit_) external onlyLRTAdmin {
if (maxNodeDelegatorLimit_ < nodeDelegatorQueue.length) {
revert InvalidMaximumNodeDelegatorLimit();
}
maxNodeDelegatorLimit = maxNodeDelegatorLimit_;
emit MaxNodeDelegatorLimitUpdated(maxNodeDelegatorLimit);
}
/// @notice update min amount to deposit
/// @dev only callable by LRT admin
/// @param minAmountToDeposit_ Minimum amount to deposit
function setMinAmountToDeposit(uint256 minAmountToDeposit_) external onlyLRTAdmin {
minAmountToDeposit = minAmountToDeposit_;
emit MinAmountToDepositUpdated(minAmountToDeposit_);
}
/// @dev Triggers stopped state. Contract must not be paused.
function pause() external onlyLRTManager {
_pause();
}
/// @dev Returns to normal state. Contract must be paused
function unpause() external onlyLRTAdmin {
_unpause();
}
receive() external payable { }
}