# Uniswap V2

The constant product market maker is the most common AMM invariant that was made popular with Uniswap. It can be simply modeled as `x * y = k`

. X and Y are the reserves for each asset, as assets are traded through this function X and Y increase or decrease in their reserves in a way that keeps a constant K (not counting fees charged).

## Example β Trader Swap Tokens

Assume there are two ERC-20 tokens: ABC and USDT, and there is a ABC/USDT Uniswap liquidity pool created. Letβs say this ABC/USDT liquidity pool currently has 10 ABC and 40 USDT in it, so the ratio is 1 ABC : 4 USDT. Assuming thatβs the current market ratio, 1 ABC is now worth 4 USDT.

Now if John swaps his 1 ABC for USDT, the pool will take his 1 ABC, transfer a certain amount of USDT from the pool to him.

Uniswap determines the output token amount by using a constant product formula:

Instead of 4 USDT, John will actually receive a bit less: 3.626444 USDT.

The constant product formula requires the trades should not change the product (K) of a pairβs reserved balances (X and Y). Letβs walk through the calculation step by step.

Originally, the liquidity pool has 10 ABC and 40 USDT, so X = 10 and Y = 40. When John swaps 1 ABC , he will pay a 0.3% fee first, which is 0.003 ABC (0.3% * 1). After the fee is paid, the remaining ABC, which is 0.997 ABC, will be swapped for USDT, so the new X becomes 10.997 ABC. Since K must remain unchanged, the new Y can be calculated from the formula `X * Y = newX * newY`

. So newY is 36.373556 `(X * Y / newX = 10 * 40 / 10.997 = 36.373556)`

. This means the pool will hold 36.373556 USDT, the remaining USDT will be sent to John, which means John will receive 3.626444 USDT`(Y β newY = 40 β 36.373556 = 3.626444)`

. After the swap, the pool has 11 ABC and 36.373556 USDT.

Last updated