DEX Quoting: V2 Constant Product vs. V3 Concentrated Liquidity

🌐 한국어

The heart of a DEX bot is the quoting logic that answers "if I put this much in, how much comes out?" — precisely. The trouble is that every protocol computes it differently. UniswapV2-style pools and V3-style (concentrated liquidity) pools are fundamentally different animals. Miss that distinction and your quotes drift, and drifted quotes are losses.

V2 — The Constant Product Formula (x * y = k)

UniswapV2-style pools run on one rule: the product of the two tokens' reserves stays constant. Put token X in and its reserve grows, so token Y comes out to keep k intact. The quoting formula, fee included (0.3%, say), is cheap enough to compute even on-chain.

// V2 quote: amountOut for a given amountIn (0.3% fee = 997/1000)

func getAmountOutV2(amountIn, reserveIn, reserveOut float64) float64 {

    amountInWithFee := amountIn * 0.997

    numerator := amountInWithFee * reserveOut

    denominator := reserveIn + amountInWithFee

    return numerator / denominator

}

V2's advantage is that the math is a closed-form formula. Given two reserve values you can quote instantly off-chain, which means you can sweep thousands of pairs fast with no external calls. The cost is capital efficiency: liquidity is spread thinly across every price from zero to infinity.

V3 — Concentrated Liquidity and Ticks

UniswapV3 lets liquidity be concentrated into specific price ranges (ticks). A liquidity provider says "I'll only supply liquidity in this price band." Capital efficiency goes way up, but quoting gets complicated — the result now depends on which tick the current price sits in, how much liquidity that range holds, and whether the swap crosses range boundaries.

So V3 doesn't stop at two reserve numbers. Price is stored as a square-root fixed-point value called sqrtPriceX96, and active liquidity is scattered tick by tick. A large swap that crosses several ticks has to be computed piecewise, because each range holds different liquidity.

For V3 Quotes, Ask the Quoter

Because of that complexity, reproducing V3 quotes perfectly off-chain by hand is awkward. That's why Uniswap ships a Quoter contract. The Quoter simulates the real swap logic via eth_call and hands back the exact result, without mutating state.

// V3 quote: have the Quoter contract simulate the real swap for an exact figure

// (more accurate than an off-chain approximation, but costs an RPC call)

amountOut, err := quoter.QuoteExactInputSingle(&bind.CallOpts{Context: ctx},

    tokenIn, tokenOut, feeTier, amountIn, big.NewInt(0))

if err != nil {

    return 0, fmt.Errorf("V3 quote failed: %w", err)

}

The trade-off is clear. V2 is free and instant (closed form); V3 is exact but requires an RPC call. So production bots typically narrow the candidate set broadly with V2 math, then confirm only the promising V3 routes through the Quoter — which keeps the call budget under control.

Slippage — the Gap Between Quote and Reality

On any protocol, the larger your size, the worse your execution price. That's slippage, and the V2 formula shows it directly: as amountIn grows relative to reserveIn, the denominator grows and you receive less per unit.

// Small vs. large — the same pool gives you a different unit price

small := getAmountOutV2(1,    1000, 1000) // ~0.997 (nearly 1:1)

large := getAmountOutV2(500,  1000, 1000) // ~332   (1:0.66, sharply worse)

// You need a guard that skips when size exceeds a set fraction of pool reserves

So a quote looking good and the pool actually absorbing that size are two separate questions. Always pair your quoting with a guard that checks entry size against available liquidity.

Summary

  • V2 is the closed-form x*y=k — free, instant, off-chain quoting, at the cost of low capital efficiency
  • V3 is tick-based concentrated liquidity — far more capital efficient, but quoting is complex enough to need Quoter simulation
  • In production, sweep broadly with V2 and confirm precisely with V3 to conserve RPC calls
  • Separately from quoting, slippage and liquidity guards are what keep you out of routes with no real substance

There's a lot of different math hiding behind the single word "swap." Understanding a protocol's quoting model is where an accurate bot starts.

This article is for educational and informational purposes and is not investment advice. Automated DeFi trading carries a risk of principal loss, and the outcomes are your own responsibility.

댓글

이 블로그의 인기 게시물

한국투자증권 KIS API로 실시간 시세 받기 (WebSocket 실전)

파이썬으로 업비트 API 연동하기 — 시세 조회부터 주문까지 기초

Go로 자동매매 신호봇 프레임워크 설계하기