MaxApyVault

The MaxApyVault contract is the core component of the MaxAPY Protocol. It serves as the main entry point for users to interact with the protocol and manages the allocation of funds to various yield-generating strategies.

The main purpose of the MaxApyVault contract is to provide users with a secure and efficient way to deposit their assets and earn yield through the protocol's strategies. It is an ERC4626-compatible vault that allows users to deposit underlying assets and receive yield-bearing shares in return.

Key features and functionalities of the MaxApyVaultV2 contract include:

Deposit and Withdrawal: Users can deposit underlying assets into the vault using the deposit and mint functions. They can also withdraw their assets using the withdraw and redeem functions. The vault supports both standard deposits and withdrawals as well as deposits and withdrawals with permit functionality, enabling gasless approvals.

Strategy Management: The vault manages a set of yield-generating strategies. It allows the protocol administrators to add new strategies, remove existing strategies, and update strategy parameters such as debt ratios, maximum debt per harvest, and performance fees. The vault also maintains a withdrawal queue that determines the order in which funds are withdrawn from the strategies.

Reporting and Harvesting: Strategies report their performance to the vault using the report function, providing information about realized gains, unrealized gains, losses, and debt payments. The vault uses this information to update its accounting and distribute profits to users. The harvest function allows keepers to trigger the harvesting process for a specific strategy, realizing profits and reinvesting a portion.

Fees and Profit Distribution: The vault implements a fee structure to incentivize development and maintain the ecosystem. It charges performance and management fees based on the strategies' yield. These fees are distributed to the protocol's treasury and the strategists.

Emergency Shutdown and Risk Management: The vault includes an emergency shutdown mechanism to protect user funds in case of unexpected events or security issues. When the emergency shutdown is activated, the vault stops accepting new deposits and instructs all strategies to return funds. The vault also enforces debt limits and monitors strategies' performance to mitigate risks.

Vault Shares and Accounting: The vault mints yield-bearing shares to users upon deposit and burns shares upon withdrawal. It tracks total assets, idle funds, and debt across all strategies. The vault provides functions for converting between assets and shares, previewing mints and withdrawals, and calculating share prices.

The MaxApyVault contract interacts closely with the strategy contracts, such as BaseStrategy, BaseSommelierStrategy, and BaseYearnV3Strategy, to allocate funds, monitor performance, and manage risks.

The vault is the central hub for user interactions and funds management while delegating yield-generating activities to the strategies. It ensures the security and integrity of user funds, enforces access control, and provides transparency and accountability through events and public functions.

Overall, the MaxApyVault contract is the core component that enables users to deposit their assets, earn yield, and benefit from the aggregated performance of multiple yield-generating strategies in the MaxAPY Protocol. It abstracts away the complexities of interacting with individual strategies and provides a user-friendly interface for managing investments and earning returns.

View Functions

name()

function name() public view override returns (string memory)

Returns the name of the vault shares token.

Returns:

The name of the token.


symbol()

function symbol() public view override returns (string memory)

Returns the symbol of the token.

Returns:

The symbol of the token.


asset()

function asset() public view override returns (address)

Returns the address of the underlying asset.

Returns:

The address of the underlying asset.


totalAssets()

function totalAssets() public view override returns (uint256)

Returns the total amount of the underlying asset managed by the vault.

Returns:

The total amount of the underlying asset.


totalAccountedAssets()

function totalAccountedAssets() public view returns (uint256)

Returns the total amount of accounted idle and strategy debt assets.

Returns:

The total amount of accounted assets.


maxDeposit()

function maxDeposit(
    address /*to*/ 
) public view override returns (uint256 maxAssets)

Returns the maximum amount of the underlying asset that can be deposited into the vault for a specific address.

Parameters:

to: The address for which to calculate the maximum deposit amount.

Returns:

The maximum amount of assets that can be deposited.


maxMint()

function maxMint(
    address /*to*/ 
) public view override returns (uint256 maxShares)

Returns the maximum amount of vault shares that can be minted for a specific address.

Parameters:

to: The address for which to calculate the maximum mint amount.

Returns:

The maximum amount of shares that can be minted.


maxWithdraw()

function maxWithdraw(
    address owner
) public view override returns (uint256 maxAssets)

Returns the maximum amount of the underlying asset that can be withdrawn from the owner's balance in the vault.

Parameters:

owner: The address of the owner.

Returns:

The maximum amount of assets that can be withdrawn.


sharePrice()

function sharePrice() external view returns (uint256)

Returns the estimated price of one vault share.

Returns:

The estimated share price.


convertToShares()

function convertToShares(
    uint256 assets
) public view override returns (uint256 shares)

Returns the amount of shares the vault will exchange for the provided assets, in an ideal scenario where all conditions are met.

Parameters:

assets: The amount of assets to convert.

Returns:

The amount of shares.


convertToAssets()

function convertToAssets(
    uint256 shares
) public view override returns (uint256 assets)

