HyperByte

How AMM module works? [x*y=k]

2026-07-20

All swapping pools between two currncies mostly base on the x·y=k equation. Usually, x: number of TokenA, y: number of TokenB. (Initial number of TokenA and TokenB are based on the exact price when the pool was created).

Here are two scenarios for buy and sell,

  1. User sells ETH for getting USDC: (x+dx)·(y-dy)=k
  2. User sells USDC for getting ETH: (x-dx)·(y+dy)=k

Let’s assume there is a pool, and there are 10ETH and 20,000USDC in it, then the k=10*20,000=200,000

  1. User sells 5 ETH for getting USDC: (10+5)·(20,000-dy)=200,000 dy=6666.67USDC

  2. User sells 10,000USDC for getting ETH: (10-dx)·(20000+10,000)=200,000 dx=3.33ETH

  3. What if I am trying to sell 10,000ETH for USDC? Calculate it, for fun ^^

    (10+10,000)·(20000-dy)=200,000 dy=19998USDC

  • Sell 5ETH get 6666.67USDC , then 6666.67/5 = 1333.33 USDC/ETH

  • Sell 10,000ETH get 19998USDC , then 19998/10,000 = 2 USDC/ETH

What is this?!! So, The bigger you try to swap , the worse the value you get. But don’t worry, usually for a pool that size, an everage ordinary trade barely moves the needle.

Question:
• And what if the price fluctuates out of the pool?
No worries, there usually is an arbitrage bot that monitors price differences between the pool and the outside world, once there's a price gap for speculation, the bot immediately sells the token which went up, this trick helps keep the pool price aligned with the broader market.

Concepts


Advice for investors

  1. Check the real‑time slippage first, then decide whether to trade

  2. If you’re a whale, choose a larger pool

← Back to Home