Skip to main content

Public Allocator

Overview

The PublicAllocator contract is a publicly callable allocator for Morpho Vaults (formerly known as MetaMorpho). It allows for the efficient reallocation of assets within the vault, enabling borrowers or Morpho's interface to trigger reallocations and amplify the available liquidity for end users while ensuring the flow caps are respected.

Code

Public Allocator Github repository

Constants

MAX_SETTABLE_FLOW_CAP

The maximum settable flow cap, defined such that caps can always be stored on 128 bits. The actual max possible flow cap is type(uint128).max / 2, which equals 170141183460469231731687303715884105727.

Structs

FlowCaps

The FlowCaps struct is defined as follows:

struct FlowCaps {
uint128 maxIn;
uint128 maxOut;
}

It contains the maximum allowed inflow and outflow for a market.

FlowCapsConfig

The FlowCapsConfig struct is defined as follows:

struct FlowCapsConfig {
Id id;
FlowCaps caps;
}

It is used to configure the flow caps for a market.

Withdrawal

The Withdrawal struct is defined as follows:

struct Withdrawal {
MarketParams marketParams;
uint128 amount;
}

It specifies the market and the amount to withdraw.

Functions

setAdmin

function setAdmin(address vault, address newAdmin) external;

Sets newAdmin as the admin for the specified vault.

Parameters:

NameTypeDescription
vaultaddressThe address of the vault.
newAdminaddressThe new admin address.

setFee

function setFee(address vault, uint256 newFee) external;

Sets the newFee for the specified vault.

Parameters:

NameTypeDescription
vaultaddressThe address of the vault.
newFeeuint256The new fee value.

setFlowCaps

function setFlowCaps(address vault, FlowCapsConfig[] calldata config) external;

Sets the maximum inflow and outflow for the specified markets in the given vault.

Parameters:

NameTypeDescription
vaultaddressThe address of the vault.
configFlowCapsConfig[] calldataThe flow caps configuration array.

transferFee

function transferFee(address vault, address payable feeRecipient) external;

Transfers the accrued fee to the specified recipient for the given vault.

Parameters:

NameTypeDescription
vaultaddressThe address of the vault.
feeRecipientaddressThe address of the recipient.

reallocateTo

function reallocateTo(
address vault,
Withdrawal[] calldata withdrawals,
MarketParams calldata supplyMarketParams
) external payable;

Reallocates assets from a list of markets to one market in the specified vault.

Parameters:

NameTypeDescription
vaultaddressThe address of the vault.
withdrawalsWithdrawal[] calldataThe markets and amounts to withdraw.
supplyMarketParamsMarketParams calldataThe market to which total withdrawn assets are supplied.

Events

PublicWithdrawal

event PublicWithdrawal(address indexed sender, address indexed vault, Id indexed id, uint256 withdrawnAssets);

Emitted during a public reallocation for each withdrawn-from market.

PublicReallocateTo

event PublicReallocateTo(address indexed sender, address indexed vault, Id indexed supplyMarketId, uint256 suppliedAssets);

Emitted at the end of a public reallocation.

SetAdmin

event SetAdmin(address indexed sender, address indexed vault, address admin);

Emitted when the admin is set for a vault.

SetFee

event SetFee(address indexed sender, address indexed vault, uint256 fee);

Emitted when the fee is set for a vault.

TransferFee

event TransferFee(address indexed sender, address indexed vault, uint256 amount, address indexed feeRecipient);

Emitted when the fee is transferred for a vault.

SetFlowCaps

event SetFlowCaps(address indexed sender, address indexed vault, FlowCapsConfig[] config);

Emitted when the flow caps are set for a vault.

Error Handling

ErrorsLib

The ErrorsLib library defines various error messages used by the PublicAllocator contract.

  • NotAdminNorVaultOwner(): Thrown when the caller is not the admin nor the owner of the vault.
  • IncorrectFee(): Thrown when the reallocation fee given is wrong.
  • AlreadySet(): Thrown when the value is already set.
  • EmptyWithdrawals(): Thrown when withdrawals is empty.
  • InconsistentWithdrawals(): Thrown when withdrawals contains a duplicate or is not sorted.
  • DepositMarketInWithdrawals(): Thrown when the deposit market is in withdrawals.
  • MarketNotEnabled(Id id): Thrown when attempting to reallocate or set flows to non-zero values for a non-enabled market.
  • WithdrawZero(Id id): Thrown when attempting to withdraw zero of a market.
  • MaxSettableFlowCapExceeded(): Thrown when attempting to set max inflow/outflow above the MAX_SETTABLE_FLOW_CAP.
  • NotEnoughSupply(Id id): Thrown when attempting to withdraw more than the available supply of a market.
  • MaxOutflowExceeded(Id id): Thrown when attempting to withdraw more than the max outflow of a market.
  • MaxInflowExceeded(Id id): Thrown when attempting to supply more than the max inflow of a market.

Interfaces

IPublicAllocatorBase

Defines the basic interface for the PublicAllocator.

IPublicAllocatorStaticTyping

Inherits from IPublicAllocatorBase and includes a function to get flow caps.

IPublicAllocator

The primary interface for the PublicAllocator.

Conclusion

The PublicAllocator contract is a crucial component for managing asset reallocations within Morpho Vaults, ensuring efficient liquidity management while adhering to set flow caps.