@@ -22,6 +22,7 @@ import { ApproveLib } from "./libraries/ApproveLib.sol";
2222import { AaveLib } from "./libraries/AaveLib.sol";
2323import { CCTPLib } from "./libraries/CCTPLib.sol";
2424import { CurveLib } from "./libraries/CurveLib.sol";
25+ import { ERC4626Lib } from "./libraries/ERC4626Lib.sol";
2526import { IDaiUsdsLike, IPSMLike, PSMLib } from "./libraries/PSMLib.sol";
2627
2728import { OptionsBuilder } from "layerzerolabs/oapp-evm/contracts/oapp/libs/OptionsBuilder.sol";
@@ -133,8 +134,6 @@ contract MainnetController is ReentrancyGuard, AccessControlEnumerable {
133134 /*** State variables ***/
134135 /**********************************************************************************************/
135136
136- uint256 public constant EXCHANGE_RATE_PRECISION = 1e36;
137-
138137 bytes32 public FREEZER = keccak256("FREEZER");
139138 bytes32 public RELAYER = keccak256("RELAYER");
140139
@@ -304,7 +303,7 @@ contract MainnetController is ReentrancyGuard, AccessControlEnumerable {
304303
305304 emit MaxExchangeRateSet(
306305 token,
307- maxExchangeRates[token] = _getExchangeRate (shares, maxExpectedAssets)
306+ maxExchangeRates[token] = ERC4626Lib.getExchangeRate (shares, maxExpectedAssets)
308307 );
309308 }
310309
@@ -457,67 +456,49 @@ contract MainnetController is ReentrancyGuard, AccessControlEnumerable {
457456 external nonReentrant returns (uint256 shares)
458457 {
459458 _checkRole(RELAYER);
460- _rateLimitedAddress(LIMIT_4626_DEPOSIT, token, amount);
461-
462- // Approve asset to token from the proxy (assumes the proxy has enough of the asset).
463- ApproveLib.approve(IERC4626(token).asset(), address(proxy), token, amount);
464-
465- // Deposit asset into the token, proxy receives token shares, decode the resulting shares.
466- shares = abi.decode(
467- proxy.doCall(
468- token,
469- abi.encodeCall(IERC4626(token).deposit, (amount, address(proxy)))
470- ),
471- (uint256)
472- );
473459
474- require(
475- _getExchangeRate(shares, amount) <= maxExchangeRates[token],
476- "MC/exchange-rate-too-high"
477- );
460+ return ERC4626Lib.deposit({
461+ proxy : address(proxy),
462+ token : token,
463+ amount : amount,
464+ maxExchangeRate : maxExchangeRates[token],
465+ rateLimits : address(rateLimits),
466+ rateLimitId : LIMIT_4626_DEPOSIT
467+ });
478468 }
479469
480470 function withdrawERC4626(address token, uint256 amount)
481471 external nonReentrant returns (uint256 shares)
482472 {
483473 _checkRole(RELAYER);
484- _rateLimitedAddress(LIMIT_4626_WITHDRAW, token, amount);
485474
486- // Withdraw asset from a token, decode resulting shares.
487- // Assumes proxy has adequate token shares.
488- shares = abi.decode(
489- proxy.doCall(
490- token,
491- abi.encodeCall(IERC4626(token).withdraw, (amount, address(proxy), address(proxy)))
492- ),
493- (uint256)
494- );
495-
496- _cancelRateLimit(RateLimitHelpers.makeAddressKey(LIMIT_4626_DEPOSIT, token), amount);
475+ return ERC4626Lib.withdraw({
476+ proxy : address(proxy),
477+ token : token,
478+ amount : amount,
479+ rateLimits : address(rateLimits),
480+ withdrawRateLimitId : LIMIT_4626_WITHDRAW,
481+ depositRateLimitId : LIMIT_4626_DEPOSIT
482+ });
497483 }
498484
499- // NOTE: !!! Rate limited at end of function !!!
500485 function redeemERC4626(address token, uint256 shares)
501486 external nonReentrant returns (uint256 assets)
502487 {
503488 _checkRole(RELAYER);
504489
505- // Redeem shares for assets from the token, decode the resulting assets.
506- // Assumes proxy has adequate token shares.
507- assets = abi.decode(
508- proxy.doCall(
509- token,
510- abi.encodeCall(IERC4626(token).redeem, (shares, address(proxy), address(proxy)))
511- ),
512- (uint256)
513- );
514-
515- rateLimits.triggerRateLimitDecrease(
516- RateLimitHelpers.makeAddressKey(LIMIT_4626_WITHDRAW, token),
517- assets
518- );
490+ return ERC4626Lib.redeem({
491+ proxy : address(proxy),
492+ token : token,
493+ shares : shares,
494+ rateLimits : address(rateLimits),
495+ withdrawRateLimitId : LIMIT_4626_WITHDRAW,
496+ depositRateLimitId : LIMIT_4626_DEPOSIT
497+ });
498+ }
519499
520- _cancelRateLimit(RateLimitHelpers.makeAddressKey(LIMIT_4626_DEPOSIT, token), assets);
500+ function EXCHANGE_RATE_PRECISION() external pure returns (uint256) {
501+ return ERC4626Lib.EXCHANGE_RATE_PRECISION;
521502 }
522503
523504 /**********************************************************************************************/
@@ -1053,18 +1034,4 @@ contract MainnetController is ReentrancyGuard, AccessControlEnumerable {
10531034 );
10541035 }
10551036
1056- /**********************************************************************************************/
1057- /*** Exchange rate helper functions ***/
1058- /**********************************************************************************************/
1059-
1060- function _getExchangeRate(uint256 shares, uint256 assets) internal pure returns (uint256) {
1061- // Return 0 for zero assets first, to handle the valid case of 0 shares and 0 assets.
1062- if (assets == 0) return 0;
1063-
1064- // Zero shares with non-zero assets is invalid (infinite exchange rate).
1065- if (shares == 0) revert("MC/zero-shares");
1066-
1067- return (EXCHANGE_RATE_PRECISION * assets) / shares;
1068- }
1069-
10701037}
0 commit comments