Skip to content

Core API

Core Primitives are the cross-protocol execution dispatchers. They mutate pool state — initializing pools, swapping, adding and removing liquidity — across Uniswap V2, V3, Balancer, and Stableswap through a single abstract interface, plus one read-only quoting class (LPQuote).

For the conceptual overview and the dispatcher-design rationale, see Core Primitives under Concepts. This page is the API reference index: every public class with its protocol-divergent shape and a link to the per-class page.

ClassOne-line shapeV2V3BalancerStableswap
JoinSeed an empty pool — V2/V3 take token amounts; Balancer takes pool shares; Stableswap takes the amplification coefficient.
SwapTrade one token for another — V2/V3 only need token_in (binary pool); Balancer/Stableswap need both token_in and token_out (N-asset).
AddLiquiditySingle-token-in deposit into an initialized pool — dispatcher auto-balances V2/V3 via lp.quote; Balancer/Stableswap absorb imbalance via the invariant.
RemoveLiquidityBurn LP shares — V2/V3 return both tokens at pool ratio; Balancer/Stableswap return a single specified token_out.
SwapDepositSingle-asset zap-in — closed-form for V2, numeric solve for V3. Bal/Ssw don’t need it; their AddLiquidity is already single-asset.
WithdrawSwapSingle-asset zap-out — V2/V3 burn + auto-swap to one token. Bal/Ssw don’t need it; their RemoveLiquidity already takes token_out.
LPQuoteRead-only quoting (price, reserve, amount, LP↔token). The cross-protocol entry point for state reads — V2/V3 today, Balancer/Stableswap planned for v2.1.🔜🔜

Every Core primitive follows the DeFiPy interface — with two real exceptions worth pinning down before you start scripting.

result = ClassName().apply(lp, ...args)
  • Construction is usually stateless, but a few primitives accept mode parameters:
  • .apply() does the work. Operation arguments — pool, user, amounts, tokens — are passed to .apply(), not to the constructor.
  • Most Core primitives mutate lp. LPQuote is the read-only exception — its methods return values without touching pool state.
  • Returns are operation-specific. Some return scalars (e.g. Swap output amount); some return dicts (AddLiquidity and V2/V3 RemoveLiquidity); some return None and rely on pool.summary() for inspection.