BaseStrategy

The BaseStrategy contract serves as the foundation for all strategies in the MaxAPY Protocol. It defines the core functionality and interface strategies that must be adhered to when interacting with the MaxAPY Vault and external protocols. The main purpose of this contract is to provide a standardized way for strategies to manage funds, interact with the vault, and report their performance.

The BaseStrategy contract includes functions for depositing and withdrawing funds, adjusting positions, liquidating assets, and preparing returns. It also defines the roles and access control mechanisms to ensure that only authorized entities can perform specific actions within the strategy.

By providing a common interface and standardized functionality, the BaseStrategy contract enables the MaxAPY Protocol to integrate with various strategies seamlessly, promoting modularity, standardization, and security. The BaseStrategy was developed, taking into consideration the Yearn v2 base strategy.

Functions

Initialize()

function __BaseStrategy_init(
    IMaxApyVaultV2 _vault,
    address[] calldata _keepers,
    bytes32 _strategyName,
    address _strategist
) internal virtual onlyInitializing

Initializes a new Strategy with the specified vault, keepers, name, and strategist.

Parameters:

_vault: The address of the MaxApy Vault associated with the strategy

_keepers: The addresses of the keepers to be granted the keeper role

_strategyName: The name of the strategy

_strategist: The address of the strategist


withdrawn()

function withdraw(uint256 amountNeeded) external virtual checkRoles(VAULT_ROLE) returns (uint256 loss)

Tries to withdraw amountNeeded to vault. This may only be called by the respective Vault.

Parameters:

amountNeeded: How much underlyingAsset to withdraw

Returns:

loss: Any realized losses


requestWithdraw()

function requestWithdraw(uint256 amountNeeded) external virtual checkRoles(VAULT_ROLE) returns (uint256 loss)

Withdraws exactly amountNeeded to vault. This may only be called by the respective Vault.

Parameters:

amountNeeded: How much underlyingAsset to withdraw

Returns:

loss: Any realized losses


harvest()

function harvest(
    uint256 minExpectedBalance,
    uint256 minOutputAfterInvestment,
    uint256 harvestedProfitBPS,
    address harvester
) external checkRoles(KEEPER_ROLE)

Harvests the Strategy, but in this case, a percentage of the profit (if any) is reinvested. In the rare case of an emergency shutdown, this will exit the Strategy's position.

Parameters:

minExpectedBalance: Minimum balance amount of underlyingAsset expected after performing any strategy unwinding

minOutputAfterInvestment: Minimum expected output after _invest()

harvestedProfitBPS: Percentage of the profit to be realized and sent to the vault as net profit

harvester: Only relevant when the harvest is triggered from the vault, is the address of the user that is enduring the harvest gas cost from the vault and will receive the management fees in return


setEmergencyExit()

function setEmergencyExit(uint256 _emergencyExit) external checkRoles(ADMIN_ROLE)

Sets the strategy in emergency exit mode.

Parameters:

_emergencyExit: The new emergency exit value: 1 for inactive, 2 for active


setStrategist()

function setStrategist(address _newStrategist) external checkRoles(ADMIN_ROLE)

Sets the strategy's new strategist.

Parameters:

_newStrategist: The new strategist address


setAutopilot()

function setAutopilot(bool _autoPilot) external checkRoles(ADMIN_ROLE)

Sets the strategy in autopilot mode, meaning that it will be automatically harvested from the vault using the strategy.

Parameters:

_autoPilot: The new autopilot status: true for active, false for inactive


_adjustPosition()

function _adjustPosition(uint256 debtOutstanding, uint256 minOutputAfterInvestment) internal virtual

Performs any adjustments to this Strategy's core position(s) given what change the MaxApy Vault made in the "investable capital" available to the Strategy.

Parameters:

debtOutstanding: Total principal + interest of debt yet to be paid back

minOutputAfterInvestment: Minimum expected output after _invest() (designated in receipt tokens obtained after depositing in a third-party protocol)


_liquidatePosition()

