-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathRouter.sol
294 lines (277 loc) · 13.8 KB
/
Router.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
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity =0.7.6;
pragma abicoder v2;
import "./actions/nTokenMintAction.sol";
import "../global/StorageLayoutV1.sol";
import "../global/Types.sol";
import {nTokenERC20} from "../../interfaces/notional/nTokenERC20.sol";
import "../../interfaces/notional/NotionalProxy.sol";
import {IVaultAction, IVaultAccountAction} from "../../interfaces/notional/IVaultController.sol";
import {nERC1155Interface} from "../../interfaces/notional/nERC1155Interface.sol";
import {NotionalGovernance} from "../../interfaces/notional/NotionalGovernance.sol";
import {NotionalCalculations} from "../../interfaces/notional/NotionalCalculations.sol";
/**
* @notice Sits behind an upgradeable proxy and routes methods to an appropriate implementation contract. All storage
* will sit inside the upgradeable proxy and this router will authorize the call and re-route the calls to implementing
* contracts.
*
* This pattern adds an additional hop between the proxy and the ultimate implementation contract, however, it also
* allows for atomic upgrades of the entire system. Individual implementation contracts will be deployed and then a
* new Router with the new hardcoded addresses will then be deployed and upgraded into place.
*/
contract Router is StorageLayoutV1 {
// These contract addresses cannot be changed once set by the constructor
address public immutable GOVERNANCE;
address public immutable VIEWS;
address public immutable INITIALIZE_MARKET;
address public immutable NTOKEN_ACTIONS;
address public immutable BATCH_ACTION;
address public immutable ACCOUNT_ACTION;
address public immutable ERC1155;
address public immutable LIQUIDATE_CURRENCY;
address public immutable LIQUIDATE_FCASH;
address public immutable cETH;
address public immutable TREASURY;
address public immutable CALCULATION_VIEWS;
address public immutable VAULT_ACCOUNT_ACTION;
address public immutable VAULT_ACTION;
address private immutable DEPLOYER;
struct DeployedContracts {
address governance;
address views;
address initializeMarket;
address nTokenActions;
address batchAction;
address accountAction;
address erc1155;
address liquidateCurrency;
address liquidatefCash;
address cETH;
address treasury;
address calculationViews;
address vaultAccountAction;
address vaultAction;
}
constructor(
DeployedContracts memory contracts
) {
GOVERNANCE = contracts.governance;
VIEWS = contracts.views;
INITIALIZE_MARKET = contracts.initializeMarket;
NTOKEN_ACTIONS = contracts.nTokenActions;
BATCH_ACTION = contracts.batchAction;
ACCOUNT_ACTION = contracts.accountAction;
ERC1155 = contracts.erc1155;
LIQUIDATE_CURRENCY = contracts.liquidateCurrency;
LIQUIDATE_FCASH = contracts.liquidatefCash;
cETH = contracts.cETH;
TREASURY = contracts.treasury;
CALCULATION_VIEWS = contracts.calculationViews;
VAULT_ACCOUNT_ACTION = contracts.vaultAccountAction;
VAULT_ACTION = contracts.vaultAction;
DEPLOYER = msg.sender;
// This will lock everyone from calling initialize on the implementation contract
hasInitialized = true;
}
function initialize(address owner_, address pauseRouter_, address pauseGuardian_) public {
// Check that only the deployer can initialize
require(msg.sender == DEPLOYER && !hasInitialized);
// Allow list currency to be called by this contract for the purposes of
// initializing ETH as a currency
owner = msg.sender;
// List ETH as currency id == 1, NOTE: return value is ignored here
// FIXME: on non-mainnet deployments we should be using WETH instead here...
(bool status, ) =
address(GOVERNANCE).delegatecall(
abi.encodeWithSelector(
NotionalGovernance.listCurrency.selector,
TokenStorage(cETH, false, TokenType.cETH, Constants.CETH_DECIMAL_PLACES, 0),
// No underlying set for cETH
TokenStorage(address(0), false, TokenType.Ether, Constants.ETH_DECIMAL_PLACES, 0),
address(0),
false,
133, // Initial settings of 133% buffer
75, // 75% haircut
108 // 8% liquidation discount
)
);
require(status);
owner = owner_;
// The pause guardian may downgrade the router to the pauseRouter
pauseRouter = pauseRouter_;
pauseGuardian = pauseGuardian_;
hasInitialized == true;
}
/// @notice Returns the implementation contract for the method signature
/// @param sig method signature to call
/// @return implementation address
function getRouterImplementation(bytes4 sig) public view returns (address) {
if (
sig == NotionalProxy.batchBalanceAction.selector ||
sig == NotionalProxy.batchBalanceAndTradeAction.selector ||
sig == NotionalProxy.batchBalanceAndTradeActionWithCallback.selector ||
sig == NotionalProxy.batchLend.selector
) {
return BATCH_ACTION;
} else if (
sig == IVaultAccountAction.enterVault.selector ||
sig == IVaultAccountAction.rollVaultPosition.selector ||
sig == IVaultAccountAction.exitVault.selector ||
sig == IVaultAccountAction.deleverageAccount.selector ||
sig == IVaultAccountAction.getVaultAccount.selector ||
sig == IVaultAccountAction.getVaultAccountDebtShares.selector ||
sig == IVaultAccountAction.getVaultAccountCollateralRatio.selector
) {
return VAULT_ACCOUNT_ACTION;
} else if (
sig == NotionalProxy.depositUnderlyingToken.selector ||
sig == NotionalProxy.depositAssetToken.selector ||
sig == NotionalProxy.withdraw.selector ||
sig == NotionalProxy.settleAccount.selector ||
sig == NotionalProxy.nTokenRedeem.selector ||
sig == NotionalProxy.enableBitmapCurrency.selector
) {
return ACCOUNT_ACTION;
} else if (
sig == nERC1155Interface.supportsInterface.selector ||
sig == nERC1155Interface.balanceOf.selector ||
sig == nERC1155Interface.balanceOfBatch.selector ||
sig == nERC1155Interface.signedBalanceOf.selector ||
sig == nERC1155Interface.signedBalanceOfBatch.selector ||
sig == nERC1155Interface.safeTransferFrom.selector ||
sig == nERC1155Interface.safeBatchTransferFrom.selector ||
sig == nERC1155Interface.decodeToAssets.selector ||
sig == nERC1155Interface.encodeToId.selector ||
sig == nERC1155Interface.setApprovalForAll.selector ||
sig == nERC1155Interface.isApprovedForAll.selector
) {
return ERC1155;
} else if (
sig == nTokenERC20.nTokenTotalSupply.selector ||
sig == nTokenERC20.nTokenTransferAllowance.selector ||
sig == nTokenERC20.nTokenBalanceOf.selector ||
sig == nTokenERC20.nTokenTransferApprove.selector ||
sig == nTokenERC20.nTokenTransfer.selector ||
sig == nTokenERC20.nTokenTransferFrom.selector ||
sig == nTokenERC20.nTokenTransferApproveAll.selector ||
sig == nTokenERC20.nTokenClaimIncentives.selector ||
sig == nTokenERC20.nTokenPresentValueAssetDenominated.selector ||
sig == nTokenERC20.nTokenPresentValueUnderlyingDenominated.selector
) {
return NTOKEN_ACTIONS;
} else if (
sig == NotionalProxy.liquidateLocalCurrency.selector ||
sig == NotionalProxy.liquidateCollateralCurrency.selector ||
sig == NotionalProxy.calculateLocalCurrencyLiquidation.selector ||
sig == NotionalProxy.calculateCollateralCurrencyLiquidation.selector
) {
return LIQUIDATE_CURRENCY;
} else if (
sig == NotionalProxy.liquidatefCashLocal.selector ||
sig == NotionalProxy.liquidatefCashCrossCurrency.selector ||
sig == NotionalProxy.calculatefCashLocalLiquidation.selector ||
sig == NotionalProxy.calculatefCashCrossCurrencyLiquidation.selector
) {
return LIQUIDATE_FCASH;
} else if (
sig == IVaultAction.updateVault.selector ||
sig == IVaultAction.setVaultPauseStatus.selector ||
sig == IVaultAction.setVaultDeleverageStatus.selector ||
sig == IVaultAction.setMaxBorrowCapacity.selector ||
sig == IVaultAction.reduceMaxBorrowCapacity.selector ||
sig == IVaultAction.updateSecondaryBorrowCapacity.selector ||
sig == IVaultAction.depositVaultCashToStrategyTokens.selector ||
sig == IVaultAction.redeemStrategyTokensToCash.selector ||
sig == IVaultAction.borrowSecondaryCurrencyToVault.selector ||
sig == IVaultAction.repaySecondaryCurrencyFromVault.selector ||
sig == IVaultAction.initiateSecondaryBorrowSettlement.selector ||
sig == IVaultAction.settleVault.selector ||
sig == IVaultAction.getVaultConfig.selector ||
sig == IVaultAction.getBorrowCapacity.selector ||
sig == IVaultAction.getSecondaryBorrow.selector ||
sig == IVaultAction.getVaultState.selector ||
sig == IVaultAction.getCashRequiredToSettle.selector
) {
return VAULT_ACTION;
} else if (
sig == NotionalProxy.initializeMarkets.selector ||
sig == NotionalProxy.sweepCashIntoMarkets.selector
) {
return INITIALIZE_MARKET;
} else if (
sig == NotionalGovernance.listCurrency.selector ||
sig == NotionalGovernance.enableCashGroup.selector ||
sig == NotionalGovernance.updateCashGroup.selector ||
sig == NotionalGovernance.updateAssetRate.selector ||
sig == NotionalGovernance.updateETHRate.selector ||
sig == NotionalGovernance.transferOwnership.selector ||
sig == NotionalGovernance.claimOwnership.selector ||
sig == NotionalGovernance.updateIncentiveEmissionRate.selector ||
sig == NotionalGovernance.updateMaxCollateralBalance.selector ||
sig == NotionalGovernance.updateDepositParameters.selector ||
sig == NotionalGovernance.updateInitializationParameters.selector ||
sig == NotionalGovernance.updateTokenCollateralParameters.selector ||
sig == NotionalGovernance.updateGlobalTransferOperator.selector ||
sig == NotionalGovernance.updateAuthorizedCallbackContract.selector ||
sig == NotionalGovernance.setLendingPool.selector ||
sig == NotionalProxy.upgradeTo.selector ||
sig == NotionalProxy.upgradeToAndCall.selector
) {
return GOVERNANCE;
} else if (
sig == NotionalTreasury.claimCOMPAndTransfer.selector ||
sig == NotionalTreasury.transferReserveToTreasury.selector ||
sig == NotionalTreasury.setTreasuryManager.selector ||
sig == NotionalTreasury.setReserveBuffer.selector ||
sig == NotionalTreasury.setReserveCashBalance.selector
) {
return TREASURY;
} else if (
sig == NotionalCalculations.calculateNTokensToMint.selector ||
sig == NotionalCalculations.getfCashAmountGivenCashAmount.selector ||
sig == NotionalCalculations.getCashAmountGivenfCashAmount.selector ||
sig == NotionalCalculations.nTokenGetClaimableIncentives.selector ||
sig == NotionalCalculations.getPresentfCashValue.selector ||
sig == NotionalCalculations.getMarketIndex.selector ||
sig == NotionalCalculations.getfCashLendFromDeposit.selector ||
sig == NotionalCalculations.getfCashBorrowFromPrincipal.selector ||
sig == NotionalCalculations.getDepositFromfCashLend.selector ||
sig == NotionalCalculations.getPrincipalFromfCashBorrow.selector ||
sig == NotionalCalculations.convertCashBalanceToExternal.selector
) {
return CALCULATION_VIEWS;
} else {
// If not found then delegate to views. This will revert if there is no method on
// the view contract
return VIEWS;
}
}
/// @dev Delegates the current call to `implementation`.
/// This function does not return to its internal call site, it will return directly to the external caller.
function _delegate(address implementation) private {
// solhint-disable-next-line no-inline-assembly
assembly {
// Copy msg.data. We take full control of memory in this inline assembly
// block because it will not return to Solidity code. We overwrite the
// Solidity scratch pad at memory position 0.
calldatacopy(0, 0, calldatasize())
// Call the implementation.
// out and outsize are 0 because we don't know the size yet.
let result := delegatecall(gas(), implementation, 0, calldatasize(), 0, 0)
// Copy the returned data.
returndatacopy(0, 0, returndatasize())
switch result
// delegatecall returns 0 on error.
case 0 {
revert(0, returndatasize())
}
default {
return(0, returndatasize())
}
}
}
fallback() external payable {
_delegate(getRouterImplementation(msg.sig));
}
// NOTE: receive() is overridden in "nProxy" to allow for eth transfers to succeed
}