From 9bf6008257a85291b8729695aa95af5680b9b70a Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Fri, 13 Oct 2023 12:28:10 +0200 Subject: [PATCH 1/3] fix: last total assets --- src/MetaMorpho.sol | 12 ++++++++---- test/forge/FeeTest.sol | 3 +-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/MetaMorpho.sol b/src/MetaMorpho.sol index 422b46c3..754197c8 100644 --- a/src/MetaMorpho.sol +++ b/src/MetaMorpho.sol @@ -482,7 +482,8 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph shares = _convertToSharesWithFeeAccrued(assets, totalSupply(), newTotalAssets, Math.Rounding.Floor); _deposit(_msgSender(), receiver, assets, shares); - _updateLastTotalAssets(newTotalAssets + assets); + // `newTotalAssets + assets` cannot be used as input because of rounding errors so we must use `totalAssets`. + _updateLastTotalAssets(totalAssets()); } /// @inheritdoc IERC4626 @@ -492,7 +493,8 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph assets = _convertToAssetsWithFeeAccrued(shares, totalSupply(), newTotalAssets, Math.Rounding.Ceil); _deposit(_msgSender(), receiver, assets, shares); - _updateLastTotalAssets(newTotalAssets + assets); + // `newTotalAssets + assets` cannot be used as input because of rounding errors so we must use `totalAssets`. + _updateLastTotalAssets(totalAssets()); } /// @inheritdoc IERC4626 @@ -508,7 +510,8 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph shares = _convertToSharesWithFeeAccrued(assets, totalSupply(), newTotalAssets, Math.Rounding.Ceil); _withdraw(_msgSender(), receiver, owner, assets, shares); - _updateLastTotalAssets(newTotalAssets - assets); + // `newTotalAssets - assets` cannot be used as input because of rounding errors so we must use `totalAssets`. + _updateLastTotalAssets(totalAssets()); } /// @inheritdoc IERC4626 @@ -524,7 +527,8 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph assets = _convertToAssetsWithFeeAccrued(shares, totalSupply(), newTotalAssets, Math.Rounding.Floor); _withdraw(_msgSender(), receiver, owner, assets, shares); - _updateLastTotalAssets(newTotalAssets - assets); + // `newTotalAssets - assets` cannot be used as input because of rounding errors so we must use `totalAssets`. + _updateLastTotalAssets(totalAssets()); } /// @inheritdoc IERC4626 diff --git a/test/forge/FeeTest.sol b/test/forge/FeeTest.sol index 0220ca1d..817082e1 100644 --- a/test/forge/FeeTest.sol +++ b/test/forge/FeeTest.sol @@ -58,7 +58,7 @@ contract FeeTest is BaseTest { vm.prank(SUPPLIER); vault.deposit(deposited, ONBEHALF); - assertApproxEqAbs(vault.lastTotalAssets(), vault.totalAssets(), 1, "lastTotalAssets"); + assertEq(vault.lastTotalAssets(), vault.totalAssets(), "lastTotalAssets"); } function testAccrueFeeWithinABlock(uint256 deposited, uint256 withdrawn) public { @@ -289,7 +289,6 @@ contract FeeTest is BaseTest { vault.deposit(deposited, ONBEHALF); uint256 lastTotalAssetsBefore = vault.lastTotalAssets(); - uint256 assetsBefore = vault.convertToAssets(shares); _forward(blocks); From fb88167f6a229c3bd191885fa3d92f2d7c63c6b6 Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Fri, 13 Oct 2023 14:43:54 +0200 Subject: [PATCH 2/3] fix: merge --- test/forge/FeeTest.sol | 1 + 1 file changed, 1 insertion(+) diff --git a/test/forge/FeeTest.sol b/test/forge/FeeTest.sol index dc66c8fd..7fc18446 100644 --- a/test/forge/FeeTest.sol +++ b/test/forge/FeeTest.sol @@ -289,6 +289,7 @@ contract FeeTest is BaseTest { vault.deposit(deposited, ONBEHALF); uint256 lastTotalAssetsBefore = vault.lastTotalAssets(); + uint256 assetsBefore = vault.convertToAssets(shares); _forward(blocks); From 8ed861cfb6331e200dcef0c014aaa1b95fb32287 Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Fri, 13 Oct 2023 16:40:51 +0200 Subject: [PATCH 3/3] refactor: clean code --- src/MetaMorpho.sol | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/MetaMorpho.sol b/src/MetaMorpho.sol index 754197c8..3783efcc 100644 --- a/src/MetaMorpho.sol +++ b/src/MetaMorpho.sol @@ -481,9 +481,6 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph shares = _convertToSharesWithFeeAccrued(assets, totalSupply(), newTotalAssets, Math.Rounding.Floor); _deposit(_msgSender(), receiver, assets, shares); - - // `newTotalAssets + assets` cannot be used as input because of rounding errors so we must use `totalAssets`. - _updateLastTotalAssets(totalAssets()); } /// @inheritdoc IERC4626 @@ -492,9 +489,6 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph assets = _convertToAssetsWithFeeAccrued(shares, totalSupply(), newTotalAssets, Math.Rounding.Ceil); _deposit(_msgSender(), receiver, assets, shares); - - // `newTotalAssets + assets` cannot be used as input because of rounding errors so we must use `totalAssets`. - _updateLastTotalAssets(totalAssets()); } /// @inheritdoc IERC4626 @@ -509,9 +503,6 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph shares = _convertToSharesWithFeeAccrued(assets, totalSupply(), newTotalAssets, Math.Rounding.Ceil); _withdraw(_msgSender(), receiver, owner, assets, shares); - - // `newTotalAssets - assets` cannot be used as input because of rounding errors so we must use `totalAssets`. - _updateLastTotalAssets(totalAssets()); } /// @inheritdoc IERC4626 @@ -526,9 +517,6 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph assets = _convertToAssetsWithFeeAccrued(shares, totalSupply(), newTotalAssets, Math.Rounding.Floor); _withdraw(_msgSender(), receiver, owner, assets, shares); - - // `newTotalAssets - assets` cannot be used as input because of rounding errors so we must use `totalAssets`. - _updateLastTotalAssets(totalAssets()); } /// @inheritdoc IERC4626 @@ -611,6 +599,9 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph super._deposit(caller, owner, assets, shares); _supplyMorpho(assets); + + // `newTotalAssets + assets` cannot be used as input because of rounding errors so we must use `totalAssets`. + _updateLastTotalAssets(totalAssets()); } /// @inheritdoc ERC4626 @@ -628,6 +619,9 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph if (_withdrawMorpho(assets) != 0) revert ErrorsLib.WithdrawMorphoFailed(); super._withdraw(caller, receiver, owner, assets, shares); + + // `newTotalAssets - assets` cannot be used as input because of rounding errors so we must use `totalAssets`. + _updateLastTotalAssets(totalAssets()); } /* INTERNAL */