Goyalayus

Notes, essays, and fragments from the edge of understanding.

so you want some ethereum

August 18, 2025

Original Substack post

suppose you have 100 DAI ( DAI is a stable coin. 1 DAI = 1 dollar ), and you want 100 Ethereum.

you can offcouse convert your DAI to dollars and then to again buy ethereum with those dollars but that’s costly and slow.

that is the reason DAI to Ethereum exchanges exist.

so how do they work.

first there are liquidity providers who deposit equal amounts of DAI and Eth to the smart contract and whenever a trade happen they earn a small trading fees.

now suppose you are a trader and you want 10Eth worth of DAI, how is it calculated that how much worth of DAI would you get.

the answer lies in the formula x * y = k (where k is a constant)

The product of the reserves must remain constant (before fees).

Let's think about what this means:

  • x = reserve of Token A (e.g., ETH)

  • y = reserve of Token B (e.g., DAI)

  • k = a constant

The formula x * y = k describes a hyperbola. This shape is the key.

How it sets the price: The "price" of Token A in terms of Token B at any given moment is simply the ratio of the reserves: Price = y / x.

Let's see what happens during a trade:

  1. Initial State: 10 ETH (x) and 20,000 DAI (y).

    • k = 10 * 20,000 = 200,000

    • Price of ETH = 20,000 / 10 = $2,000 per ETH.

  2. A Trader Buys 1 ETH: They put DAI into the pool and take ETH out. Let's see how much DAI they need. They want to receive 1 ETH, so the new x reserve will be 9 ETH.

  3. The Contract's Calculation:

    • The contract must maintain k. So, 9 * new_y = 200,000.

    • Solving for new_y: new_y = 200,000 / 9 ≈ 22,222 DAI.

    • The amount of DAI the trader had to pay is new_y - y = 22,222 - 20,000 = 2,222 DAI.

  4. Final State: 9 ETH (x) and 22,222 DAI (y).

    • k = 9 * 22,222 ≈ 200,000 (it's constant).

    • The new price of ETH = 22,222 / 9 = $2,469 per ETH.

The formula works because it automatically implements the law of supply and demand. By removing ETH (decreasing its supply in the pool), the trader has made ETH more expensive. This is called price slippage. The larger the trade relative to the size of the pool, the more the price moves.


a lot of you might be thinking that how is this constant k is decided upon

The answer is that k is not calculated initially by the protocol. It is implicitly set by the very first person to provide liquidity.

State 1: The Empty Pool

  • A new, empty ETH/DAI AMM smart contract is deployed.

  • reserve_ETH (x) = 0

  • reserve_DAI (y) = 0

  • k = x * y = 0 * 0 = 0.

  • The market is not functional. The price is undefined, and no trades can happen.

State 2: The First Liquidity Provider (LP) Arrives

  • Alice wants to be the first person to provide liquidity. Let's say she has 10 ETH and a large amount of DAI.

  • She needs to decide what the initial price of ETH should be in this pool. She looks at other exchanges (like Coinbase or another DEX) and sees that the current global market price for ETH is $2,000.

  • To set this price in the pool, she must deposit her assets at that exact ratio.

    • Price = reserve_DAI / reserve_ETH

    • $2,000 = reserve_DAI / 10 ETH

    • Solving for reserve_DAI: $2,000 * 10 ETH = 20,000 DAI.

  • Alice now calls the add_liquidity function on the smart contract, depositing 10 ETH and 20,000 DAI simultaneously.

That’s it for this essay, follow for more “How things really work” Essays