Decentralized Exchanges

Victor Evangelista

Victor Evangelista / Oct 28, 2022

Crypto • 4-minute read

Luna Chart

During the crypto craze of 2021, I was astounded at the sheer amount of activity within Decentralized Finance.

Everyone and their mother wanted a cryptocurrency exchange, liquidity mining, staking, an NFT marketplace, and more.

Some examples of successful DEX platforms:

  1. Uniswap
  2. PancakeSwap
  3. SushiSwap
  4. ShibaSwap
  5. ApeSwap

I jumped into the fray creating dApps on a per-contract basis.

One type of project my team and I worked on was Decentralized Exchanges, or DEX for short. The trustworthy and open-source Uniswap V2 Protocol proved to be excellent starter code.

In this article, we’ll focus mainly on understanding Uniswap V2’s Protocol at a high level.

The General Idea

Uniswap is an automated liquidity protocol powered by a deterministic algorithm, known as an automated market maker (AMM).

Uniswap’s AMM is based off of the “Constant Product Market Maker Model”, otherwise known as the “x * y = k Model”. For an in-depth look, check out the official specification.

The basic idea is that the price of asset pairs is the ratio set by the XYK model, instead of a traditional order book model.

one to one pair price ratio

Let’s go through a simple example. A “liquidity pool” was created with 1000 ASSET1 (x) tokens, and 1000 ASSET2 (y) tokens. That would make k equal to 1,000,000. The initial price ratio between the tokens is 1:1.

It’s called the constant product formula for a reason- k remains constant.

attempting to add more liquidity

The only way to “change” k would be to modify the liquidity pool. However, we can only add or remove tokens equal to the price ratio of 1:1 currently set by the pair. Therefore, if we wanted to add 10 ASSET1 tokens, we would also need to add 10 ASSET2 tokens.

Onto trading. In order to keep k constant, the supply of x (ASSET1) and y (ASSET2) can only move inverse to each other.

x_required = (k / (x - y_amount)) - x

Say you wanted to purchase 10 ASSET2 tokens (y). The price would be dictated by the above formula. Why? Because the “x_required” is added back into the liquidity pool to keep k constant.

10.1010 (Cost in ASSET1) = (1,000,000 / (1,000 - 10)) - 1,000

x _ y = k => 1,010.10 _ 990 = 1,000,000

Buying 10 units of ASSET2

In practice, we’re adding a transaction fee of 0.20% here, bringing the total price of 10 ASSET2 equal to 10.1212 ASSET1.

What if we tried purchasing 100 ASSET2 instead? How much ASSET1 would we need to provide before fees?

111.1111111111 (Cost in ASSET1) = (1,000,000 / (1,000 - 100)) - 1,000

Buying 10 units of ASSET2

You may have noticed the “bang for buck” of our ASSET1 is decreasing the more ASSET2 we purchase (price of ASSET2 per ASSET1). That’s because in order to keep k constant, we need to provide more and more of ASSET1 relative to the amount of ASSET2 we purchase.

We can now lay out a chart of prices before fees.

Price (ASSET2 per ASSET1) ASSET2 BUY ASSET1 SELL
1 1 1
0.99 10 10.10
0.95 50 52.63
0.91 100 111.11
0.83 200 250
0.33 500 1000

You should now be able to see the automated market making in process! There are no order books to fill and no liquidity gaps. The price sets itself.

The idea is that if the price is different on the DEX than other exchanges, traders are incentivized to use arbitrage trading equalizing the market.

For example, if the price is lower on say Binance and higher on our DEX, users are incentivized to sell their tokens on the DEX for a profit, while at the same time equalizing the markets.

Easing In

Now that we understand the general idea behind automated market making and Uniswap’s constant product formula, we can finally get to the meat and potatoes.

Uniswap V2 architecture diagram

Uniswap V2 is comprised of two main repositories:

  • The Core
    • Core contains the main low level smart contracts.
      1. Pair
        • Token pair pool instance enabling swapping
        • Mints and burns liquidity pool tokens representative of stake
        • Responsible for constant product market maker implementation
      2. Factory
        • Creates pairs, tracks them, and sets fee receiver
  • The Periphery
    • Periphery contains smart contracts to interact with the core.
      1. Router
        • Provides external convenience functions for swapping tokens
      2. Library
        • Provides external convenience functions for reading pair data

Related Article