Uniswap (V1)

Resources

Contracts

Factory/Registry Contract

Exchange Contract

Liquidity Tokens (LT)

How Many LT to Receive

AmountMinted = (TotalAmount * ETHDeposited) / ETHPool

# E.g.
(1 ETH * 100 LT) / 10 ETH
= 10 LT

How Many Tokens to Deposit

TokensDeposited = (TokenPool * ETHDeposited) / ETHPool

# E.g.
(500 OMG * 1 ETH) / 10 ETH
= 50 OMG

How Many LT to Burn

ETHWithdraw = (AmountBurned * ETHPool) / TotalAmount
TokensWithdraw = (AmountBurned * TokenPool) / TotalAmount

# E.g.
(10 * 11 ETH) / 110
= 1 ETH

(10 * 550 OMG) / 110
= 50 OMG

Example (ETH -> OMG)

Fee = 0.25%

(x) ETHPool = 10
(y) OMGPool = 500
k = 10 * 500
  = 5000
# OMG buyer sends 1 ETH to the exchange contract
ETHBuy = 1

# Calculate fee that will go back to the pool
Fee = ETHBuy * 0.25%
    = 0.0025

# Calculate new ETH pool minus fees
ETHPool = ETHPool + ETHBuy - Fee
        = 10 + 1 - 0.0025
        = 10.9975

# Calculate OMG that will be received according to the invariant k
OMGRecv = OMGPool - k / ETHPool
        = 500 - 5000 / 10.9975
        = 45.35

# Calculate new OMG pool size
OMGPool = k / ETHPool
        = 5000 / 10.9975
        = 454.65

# Now add fee to ETH pool
ETHPool = ETHPool + Fee
        = 10.9975 + 0.0025
        = 11
OMGPool = 454.65

# Invariant gets shifted slightly each trade encouraging purchases in the opposite
# direction.
k = ETHPool * OMGPool
  = 11 * 454.65
  = 5001.15

Example Cont. (OMG -> ETH)

# OMG seller sends 45.35 OMG to the exchange contract
OMGBuy = 45.35

# Calculate fee that will go back to the pool
Fee = OMGBuy * 0.25%
    = 0.113375

# Calculate new OMG pool minus fees
OMGPool = OMGPool + OMGBuy - Fee
        = 454.65 + 45.35 - 0.113375
        = 499.886625

# Calculate ETH that will be received according to the invariant k
ETHRecv = ETHPool - k / OMGPool
        = 11 - 5001.15 / 499.886625
        = 0.9954

# Calculate new ETH pool size
ETHPool = k / OMGPool
        = 5001.15 / 499.886625
        = 10.0046

# Now add fee to OMG pool
OMGPool = OMGPool + Fee
        = 499.886625 + 0.113375
        = 500
ETHPool = 10.0046

k = ETHPool * OMGPool
  = 10.0046 * 500
  = 5002.3