Skip to content

Commit

Permalink
handle max functions compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
dglowinski committed Mar 28, 2024
1 parent a07b417 commit 523a7b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/EVault/modules/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ abstract contract VaultModule is IVault, Base, AssetTransfers, BalanceUtils {
}

/// @inheritdoc IERC4626
/// @dev Because `nonReentrantView` can revert, the function might be considered not fully compliant with ERC4626
function maxDeposit(address account) public view virtual nonReentrantView returns (uint256) {
VaultCache memory vaultCache = loadVault();

Expand All @@ -52,12 +53,17 @@ abstract contract VaultModule is IVault, Base, AssetTransfers, BalanceUtils {
}

/// @inheritdoc IERC4626
/// @dev If the hook on `mint` allows only certain amounts, maxMint function might not be fully compliant with ERC4626
/// @dev Because `nonReentrantView` can revert, the function might be considered not fully compliant with ERC4626
function maxMint(address account) public view virtual nonReentrantView returns (uint256) {
VaultCache memory vaultCache = loadVault();

return validateAndCallHookView(vaultCache.hookedOps, OP_MINT)
? maxDepositInternal(vaultCache, account).toAssets().toSharesDown(vaultCache).toUint()
: 0;
if (!validateAndCallHookView(vaultCache.hookedOps, OP_MINT)) return 0;

// make sure to not revert on conversion
uint256 shares = maxDepositInternal(vaultCache, account).toAssets().toUint256SharesDown(vaultCache);

return shares < MAX_SANE_AMOUNT ? shares : MAX_SANE_AMOUNT;
}

/// @inheritdoc IERC4626
Expand All @@ -67,6 +73,7 @@ abstract contract VaultModule is IVault, Base, AssetTransfers, BalanceUtils {
}

/// @inheritdoc IERC4626
/// @dev Because `nonReentrantView` can revert, the function might be considered not fully compliant with ERC4626
function maxWithdraw(address owner) public view virtual nonReentrantView returns (uint256) {
VaultCache memory vaultCache = loadVault();

Expand All @@ -82,6 +89,7 @@ abstract contract VaultModule is IVault, Base, AssetTransfers, BalanceUtils {
}

/// @inheritdoc IERC4626
/// @dev Because `nonReentrantView` can revert, the function might be considered not fully compliant with ERC4626
function maxRedeem(address owner) public view virtual nonReentrantView returns (uint256) {
return validateAndCallHookView(vaultStorage.hookedOps, OP_REDEEM) ? maxRedeemInternal(owner).toUint() : 0;
}
Expand Down
6 changes: 5 additions & 1 deletion src/EVault/shared/types/Assets.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ library AssetsLib {
}

function toSharesDown(Assets amount, VaultCache memory vaultCache) internal pure returns (Shares) {
return TypesLib.toShares(toUint256SharesDown(amount, vaultCache));
}

function toUint256SharesDown(Assets amount, VaultCache memory vaultCache) internal pure returns (uint256) {
(uint256 totalAssets, uint256 totalShares) = ConversionHelpers.conversionTotals(vaultCache);
unchecked {
return TypesLib.toShares(amount.toUint() * totalShares / totalAssets);
return amount.toUint() * totalShares / totalAssets;
}
}

Expand Down

0 comments on commit 523a7b6

Please sign in to comment.