# 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 Market Maker](https://lh7-us.googleusercontent.com/3KmxSWE13rMeO7ZuD80lm5b8h_6PUddODif1VWIkQX45N3UE4PSYi08YCEA8aByojxyVsvD_VVb8pPw-DM-EEojCF31oGSU3SgBgxmwotF-scjYjYPxj17n-YoSuixG5UvPu7fTKM3ooJP-uqFaz2bY)

Constant Product

<figure><img src="https://4098148745-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FShLJeuzlAQmW4qw5XrS7%2Fuploads%2F7pBaSsGpdse7DbJS87qS%2Fimage.png?alt=media&#x26;token=e9ab9325-1c77-4734-afb8-1bcf8bcbb339" alt=""><figcaption><p>Swap Page</p></figcaption></figure>

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