Math Utilities
Pure helpers for curve quotes, AMM quotes, LP math, and reserve-derived checks.
SDK
Raw math helpers
These helpers are for simulations, quoting, bots, and tests. They do not fetch accounts for you.
Initialize a fresh curve state
Start with newBondingCurve(global) when you need the starting reserve state implied by the current global config.
const curve = newBondingCurve(global);This is useful in:
- simulations
- tests
- tooling that needs to reason about a not-yet-created token
Estimate current curve market cap
Estimate market cap directly from reserves and supply:
const marketCap = bondingCurveMarketCap({
mintSupply: bondingCurve.tokenTotalSupply,
virtualGorReserves: bondingCurve.virtualGorReserves,
virtualTokenReserves: bondingCurve.virtualTokenReserves,
});This is useful for:
- fee-tier checks
- analytics
- dashboards or monitoring that need a reserve-derived market cap
Detect migrated curves
isMigrated(bondingCurve) distinguishes between:
- a completed curve that still has reserves, and
- a curve that already migrated and reset its reserves
const migrated = isMigrated(bondingCurve);Quote bonding-curve trades
These helpers answer three different questions. Choose the one that matches your workflow.
1. Spend a known amount of GOR and estimate token output
const tokenAmountOut = getBuyTokenAmountFromGorAmount({
feeConfig,
bondingCurve,
amount: gorAmount,
});This matches the case:
- "I want to spend 250 GOR"
2. Target an exact token output and estimate GOR cost
const gorCost = getBuyGorAmountFromTokenAmount({
feeConfig,
bondingCurve,
amount: tokenAmountOut,
});This matches the case:
- "I want exactly 1,000,000 base units"
3. Sell a known token amount and estimate GOR back
const gorOut = getSellGorAmountFromTokenAmount({
feeConfig,
bondingCurve,
amount: tokenAmountIn,
});This matches the case:
- "I want to sell 5,000,000 base units"
Prefer previews when you need more than one number back
If you need fees, price impact, and post-trade reserves too, use client.preview.curveBuy or client.preview.curveSell instead of the raw math helpers.
Quote DumpsterSwap trades
These helpers work directly from pool reserves.
Buy exact output
getAmmBuyQuote(baseReserves, quoteReserves, baseOut) returns the quote input required for a target base output.
const quoteIn = getAmmBuyQuote(
baseReserves,
quoteReserves,
baseAmountOut
);Sell exact input
getAmmSellQuote(baseReserves, quoteReserves, baseIn) returns the quote output for a known base input.
const quoteOut = getAmmSellQuote(
baseReserves,
quoteReserves,
baseAmountIn
);Prefer previews for fee-aware AMM numbers
The raw AMM quote helpers do not attach protocol, creator, or LP fees. client.preview.swapBuy and client.preview.swapSell return the full execution shape.
LP math
Initial LP supply
calculateInitialLp(baseIn, quoteIn) is useful when previewing pool creation.
const lpSupply = calculateInitialLp(baseIn, quoteIn);Deposit amounts
calculateDepositAmounts(lpOut, baseReserves, quoteReserves, lpSupply) is useful when a depositor targets a specific LP token output.
const { baseIn, quoteIn } = calculateDepositAmounts(
lpOut,
baseReserves,
quoteReserves,
lpSupply
);Withdraw amounts
calculateWithdrawAmounts(lpIn, baseReserves, quoteReserves, lpSupply) is useful when a user burns LP and wants the underlying token outputs.
const { baseOut, quoteOut } = calculateWithdrawAmounts(
lpIn,
baseReserves,
quoteReserves,
lpSupply
);MINIMUM_LIQUIDITY is the locked floor used during initial pool creation.