πŸ†AMM Swap

The concept

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).

Constant Product

Example β€” Trader Swap Tokens

Assume there are two BRC20 tokens: ORDI and SATS, and there is a ORDI/SATS Uniswap liquidity pool created. Let’s say this ORDI/SATS liquidity pool currently has 10 ORDI and 40 SATS in it, so the ratio is 1 ORDI : 4 SATS. Assuming that’s the current market ratio, 1 ORDI is now worth 4 SATS.

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

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

x * y = k

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

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 ORDI and 40 SATS, so X = 10 and Y = 40. When John swaps 1 ORDI , he will pay a 0.3% fee first, which is 0.003 ORDI (0.3% * 1). After the fee is paid, the remaining ORDI, which is 0.997 ORDI, will be swapped for SATS, so the new X becomes 10.997 ORDI. 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 SATS, the remaining SATS will be sent to John, which means John will receive 3.626444 SATS(Y β€” newY = 40 – 36.373556 = 3.626444). After the swap, the pool has 11 ORDI and 36.373556 SATS.

Last updated