Skip to content

Uniswap V2 API

Protocol-specific classes for Uniswap V2 — the constant-product (x · y = k) AMM. These live in uniswappy and are imported into DeFiPy’s namespace at install time.

When working through DeFiPy’s Core API, you typically don’t touch these classes directly — Join, Swap, AddLiquidity, etc., dispatch to them under the hood. You’ll reach for them when you need to construct a pool from scratch (the factory.deploy(exchg_data) pattern), inspect protocol-specific state, or extend the library.

ClassModulePurpose
UniswapFactoryuniswappy.cpt.factoryDeploys new pools. The Join operation runs against pool instances returned by factory.deploy(exchg_data).
UniswapExchangeuniswappy.cpt.exchgThe pool itself — holds reserves, fee accumulators, LP-token supply, and the per-user LP-balance ledger.
UniswapExchangeDatauniswappy.cpt.exchgConfiguration object passed to the factory: token pair, symbol, address.
ClassModulePurpose
ERC20uniswappy.ercTest token type used throughout DeFiPy. Constructor takes a name and address; instances are passed by reference to swap/deposit ops.
LPERC20uniswappy.ercLP-token type. Returned by factory.deploy; can itself be used as a base token in a layered pool.
ModulePurpose
uniswappy.cpt.indexRebaseIndexToken, SettlementLPToken — the inverse-token primitives DeFiPy uses for paper-vs-settlement value conversion.
uniswappy.cpt.quoteLPQuote, IndexTokenQuote — fee-aware and fee-free quoting. The DeFiPy LPQuote page documents the full surface.
uniswappy.analytics.riskUniswapImpLoss — V2 IL math used by AnalyzePosition and SimulatePriceMove.
from defipy import *
tkn = ERC20("TKN", "0x111")
eth = ERC20("ETH", "0x09")
exchg_data = UniswapExchangeData(tkn0 = eth, tkn1 = tkn, symbol = "LP", address = "0x011")
factory = UniswapFactory("ETH pool factory", "0x2")
lp = factory.deploy(exchg_data)
# lp is a UniswapExchange instance, ready for Join().apply(...)
  • The fee is hard-coded at 30 bps. UniswapExchange has no .fee attribute — the multiplier 997/1000 is baked into get_amount_out0/1. This is correct for canonical V2; non-30-bps V2 forks would need a different exchange class.
  • First-mint sentinel. V2’s MINIMUM_LIQUIDITY is burned to address "0" on first Join. Filter out the "0" entry when iterating LP holders.
  • token0 / token1 are bare strings. Not full ERC20 objects. The full ERC20 instance lives at factory.token_from_exchange[lp.name][lp.token0].