reserve0 × reserve1 = k. Each swap pays a 0.3% fee to liquidity providers. Liquidity is represented by a fungible LP token whose token ID equals the pool ID.
Guide: swap on CPMM
Guide: provide CPMM liquidity
Pool lookups
getPoolId(tokenA, tokenB)
getPoolAddress(tokenA, tokenB)
Returns the pair’s contract address (addressFromContractId(getPoolId(...))).
poolExists(tokenA, tokenB)
getPoolState(tokenA, tokenB)
PoolNotFoundError if the pair doesn’t exist. Both tokens must be in the token list.
getPoolProtocolFees(poolAddress)
Quotes (static, offline)
All quote helpers are static and pure. Pass aCpmmPoolContractState you already fetched.
CpmmModule.computeSwapAmount(params)
InsufficientLiquidityError for exact-out when amountOut >= reserveOut.
CpmmModule.getAmountOut(state, tokenInId, amountIn) / getAmountIn(state, tokenOutId, amountOut)
The raw x·y=k formulas with the 0.3% fee:
CpmmModule.calcPriceImpact(reserve0, reserve1, tokenInId, token0Id, amountIn, amountOut)
Returns the price impact in percent.
CpmmModule.computeLiquidityAmounts(params)
- Existing pool (
poolStateset): give the amount of one side (inputType: 'TokenA'usesamountA,'TokenB'usesamountB) and it derives the other side from the reserves, plus the LP tokens you’d mint. - New pool (
poolStateundefined): bothamountAandamountBare required. LP minted =sqrt(amountA × amountB) − MINIMUM_LIQUIDITY(1000 units are locked forever). ThrowsInsufficientLiquidityErrorifsqrt(amountA × amountB) <= 1000.
{ tokenAId, tokenBId, amountA, amountB, shareAmount, sharePercentage, state? }, where sharePercentage is your share of the pool after the deposit, in percent.
CpmmModule.computeRemoveLiquidityAmounts(state, totalLiquidity, liquidityToRemove)
Token amounts returned for burning liquidityToRemove LP tokens, plus your remaining share. totalLiquidity is your LP balance. Throws if liquidityToRemove > totalLiquidity.
CpmmModule.computeClaimableAmounts(state, liquidityBalance)
The full underlying value of an LP balance: { token0, amount0, token1, amount1, ... }.
CpmmModule.minimalAmount(amount, slippage) / maximalAmount(amount, slippage)
Slippage helpers. See Amounts & slippage.
Swaps
swap(params, balances?)
SwapMinOut (exact-in) or SwapMaxIn (exact-out) through the router. Requires a signer.
Optional integrator fee, in the input token.
Optional pre-flight check: if provided and
balances.get(tokenIn) < input + fee, throws InsufficientBalanceError before signing. For ALPH, key the map by ALPH_TOKEN_ID.PriceImpactTooHighError if priceImpact >= 5.
simSwap(params)
swap would use, without sending anything. slippageBps defaults to 100n if nullish.
swapTo(params)
targetPrice and executes it. Direction is inferred automatically. Skips the price-impact check. Throws if the target equals the current price. Useful for arbitrage and for re-pegging test pools.
Liquidity
addLiquidity(params, balances?)
Desired amounts. Both must be greater than 0. Use
computeLiquidityAmounts to get a balanced pair.sender.
removeLiquidity(params)
computeClaimableAmounts(tokenAId, tokenBId, liquidityBalance)
Instance version: fetches pool state, then returns { token0, amount0, token1, amount1 } for an LP balance.
Pool creation
createPool(params)
If set, creates the pair and deposits initial liquidity in one transaction (
CreatePairAndAddLiquidity). The ratio sets the starting price. If omitted, only the pair is created (CreatePair, which attaches 1 base unit of each token).Configuration
getConfig() returns the active CpmmConfig (groupIndex, factoryId, routerId). setConfig(config) overrides it. getCpmmConfig() re-reads the bundled deployment.