Returns the amount of assets that the vault will exchange for the provided amount of shares, in an ideal scenario where all conditions are met.

Parameters:

shares: The amount of shares to convert.

Returns:

The amount of assets.


previewMint()

function previewMint(
    uint256 shares
) public view override returns (uint256 assets)

Allows an on-chain or off-chain user to simulate the effects of minting a specific share amount at the current block, given current on-chain conditions.

Parameters:

shares: The amount of shares to mint.

Returns:

The amount of assets required for minting.


previewWithdraw()

function previewWithdraw(
    uint256 assets
) public view override returns (uint256 shares)

Allows an on-chain or off-chain user to simulate the effects of withdrawing a specific amount of assets at the current block, given current on-chain conditions.

Parameters:

assets: The amount of assets to withdraw.

Returns:

The amount of shares required for withdrawal.


previewRedeem()

function previewRedeem(
    uint256 shares
) public view override returns (uint256 assets)

Allows an on-chain or off-chain user to simulate the effects of redeeming a specific share amount at the current block, given current on-chain conditions.

Parameters:

shares: The amount of shares to redeem.

Returns:

The amount of assets that would be received.


creditAvailable()

function creditAvailable(
    address strategy
) external view returns (uint256)

Returns the amount of tokens a strategy can access as a credit line from the vault.

Parameters:

strategy: The address of the strategy.

Returns:

The quantity of tokens available for the strategy to draw on.


debtOutstanding()

function debtOutstanding(
    address strategy
) external view returns (uint256)

Determines if a strategy is past its debt limit and if any tokens should be withdrawn to the vault.

Parameters:

strategy: The address of the strategy.

Returns:

The quantity of tokens to withdraw.


getStrategyTotalDebt()

function getStrategyTotalDebt(
    address strategy
) external view returns (uint256 strategyTotalDebt)

Returns the total debt of a strategy.

Parameters:

strategy: The address of the strategy.

Returns:

The strategy's total debt.


Functions

deposit()

function deposit(
    uint256 assets, 
    address receiver
) public virtual override 
noEmergencyShutdown 
nonReentrant 
returns (uint256 shares)

The deposit function allows users to deposit underlying assets into the vault and mint corresponding shares. The function takes two parameters:

Parameters

assets: The amount of underlying assets to be deposited.

receiver: The address that will receive the minted shares.


depositWithPermit()

function depositWithPermit(
    address owner,
    uint256 assets,
    uint256 deadline,
    uint8 v,
    bytes32 r,
    bytes32 s,
    address receiver
) public noEmergencyShutdown nonReentrant returns (uint256 shares)

Users can deposit underlying assets into the vault and mint corresponding shares using a permit signature.

Parameters:

owner: The address of the owner of the assets.

assets: The amount of underlying assets to be deposited.

deadline: The timestamp until which the permit signature is valid.

v, r, s: The components of the permit signature.

receiver: The address that will receive the minted shares.

Returns:

The number of shares minted.


mint()

function mint(uint256 shares, address receiver)
    public
    virtual
    override
    noEmergencyShutdown
    nonReentrant
returns (uint256 assets)

Mints a specific number of shares to the receiver by depositing the necessary amount of underlying assets.

Parameters:

shares: The number of shares to be minted.

receiver: The address that will receive the minted shares.

Returns:

The amount of underlying assets deposited.


mintWithPermit()

function mintWithPermit(
    address owner,
    uint256 shares,
    uint256 deadline,
    uint8 v,
    bytes32 r,
    bytes32 s,
    address receiver
) public noEmergencyShutdown nonReentrant returns (uint256 assets)

Mints a specific number of shares to the receiver by depositing the necessary amount of underlying assets using a permit signature.

Parameters:

owner: The address of the owner of the assets.

shares: The number of shares to be minted.

deadline: The timestamp until which the permit signature is valid.

v, r, s: The components of the permit signature.

receiver: The address that will receive the minted shares.

Returns:

The amount of underlying assets deposited.


withdraw()

function withdraw(
    uint256 assets, 
    address to, 
    address owner
) public override nonReentrant returns (uint256 shares)

The withdraw function enables users to redeem their shares and withdraw the underlying assets from the vault. The function takes three parameters:

Parameters:

assets: The amount of underlying assets to be withdrawn.

to: The address that will receive the withdrawn assets.

owner: The address of the owner of the shares being redeemed.

Returns:

The number of shares burned.


redeem()

function redeem(
    uint256 shares, 
    address to, 
    address owner
) public override nonReentrant returns (uint256 assets)

Redeems a specific number of shares and sends the corresponding amount of underlying assets to the specified address.

Parameters:

shares: The number of shares to be redeemed.

to: The address that will receive the withdrawn assets.

owner: The address of the owner of the shares being redeemed.

Returns:

The amount of underlying assets withdrawn.


addStrategy()

