Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Roll back withdraw and deposit logic #84

Merged
merged 7 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/MetaMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,11 @@ contract MetaMorpho is ERC4626, Ownable2Step, IMetaMorpho {
}

/// @dev Used in redeem or withdraw to withdraw the underlying asset from Blue markets.
/// @dev When withdrawing "too much", the error logged depends on 3 cases:
/// 1. when withdrawing more than balance but less than vault's total assets "ERC20: burn amount exceeds balance" is
/// logged.
/// 2. when withdrawing more than vault's total assets "withdraw failed on Morpho" is logged.
/// 3. when withdrawing more than balance but less than liquidity "withdraw failed on Morpho" is logged.
MerlinEgalite marked this conversation as resolved.
Show resolved Hide resolved
function _withdraw(address caller, address receiver, address owner, uint256 assets, uint256 shares)
internal
override
Expand Down
46 changes: 45 additions & 1 deletion test/forge/ERC4626Test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ contract ERC4626Test is BaseTest {
vault.transferFrom(ONBEHALF, RECEIVER, shares);
}

function testWithdrawTooMuch(uint256 deposited, uint256 assets) public {
function testWithdrawMoreThanBalanceButLessThanTotalAssets(uint256 deposited, uint256 assets) public {
deposited = bound(deposited, MIN_TEST_ASSETS, MAX_TEST_ASSETS);

borrowableToken.setBalance(SUPPLIER, deposited);
Expand All @@ -177,6 +177,50 @@ contract ERC4626Test is BaseTest {

assets = bound(assets, deposited + 1, type(uint256).max / (deposited + 10 ** DECIMALS_OFFSET));

uint256 toAdd = assets - deposited + 1;
borrowableToken.setBalance(SUPPLIER, toAdd);

vm.prank(SUPPLIER);
vault.deposit(toAdd, SUPPLIER);

vm.prank(ONBEHALF);
vm.expectRevert("ERC20: burn amount exceeds balance");
vault.withdraw(assets, RECEIVER, ONBEHALF);
}

function testWithdrawMoreThanTotalAssets(uint256 deposited, uint256 assets) public {
deposited = bound(deposited, MIN_TEST_ASSETS, MAX_TEST_ASSETS);

borrowableToken.setBalance(SUPPLIER, deposited);

vm.prank(SUPPLIER);
vault.deposit(deposited, ONBEHALF);

assets = bound(assets, deposited + 1, type(uint256).max / (deposited + 10 ** DECIMALS_OFFSET));

vm.prank(ONBEHALF);
vm.expectRevert(bytes(ErrorsLib.WITHDRAW_FAILED_MORPHO));
vault.withdraw(assets, RECEIVER, ONBEHALF);
}

function testWithdrawMoreThanBalanceButLessThanLiquidity(uint256 deposited, uint256 assets) public {
deposited = bound(deposited, MIN_TEST_ASSETS, MAX_TEST_ASSETS);

borrowableToken.setBalance(SUPPLIER, deposited);

vm.prank(SUPPLIER);
vault.deposit(deposited, ONBEHALF);

assets = bound(assets, deposited + 1, type(uint256).max / (deposited + 10 ** DECIMALS_OFFSET));

collateralToken.setBalance(BORROWER, type(uint128).max);

// Borrow liquidity.
vm.startPrank(BORROWER);
morpho.supplyCollateral(allMarkets[0], type(uint128).max, BORROWER, hex"");
morpho.borrow(allMarkets[0], 1, 0, BORROWER, BORROWER);
vm.stopPrank();

vm.prank(ONBEHALF);
vm.expectRevert(bytes(ErrorsLib.WITHDRAW_FAILED_MORPHO));
vault.withdraw(assets, RECEIVER, ONBEHALF);
Expand Down
Loading