/DEX

Decentralized Exchange

Primary LanguageSolidity

Decentralized Exchange - Automatic Market Maker

This project aims to create a simplified Decentralized Exchange using the automatic market maker algorithm of constant product to swap between two ERC20 tokens.

Theory : xy = k

Swap

How much dy for dx?

    xy = k
    (x + dx)(y - dy) = k
    y - dy = k / (x + dx)
    y - k / (x + dx) = dy
    y - xy / (x + dx) = dy
    (yx + ydx - xy) / (x + dx) = dy
    ydx / (x + dx) = dy

Liquidity

Adding Liquidity

How much dx, dy to add?

    xy = k
    (x + dx)(y + dy) = k'

    No price change, before and after adding liquidity
    x / y = (x + dx) / (y + dy)

    x(y + dy) = y(x + dx)
    x * dy = y * dx

    x / y = dx / dy
    dy = y / x * dx

How much shares to mint?

    f(x, y) = value of liquidity
    We will define f(x, y) = sqrt(xy)

    L0 = f(x, y)
    L1 = f(x + dx, y + dy)
    T = total shares
    s = shares to mint

    Total shares should increase proportional to increase in liquidity
    L1 / L0 = (T + s) / T

    L1 * T = L0 * (T + s)

    (L1 - L0) * T / L0 = s 
    

    
    Claim
    (L1 - L0) / L0 = dx / x = dy / y

    Proof
    --- Equation 1 ---
    (L1 - L0) / L0 = (sqrt((x + dx)(y + dy)) - sqrt(xy)) / sqrt(xy)
    
    dx / dy = x / y so replace dy = dx * y / x

    --- Equation 2 ---
    Equation 1 = (sqrt(xy + 2ydx + dx^2 * y / x) - sqrt(xy)) / sqrt(xy)

    Multiply by sqrt(x) / sqrt(x)
    Equation 2 = (sqrt(x^2y + 2xydx + dx^2 * y) - sqrt(x^2y)) / sqrt(x^2y)
               = (sqrt(y)(sqrt(x^2 + 2xdx + dx^2) - sqrt(x^2)) / (sqrt(y)sqrt(x^2))
    
    sqrt(y) on top and bottom cancels out

    --- Equation 3 ---
    Equation 2 = (sqrt(x^2 + 2xdx + dx^2) - sqrt(x^2)) / (sqrt(x^2)
    = (sqrt((x + dx)^2) - sqrt(x^2)) / sqrt(x^2)  
    = ((x + dx) - x) / x
    = dx / x

    Since dx / dy = x / y,
    dx / x = dy / y

    Finally
    (L1 - L0) / L0 = dx / x = dy / y
Removing Liquidity

Claim dx, dy = amount of liquidity to remove dx = s / T * x dy = s / T * y

    Proof
    Let's find dx, dy such that
    v / L = s / T
    
    where
    v = f(dx, dy) = sqrt(dxdy)
    L = total liquidity = sqrt(xy)
    s = shares
    T = total supply

    --- Equation 1 ---
    v = s / T * L
    sqrt(dxdy) = s / T * sqrt(xy)

    Amount of liquidity to remove must not change price so 
    dx / dy = x / y

    replace dy = dx * y / x
    sqrt(dxdy) = sqrt(dx * dx * y / x) = dx * sqrt(y / x)

    Divide both sides of Equation 1 with sqrt(y / x)
    dx = s / T * sqrt(xy) / sqrt(y / x)
       = s / T * sqrt(x^2) = s / T * x

    Likewise
    dy = s / T * y

Requirements

  • Foundry

To get started with Foundry, run the following commands:

curl -L https://foundry.paradigm.xyz | bash
foundryup
git clone https://github.com/HemaDeviU/DEX
cd dex
forge build

Usage

  • For local Deployment

  1. Start a local node make anvil
  2. Deploy make deploy
  • For testnet deployment

  1. Setup environment variables You'll want to set your SEPOLIA_RPC_URL and PRIVATE_KEY as environment variables. You can add them to a .env file.
  • PRIVATE_KEY: The private key of your account (like from metamask) which has testnet ETH.
  • SEPOLIA_RPC_URL: This is url of the sepolia testnet node you're working with. You can get setup with one for free from Alchemy.
  • ETHERSCAN_API_KEY: To verify the contract,get the api key from etherscan account.
  1. Deploy make deploy ARGS="--network sepolia"