This is a constant-product Automated Market Maker (AMM), the mechanism behind Uniswap-style decentralized exchanges. A liquidity pool holds two reserves, x (Token A) and y (Token B), and enforces:
x · y = k (invariant, only grows from trading fees)
spot price P = y / x
swap A→B: Δy_out = y − k / (x + Δx·(1−fee))
slippage = (executionPrice − spotPrice) / spotPrice
impermanent loss = 2·√r / (1+r) − 1, r = P_now / P_initial
- Execute Swap — routes the trade size through the formula above; the curve marker slides to the new (x,y) point and the pool bars resize.
- LP fee — the input amount is reduced by the fee before it hits the formula, but the full amount (including fee) is added to the reserve, so k strictly increases with every trade — that growth is the fee revenue liquidity providers earn.
- Add/Remove Liquidity — scales both reserves proportionally, leaving the spot price unchanged but changing pool depth (and therefore future slippage).
- External Price Shock — simulates an arbitrageur trading the pool back to a randomly shifted "true" market price, which is exactly how impermanent loss arises for real LPs: the pool price is forced to follow the external market via arbitrage.
- Impermanent loss — the gap between the value of an LP position and simply holding the two tokens, purely as a function of how far the price has moved from when liquidity was deposited.
Real-world relevance: this exact formula (with a 0.3% fee) runs inside Uniswap v2, SushiSwap and dozens of other DEXs, routing hundreds of billions of dollars in trades without an order book or a counterparty — only reserves and math.