Get liquidity depth
MetricOmmPoolDataProvider returns a pool's depth ladder: every bin from the current one outward, with fee-adjusted prices and exact cumulative amounts. Use it to build liquidity curves for routing instead of simulating one swap per size.
- One call per pool per block replaces many
simulateSwapAndRevertcalls. - Prices match execution. Cumulative amounts come from the pool's own
simulateSwapAndRevert, not re-derived swap math. eth_callonly. Both functions are non-viewin the ABI because they simulate internally. Never send them as a transaction or call them from a contract.
> On Ethereum, use the API instead. Ethereum pools receive their price update in the same block as the trade, so between trades an onchain read can be stale or revert. The bid and ask endpoints return the same per-bin depth, simulated against the pending update. On other chains the onchain ladder is authoritative.
Functions
// Live: reads the pool's active Price Provider
function getLiquidityDepthLive(address pool, uint8 maxBinsPerSide)
external returns (LiquidityDepth memory depth);
// Hypothetical: caller-supplied provider quote (raw Q64.64, same rules as Get quote)
function getLiquidityDepthHypothetical(
address pool,
uint8 maxBinsPerSide,
uint128 bidPriceX64,
uint128 askPriceX64,
uint128 referencePriceX64
) external returns (LiquidityDepth memory depth);maxBinsPerSide: bins to walk beyond the current bin on each side. Each side returns up tomaxBinsPerSide + 1rows, clamped to the pool's lowest and highest bin. Must be non-zero.- Leave
fromempty in theeth_call. The Live function reads the Price Provider outside a swap, and restricted providers (Chainlink, Pyth) refuse that read unlesstx.originis zero, reverting with no data.
What it returns
struct LiquidityDepth {
uint128 oracleBidX64; // provider quote used for the snapshot
uint128 oracleAskX64;
uint128 oracleReferenceX64;
uint128 effectiveCurrentBidX64; // fee-adjusted sell price at the exact current position (0 if no bids)
uint128 effectiveCurrentAskX64; // fee-adjusted buy price at the exact current position (0 if no asks)
DepthLevel[] asks; // buy token0: current bin upward
DepthLevel[] bids; // sell token0: current bin downward
}
struct DepthLevel {
int16 binIdx;
uint256 lowerEffPriceX64; // fee-adjusted execution price at the bin's lower bound
uint256 upperEffPriceX64; // fee-adjusted execution price at the bin's upper bound
uint256 amountAvailableInBin; // amount in the bin per pool state (partial for the current bin)
uint256 amountTradeableInBin; // amount reachable once extension gates apply
uint256 cumulativeOut; // total received through this bin (token0 for asks, token1 for bids)
uint256 cumulativeIn; // total paid through this bin (token1 for asks, token0 for bids)
}- Prices are Q64.64, token1 per token0. Amounts are in each token's native decimals.
cumulativeOutis exact at each bin boundary. For a size inside a bin, interpolate between the bin'slowerEffPriceX64andupperEffPriceX64.cumulativeInis exact on the rows the ladder simulated on their own: a side's last funded row, and the row where an extension stops the side. On other filled rows it can be0, because the ladder simulates the side as a whole rather than one bin at a time. Treat a0beside a non-zerocumulativeOutas "not computed", and derive the input from the bin prices instead.- Extension gates. If an extension such as StopLoss rejects part of a bin, the ladder finds the largest amount that still succeeds, sets
amountTradeableInBinbelowamountAvailableInBinon that row, and stops. - Sender context. Depth is simulated with the data provider as sender and recipient, with empty extension data. For pools whose extensions gate on the caller, confirm your final size with a Live quote using your router as
sender. - Invalid prices. Provider prices that fail the pool's own validation (
bid == ask, the asymmetry bound) return empty ladders instead of reverting.
Example
cast call <dataProviderAddress> \ "getLiquidityDepthLive(address,uint8)((uint128,uint128,uint128,uint128,uint128,(int16,uint256,uint256,uint256,uint256,uint256,uint256)[],(int16,uint256,uint256,uint256,uint256,uint256,uint256)[]))" \ <poolAddress> 8 \ --rpc-url $RPC_URL
No --from. The data provider address is on Deployments and in each pool's dataProviderAddress from the Metadata endpoint.
Gas and caching
The ladder runs one simulation per side, plus a binary search over the side's bins when an extension cuts it off. With maxBinsPerSide = 8, a call typically uses about 1 million gas, and up to about 3.5 million on the heaviest pools.
- Keep
maxBinsPerSidemodest (for example 5 to 10) and raise it only for pools whose curve you need deeper. Very large values can hit your RPC'seth_callgas cap. - Rebuild once per block. The snapshot is tied to the current provider quote and pool state.
Reading raw bin state
Each bin is one 32-byte storage word, so you can also cache raw state cheaply and rebuild only the bins that changed:
token0BalanceScaled (uint104)token1BalanceScaled (uint104)lengthE6 (bin width, uint16)addFeeBuyE6 (uint16)addFeeSellE6 (uint16)Balances are in the pool's scaled internal units. Read them with the data provider's binState(pool, binIdx), or binStates(pool, int16[] binIdxs) for many bins in one call. Read the current bin and position with slot0(pool). Raw state tells you where liquidity sits; execution prices still come from the ladder or a quote.
Batching several pools
The data provider takes one pool per call. To read many pools at once:
- JSON-RPC batch (recommended). Send an array of
eth_callrequests in one HTTP request, all pinned to the same block number instead oflatestso every ladder comes from the same state. Each call gets its own gas cap, and one failing pool doesn't affect the others. - Multicall3
aggregate3. Oneeth_callto Multicall3 (0xcA11bde05977b3631167028862bE2a173976CA11) with one entry per pool andallowFailure = true. Gas adds up across pools (typically under 1 million per pool atmaxBinsPerSide = 4), so split large batches to stay under your RPC'seth_callgas cap.
Leave from empty either way.
Compared with quoting each size
The ladder runs the same simulateSwapAndRevert as a quote, so extensions are applied. It differs from quoting each size yourself in a few ways:
- Fixed sender context. Depth is simulated with the data provider as sender and recipient and empty extension data. Extensions that gate on the caller, recipient or extension data can behave differently for your router.
- Exact only at bin boundaries. For sizes inside a bin, you interpolate between its bounds.
- Exact-output simulation. Cumulative amounts come from exact-output simulations. Exact-input routing may differ by rounding.
- Extension cutoffs are searched. When an extension rejects part of a bin, the ladder binary-searches the largest amount that still succeeds (up to 20 probes) and stops, assuming larger amounts keep failing.
- No explicit pause signal. Unlike the quoter's
PoolPausederror, a paused pool is not flagged. CheckpauseLevelfromslot0(pool), or skip pools that return empty ladders.
Possible pattern: build your routing curve from the ladder, then confirm the final routed size with one Live quote using your router as sender.
Related: Get quote · Contract addresses · Bid and ask API
Last updated 29 days ago
Relatick