Skip to content

Commit

Permalink
Merge pull request #379 from morpho-org/fix/max-mint
Browse files Browse the repository at this point in the history
fix(erc4626): remove call to max functions
  • Loading branch information
MerlinEgalite authored Dec 21, 2023
2 parents fcb2740 + 1571a4f commit 6587ae0
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions src/ERC4626Bundler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,16 @@ abstract contract ERC4626Bundler is BaseBundler {
/// @dev Initiator must have previously transferred their assets to the bundler.
/// @dev Assumes the given `vault` implements EIP-4626.
/// @param vault The address of the vault.
/// @param shares The amount of shares to mint. Pass `type(uint256).max` to mint max.
/// @param shares The amount of shares to mint.
/// @param maxAssets The maximum amount of assets to deposit in exchange for `shares`.
/// @param receiver The address to which shares will be minted.
function erc4626Mint(address vault, uint256 shares, uint256 maxAssets, address receiver)
external
payable
protected
{
require(receiver != address(0), ErrorsLib.ZERO_ADDRESS);
/// Do not check `receiver != address(this)` to allow the bundler to receive the vault's shares.

shares = Math.min(shares, IERC4626(vault).maxMint(receiver));

require(receiver != address(0), ErrorsLib.ZERO_ADDRESS);
require(shares != 0, ErrorsLib.ZERO_SHARES);

_approveMaxTo(IERC4626(vault).asset(), vault);
Expand All @@ -47,20 +44,19 @@ abstract contract ERC4626Bundler is BaseBundler {
/// @dev Initiator must have previously transferred their assets to the bundler.
/// @dev Assumes the given `vault` implements EIP-4626.
/// @param vault The address of the vault.
/// @param assets The amount of assets to deposit. Pass `type(uint256).max` to deposit max.
/// @param assets The amount of assets to deposit. Pass `type(uint256).max` to deposit the bundler's assets.
/// @param minShares The minimum amount of shares to mint in exchange for `assets`.
/// @param receiver The address to which shares will be minted.
function erc4626Deposit(address vault, uint256 assets, uint256 minShares, address receiver)
external
payable
protected
{
require(receiver != address(0), ErrorsLib.ZERO_ADDRESS);
/// Do not check `receiver != address(this)` to allow the bundler to receive the vault's shares.
require(receiver != address(0), ErrorsLib.ZERO_ADDRESS);

address asset = IERC4626(vault).asset();

assets = Math.min(assets, IERC4626(vault).maxDeposit(receiver));
assets = Math.min(assets, ERC20(asset).balanceOf(address(this)));

require(assets != 0, ErrorsLib.ZERO_AMOUNT);
Expand All @@ -75,7 +71,7 @@ abstract contract ERC4626Bundler is BaseBundler {
/// `receiver`.
/// @dev Assumes the given `vault` implements EIP-4626.
/// @param vault The address of the vault.
/// @param assets The amount of assets to withdraw. Pass `type(uint256).max` to withdraw max.
/// @param assets The amount of assets to withdraw.
/// @param maxShares The maximum amount of shares to redeem in exchange for `assets`.
/// @param receiver The address that will receive the withdrawn assets.
/// @param owner The address on behalf of which the assets are withdrawn. Can only be the bundler or the initiator.
Expand All @@ -89,9 +85,6 @@ abstract contract ERC4626Bundler is BaseBundler {
/// Do not check `receiver != address(this)` to allow the bundler to receive the underlying asset.
require(receiver != address(0), ErrorsLib.ZERO_ADDRESS);
require(owner == address(this) || owner == initiator(), ErrorsLib.UNEXPECTED_OWNER);

assets = Math.min(assets, IERC4626(vault).maxWithdraw(owner));

require(assets != 0, ErrorsLib.ZERO_AMOUNT);

uint256 shares = IERC4626(vault).withdraw(assets, receiver, owner);
Expand All @@ -101,7 +94,7 @@ abstract contract ERC4626Bundler is BaseBundler {
/// @notice Redeems the given amount of `shares` from the given ERC4626 `vault`, transferring assets to `receiver`.
/// @dev Assumes the given `vault` implements EIP-4626.
/// @param vault The address of the vault.
/// @param shares The amount of shares to burn. Pass `type(uint256).max` to redeem max.
/// @param shares The amount of shares to burn. Pass `type(uint256).max` to redeem the bundler's shares.
/// @param minAssets The minimum amount of assets to withdraw in exchange for `shares`.
/// @param receiver The address that will receive the withdrawn assets.
/// @param owner The address on behalf of which the shares are redeemed. Can only be the bundler or the initiator.
Expand All @@ -116,7 +109,7 @@ abstract contract ERC4626Bundler is BaseBundler {
require(receiver != address(0), ErrorsLib.ZERO_ADDRESS);
require(owner == address(this) || owner == initiator(), ErrorsLib.UNEXPECTED_OWNER);

shares = Math.min(shares, IERC4626(vault).maxRedeem(owner));
shares = Math.min(shares, IERC4626(vault).balanceOf(owner));

require(shares != 0, ErrorsLib.ZERO_SHARES);

Expand Down

0 comments on commit 6587ae0

Please sign in to comment.