From 02be7fe772e24aa47d5b4008a4cc6a33b7a6910f Mon Sep 17 00:00:00 2001 From: Rubilmax Date: Tue, 26 Sep 2023 17:56:57 +0200 Subject: [PATCH 1/2] refactor(reallocate): use sum of assets --- src/MetaMorpho.sol | 28 +++++++++++++++++++++------- src/libraries/ErrorsLib.sol | 2 ++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/MetaMorpho.sol b/src/MetaMorpho.sol index af32f5b2..6f0bb335 100644 --- a/src/MetaMorpho.sol +++ b/src/MetaMorpho.sol @@ -283,22 +283,29 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph external onlyAllocator { - uint256 balanceBefore = ERC20(asset()).balanceOf(address(this)); - + uint256 totalWithdrawn; uint256 nbWithdrawn = withdrawn.length; for (uint256 i; i < nbWithdrawn; ++i) { MarketAllocation memory allocation = withdrawn[i]; - MORPHO.withdraw(allocation.marketParams, allocation.assets, allocation.shares, address(this), address(this)); + (uint256 withdrawnAssets,) = MORPHO.withdraw( + allocation.marketParams, allocation.assets, allocation.shares, address(this), address(this) + ); + + totalWithdrawn += withdrawnAssets; } + uint256 totalSupplied; uint256 nbSupplied = supplied.length; for (uint256 i; i < nbSupplied; ++i) { MarketAllocation memory allocation = supplied[i]; - MORPHO.supply(allocation.marketParams, allocation.assets, allocation.shares, address(this), hex""); + (uint256 suppliedAssets,) = + MORPHO.supply(allocation.marketParams, allocation.assets, allocation.shares, address(this), hex""); + + totalSupplied += suppliedAssets; require( _supplyBalance(allocation.marketParams) <= config[allocation.marketParams.id()].cap, @@ -306,10 +313,17 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph ); } - uint256 balanceAfter = ERC20(asset()).balanceOf(address(this)); + if (totalWithdrawn > totalSupplied) { + idle += totalWithdrawn - totalSupplied; + } else { + uint256 idleSupplied = totalSupplied - totalWithdrawn; + require(idle >= idleSupplied, ErrorsLib.INSUFFICIENT_IDLE); - if (balanceAfter > balanceBefore) idle += balanceAfter - balanceBefore; - else idle -= balanceBefore - balanceAfter; + unchecked { + // Underflow not possible: idle >= idleSupplied. + idle -= idleSupplied; + } + } } /* EXTERNAL */ diff --git a/src/libraries/ErrorsLib.sol b/src/libraries/ErrorsLib.sol index a593ec11..fb5730c0 100644 --- a/src/libraries/ErrorsLib.sol +++ b/src/libraries/ErrorsLib.sol @@ -43,4 +43,6 @@ library ErrorsLib { string internal constant MAX_QUEUE_SIZE_EXCEEDED = "max queue size exceeded"; string internal constant ZERO_FEE_RECIPIENT = "fee recipient is zero"; + + string internal constant INSUFFICIENT_IDLE = "insufficient idle liquidity"; } From ff9198428944dfa8f05a1b01cd205d2be98ab859 Mon Sep 17 00:00:00 2001 From: Rubilmax Date: Thu, 28 Sep 2023 14:18:25 +0200 Subject: [PATCH 2/2] refactor(metamorpho): remove overwhelming unchecked --- src/MetaMorpho.sol | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/MetaMorpho.sol b/src/MetaMorpho.sol index 0c518b4b..81f99597 100644 --- a/src/MetaMorpho.sol +++ b/src/MetaMorpho.sol @@ -319,10 +319,7 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph uint256 idleSupplied = totalSupplied - totalWithdrawn; require(idle >= idleSupplied, ErrorsLib.INSUFFICIENT_IDLE); - unchecked { - // Underflow not possible: idle >= idleSupplied. - idle -= idleSupplied; - } + idle -= idleSupplied; } }