Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.geode.ag/llms.txt

Use this file to discover all available pages before exploring further.

Overview

ClearingPriceLib is a pure Solidity library that computes the uniform clearing price, determines which intents fill, calculates internal match amounts, and computes the residual to route through the AMM. This library is used for standard mode and post-graduation pools. Launch-mode pools use ConstantProductCurveLib instead.

Core Function

computeClearingPrice()

function computeClearingPrice(
    GeodeIntent[] memory buys,
    GeodeIntent[] memory sells,
    uint256 maxBatchSize,
    uint256 ammSpotPriceQ128,
    uint256 settlementFeeBps
) internal pure returns (
    SettlementResult memory result,
    bool[] memory buyFills,
    bool[] memory sellFills
)
Parameters:
  • buys — Buy intents, sorted by implicit limit price descending (most generous first)
  • sells — Sell intents, sorted by implicit limit price ascending (cheapest first)
  • maxBatchSize — Maximum allowed intents per side
  • ammSpotPriceQ128 — Current AMM spot price in Q128 fixed-point
  • settlementFeeBps — Fee charged on each filled intent’s input (in basis points)
Returns: SettlementResult with exact deposit-denominated quantities, plus boolean fill arrays.

Algorithm

1

Enforce Batch Size

Revert if either side exceeds maxBatchSize (default: 128, absolute max: 256).
2

Verify Sort Order

Buy intents must be sorted by descending limit price. Sell intents must be sorted by ascending limit price. Reverts with BuysNotSorted() or SellsNotSorted() if violated.Sort verification uses cross-multiplication to avoid division:
// Buys: buys[i-1].limitPrice >= buys[i].limitPrice
buys[i-1].amountIn * buys[i].minAmountOut >= buys[i].amountIn * buys[i-1].minAmountOut
3

Set Clearing Price

Clearing price = AMM spot price (v1 simplification).
uint256 clearingPriceQ128 = ammSpotPriceQ128;
4

Determine Fills

For each intent, compute its implicit limit price and compare against the clearing price:
// Buy fills if: amountIn/minAmountOut >= clearingPrice
buyLimitQ128 = (buys[i].amountIn * Q128) / buys[i].minAmountOut;
buyFills[i] = (buyLimitQ128 >= clearingPriceQ128);

// Sell fills if: minAmountOut/amountIn <= clearingPrice
sellLimitQ128 = (sells[i].minAmountOut * Q128) / sells[i].amountIn;
sellFills[i] = (sellLimitQ128 <= clearingPriceQ128);
Intents that don’t meet the clearing price are skipped — their tokens are never pulled.
5

Compute Fees

Settlement fees are deducted from each filled intent’s input before any routing:
buyFeesAmt  = (totalBuyInput  * settlementFeeBps) / BPS_DENOMINATOR;
sellFeesAmt = (totalSellInput * settlementFeeBps) / BPS_DENOMINATOR;

netBuyInput  = totalBuyInput  - buyFeesAmt;
netSellInput = totalSellInput - sellFeesAmt;
6

Compute Internal Match

Convert net sell volume to buyer-side units and determine the match:
sellVolInQuote = (netSellInput * clearingPriceQ128) / Q128;

if (netBuyInput >= sellVolInQuote) {
    // Net buy pressure: all sellers matched
    internalMatch0 = sellVolInQuote;
    internalMatch1 = netSellInput;
    residualSwapAmount = netBuyInput - sellVolInQuote;
    residualZeroForOne = true;  // excess buys → AMM
} else {
    // Net sell pressure: all buyers matched
    internalMatch0 = netBuyInput;
    internalMatch1 = (netBuyInput * Q128) / clearingPriceQ128;
    residualSwapAmount = netSellInput - internalMatch1;
    residualZeroForOne = false; // excess sells → AMM
}

Key Properties

Pure Function

No state reads or writes. The entire computation is deterministic from its inputs.

Deposit-Denominated

All output quantities are in exact deposit units. No predictions about AMM output — the hook captures actual swap deltas at execution time.

Input-Side Fees

Fees are carved from gross input before matching. Net input drives all routing decisions.

Settler Competition

A malicious settler can omit intents to manipulate the clearing price. The mitigation is competition — any settler can submit a more complete batch. First valid settlement wins.

Source

ClearingPriceLib.sol

View the full source code on GitHub (~143 lines).