Skip to content

Commit

Permalink
FIX: properly validate if all sub assets could be removed
Browse files Browse the repository at this point in the history
  • Loading branch information
bugduino committed Feb 13, 2024
1 parent 6c9af2b commit f263fc1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/metamorpho/MetaMorphoSnippets.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,16 @@ contract MetaMorphoSnippets {
uint256 expectedSupply;
uint256 queueLength = IMetaMorpho(vault).withdrawQueueLength();
uint256 supplyQueueLength = IMetaMorpho(vault).supplyQueueLength();
// simulate change in vault total assets
uint256 newTotalAmount = IMetaMorpho(vault).totalAssets();
uint256 totRemoved;

if (sub > 0 && (newTotalAmount + add) <= sub) {
// impossible to remove more than the vault has
return 0;
}

// simulate change in vault total assets
newTotalAmount = newTotalAmount + add - sub;

for (uint256 i; i < queueLength; ++i) {
Id idMarket = IMetaMorpho(vault).withdrawQueue(i);
MarketParams memory marketParams = morpho.idToMarketParams(idMarket);
Expand All @@ -105,18 +108,21 @@ contract MetaMorphoSnippets {

expectedSupply = morpho.expectedSupplyAssets(marketParams, vault);
if (toSub > 0 && (expectedSupply + toAdd) < toSub) {
// impossible to remove more than the vault assets
continue;
}
// Use scaled add and sub values to calculate current supply APR Market
ratio += supplyAPRMarket(marketParams, morpho.market(idMarket), toAdd, toSub).wMulDown(
// Use scaled add and sub values to calculate assets supplied
expectedSupply + toAdd - toSub
);
// update amount subtracted from vault
totRemoved += toSub;
}

// If there is still some liquidity to remove here it means there is not enough liquidity
// in the vault to cover the requested withdraw amount
if (sub > 0) {
if (sub - totRemoved > 0) {
return 0;
}

Expand Down
5 changes: 5 additions & 0 deletions test/forge/metamorpho/TestMetaMorphoSnippets.sol
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ contract TestMetaMorphoSnippets is IntegrationTest {
avgSupplyRateSnippets = snippets.supplyAPRVault(address(vault), add, sub);
expectedAvgRate = _calcVaultAPR(id0, id1, firstDeposit, secondDeposit, add, sub);

// Check that the amount we want to redeem is actually available
uint256 maxWithdrawable = vault.maxWithdraw(ONBEHALF);
if (sub > maxWithdrawable) {
expectedAvgRate = 0;
}
assertApproxEqAbs(avgSupplyRateSnippets, expectedAvgRate, 100, "Diff in supplyAPRVault with add/sub values");
}

Expand Down

0 comments on commit f263fc1

Please sign in to comment.