-
Notifications
You must be signed in to change notification settings - Fork 6
/
NodeDelegator.sol
135 lines (115 loc) · 5.31 KB
/
NodeDelegator.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
// SPDX-License-Identifier: GPL-3.0-or-later
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 { INodeDelegator } from "./interfaces/INodeDelegator.sol";
import { IStrategy } from "./interfaces/IStrategy.sol";
import { IEigenStrategyManager } from "./interfaces/IEigenStrategyManager.sol";
import { IERC20 } from "@openzeppelin/contracts/interfaces/IERC20.sol";
import { PausableUpgradeable } from "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";
import { ReentrancyGuardUpgradeable } from "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol";
/// @title NodeDelegator Contract
/// @notice The contract that handles the depositing of assets into strategies
contract NodeDelegator is INodeDelegator, LRTConfigRoleChecker, PausableUpgradeable, ReentrancyGuardUpgradeable {
/// @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();
lrtConfig = ILRTConfig(lrtConfigAddr);
emit UpdatedLRTConfig(lrtConfigAddr);
}
/// @notice Approves the maximum amount of an asset to the eigen strategy manager
/// @dev only supported assets can be deposited and only called by the LRT manager
/// @param asset the asset to deposit
function maxApproveToEigenStrategyManager(address asset)
external
override
onlySupportedAsset(asset)
onlyLRTManager
{
address eigenlayerStrategyManagerAddress = lrtConfig.getContract(LRTConstants.EIGEN_STRATEGY_MANAGER);
IERC20(asset).approve(eigenlayerStrategyManagerAddress, type(uint256).max);
}
/// @notice Deposits an asset lying in this NDC into its strategy
/// @dev only supported assets can be deposited and only called by the LRT manager
/// @param asset the asset to deposit
function depositAssetIntoStrategy(address asset)
external
override
whenNotPaused
nonReentrant
onlySupportedAsset(asset)
onlyLRTManager
{
address strategy = lrtConfig.assetStrategy(asset);
IERC20 token = IERC20(asset);
address eigenlayerStrategyManagerAddress = lrtConfig.getContract(LRTConstants.EIGEN_STRATEGY_MANAGER);
uint256 balance = token.balanceOf(address(this));
emit AssetDepositIntoStrategy(asset, strategy, balance);
IEigenStrategyManager(eigenlayerStrategyManagerAddress).depositIntoStrategy(IStrategy(strategy), token, balance);
}
/// @notice Transfers an asset back to the LRT deposit pool
/// @dev only supported assets can be transferred and only called by the LRT manager
/// @param asset the asset to transfer
/// @param amount the amount to transfer
function transferBackToLRTDepositPool(
address asset,
uint256 amount
)
external
whenNotPaused
nonReentrant
onlySupportedAsset(asset)
onlyLRTManager
{
address lrtDepositPool = lrtConfig.getContract(LRTConstants.LRT_DEPOSIT_POOL);
if (!IERC20(asset).transfer(lrtDepositPool, amount)) {
revert TokenTransferFailed();
}
}
/// @notice Fetches balance of all assets staked in eigen layer through this contract
/// @return assets the assets that the node delegator has deposited into strategies
/// @return assetBalances the balances of the assets that the node delegator has deposited into strategies
function getAssetBalances()
external
view
override
returns (address[] memory assets, uint256[] memory assetBalances)
{
address eigenlayerStrategyManagerAddress = lrtConfig.getContract(LRTConstants.EIGEN_STRATEGY_MANAGER);
(IStrategy[] memory strategies,) =
IEigenStrategyManager(eigenlayerStrategyManagerAddress).getDeposits(address(this));
uint256 strategiesLength = strategies.length;
assets = new address[](strategiesLength);
assetBalances = new uint256[](strategiesLength);
for (uint256 i = 0; i < strategiesLength;) {
assets[i] = address(IStrategy(strategies[i]).underlyingToken());
assetBalances[i] = IStrategy(strategies[i]).userUnderlyingView(address(this));
unchecked {
++i;
}
}
}
/// @dev Returns the balance of an asset that the node delegator has deposited into the strategy
/// @param asset the asset to get the balance of
/// @return stakedBalance the balance of the asset
function getAssetBalance(address asset) external view override returns (uint256) {
address strategy = lrtConfig.assetStrategy(asset);
return IStrategy(strategy).userUnderlyingView(address(this));
}
/// @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();
}
}