Skip to content

Commit

Permalink
feat: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Grimal committed Dec 7, 2023
1 parent 510b34d commit 07d288d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/MetaMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph
bool[] memory seen = new bool[](currLength);
Id[] memory newWithdrawQueue = new Id[](newLength);

uint256 lostSupply;
uint256 lostAssets;

for (uint256 i; i < newLength; ++i) {
uint256 prevIndex = indexes[i];
Expand Down Expand Up @@ -384,7 +384,10 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph
slot[0] = id.marketTotalSupplyAssetsAndSharesSlot();
bytes32[] memory res = MORPHO.extSloads(slot);

lostSupply += supplyShares.toAssetsDown(uint128(uint256(res[0])), uint256(res[0] >> 128));
uint256 totalSupplyAssets = uint128(uint256(res[0]));
uint256 totalSupplyShares = uint256(res[0] >> 128);

lostAssets += supplyShares.toAssetsDown(totalSupplyAssets, totalSupplyShares);
}

delete config[id];
Expand All @@ -394,8 +397,7 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph
withdrawQueue = newWithdrawQueue;

// Accrue interests on all the enabled markets except the removed ones.
_updateLastTotalAssets(lastTotalAssets.zeroFloorSub(lostSupply));
_updateLastTotalAssets(_accrueFee());
_updateLastTotalAssets(lastTotalAssets.zeroFloorSub(lostAssets));

emit EventsLib.SetWithdrawQueue(_msgSender(), newWithdrawQueue);
}
Expand Down
54 changes: 53 additions & 1 deletion test/forge/MarketTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,59 @@ contract MarketTest is IntegrationTest {
assertEq(Id.unwrap(vault.withdrawQueue(3)), Id.unwrap(expectedWithdrawQueue[3]));
}

function testUpdateWithdrawQueueRemovingDisabledMarket() public {
function testUpdateWithdrawQueueRemovingDisabledMarket(uint256 firstAmountSupplied, uint256 secondAmountSupplied)
public
{
firstAmountSupplied = bound(firstAmountSupplied, MIN_TEST_ASSETS, MAX_TEST_ASSETS);
secondAmountSupplied = bound(secondAmountSupplied, MIN_TEST_ASSETS, MAX_TEST_ASSETS);

_setCap(allMarkets[0], firstAmountSupplied);
_setCap(allMarkets[1], secondAmountSupplied);

Id[] memory supplyQueue = new Id[](2);
supplyQueue[0] = allMarkets[0].id();
supplyQueue[1] = allMarkets[1].id();

_setCap(allMarkets[0], firstAmountSupplied);
_setCap(allMarkets[1], secondAmountSupplied);
vm.prank(ALLOCATOR);
vault.setSupplyQueue(supplyQueue);

loanToken.setBalance(SUPPLIER, firstAmountSupplied + secondAmountSupplied);

vm.prank(SUPPLIER);
vault.deposit(firstAmountSupplied + secondAmountSupplied, ONBEHALF);

_setCap(allMarkets[1], 0);

vm.prank(CURATOR);
vault.submitMarketRemoval(allMarkets[1].id());

vm.warp(block.timestamp + TIMELOCK);

uint256[] memory indexes = new uint256[](3);
indexes[0] = 0;
indexes[1] = 1;
indexes[2] = 3;

Id[] memory expectedWithdrawQueue = new Id[](3);
expectedWithdrawQueue[0] = idleParams.id();
expectedWithdrawQueue[1] = allMarkets[0].id();
expectedWithdrawQueue[2] = allMarkets[2].id();

vm.expectEmit();
emit EventsLib.SetWithdrawQueue(ALLOCATOR, expectedWithdrawQueue);
vm.prank(ALLOCATOR);
vault.updateWithdrawQueue(indexes);

assertEq(Id.unwrap(vault.withdrawQueue(0)), Id.unwrap(expectedWithdrawQueue[0]));
assertEq(Id.unwrap(vault.withdrawQueue(1)), Id.unwrap(expectedWithdrawQueue[1]));
assertEq(Id.unwrap(vault.withdrawQueue(2)), Id.unwrap(expectedWithdrawQueue[2]));
assertFalse(vault.config(allMarkets[1].id()).enabled);
assertEq(vault.totalAssets(), firstAmountSupplied);
}

function testUpdateWithdrawQueueRemovingDisabledMarketWithSupply() public {
_setCap(allMarkets[2], 0);

vm.prank(CURATOR);
Expand Down

0 comments on commit 07d288d

Please sign in to comment.