function _liquidatePosition(uint256 amountNeeded)
    internal
    virtual
    returns (uint256 liquidatedAmount, uint256 loss)

Liquidates up to amountNeeded of MaxApy Vault's underlyingAsset of this strategy's positions, regardless of slippage. Any excess will be re-invested with _adjustPosition().

Parameters:

amountNeeded: Amount of MaxApy Vault's underlyingAsset needed to be liquidated

Returns:

liquidatedAmount: The actual liquidated amount

loss: Difference between the expected amount needed to reach amountNeeded and the actual liquidated amount


_liquidateAllPositions()

function _liquidateAllPositions() internal virtual returns (uint256 amountFreed)

Liquidates everything and returns the amount that got freed. This function is used during emergency exit instead of _prepareReturn() liquidating all of the Strategy's positions back to the MaxApy Vault.

Returns:

amountFreed: The amount of underlying assets freed


_prepareReturn()

function _prepareReturn(uint256 debtOutstanding, uint256 minExpectedBalance, uint256 harvestedProfitBPS)
    internal
    virtual
    returns (uint256 realizedProfit, uint256 unrealizedProfit, uint256 loss, uint256 debtPayment)

Performs any Strategy unwinding or other calls necessary to capture the "free return" this Strategy has generated since the last time its core position(s) were adjusted.

Parameters:

debtOutstanding: Outstanding debt of the strategy

minExpectedBalance: Minimum expected balance of underlying assets after unwinding

harvestedProfitBPS: Percentage of profits to be harvested (in basis points)

Returns:

realizedProfit: The realized profit

unrealizedProfit: The unrealized profit

loss: The realized losses

debtPayment: The amount of debt to be paid back to the vault


_underlyingBalance()

function _underlyingBalance() internal view returns (uint256)

Returns the current strategy's balance in underlying tokens.

Returns:

The strategy's balance of underlying tokens


_estimatedTotalAssets()

function _estimatedTotalAssets() internal view virtual returns (uint256)

Returns the real-time estimation of the value in assets held by the strategy.

Returns:

The strategy's total assets (idle + investment positions)


unharvestedAmount()

function unharvestedAmount() external view virtual returns (int256)

Returns the real gain/loss from the last harvest.

Returns:

The difference between the current estimated total assets and the last estimated total assets


estimatedTotalAssets()

function estimatedTotalAssets() public view returns (uint256)

Provides an accurate estimate of the total amount of assets (principal + return) that this Strategy is currently managing, denominated in terms of underlyingAsset tokens.

Returns:

The estimated total assets in this Strategy


isActive()

function isActive() public view returns (bool)

Indicates whether this strategy is currently "active" in that it is managing an active position or will manage a position in the future.

Returns:

True if the strategy is actively managing a position


previewLiquidate()

function previewLiquidate(uint256 requestedAmount) public view virtual returns (uint256 liquidatedAmount)

Calculates the estimated real output of a withdrawal (including losses) for a given requested amount. This function is meant to be called from the vault.

Parameters:

requestedAmount: The requested amount of underlying assets to withdraw

Returns:

liquidatedAmount: The estimated output in assets


previewLiquidateExact()

function previewLiquidateExact(uint256 liquidatedAmount) public view virtual returns (uint256 requestedAmount)

Calculates the estimated requested amount the vault has to request from this strategy actually to get the specified liquidated amount of assets when calling previewWithdraw. This function is meant to be called from the vault.

Parameters:

liquidatedAmount: The desired liquidated amount of assets

Returns:

requestedAmount: The estimated requested amount


maxRequest()

function maxRequest() public view virtual returns (uint256)

Returns the maximum amount of assets that the strategy can liquidate before realizing losses.

Returns:

The maximum amount of assets that can be requested


maxWithdrawn()

function maxWithdraw() public view virtual returns (uint256)

Returns the maximum amount of assets that the strategy can withdraw after losses.

Returns:

The maximum amount of assets that can be withdrawn

Last updated