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.
Class index
Section titled “Class index”| Class | One-line shape | V2 | V3 | Balancer | Stableswap |
|---|---|---|---|---|---|
Join | Seed an empty pool — V2/V3 take token amounts; Balancer takes pool shares; Stableswap takes the amplification coefficient. | ✅ | ✅ | ✅ | ✅ |
Swap | Trade one token for another — V2/V3 only need token_in (binary pool); Balancer/Stableswap need both token_in and token_out (N-asset). | ✅ | ✅ | ✅ | ✅ |
AddLiquidity | Single-token-in deposit into an initialized pool — dispatcher auto-balances V2/V3 via lp.quote; Balancer/Stableswap absorb imbalance via the invariant. | ✅ | ✅ | ✅ | ✅ |
RemoveLiquidity | Burn LP shares — V2/V3 return both tokens at pool ratio; Balancer/Stableswap return a single specified token_out. | ✅ | ✅ | ✅ | ✅ |
SwapDeposit | Single-asset zap-in — closed-form for V2, numeric solve for V3. Bal/Ssw don’t need it; their AddLiquidity is already single-asset. | ✅ | ✅ | ❌ | ❌ |
WithdrawSwap | Single-asset zap-out — V2/V3 burn + auto-swap to one token. Bal/Ssw don’t need it; their RemoveLiquidity already takes token_out. | ✅ | ✅ | ❌ | ❌ |
LPQuote | Read-only quoting (price, reserve, amount, LP↔token). The cross-protocol entry point for state reads — V2/V3 today, Balancer/Stableswap planned for v2.1. | ✅ | ✅ | 🔜 | 🔜 |
Common interface
Section titled “Common interface”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:
Swap(kind=Proc.SWAPIN | Proc.SWAPOUT)on Balancer toggles exact-output vs exact-input.AddLiquidity(kind=Proc.ADDTKN | Proc.ADDSHARES)on Balancer toggles deposit-by-token-amount vs deposit-by-target-shares.RemoveLiquidity(kind=Proc.REMOVETKN | Proc.REMOVESHARES)on Balancer toggles withdraw-by-token-amount vs withdraw-by-shares.LPQuote(quote_opposing=True, include_fee=False)toggles passthrough vs opposing-token output and paper-value vs settlement-value.
.apply()does the work. Operation arguments — pool, user, amounts, tokens — are passed to.apply(), not to the constructor.- Most Core primitives mutate
lp.LPQuoteis the read-only exception — its methods return values without touching pool state. - Returns are operation-specific. Some return scalars (e.g.
Swapoutput amount); some return dicts (AddLiquidityand V2/V3RemoveLiquidity); some returnNoneand rely onpool.summary()for inspection.
See also
Section titled “See also”- Core Primitives — narrative / conceptual overview
- The Primitive Interface — cross-cutting invariants for every dispatcher
- Tutorials → Uniswap V2 — end-to-end runnable walkthrough of every Core primitive
- Protocol API — protocol-specific factories and exchange classes that Core primitives dispatch to