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

Fix rounding in max deposit #346

Merged
merged 7 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 10 additions & 3 deletions src/MetaMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,10 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph
uint256 supplyCap = config[id].cap;
if (supplyCap == 0) continue;

uint256 supplyAssets = MORPHO.expectedSupplyAssets(_marketParams(id), address(this));
MathisGD marked this conversation as resolved.
Show resolved Hide resolved
uint256 supplyShares = MORPHO.supplyShares(id, address(this));
(uint256 totalSupplyAssets, uint256 totalSupplyShares,,) = MORPHO.expectedMarketBalances(_marketParams(id));

totalSuppliable += supplyCap.zeroFloorSub(supplyAssets);
totalSuppliable += supplyCap.zeroFloorSub(supplyShares.toAssetsUp(totalSupplyAssets, totalSupplyShares));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like you have done for _supplyMorpho maybe would be good to also add a comment here. I know that this is more a "utility" function and not a "core" one like _supplyMorpho but it will be probably used very much by integrators.

Consider also adding it as a natspec @dev comment.

}
}

Expand Down Expand Up @@ -768,7 +769,13 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph
if (supplyCap == 0) continue;

MarketParams memory marketParams = _marketParams(id);
(uint256 supplyAssets,,) = _accruedSupplyBalance(marketParams, id);
MathisGD marked this conversation as resolved.
Show resolved Hide resolved

MORPHO.accrueInterest(marketParams);

Market memory market = MORPHO.market(id);
uint256 supplyShares = MORPHO.supplyShares(id, address(this));
// `supplyAssets` needs to be rounded up for `toSupply` to be rounded down.
uint256 supplyAssets = supplyShares.toAssetsUp(market.totalSupplyAssets, market.totalSupplyShares);
Rubilmax marked this conversation as resolved.
Show resolved Hide resolved

uint256 toSupply = UtilsLib.min(supplyCap.zeroFloorSub(supplyAssets), assets);

Expand Down
30 changes: 30 additions & 0 deletions test/forge/ERC4626Test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,36 @@ contract ERC4626Test is IntegrationTest, IMorphoFlashLoanCallback {
morpho.flashLoan(address(loanToken), loanToken.balanceOf(address(morpho)), hex"");
}

function testMaxDeposit() public {
_setCap(allMarkets[0], 1 ether);

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

vm.prank(ALLOCATOR);
vault.setSupplyQueue(supplyQueue);

loanToken.setBalance(SUPPLIER, 1 ether);
collateralToken.setBalance(BORROWER, 2 ether);

vm.prank(SUPPLIER);
morpho.supply(allMarkets[0], 1 ether, 0, SUPPLIER, hex"");

vm.startPrank(BORROWER);
morpho.supplyCollateral(allMarkets[0], 2 ether, BORROWER, hex"");
morpho.borrow(allMarkets[0], 1 ether, 0, BORROWER, BORROWER);
vm.stopPrank();

_forward(1_000);

loanToken.setBalance(SUPPLIER, 1 ether);

vm.prank(SUPPLIER);
vault.deposit(1 ether, ONBEHALF);
MerlinEgalite marked this conversation as resolved.
Show resolved Hide resolved

assertEq(vault.maxDeposit(SUPPLIER), 0);
}

function onMorphoFlashLoan(uint256, bytes memory) external {
assertEq(vault.maxWithdraw(ONBEHALF), 0);
}
Expand Down