DEX, AMM, and Strange Behaviour of Exchange Rates Explained in 5 Minutes

DEX, AMM, and Strange Behaviour of Exchange Rates Explained in 5 Minutes

You might have noticed that when you exchange funds at Quipuswap, Uniswap, Pancake Swap and other DEX, the more the amount, the worse the rate. Why is this happening and can developers do anything about it?

Let’s take a look at how DEX operate, the principles of automatic market-making (AMM), and popular formulae to calculate the rate in a liquidity pool.

DEX and CEX: What’s the Difference?

Exchanges must gather liquidity in one place and automatically match opposing orders while calculating the market price of a trading pair. For that purpose, centralised exchanges (CEX) use an order book. It stores active limited orders like “I’ll sell 10 tez for $7 each” and “I’ll buy 50 tez for $6 each.”

  1. Alice buys 100 tez with the market price of $7.
  2. The exchange executes the transaction at the expense of the most profitable order, i.e. selling 10 tez for $7.
  3. The order’s liquidity is insufficient to close the deal.
  4. The exchange takes the next best thing, such as selling 200 tez for $7.10 each.
  5. Alice gets 100 tez at the average price of $7.09.
  6. The market price increases to $7.10.

This logic is built in the exchange core. Aside from sorting the orders, it checks the balances of traders, stores the history of transactions, and sends data to the trading interface.

Exchange cores are efficient but relatively big: an open-source implementation is about 330 kB, while commercial options like LSEG Millenium Exchange start at 2 MB. Meanwhile, the maximum volume of a Tezos smart contract is 32 kB, and 24 kB on Ethereum, so the core just can’t fit into a smart contract. That’s why DEX use another method to store liquidity and match orders, which is called AMM, or automatic market-making.

How AMM Works on DEXs

Just like CEX, DEX must store liquidity, match orders, and calculate the exchange rate. Storing each byte on a blockchain costs money: in Tezos, for example, storing 1 kB costs 0.25 tez. That’s why DEXs don’t record each order in a book but pool them all together. In fact, it’s two big orders to buy and sell. Therefore, a DEX executes orders at the expense of the pool.

As there is no order book, DEXs calculate the price from formulae, with X × Y = K (aka “constant product market maker”) being the most popular. X and Y stand for the number of tokens in the pool, and K is a constant. The value of K changes only when someone deposits or withdraws funds from the pool. K remains the same in the case of tokens’ exchange.

Trading with AMM works like this:

  1. There are 100 tez (X) and 700 kUSD (Y) in the pool. K is therefore 70 000, and the exchange rate is 1:7.
  2. Bob buys 10 tez for kUSD.
  3. АММ calculates how many kUSD are to be taken from Bob to retain K:

    (100 − 10) × (700 + А) = 70 000
    63 000 + 90А = 70 000
    90А = 7000
    А = 77.7

  4. Bob pays 77.7 kUSD and gets 10 tez.
  5. The pair’s exchange rate increases to $7.77.

The more tokens there are in the liquidity pool, the less the rate changes during the exchange. Still, it always does.

Can АММ Be More Efficient?

It can, but not for all use cases. Thus, exchanging stablecoins works better with X + Y = K.

  1. There are 100 uUSD and 700 kUSD in the pool, K equals 800.
  2. Bob buys 10 uUSD for kUSD.
  3. АММ calculates the price with the retention of K:

    (100 − 10) + (700 + А) = 800
    790 + A = 800
    A = 10

  4. Bob sells 10 kUSD and buys 10 uUSD.
  5. The rate remains 1:1.

Developers and mathematicians have devised dozens of formulae for AMM. For example, that’s what a dynamic search for a weighted price with a built-in oracle looks like:

Complex formulae solve one problem but create another one. E.g., AMM for small transactions does not work well for big ones. An algorithm for concentrating liquidity around the existing price ties down its dynamics, and will be profitable for traders and unfavourable for liquidity providers. For that reason, most DEX devs remain faithful to the good old X × Y = K.

Subscribe and never miss updates from the world of Tezos:

  1. Telegram channel
  2. Facebook
  3. Twitter in Russian and Ukrainian
  4. Twitter in English
  5. YouTube channel
  6. Instagram
  7. LinkedIn
  8. hub at ForkLog


Photographs as an Investment: an Arago NFT Marketplace Review

Read similar posts

Read our blog and never miss news about TezosRead Blog