Skip to content

LPQuote

class defipy.cpt.quote.LPQuote(include_fee=True)

Read-only quoting. LPQuote answers the questions a UI or analytics layer asks before executing a swap or computing a position value: what’s the spot price, what would I receive for this swap, what’s my LP balance worth, what would 1 token mint in LP tokens?

LPQuote is the nucleus of cross-protocol routing in DeFiPy. Every meaningful read — spot price, reserve access, LP↔token conversion, trade simulation — ultimately routes through it. When other primitives need a fee-aware quote, they call LPQuote rather than pool.get_amount_out directly, because the latter is V2-specific.

Available on Uniswap V2 and Uniswap V3. Balancer and Stableswap support is in development.

NameTypeDefaultDescription
include_feeboolTrueWhether returned amounts account for the pool’s swap fee. Set False for fee-free invariant reads (e.g. when computing IL with a fee-free spot ratio, the way BalancerImpLoss does internally).
price = LPQuote().get_price(pool, token_in)

Spot price of token_in denominated in the other token of the pair. Polymorphic across V2 and V3 — returns the raw reserve ratio, no fee applied.

ParameterTypeDescription
poolexchangePool to read.
token_inERC20The token whose price you want.

Returns: float — units of the other token per unit of token_in.

amount_out = LPQuote(include_fee=True).get_amount(pool, token_in, amount_in)

Forecast the swap output for swapping amount_in of token_in into the pool. With include_fee=True (the default), returns the same value Swap would produce — useful for slippage previews.

ParameterTypeDescription
poolexchangePool to read.
token_inERC20The token being swapped in.
amount_infloatAmount of token_in.

Returns: float — the amount of the other token a user would receive.

amount = LPQuote().get_amount_from_lp(pool, token, amount_lp)

Convert an LP-token balance into a token-denominated amount. Useful for “what’s my LP position worth in ETH” displays.

The include_fee=False form returns settlement value via an internal RebaseIndexToken swap; this is scale-dependent and crashes with ZeroDivisionError on V3 at 100% pool ownership — prefer the paper-share interpretation (linear share of reserves) for analytics primitives.

ParameterTypeDescription
poolexchangePool to read.
tokenERC20The token to denominate the value in.
amount_lpfloatLP-token amount to convert.

Returns: float — equivalent amount of token.

amount_lp = LPQuote().get_lp_from_amount(pool, token, amount_tkn)

The inverse of get_amount_from_lp — convert a token amount into the equivalent LP-token amount.

ParameterTypeDescription
poolexchangePool to read.
tokenERC20The token whose amount you’re converting from.
amount_tknfloatToken amount to convert.

Returns: float — equivalent amount of LP tokens.

from defipy import *
user_nm = 'user'
eth_amount = 1000
tkn_amount = 100000
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)
Join().apply(lp, user_nm, eth_amount, tkn_amount)
p_eth = LPQuote().get_price(lp, eth)
p_tkn = LPQuote().get_price(lp, tkn)
print(f'1 ETH = {p_eth} TKN')
print(f'1 TKN = {p_tkn} ETH')
1 ETH = 100.0 TKN 1 TKN = 0.01 ETH
amt_eth = LPQuote(include_fee=True).get_amount(lp, eth, 1)
amt_tkn = LPQuote(include_fee=True).get_amount(lp, tkn, 1)
print(f'1 ETH would swap to {amt_tkn} TKN')
print(f'1 TKN would swap to {amt_eth} ETH')
1 ETH would swap to 0.009969900600091019 TKN 1 TKN would swap to 99.60069810399033 ETH
amt_eth = LPQuote(False).get_amount_from_lp(lp, eth, 1)
amt_tkn = LPQuote().get_amount_from_lp(lp, eth, 1)
print(f'1 LP token settles to {amt_eth} ETH (scale-dependent)')
print(f'1 LP token settles to {amt_tkn} TKN')
1 LP token settles to 0.19969005990701796 ETH (scale-dependent) 1 LP token settles to 19.969005990701795 TKN
amt_eth_lp = LPQuote(False).get_lp_from_amount(lp, eth, 1)
amt_tkn_lp = LPQuote(False).get_lp_from_amount(lp, tkn, 1)
print(f'1 ETH = {amt_eth_lp} LP tokens')
print(f'1 TKN = {amt_tkn_lp} LP tokens')
1 ETH = 5.00876001071831 LP tokens 1 TKN = 0.05007523748156423 LP tokens
  • LPQuote.get_amount dispatches across V2 and V3. Direct calls to pool.get_amount_out are V2-specific; V3 doesn’t expose that method on the exchange object.
  • Paper value vs. settlement value. Position-level primitives use linear-share token amounts (paper value); LPQuote(False).get_amount_from_lp returns settlement value via a synthetic exit swap. Both are valid; pick based on whether you’re answering “what would I get if I closed?” (settlement) or “what’s my proportional reserve share?” (paper).