Q&A-1Explain and implement a constant-product swap quote.
Answer
Core answer (first 90 seconds). Assume a Uniswap V2-style pool: two tokens with reserves x and y, invariant x·y = k, a 0.3% fee taken from the input (997/1000), and integer reserves. A quote answers one of two questions: given Δx in, how much Δy out (exact-in), or given Δy out, how much Δx in (exact-out). Both fall out of solving the post-trade invariant.
Derivation. The fee is charged on the input, so effective input is Δx' = Δx·997/1000. After the swap the pool holds x + Δx' and y − Δy, and the invariant must still hold:
(x + Δx')(y − Δy) = x·y
Δy = y·Δx' / (x + Δx')That is the whole quote. Spot price is y/x; execution price Δy/Δx is worse by the fee plus price impact, which grows with Δx/x. Inverting for exact-out gives Δx = x·Δy / ((y − Δy)·0.997), rounded up.
Implementation (Solidity ^0.8.20; 0.8 has built-in overflow checks, so no SafeMath).
library ConstantProduct {
uint256 constant FEE_NUM = 997;
uint256 constant FEE_DEN = 1000;
// Exact-in: floor the output so the pool never pays out more than k allows.
function getAmountOut(uint256 amountIn, uint256 reserveIn, uint256 reserveOut)
internal pure returns (uint256 amountOut)
{
require(amountIn > 0, "INSUFFICIENT_INPUT");
require(reserveIn > 0 && reserveOut > 0, "INSUFFICIENT_LIQUIDITY");
uint256 inWithFee = amountIn * FEE_NUM;
amountOut = (inWithFee * reserveOut) / (reserveIn * FEE_DEN + inWithFee);
}
// Exact-out: ceil the input so the user always pays at least the invariant cost.
function getAmountIn(uint256 amountOut, uint256 reserveIn, uint256 reserveOut)
internal pure returns (uint256 amountIn)
{
require(amountOut > 0, "INSUFFICIENT_OUTPUT");
require(reserveIn > 0 && reserveOut > 0, "INSUFFICIENT_LIQUIDITY");
require(amountOut < reserveOut, "INSUFFICIENT_LIQUIDITY");
uint256 numerator = reserveIn * amountOut * FEE_DEN;
uint256 denominator = (reserveOut - amountOut) * FEE_NUM;
amountIn = numerator / denominator + 1;
}
}Worked trace. Reserves x = 10,000, y = 20,000,000; swap Δx = 100 in.
inWithFee = 100 * 997 = 99,700
numerator = 99,700 * 20,000,000 = 1,994,000,000,000
denominator= 10,000*1000 + 99,700 = 10,099,700
amountOut = 1,994,000,000,000 / 10,099,700 = 197,431 (floor)Spot is 20,000,000/10,000 = 2,000; execution is 197,431/100 = 1,974.31, a 1.28% hit. Running getAmountIn(197,431) returns 100 (the +1 rounds up), so the round trip is consistent.
Trade-offs. Exact-in floors the output (user receives ≤); exact-out ceils the input (user pays ≥). Pick by who should absorb the rounding dust. An on-chain quote is atomic and current but costs gas and can be front-run; an off-chain quote is cheap but stale. Constant product is always liquid across the curve; Uniswap V3 quotes depend on the tick range and in-range liquidity, not a single k. A TWAP resists manipulation but lags the true price.
Failure modes and detection.
- Stale reserves: reserves move before the tx lands. Detect by comparing quote reserves to on-chain at execution and enforcing
amountOutMin. - Fee mismatch: pool is a 0.05% or 1% tier, not 0.3%. Read the fee from the pool; never hardcode.
- Rounding direction: floor on out, ceil on in. Fuzz
getAmountIn(getAmountOut(x)) >= x. - Fee-on-transfer / rebasing tokens: received amount ≠
amountIn, breaking the invariant. Check the balance delta. - Sandwich: attacker moves price ahead of you. Bound slippage and set a deadline.
- Insufficient liquidity:
amountOut >= reserveOutreverts or divides by zero.require(amountOut < reserveOut). - Quote ≠ execution: a quote is a snapshot, not a guarantee. Enforce the bound on-chain.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.