Triggering the Public Allocator
This tutorial explains how to trigger a reallocation using the Public Allocator to provide just-in-time liquidity for a borrow transaction.
Before you begin, make sure you understand the core concepts behind the Public Allocator and why it's a powerful tool for enhancing liquidity.
Review the Public Allocator Concept Page first.The Core Action: reallocateTo
The central function of the Public Allocator is reallocateTo
. This function pulls liquidity from a specified list of source markets within a vault and supplies it to a single destination market.
function reallocateTo(
address vault,
Withdrawal[] calldata withdrawals,
MarketParams calldata supplyMarketParams
) external payable;
vault
: The vault whose funds are being reallocated.withdrawals
: An array of structs, each specifying a source market and the amount to withdraw.supplyMarketParams
: The destination market where all withdrawn funds will be deposited.
Best Practice: Bundle Reallocation with Borrow
For the best user experience, the reallocateTo
call should be bundled with the user's borrow
call in a single atomic transaction using a Morpho Bundler.
This ensures that if the reallocation fails for any reason (e.g., insufficient maxOut
on a source market), the user's borrow transaction also reverts, preventing confusion or failed transactions.
Method 1: Manual Trigger via Etherscan (For Learning)
Manually triggering the reallocateTo
function on a block explorer is a great way to understand its inputs.
1. Check Prerequisites
Before sending a transaction, verify:
- The Public Allocator has the allocator role for the target vault.
- The source markets have sufficient
maxOut
capacity and the destination market has sufficientmaxIn
capacity. - You have the correct
marketId
for all source markets and themarketParams
for the destination market. - You know the
fee
required for the transaction, which can be read from thefee(vaultAddress)
view function on the Public Allocator contract.
2. Structure the withdrawals
Array
The withdrawals
array must be sorted in ascending order by market ID.
3. Execute the Transaction
On a block explorer like Etherscan, navigate to the Public Allocator contract, connect your wallet, and call reallocateTo
. You must send the required fee
as the value
of the transaction.
Example Input: This example reallocates 70 WETH from an Idle Market and 800 WETH from a wstETH market, supplying a total of 870 WETH to an rETH market.
// Payable Amount (Value): [Fee in ETH, e.g., 0.001]
{
"vault": "0x38989bba00bdf8181f4082995b3deae96163ac5d",
"withdrawals": [
// Withdrawal from Idle Market (assuming it has the lower marketId)
{
"marketParams": { "loanToken": "0xc02...", "collateralToken": "0x000...", ... },
"amount": "70000000000000000000"
},
// Withdrawal from wstETH/WETH market
{
"marketParams": { "loanToken": "0xc02...", "collateralToken": "0x7f3...", ... },
"amount": "800000000000000000000"
}
],
"supplyMarketParams": {
"loanToken": "0xc02...",
"collateralToken": "0xae7...",
...
}
}
After this transaction, the rETH market will have an additional 870 WETH of liquidity available to borrow.
Method 2: Programmatic Trigger (SDK & API)
For a dApp integration, you will programmatically construct and send this transaction. The recommended flow is to use the Morpho API to find available liquidity and the SDK to simulate and execute.
The general logic is:
- Check Target Market Liquidity: See if the user's borrow can be fulfilled without reallocation.
- Query Available Liquidity: If not, use the Morpho API to query the
publicAllocatorSharedLiquidity
for the target market. This returns a list of all markets in all vaults that can reallocate funds to your target. - Simulate and Build: Select the necessary source markets from the API response and construct the
withdrawals
array. The Morpho SDKs are ideal for simulating the transaction to ensure it will succeed and to calculate the precise inputs. - Bundle and Execute: Use the Morpho Bundler to combine the
reallocateTo
call and the user'sborrow
call into a single transaction.