Skip to main content

Integrating the Public Allocator

This guide provides instructions to integrate the Public Allocator with Morpho API, fetch data, and prepare transactions for reallocating liquidity using the SDK Bundler.

The original code is available in the public-allocator-scripts repository.

Quick Start

  1. Clone the repository:

    git clone https://github.com/morpho-org/public-allocator-scripts
    cd public-allocator-scripts
  2. Install dependencies:

    yarn add @morpho-org/blue-sdk@next
    yarn add @morpho-org/blue-sdk-viem@next
    yarn add @morpho-org/liquidity-sdk-viem@1.1.0
    yarn add @morpho-org/morpho-blue-bundlers@latest
    yarn add @morpho-org/morpho-ts@next
    yarn add @morpho-org/simulation-sdk@next
    yarn add viem@^2.21.54
    yarn add dotenv

    Note: npm has potential issues parsing package names containing '@' symbols. We recommend using yarn or pnpm instead.

  3. Configure environment:

    Create a .env file in the root directory:

    RPC_URL_MAINNET=your_mainnet_rpc_url_here
    RPC_URL_BASE=your_base_rpc_url_here

Implementation Overview

The SDK Bundler implementation provides a robust way to:

  • Fetch initial market metrics from the GraphQL API
  • Get current market data and liquidity state
  • Calculate utilization and required reallocation
  • Process withdrawals if needed
  • Run market simulations for impact analysis
  • Generate transaction data when reallocation is required

The script uses a two-step data fetching process for optimal performance:

  1. Initial API Check: Uses Morpho API for basic liquidity data (~1 minute delay)
  2. Detailed RPC Data: Real-time data fetched only when needed for reallocation

Performance Considerations

The implementation optimizes RPC usage through a two-step process:

  • Use the Morpho API (blue-api.morpho.org) for initial checks and UI displays
  • Only fetch detailed RPC data when preparing actual reallocations
  • Reallocatable liquidity calculations behave as implemented in the Morpho Interface, without considering the impact the reallocation might have on the other markets. One can reallocate up to 100% of the liquidity displayed in the available liquidity section.

This approach balances performance with accuracy for different use cases.

Usage

  1. Update the market parameters in your script:
const chainId = 8453 as ChainId;
const marketId =
"0x9103c3b4e834476c9a62ea009ba2c884ee42e94e6e314a26f04d312434191836" as MarketId;

// Example amounts: 20 USDC and 20M USDC
const examples = [
{ amount: 20, label: "20 USDC" },
{ amount: 20_000_000, label: "20M USDC" },
];
  1. Run the script:
npx tsx scripts/sdks/viem/sdkBundler.ts

The script will execute the reallocation flow and provide detailed results including:

  • Current market liquidity
  • API metrics
  • Simulation results (if reallocation is needed)
  • Reallocation details
  • Transaction data (if applicable)
  • Status messages

Example Output

For a small amount (20 USDC), where no reallocation is needed:

{
requestedLiquidity: 20n,
currentMarketLiquidity: 17459415476544n,
apiMetrics: {
utilization: 778587141684416512n,
currentMarketLiquidity: 17384153747403n,
// ... other metrics
},
reason: {
type: 'success',
message: 'Sufficient liquidity already available in the market, no reallocation needed'
}
}

For a larger amount (20M USDC), where reallocation is needed:

{
// ... market metrics ...
simulation: {
targetMarket: {
preReallocation: { /* ... */ },
postReallocation: { /* ... */ },
postBorrow: { /* ... */ }
},
sourceMarkets: { /* ... */ }
},
reallocation: {
withdrawals: { /* ... */ },
totalReallocated: 5032458997827n,
isLiquidityFullyMatched: true
},
rawTransaction: {
to: '0x23055618898e202386e6c13955a58D3C68200BFB',
data: '0xac9650d8...',
value: '0'
},
reason: {
type: 'success',
message: 'Successfully generated reallocation transaction'
}
}

For support, reach out on Discord.