This repository has been archived by the owner on Mar 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SystemRegistry.sol
443 lines (344 loc) · 16.4 KB
/
SystemRegistry.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
438
439
440
441
442
443
// SPDX-License-Identifier: UNLICENSED
// Copyright (c) 2023 Tokemak Foundation. All rights reserved.
pragma solidity 0.8.17;
import { Errors } from "src/utils/Errors.sol";
import { IWETH9 } from "src/interfaces/utils/IWETH9.sol";
import { Ownable2Step } from "./access/Ownable2Step.sol";
import { IGPToke } from "src/interfaces/staking/IGPToke.sol";
import { ISystemRegistry } from "src/interfaces/ISystemRegistry.sol";
import { ISwapRouter } from "src/interfaces/swapper/ISwapRouter.sol";
import { ICurveResolver } from "src/interfaces/utils/ICurveResolver.sol";
import { ILMPVaultRouter } from "src/interfaces/vault/ILMPVaultRouter.sol";
import { ILMPVaultFactory } from "src/interfaces/vault/ILMPVaultFactory.sol";
import { ISystemSecurity } from "src/interfaces/security/ISystemSecurity.sol";
import { ILMPVaultRegistry } from "src/interfaces/vault/ILMPVaultRegistry.sol";
import { IRootPriceOracle } from "src/interfaces/oracles/IRootPriceOracle.sol";
import { IAccessController } from "src/interfaces/security/IAccessController.sol";
import { EnumerableSet } from "openzeppelin-contracts/utils/structs/EnumerableSet.sol";
import { IDestinationRegistry } from "src/interfaces/destinations/IDestinationRegistry.sol";
import { IStatsCalculatorRegistry } from "src/interfaces/stats/IStatsCalculatorRegistry.sol";
import { IAsyncSwapperRegistry } from "src/interfaces/liquidation/IAsyncSwapperRegistry.sol";
import { IDestinationVaultRegistry } from "src/interfaces/vault/IDestinationVaultRegistry.sol";
import { IERC20Metadata } from "openzeppelin-contracts/token/ERC20/extensions/IERC20Metadata.sol";
/// @notice Root contract of the system instance.
/// @dev All contracts in this instance of the system should be reachable from this contract
contract SystemRegistry is ISystemRegistry, Ownable2Step {
using EnumerableSet for EnumerableSet.Bytes32Set;
using EnumerableSet for EnumerableSet.AddressSet;
/* ******************************** */
/* State Variables */
/* ******************************** */
IERC20Metadata public immutable toke;
IWETH9 public immutable weth;
IGPToke private _gpToke;
ILMPVaultRegistry private _lmpVaultRegistry;
IDestinationVaultRegistry private _destinationVaultRegistry;
IAccessController private _accessController;
IDestinationRegistry private _destinationTemplateRegistry;
ILMPVaultRouter private _lmpVaultRouter;
IRootPriceOracle private _rootPriceOracle;
IAsyncSwapperRegistry private _asyncSwapperRegistry;
ISwapRouter private _swapRouter;
ICurveResolver private _curveResolver;
ISystemSecurity private _systemSecurity;
mapping(bytes32 => ILMPVaultFactory) private _lmpVaultFactoryByType;
EnumerableSet.Bytes32Set private _lmpVaultFactoryTypes;
IStatsCalculatorRegistry private _statsCalculatorRegistry;
EnumerableSet.AddressSet private _rewardTokens;
/* ******************************** */
/* Events */
/* ******************************** */
event GPTokeSet(address newAddress);
event LMPVaultRegistrySet(address newAddress);
event DestinationVaultRegistrySet(address newAddress);
event AccessControllerSet(address newAddress);
event DestinationTemplateRegistrySet(address newAddress);
event LMPVaultRouterSet(address newAddress);
event LMPVaultFactorySet(bytes32 vaultType, address factoryAddress);
event LMPVaultFactoryRemoved(bytes32 vaultType, address factoryAddress);
event StatsCalculatorRegistrySet(address newAddress);
event RootPriceOracleSet(address rootPriceOracle);
event AsyncSwapperRegistrySet(address newAddress);
event SwapRouterSet(address swapRouter);
event CurveResolverSet(address curveResolver);
event RewardTokenAdded(address rewardToken);
event RewardTokenRemoved(address rewardToken);
event SystemSecuritySet(address security);
/* ******************************** */
/* Errors */
/* ******************************** */
error InvalidContract(address addr);
error DuplicateSet(address addr);
/* ******************************** */
/* Constructor */
/* ******************************** */
constructor(address _toke, address _weth) {
Errors.verifyNotZero(address(_toke), "_toke");
Errors.verifyNotZero(address(_weth), "_weth");
toke = IERC20Metadata(_toke);
weth = IWETH9(_weth);
}
/* ******************************** */
/* Views */
/* ******************************** */
/// @inheritdoc ISystemRegistry
function gpToke() external view returns (IGPToke) {
return _gpToke;
}
/// @inheritdoc ISystemRegistry
function lmpVaultRegistry() external view returns (ILMPVaultRegistry) {
return _lmpVaultRegistry;
}
/// @inheritdoc ISystemRegistry
function destinationVaultRegistry() external view returns (IDestinationVaultRegistry) {
return _destinationVaultRegistry;
}
/// @inheritdoc ISystemRegistry
function accessController() external view returns (IAccessController) {
return _accessController;
}
/// @inheritdoc ISystemRegistry
function destinationTemplateRegistry() external view returns (IDestinationRegistry) {
return _destinationTemplateRegistry;
}
/// @inheritdoc ISystemRegistry
function lmpVaultRouter() external view returns (ILMPVaultRouter router) {
return _lmpVaultRouter;
}
/// @inheritdoc ISystemRegistry
function getLMPVaultFactoryByType(bytes32 vaultType) external view returns (ILMPVaultFactory vaultFactory) {
if (!_lmpVaultFactoryTypes.contains(vaultType)) {
revert Errors.ItemNotFound();
}
return _lmpVaultFactoryByType[vaultType];
}
/// @inheritdoc ISystemRegistry
function statsCalculatorRegistry() external view returns (IStatsCalculatorRegistry) {
return _statsCalculatorRegistry;
}
/// @inheritdoc ISystemRegistry
function rootPriceOracle() external view returns (IRootPriceOracle) {
return _rootPriceOracle;
}
/// @inheritdoc ISystemRegistry
function asyncSwapperRegistry() external view returns (IAsyncSwapperRegistry) {
return _asyncSwapperRegistry;
}
/// @inheritdoc ISystemRegistry
function swapRouter() external view returns (ISwapRouter) {
return _swapRouter;
}
/// @inheritdoc ISystemRegistry
function curveResolver() external view returns (ICurveResolver) {
return _curveResolver;
}
/// @inheritdoc ISystemRegistry
function systemSecurity() external view returns (ISystemSecurity) {
return _systemSecurity;
}
/* ******************************** */
/* Function */
/* ******************************** */
/// @notice Set the GPToke for this instance of the system
/// @dev Should only be able to set this value one time
/// @param newGPToke Address of the gpToke contract
function setGPToke(address newGPToke) external onlyOwner {
Errors.verifyNotZero(newGPToke, "newGPToke");
if (address(_gpToke) != address(0)) {
revert Errors.AlreadySet("gpToke");
}
_gpToke = IGPToke(newGPToke);
emit GPTokeSet(newGPToke);
_verifySystemsAgree(address(newGPToke));
}
/// @notice Set the LMP Vault Registry for this instance of the system
/// @dev Should only be able to set this value one time
/// @param registry Address of the registry
function setLMPVaultRegistry(address registry) external onlyOwner {
Errors.verifyNotZero(registry, "lmpVaultRegistry");
if (address(_lmpVaultRegistry) != address(0)) {
revert Errors.AlreadySet("lmpVaultRegistry");
}
emit LMPVaultRegistrySet(registry);
_lmpVaultRegistry = ILMPVaultRegistry(registry);
_verifySystemsAgree(registry);
}
/// @notice Set the LMP Vault Router for this instance of the system
/// @dev allows setting multiple times
/// @param router Address of the LMP Vault Router
function setLMPVaultRouter(address router) external onlyOwner {
Errors.verifyNotZero(router, "lmpVaultRouter");
_lmpVaultRouter = ILMPVaultRouter(router);
emit LMPVaultRouterSet(router);
}
/// @notice Set the Destination Vault Registry for this instance of the system
/// @dev Should only be able to set this value one time
/// @param registry Address of the registry
function setDestinationVaultRegistry(address registry) external onlyOwner {
Errors.verifyNotZero(registry, "destinationVaultRegistry");
if (address(_destinationVaultRegistry) != address(0)) {
revert Errors.AlreadySet("destinationVaultRegistry");
}
emit DestinationVaultRegistrySet(registry);
_destinationVaultRegistry = IDestinationVaultRegistry(registry);
_verifySystemsAgree(registry);
}
/// @notice Set the Access Controller for this instance of the system
/// @dev Should only be able to set this value one time
/// @param controller Address of the access controller
function setAccessController(address controller) external onlyOwner {
Errors.verifyNotZero(controller, "accessController");
if (address(_accessController) != address(0)) {
revert Errors.AlreadySet("accessController");
}
emit AccessControllerSet(controller);
_accessController = IAccessController(controller);
_verifySystemsAgree(controller);
}
/// @notice Set the Destination Template Registry for this instance of the system
/// @dev Should only be able to set this value one time
/// @param registry Address of the registry
function setDestinationTemplateRegistry(address registry) external onlyOwner {
Errors.verifyNotZero(registry, "destinationTemplateRegistry");
if (address(_destinationTemplateRegistry) != address(0)) {
revert Errors.AlreadySet("destinationTemplateRegistry");
}
emit DestinationTemplateRegistrySet(registry);
_destinationTemplateRegistry = IDestinationRegistry(registry);
_verifySystemsAgree(registry);
}
/// @notice Set the Stats Calculator Registry for this instance of the system
/// @dev Should only be able to set this value one time
/// @param registry Address of the registry
function setStatsCalculatorRegistry(address registry) external onlyOwner {
Errors.verifyNotZero(registry, "statsCalculatorRegistry");
if (address(_statsCalculatorRegistry) != address(0)) {
revert Errors.AlreadySet("statsCalculatorRegistry");
}
emit StatsCalculatorRegistrySet(registry);
_statsCalculatorRegistry = IStatsCalculatorRegistry(registry);
_verifySystemsAgree(registry);
}
/// @notice Set the Root Price Oracle for this instance of the system
/// @dev This value can be set multiple times, but never back to 0
/// @param oracle Address of the oracle
function setRootPriceOracle(address oracle) external onlyOwner {
Errors.verifyNotZero(oracle, "oracle");
if (oracle == address(_rootPriceOracle)) {
revert DuplicateSet(oracle);
}
emit RootPriceOracleSet(oracle);
_rootPriceOracle = IRootPriceOracle(oracle);
_verifySystemsAgree(oracle);
}
/// @notice Set the Async Swapper Registry for this instance of the system
/// @dev Should only be able to set this value one time
/// @param registry Address of the registry
function setAsyncSwapperRegistry(address registry) external onlyOwner {
Errors.verifyNotZero(registry, "asyncSwapperRegistry");
if (address(_asyncSwapperRegistry) != address(0)) {
revert Errors.AlreadySet("asyncSwapperRegistry");
}
emit AsyncSwapperRegistrySet(registry);
_asyncSwapperRegistry = IAsyncSwapperRegistry(registry);
_verifySystemsAgree(address(_asyncSwapperRegistry));
}
/// @notice Set the Swap Router for this instance of the system
/// @dev This value can be set multiple times, but never back to 0
/// @param router Address of the router
function setSwapRouter(address router) external onlyOwner {
Errors.verifyNotZero(router, "router");
if (router == address(_swapRouter)) {
revert DuplicateSet(router);
}
emit SwapRouterSet(router);
_swapRouter = ISwapRouter(router);
_verifySystemsAgree(router);
}
/// @notice Set the Curve Resolver for this instance of the system
/// @dev This value can be set multiple times, but never back to 0
/// @param resolver Address of the resolver
function setCurveResolver(address resolver) external onlyOwner {
Errors.verifyNotZero(resolver, "resolver");
if (resolver == address(_curveResolver)) {
revert DuplicateSet(resolver);
}
emit CurveResolverSet(resolver);
_curveResolver = ICurveResolver(resolver);
// Has no other dependencies in the system so no call
// to verifySystemsAgree
}
/// @inheritdoc ISystemRegistry
function addRewardToken(address rewardToken) external onlyOwner {
Errors.verifyNotZero(rewardToken, "rewardToken");
bool success = _rewardTokens.add(rewardToken);
if (!success) {
revert Errors.ItemExists();
}
emit RewardTokenAdded(rewardToken);
}
/// @inheritdoc ISystemRegistry
function removeRewardToken(address rewardToken) external onlyOwner {
Errors.verifyNotZero(rewardToken, "rewardToken");
bool success = _rewardTokens.remove(rewardToken);
if (!success) {
revert Errors.ItemNotFound();
}
emit RewardTokenRemoved(rewardToken);
}
/// @inheritdoc ISystemRegistry
function isRewardToken(address rewardToken) external view returns (bool) {
return _rewardTokens.contains(rewardToken);
}
/* ******************************** */
/* LMP Vault Factories */
/* ******************************** */
function setLMPVaultFactory(bytes32 vaultType, address factoryAddress) external onlyOwner {
Errors.verifyNotZero(factoryAddress, "factoryAddress");
Errors.verifyNotZero(vaultType, "vaultType");
// set the factory (note: slither exception due to us hard setting it regardless / no diff use case)
// slither-disable-next-line unused-return
_lmpVaultFactoryTypes.add(vaultType);
_lmpVaultFactoryByType[vaultType] = ILMPVaultFactory(factoryAddress);
emit LMPVaultFactorySet(vaultType, factoryAddress);
}
function removeLMPVaultFactory(bytes32 vaultType) external onlyOwner {
Errors.verifyNotZero(vaultType, "vaultType");
address factoryAddress = address(_lmpVaultFactoryByType[vaultType]);
// if returned false when trying to remove, means item wasn't in the list
if (!_lmpVaultFactoryTypes.remove(vaultType)) {
revert Errors.ItemNotFound();
}
delete _lmpVaultFactoryByType[vaultType];
emit LMPVaultFactoryRemoved(vaultType, factoryAddress);
}
/// @notice Set the System Security instance for this system
/// @dev Should only be able to set this value one time
/// @param security Address of the security contract
function setSystemSecurity(address security) external onlyOwner {
Errors.verifyNotZero(security, "security");
if (address(_systemSecurity) != address(0)) {
revert Errors.AlreadySet("security");
}
emit SystemSecuritySet(security);
_systemSecurity = ISystemSecurity(security);
_verifySystemsAgree(security);
}
/// @notice Verifies that a system bound contract matches this contract
/// @dev All system bound contracts must match a registry contract. Will revert on mismatch
/// @param dep The contract to check
function _verifySystemsAgree(address dep) internal view {
// slither-disable-start low-level-calls
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytes memory data) = dep.staticcall(abi.encodeWithSignature("getSystemRegistry()"));
// slither-disable-end low-level-calls
if (success) {
address depRegistry = abi.decode(data, (address));
if (depRegistry != address(this)) {
revert Errors.SystemMismatch(address(this), depRegistry);
}
} else {
revert InvalidContract(dep);
}
}
}