-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMainnetController.sol
594 lines (493 loc) · 21.8 KB
/
MainnetController.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.21;
import { IAToken } from "aave-v3-origin/src/core/contracts/interfaces/IAToken.sol";
import { IPool as IAavePool } from "aave-v3-origin/src/core/contracts/interfaces/IPool.sol";
import { IERC20 } from "forge-std/interfaces/IERC20.sol";
import { IERC4626 } from "forge-std/interfaces/IERC4626.sol";
import { AccessControl } from "openzeppelin-contracts/contracts/access/AccessControl.sol";
import { Ethereum } from "spark-address-registry/src/Ethereum.sol";
import { IALMProxy } from "./interfaces/IALMProxy.sol";
import { ICCTPLike } from "./interfaces/CCTPInterfaces.sol";
import { IRateLimits } from "./interfaces/IRateLimits.sol";
import { RateLimitHelpers } from "./RateLimitHelpers.sol";
interface IDaiUsdsLike {
function dai() external view returns(address);
function daiToUsds(address usr, uint256 wad) external;
function usdsToDai(address usr, uint256 wad) external;
}
interface IEthenaMinterLike {
function setDelegatedSigner(address delegateSigner) external;
function removeDelegatedSigner(address delegateSigner) external;
}
interface ISUSDELike is IERC4626 {
function cooldownAssets(uint256 usdeAmount) external;
function cooldownShares(uint256 susdeAmount) external;
function unstake(address receiver) external;
}
interface IVaultLike {
function buffer() external view returns(address);
function draw(uint256 usdsAmount) external;
function wipe(uint256 usdsAmount) external;
}
interface IPSMLike {
function buyGemNoFee(address usr, uint256 usdcAmount) external returns (uint256 usdsAmount);
function fill() external returns (uint256 wad);
function gem() external view returns(address);
function sellGemNoFee(address usr, uint256 usdcAmount) external returns (uint256 usdsAmount);
function to18ConversionFactor() external view returns (uint256);
}
interface IATokenWithPool is IAToken {
function POOL() external view returns(address);
}
contract MainnetController is AccessControl {
/**********************************************************************************************/
/*** Events ***/
/**********************************************************************************************/
// NOTE: This is used to track individual transfers for offchain processing of CCTP transactions
event CCTPTransferInitiated(
uint64 indexed nonce,
uint32 indexed destinationDomain,
bytes32 indexed mintRecipient,
uint256 usdcAmount
);
event Frozen();
event MintRecipientSet(uint32 indexed destinationDomain, bytes32 mintRecipient);
event Reactivated();
/**********************************************************************************************/
/*** State variables ***/
/**********************************************************************************************/
bytes32 public constant FREEZER = keccak256("FREEZER");
bytes32 public constant RELAYER = keccak256("RELAYER");
bytes32 public constant LIMIT_4626_DEPOSIT = keccak256("LIMIT_4626_DEPOSIT");
bytes32 public constant LIMIT_4626_WITHDRAW = keccak256("LIMIT_4626_WITHDRAW");
bytes32 public constant LIMIT_AAVE_DEPOSIT = keccak256("LIMIT_AAVE_DEPOSIT");
bytes32 public constant LIMIT_AAVE_WITHDRAW = keccak256("LIMIT_AAVE_WITHDRAW");
bytes32 public constant LIMIT_SUSDE_COOLDOWN = keccak256("LIMIT_SUSDE_COOLDOWN");
bytes32 public constant LIMIT_USDC_TO_CCTP = keccak256("LIMIT_USDC_TO_CCTP");
bytes32 public constant LIMIT_USDC_TO_DOMAIN = keccak256("LIMIT_USDC_TO_DOMAIN");
bytes32 public constant LIMIT_USDE_BURN = keccak256("LIMIT_USDE_BURN");
bytes32 public constant LIMIT_USDE_MINT = keccak256("LIMIT_USDE_MINT");
bytes32 public constant LIMIT_USDS_MINT = keccak256("LIMIT_USDS_MINT");
bytes32 public constant LIMIT_USDS_TO_USDC = keccak256("LIMIT_USDS_TO_USDC");
address public immutable buffer;
IALMProxy public immutable proxy;
ICCTPLike public immutable cctp;
IDaiUsdsLike public immutable daiUsds;
IEthenaMinterLike public immutable ethenaMinter;
IPSMLike public immutable psm;
IRateLimits public immutable rateLimits;
IVaultLike public immutable vault;
IERC20 public immutable dai;
IERC20 public immutable usds;
IERC20 public immutable usde;
IERC20 public immutable usdc;
ISUSDELike public immutable susde;
uint256 public immutable psmTo18ConversionFactor;
bool public active;
mapping(uint32 destinationDomain => bytes32 mintRecipient) public mintRecipients;
/**********************************************************************************************/
/*** Initialization ***/
/**********************************************************************************************/
constructor(
address admin_,
address proxy_,
address rateLimits_,
address vault_,
address psm_,
address daiUsds_,
address cctp_
) {
_grantRole(DEFAULT_ADMIN_ROLE, admin_);
proxy = IALMProxy(proxy_);
rateLimits = IRateLimits(rateLimits_);
vault = IVaultLike(vault_);
buffer = IVaultLike(vault_).buffer();
psm = IPSMLike(psm_);
daiUsds = IDaiUsdsLike(daiUsds_);
cctp = ICCTPLike(cctp_);
ethenaMinter = IEthenaMinterLike(Ethereum.ETHENA_MINTER);
susde = ISUSDELike(Ethereum.SUSDE);
dai = IERC20(daiUsds.dai());
usdc = IERC20(psm.gem());
usds = IERC20(Ethereum.USDS);
usde = IERC20(Ethereum.USDE);
psmTo18ConversionFactor = psm.to18ConversionFactor();
active = true;
}
/**********************************************************************************************/
/*** Modifiers ***/
/**********************************************************************************************/
modifier isActive {
require(active, "MainnetController/not-active");
_;
}
modifier rateLimited(bytes32 key, uint256 amount) {
rateLimits.triggerRateLimitDecrease(key, amount);
_;
}
modifier cancelRateLimit(bytes32 key, uint256 amount) {
rateLimits.triggerRateLimitIncrease(key, amount);
_;
}
/**********************************************************************************************/
/*** Admin functions ***/
/**********************************************************************************************/
function setMintRecipient(uint32 destinationDomain, bytes32 mintRecipient)
external onlyRole(DEFAULT_ADMIN_ROLE)
{
mintRecipients[destinationDomain] = mintRecipient;
emit MintRecipientSet(destinationDomain, mintRecipient);
}
/**********************************************************************************************/
/*** Freezer functions ***/
/**********************************************************************************************/
function freeze() external onlyRole(FREEZER) {
active = false;
emit Frozen();
}
function reactivate() external onlyRole(DEFAULT_ADMIN_ROLE) {
active = true;
emit Reactivated();
}
/**********************************************************************************************/
/*** Relayer vault functions ***/
/**********************************************************************************************/
function mintUSDS(uint256 usdsAmount)
external onlyRole(RELAYER) isActive rateLimited(LIMIT_USDS_MINT, usdsAmount)
{
// Mint USDS into the buffer
proxy.doCall(
address(vault),
abi.encodeCall(vault.draw, (usdsAmount))
);
// Transfer USDS from the buffer to the proxy
proxy.doCall(
address(usds),
abi.encodeCall(usds.transferFrom, (buffer, address(proxy), usdsAmount))
);
}
function burnUSDS(uint256 usdsAmount)
external onlyRole(RELAYER) isActive cancelRateLimit(LIMIT_USDS_MINT, usdsAmount)
{
// Transfer USDS from the proxy to the buffer
proxy.doCall(
address(usds),
abi.encodeCall(usds.transfer, (buffer, usdsAmount))
);
// Burn USDS from the buffer
proxy.doCall(
address(vault),
abi.encodeCall(vault.wipe, (usdsAmount))
);
}
/**********************************************************************************************/
/*** Relayer ERC4626 functions ***/
/**********************************************************************************************/
function depositERC4626(address token, uint256 amount)
external
onlyRole(RELAYER)
isActive
rateLimited(
RateLimitHelpers.makeAssetKey(LIMIT_4626_DEPOSIT, token),
amount
)
returns (uint256 shares)
{
// Note that whitelist is done by rate limits
IERC20 asset = IERC20(IERC4626(token).asset());
// Approve asset to token from the proxy (assumes the proxy has enough of the asset).
proxy.doCall(
address(asset),
abi.encodeCall(asset.approve, (token, amount))
);
// Deposit asset into the token, proxy receives token shares, decode the resulting shares
shares = abi.decode(
proxy.doCall(
token,
abi.encodeCall(IERC4626(token).deposit, (amount, address(proxy)))
),
(uint256)
);
}
function withdrawERC4626(address token, uint256 amount)
external
onlyRole(RELAYER)
isActive
rateLimited(
RateLimitHelpers.makeAssetKey(LIMIT_4626_WITHDRAW, token),
amount
)
returns (uint256 shares)
{
// Withdraw asset from a token, decode resulting shares.
// Assumes proxy has adequate token shares.
shares = abi.decode(
proxy.doCall(
token,
abi.encodeCall(IERC4626(token).withdraw, (amount, address(proxy), address(proxy)))
),
(uint256)
);
}
// NOTE: !!! Rate limited at end of function !!!
function redeemERC4626(address token, uint256 shares)
external onlyRole(RELAYER) isActive returns (uint256 assets)
{
// Redeem shares for assets from the token, decode the resulting assets.
// Assumes proxy has adequate token shares.
assets = abi.decode(
proxy.doCall(
token,
abi.encodeCall(IERC4626(token).redeem, (shares, address(proxy), address(proxy)))
),
(uint256)
);
rateLimits.triggerRateLimitDecrease(
RateLimitHelpers.makeAssetKey(LIMIT_4626_WITHDRAW, token),
assets
);
}
/**********************************************************************************************/
/*** Relayer Aave functions ***/
/**********************************************************************************************/
function depositAave(address aToken, uint256 amount)
external
onlyRole(RELAYER)
isActive
rateLimited(
RateLimitHelpers.makeAssetKey(LIMIT_AAVE_DEPOSIT, aToken),
amount
)
{
IERC20 underlying = IERC20(IATokenWithPool(aToken).UNDERLYING_ASSET_ADDRESS());
IAavePool pool = IAavePool(IATokenWithPool(aToken).POOL());
// Approve underlying to Aave pool from the proxy (assumes the proxy has enough underlying).
proxy.doCall(
address(underlying),
abi.encodeCall(underlying.approve, (address(pool), amount))
);
// Deposit underlying into Aave pool, proxy receives aTokens
proxy.doCall(
address(pool),
abi.encodeCall(pool.supply, (address(underlying), amount, address(proxy), 0))
);
}
// NOTE: !!! Rate limited at end of function !!!
function withdrawAave(address aToken, uint256 amount)
external onlyRole(RELAYER) isActive returns (uint256 amountWithdrawn)
{
IAavePool pool = IAavePool(IATokenWithPool(aToken).POOL());
// Withdraw underlying from Aave pool, decode resulting amount withdrawn.
// Assumes proxy has adequate aTokens.
amountWithdrawn = abi.decode(
proxy.doCall(
address(pool),
abi.encodeCall(
pool.withdraw,
(IATokenWithPool(aToken).UNDERLYING_ASSET_ADDRESS(), amount, address(proxy))
)
),
(uint256)
);
rateLimits.triggerRateLimitDecrease(
RateLimitHelpers.makeAssetKey(LIMIT_AAVE_WITHDRAW, aToken),
amountWithdrawn
);
}
/**********************************************************************************************/
/*** Relayer Ethena functions ***/
/**********************************************************************************************/
function setDelegatedSigner(address delegatedSigner) external onlyRole(RELAYER) isActive {
proxy.doCall(
address(ethenaMinter),
abi.encodeCall(ethenaMinter.setDelegatedSigner, (address(delegatedSigner)))
);
}
function removeDelegatedSigner(address delegatedSigner) external onlyRole(RELAYER) isActive {
proxy.doCall(
address(ethenaMinter),
abi.encodeCall(ethenaMinter.removeDelegatedSigner, (address(delegatedSigner)))
);
}
// Note that Ethena's mint/redeem per-block limits include other users
function prepareUSDeMint(uint256 usdcAmount)
external onlyRole(RELAYER) isActive rateLimited(LIMIT_USDE_MINT, usdcAmount)
{
proxy.doCall(
address(usdc),
abi.encodeCall(usdc.approve, (address(ethenaMinter), usdcAmount))
);
}
function prepareUSDeBurn(uint256 usdeAmount)
external onlyRole(RELAYER) isActive rateLimited(LIMIT_USDE_BURN, usdeAmount)
{
proxy.doCall(
address(usde),
abi.encodeCall(usde.approve, (address(ethenaMinter), usdeAmount))
);
}
function cooldownAssetsSUSDe(uint256 usdeAmount)
external onlyRole(RELAYER) isActive rateLimited(LIMIT_SUSDE_COOLDOWN, usdeAmount)
{
proxy.doCall(
address(susde),
abi.encodeCall(susde.cooldownAssets, (usdeAmount))
);
}
// NOTE: !!! Rate limited at end of function !!!
function cooldownSharesSUSDe(uint256 susdeAmount)
external
onlyRole(RELAYER)
isActive
returns (uint256 cooldownAmount)
{
cooldownAmount = abi.decode(
proxy.doCall(
address(susde),
abi.encodeCall(susde.cooldownShares, (susdeAmount))
),
(uint256)
);
rateLimits.triggerRateLimitDecrease(LIMIT_SUSDE_COOLDOWN, cooldownAmount);
}
function unstakeSUSDe() external onlyRole(RELAYER) isActive {
proxy.doCall(
address(susde),
abi.encodeCall(susde.unstake, (address(proxy)))
);
}
/**********************************************************************************************/
/*** Relayer PSM functions ***/
/**********************************************************************************************/
// NOTE: The param `usdcAmount` is denominated in 1e6 precision to match how PSM uses
// USDC precision for both `buyGemNoFee` and `sellGemNoFee`
function swapUSDSToUSDC(uint256 usdcAmount)
external onlyRole(RELAYER) isActive rateLimited(LIMIT_USDS_TO_USDC, usdcAmount)
{
uint256 usdsAmount = usdcAmount * psmTo18ConversionFactor;
// Approve USDS to DaiUsds migrator from the proxy (assumes the proxy has enough USDS)
proxy.doCall(
address(usds),
abi.encodeCall(usds.approve, (address(daiUsds), usdsAmount))
);
// Swap USDS to DAI 1:1
proxy.doCall(
address(daiUsds),
abi.encodeCall(daiUsds.usdsToDai, (address(proxy), usdsAmount))
);
// Approve DAI to PSM from the proxy because conversion from USDS to DAI was 1:1
proxy.doCall(
address(dai),
abi.encodeCall(dai.approve, (address(psm), usdsAmount))
);
// Swap DAI to USDC through the PSM
proxy.doCall(
address(psm),
abi.encodeCall(psm.buyGemNoFee, (address(proxy), usdcAmount))
);
}
function swapUSDCToUSDS(uint256 usdcAmount)
external onlyRole(RELAYER) isActive cancelRateLimit(LIMIT_USDS_TO_USDC, usdcAmount)
{
// Approve USDC to PSM from the proxy (assumes the proxy has enough USDC)
proxy.doCall(
address(usdc),
abi.encodeCall(usdc.approve, (address(psm), usdcAmount))
);
// Max USDC that can be swapped to DAI in one call
uint256 limit = dai.balanceOf(address(psm)) / psmTo18ConversionFactor;
if (usdcAmount <= limit) {
_swapUSDCToDAI(usdcAmount);
} else {
uint256 remainingUsdcToSwap = usdcAmount;
// Refill the PSM with DAI as many times as needed to get to the full `usdcAmount`.
// If the PSM cannot be filled with the full amount, psm.fill() will revert
// with `DssLitePsm/nothing-to-fill` since rush() will return 0.
// This is desired behavior because this function should only succeed if the full
// `usdcAmount` can be swapped.
while (remainingUsdcToSwap > 0) {
psm.fill();
limit = dai.balanceOf(address(psm)) / psmTo18ConversionFactor;
uint256 swapAmount = remainingUsdcToSwap < limit ? remainingUsdcToSwap : limit;
_swapUSDCToDAI(swapAmount);
remainingUsdcToSwap -= swapAmount;
}
}
uint256 daiAmount = usdcAmount * psmTo18ConversionFactor;
// Approve DAI to DaiUsds migrator from the proxy (assumes the proxy has enough DAI)
proxy.doCall(
address(dai),
abi.encodeCall(dai.approve, (address(daiUsds), daiAmount))
);
// Swap DAI to USDS 1:1
proxy.doCall(
address(daiUsds),
abi.encodeCall(daiUsds.daiToUsds, (address(proxy), daiAmount))
);
}
/**********************************************************************************************/
/*** Relayer bridging functions ***/
/**********************************************************************************************/
function transferUSDCToCCTP(uint256 usdcAmount, uint32 destinationDomain)
external
onlyRole(RELAYER)
isActive
rateLimited(LIMIT_USDC_TO_CCTP, usdcAmount)
rateLimited(
RateLimitHelpers.makeDomainKey(LIMIT_USDC_TO_DOMAIN, destinationDomain),
usdcAmount
)
{
bytes32 mintRecipient = mintRecipients[destinationDomain];
require(mintRecipient != 0, "MainnetController/domain-not-configured");
// Approve USDC to CCTP from the proxy (assumes the proxy has enough USDC)
proxy.doCall(
address(usdc),
abi.encodeCall(usdc.approve, (address(cctp), usdcAmount))
);
// If amount is larger than limit it must be split into multiple calls
uint256 burnLimit = cctp.localMinter().burnLimitsPerMessage(address(usdc));
while (usdcAmount > burnLimit) {
_initiateCCTPTransfer(burnLimit, destinationDomain, mintRecipient);
usdcAmount -= burnLimit;
}
// Send remaining amount (if any)
if (usdcAmount > 0) {
_initiateCCTPTransfer(usdcAmount, destinationDomain, mintRecipient);
}
}
/**********************************************************************************************/
/*** Internal helper functions ***/
/**********************************************************************************************/
function _initiateCCTPTransfer(
uint256 usdcAmount,
uint32 destinationDomain,
bytes32 mintRecipient
)
internal
{
uint64 nonce = abi.decode(
proxy.doCall(
address(cctp),
abi.encodeCall(
cctp.depositForBurn,
(
usdcAmount,
destinationDomain,
mintRecipient,
address(usdc)
)
)
),
(uint64)
);
emit CCTPTransferInitiated(nonce, destinationDomain, mintRecipient, usdcAmount);
}
function _swapUSDCToDAI(uint256 usdcAmount) internal {
// Swap USDC to DAI through the PSM (1:1 since sellGemNoFee is used)
proxy.doCall(
address(psm),
abi.encodeCall(psm.sellGemNoFee, (address(proxy), usdcAmount))
);
}
}