Core Primitives
Core Primitives are the cross-protocol execution dispatchers that have been the recommended DeFiPy interface since v1.0. They handle pool initialization, swapping, liquidity management, and quoting across Uniswap V2/V3, Balancer, and Stableswap through a single abstract interface.
These primitives mutate pool state — they are execution operations (mint, burn, swap, zap). For read-only analytics (position analysis, risk assessment, price scenarios), see The Primitive Interface.
Availability
| Operation | Uniswap V2 | Uniswap V3 | Balancer | Stableswap |
|---|---|---|---|---|
Join() | ✅ | ✅ | ✅ | ✅ |
Swap() | ✅ | ✅ | ✅ | ✅ |
AddLiquidity() | ✅ | ✅ | ✅ | ✅ |
RemoveLiquidity() | ✅ | ✅ | ✅ | ✅ |
SwapDeposit() | ✅ | ✅ | ❌ | ❌ |
WithdrawSwap() | ✅ | ✅ | ❌ | ❌ |
LPQuote() | ✅ | ✅ | 🔜 | 🔜 |
Operation descriptions
Join()— Initialize a pool with starting liquidity. V2/V3 take token amounts (amount0,amount1); V3 also requireslwr_tickandupr_tick. Balancer takes apool_sharesquantity against an already-funded vault. Stableswap takes the amplification coefficientA.Swap()— Exchange one token for another. V2/V3 are binary pools —token_inis enough to determine the output. Balancer and Stableswap may haveN ≥ 2assets, so bothtoken_inandtoken_outmust be named.AddLiquidity()— Single-token-in deposit into an initialized pool. V2/V3 auto-balance the matching second-token amount vialp.quote(...)at the current price. Balancer accepts akindconstructor flag for token-amount vs target-shares semantics. Stableswap takes a token amount and absorbs the imbalance against the invariant.RemoveLiquidity()— Burn LP shares. V2/V3 return both tokens at the pool’s current ratio (useWithdrawSwap()for single-asset exits). Balancer and Stableswap return only the specifiedtoken_outbecause the invariant absorbs the imbalance.SwapDeposit()— V2/V3 single-asset zap-in. Solves for the optimal swap fraction (closed-form on V2, scipy.optimize on V3), swaps that portion, then deposits both balances. Balancer/Stableswap don’t need this primitive — theirAddLiquidity()is already single-asset.WithdrawSwap()— V2/V3 single-asset zap-out. Burns the optimal LP fraction, then swaps the unwanted side back intotoken_out. Balancer/Stableswap don’t need this primitive — theirRemoveLiquidity()already takestoken_out.LPQuote()— Read-only quoting class with named methods (get_price,get_reserve,get_amount,get_amount_from_lp,get_lp_from_amount,get_liquidity,get_opposing_token). Recommended cross-protocol entry point for state reads —lp.get_amount_outandlp.get_reserveare V2-specific and raiseAttributeErroron V3, butLPQuotedispatches correctly. V2/V3 today; Balancer/Stableswap planned for v2.1 (add-only).
Relationship to other sections
Core Primitives and The Primitive Interface are both abstract — they dispatch across protocols without the caller knowing protocol details. The distinction is what they do:
- Core Primitives mutate pool state (execution: mint, burn, swap, zap)
- Agentic Primitives read pool state and return analytics (position analysis, risk, scenarios)
For the protocol-specific exchange classes underneath both layers, see Protocol API → Uniswap V2 and siblings.
For the full Core API reference — class signatures, parameters, and runnable examples — see Core API.