function addStrategy(
    address newStrategy,
    uint256 strategyDebtRatio,
    uint256 strategyMaxDebtPerHarvest,
    uint256 strategyMinDebtPerHarvest,
    uint256 strategyPerformanceFee
) external checkRoles(ADMIN_ROLE) noEmergencyShutdown

Adds a new strategy to the vault.

Parameters:

newStrategy: The address of the new strategy.

strategyDebtRatio: The debt ratio for the strategy (in basis points).

strategyMaxDebtPerHarvest: The maximum debt per harvest for the strategy.

strategyMinDebtPerHarvest: The minimum debt per harvest for the strategy.

strategyPerformanceFee: The performance fee for the strategy (in basis points).


removeStrategy()

function removeStrategy(
    address strategy
) external checkRoles(ADMIN_ROLE) noEmergencyShutdown

Removes a strategy from the vault.

Parameters:

strategy: The address of the strategy to be removed.


revokeStrategy()

function revokeStrategy(
    address strategy
) external checkRoles(ADMIN_ROLE)

Revokes a strategy, setting its debt limit to 0 and preventing future deposits.

Parameters:

strategy: The address of the strategy to be revoked.


exitStrategy()

function exitStrategy(
    address strategy
) external checkRoles(ADMIN_ROLE)

Fully exits a strategy, liquidating all its positions and removing it from the withdrawal queue.

Parameters:

strategy: The address of the strategy to be exited.


updateStrategyData()

function updateStrategyData(
    address strategy,
    uint256 newDebtRatio,
    uint256 newMaxDebtPerHarvest,
    uint256 newMinDebtPerHarvest,
    uint256 newPerformanceFee
) external checkRoles(ADMIN_ROLE)

Updates the configuration data for a strategy.

Parameters:

strategy: The address of the strategy to be updated.

newDebtRatio: The new debt ratio for the strategy (in basis points).

newMaxDebtPerHarvest: The new maximum debt per harvest for the strategy.

newMinDebtPerHarvest: The new minimum debt per harvest for the strategy.

newPerformanceFee: The new performance fee for the strategy (in basis points).


setWithdrawalQueue()

function setWithdrawalQueue(
    address[MAXIMUM_STRATEGIES] calldata queue
) external checkRoles(ADMIN_ROLE)

Sets the withdrawal queue order for the strategies.

Parameters:

queue: An array of strategy addresses representing the new withdrawal queue order.


setPerformanceFee()

function setPerformanceFee(
    uint256 _performanceFee
) external checkRoles(ADMIN_ROLE)

Sets the performance fee for the vault.

Parameters:

_performanceFee: The new performance fee (in basis points).


setManagementFee()

function setManagementFee(
    uint256 _managementFee
) external checkRoles(ADMIN_ROLE)

Sets the management fee for the vault.

Parameters:

_managementFee: The new management fee (in basis points).


setDepositLimit()

function setDepositLimit(
    uint256 _depositLimit
) external checkRoles(ADMIN_ROLE)

Sets the deposit limit for the vault.

Parameters:

_depositLimit: The new deposit limit.


setEmergencyShutdown()

function setEmergencyShutdown(
    bool _emergencyShutdown
) external checkRoles(EMERGENCY_ADMIN_ROLE)

Sets the emergency shutdown state for the vault.

Parameters:

_emergencyShutdown: The new emergency shutdown state (true for active, false for inactive).


setTreasury()

function setTreasury(
    address _treasury
) external checkRoles(ADMIN_ROLE)

Sets the treasury address for the vault.

Parameters:

_treasury: The new treasury address.


report()

function report(
    uint128 realizedGain,
    uint128 unrealizedGain,
    uint128 loss,
    uint128 debtPayment,
    address managementFeeReceiver
) external checkRoles(STRATEGY_ROLE) returns (uint256)

The report function is used by strategies to report their performance to the vault. The strategy provides information about its realized gains, unrealized gains, losses, and debt payments. The vault updates its internal accounting based on these reported values.

The function first performs some checks to ensure the validity of the reported values, such as verifying that the strategy has enough funds to cover the reported gains and debt payment.

If the strategy reports a loss, the _reportLoss internal function is called to handle the loss and adjust the strategy's parameters accordingly.

The _assessFees internal function is then called to calculate and mint shares to cover the performance, management, and strategist fees based on the reported gains.

The function calculates the strategy's available credit based on its current debt and the vault's overall debt limit. It also determines the strategy's excess debt, which it wants to repay to the vault.

Finally, the function updates the strategy's total debt. Total realized gain and last report timestamp. Based on the net difference between the reported gains and the debt payment, it transfers the appropriate amount of underlying assets between the vault and the strategy.

The updated debt outstanding for the strategy is returned.

Parameters:

realizedGain: The realized gain reported by the strategy.

unrealizedGain: The unrealized gain reported by the strategy.

loss: The loss reported by the strategy.

debtPayment: The amount of debt paid back by the strategy.

managementFeeReceiver: The address that will receive the management fee.

Returns:

The updated debt outstanding for the strategy.


// Some code

Last updated