This repository has been archived by the owner on Jan 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfiguratorLogic.sol
215 lines (187 loc) · 7.16 KB
/
ConfiguratorLogic.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.10;
import {IPool} from '../../../interfaces/IPool.sol';
import {IInitializableAToken} from '../../../interfaces/IInitializableAToken.sol';
import {IInitializableDebtToken} from '../../../interfaces/IInitializableDebtToken.sol';
import {InitializableImmutableAdminUpgradeabilityProxy} from '../../../misc/aave-upgradeability/InitializableImmutableAdminUpgradeabilityProxy.sol';
import {IReserveInterestRateStrategy} from '../../../interfaces/IReserveInterestRateStrategy.sol';
import {ReserveConfiguration} from '../configuration/ReserveConfiguration.sol';
import {DataTypes} from '../types/DataTypes.sol';
import {Errors} from '../helpers/Errors.sol';
import {ConfiguratorInputTypes} from '../types/ConfiguratorInputTypes.sol';
import {IERC20Detailed} from '../../../dependencies/openzeppelin/contracts/IERC20Detailed.sol';
/**
* @title ConfiguratorLogic library
* @author Aave
* @notice Implements the functions to initialize reserves and update aTokens and debtTokens
*/
library ConfiguratorLogic {
using ReserveConfiguration for DataTypes.ReserveConfigurationMap;
// See `IPoolConfigurator` for descriptions
event ReserveInitialized(
address indexed asset,
address indexed aToken,
address stableDebtToken,
address variableDebtToken,
address interestRateStrategyAddress
);
event ATokenUpgraded(
address indexed asset,
address indexed proxy,
address indexed implementation
);
event VariableDebtTokenUpgraded(
address indexed asset,
address indexed proxy,
address indexed implementation
);
/**
* @notice Initialize a reserve by creating and initializing aToken and variable debt token
* @dev Emits the `ReserveInitialized` event
* @param pool The Pool in which the reserve will be initialized
* @param input The needed parameters for the initialization
*/
function executeInitReserve(
IPool pool,
ConfiguratorInputTypes.InitReserveInput calldata input
) external {
// It is an assumption that the asset listed is non-malicious, and the external call doesn't create re-entrancies
uint8 underlyingAssetDecimals = IERC20Detailed(input.underlyingAsset).decimals();
require(underlyingAssetDecimals > 5, Errors.INVALID_DECIMALS);
address aTokenProxyAddress = _initTokenWithProxy(
input.aTokenImpl,
abi.encodeWithSelector(
IInitializableAToken.initialize.selector,
pool,
input.treasury,
input.underlyingAsset,
input.incentivesController,
underlyingAssetDecimals,
input.aTokenName,
input.aTokenSymbol,
input.params
)
);
address variableDebtTokenProxyAddress = _initTokenWithProxy(
input.variableDebtTokenImpl,
abi.encodeWithSelector(
IInitializableDebtToken.initialize.selector,
pool,
input.underlyingAsset,
input.incentivesController,
underlyingAssetDecimals,
input.variableDebtTokenName,
input.variableDebtTokenSymbol,
input.params
)
);
pool.initReserve(
input.underlyingAsset,
aTokenProxyAddress,
variableDebtTokenProxyAddress,
input.interestRateStrategyAddress
);
DataTypes.ReserveConfigurationMap memory currentConfig = DataTypes.ReserveConfigurationMap(0);
currentConfig.setDecimals(underlyingAssetDecimals);
currentConfig.setActive(true);
currentConfig.setPaused(false);
currentConfig.setFrozen(false);
currentConfig.setVirtualAccActive(input.useVirtualBalance);
pool.setConfiguration(input.underlyingAsset, currentConfig);
IReserveInterestRateStrategy(input.interestRateStrategyAddress).setInterestRateParams(
input.underlyingAsset,
input.interestRateData
);
emit ReserveInitialized(
input.underlyingAsset,
aTokenProxyAddress,
address(0),
variableDebtTokenProxyAddress,
input.interestRateStrategyAddress
);
}
/**
* @notice Updates the aToken implementation and initializes it
* @dev Emits the `ATokenUpgraded` event
* @param cachedPool The Pool containing the reserve with the aToken
* @param input The parameters needed for the initialize call
*/
function executeUpdateAToken(
IPool cachedPool,
ConfiguratorInputTypes.UpdateATokenInput calldata input
) external {
address aTokenAddress = cachedPool.getReserveAToken(input.asset);
uint256 decimals = cachedPool.getConfiguration(input.asset).getDecimals();
bytes memory encodedCall = abi.encodeWithSelector(
IInitializableAToken.initialize.selector,
cachedPool,
input.treasury,
input.asset,
input.incentivesController,
decimals,
input.name,
input.symbol,
input.params
);
_upgradeTokenImplementation(aTokenAddress, input.implementation, encodedCall);
emit ATokenUpgraded(input.asset, aTokenAddress, input.implementation);
}
/**
* @notice Updates the variable debt token implementation and initializes it
* @dev Emits the `VariableDebtTokenUpgraded` event
* @param cachedPool The Pool containing the reserve with the variable debt token
* @param input The parameters needed for the initialize call
*/
function executeUpdateVariableDebtToken(
IPool cachedPool,
ConfiguratorInputTypes.UpdateDebtTokenInput calldata input
) external {
address variableDebtTokenAddress = cachedPool.getReserveVariableDebtToken(input.asset);
uint256 decimals = cachedPool.getConfiguration(input.asset).getDecimals();
bytes memory encodedCall = abi.encodeWithSelector(
IInitializableDebtToken.initialize.selector,
cachedPool,
input.asset,
input.incentivesController,
decimals,
input.name,
input.symbol,
input.params
);
_upgradeTokenImplementation(variableDebtTokenAddress, input.implementation, encodedCall);
emit VariableDebtTokenUpgraded(input.asset, variableDebtTokenAddress, input.implementation);
}
/**
* @notice Creates a new proxy and initializes the implementation
* @param implementation The address of the implementation
* @param initParams The parameters that is passed to the implementation to initialize
* @return The address of initialized proxy
*/
function _initTokenWithProxy(
address implementation,
bytes memory initParams
) internal returns (address) {
InitializableImmutableAdminUpgradeabilityProxy proxy = new InitializableImmutableAdminUpgradeabilityProxy(
address(this)
);
proxy.initialize(implementation, initParams);
return address(proxy);
}
/**
* @notice Upgrades the implementation and makes call to the proxy
* @dev The call is used to initialize the new implementation.
* @param proxyAddress The address of the proxy
* @param implementation The address of the new implementation
* @param initParams The parameters to the call after the upgrade
*/
function _upgradeTokenImplementation(
address proxyAddress,
address implementation,
bytes memory initParams
) internal {
InitializableImmutableAdminUpgradeabilityProxy proxy = InitializableImmutableAdminUpgradeabilityProxy(
payable(proxyAddress)
);
proxy.upgradeToAndCall(implementation, initParams);
}
}