Uniswap Introduction — 1

Recap Father of DeFi — Uniswap and how it works

Greg Shen
4 min readJun 21, 2022

In this article, we will discuss what decentralized exchange(DEX) is and the structure of it.

Uniswap, as known as Father of DeFi is a project which starts DeFi summer and explodes the crypto market. That’s why I’m introducing DEX using Uniswap and there might be two or three articles.

We will go through basic concepts such as CEX/DEX, constant product market maker and even walking through smart contracts. That is, we are going to examine hundreds of lines of Solidity code that generates $1.28 billion revenue everyday!

Uniswap — A project that starts DeFi Summer

Before we get into decentralized exchange(DEX), we have to understand what Centralized Exchange is.

The biggest CEX in the world (now)
Bybit has the best UX for trading in my opinion

Advantages for CEX

  • Easily transform fiat to crypto
  • Has a lot of features to offer (Perpetual Swap Contract, Gift Card, etc..)
  • Sufficient Liquidity
  • Order book available
  • Orders are executed within a second

Disadvantages for CEX

  • Not decentralized enough( because it’s centralized)
  • CEX has the right to control your assets
  • Strict regulations( such as KYC)
  • Need multiple fees if there’s no liquidity pair provided

What about DEX?

Advantages for DEX

  • Assets are controlled by owner, not DEX
  • KYC isn’t needed, it’s fine even if you’re totally anonymous
  • Hard to be attack( most of the time)
  • Primary market crypto available
  • Executes swap immediately(still depends on speed of blockchain), you trade with liquidity pools, no order book or opponents

Disadvantages for DEX

  • High risks in primary markets
  • Must import token address ourselves, there’s chance to be scammed
  • Orders execution time depends on blockchain speed

Before we dive into Uniswap…

Uniswap was a protocol for automated token exchange on Ethereum, now it’s also on Polygon(Sidechain for Ethereum).

It’s basically Automated Market Maker.

What’s market maker?

1.Traditional Market Maker

Traditional Market Maker such as currency exchange provides service for us to purchase or sell foreign money. They earn the price difference and service fees. (Man they actually earned a lot!)

Requirement for being a market maker

  • Reputation Endorsement
    Banks that have permission from governments would make people feel safe to trade.
  • Adequate Liquidity
    They have to have enough liquidity to ensure the prices don’t fluctuate too much

2. Automated Market Maker(AMM)

How does AMM works?

Uniswap uses Constant Product Formula to handle it.

Constant Product Formula

k=x*y

k for constant, x for reserves amount for X token and y for reserves amount for Y token

Constant Product

For instance…

There are 350 $AZ and $BNT in a pool, the market maker takes 1% of trading volume for service fees. We have a constant k=350*100 =35000. Take note that k is a constant 35000.

How each data changes after different amount of AZ spent

Has AMM satisfied the two requirements we mentioned above?

  • Reputation Endorsement
    Smart contracts are exposed on blockchain explorer so everyone can trust it if the code is legit.
  • Adequate Liquidity
    Everyone can be liquidity provider to earn not only swap fees but also staking rewards with high APY. It motivates people to stake their LP token to make liquidity pool big enough.

Note that anyone can be liquidity provider to earn fees and also get additional rewards with DEX token. By way of illustration, Uniswap rewards liquidity providers with UNI token.

Kindly follow to keep updated with my latest release.

Feel free to contact anytime.

Email: gregshen0925@gmail.com

Telegram: @gregshen0925

Uniswap

Now we are finally prepared for learning Uniswap itself. Starting with Uniswap V2 main features.

Uniswap V2 Main Features

  • Token Swap
  • Liquidity Providing
    (1) Provide two tokens to mint LP token
    (2) Stake LP token
    (3) Unstake LP token
    (4) Burn LP token back to two tokens
  • Oracle
  • Flash Loans

There are three versions of Uniswap but I’m just walking through V2 and here’s why.
1. V1 is too simple and does not have all the modern features.
2. V3 is more efficient than V2 and it also optimizes funds utilization by adding “tick”. Hence it’s way much complicated

Since we’re introducing what AMM is and how it works. V2 is the best choice to walk through.

Architecture of Uniswap Smart Contracts

There are two repositories which both contains two main smart contracts.

Architecture of Uniswap Repository

Core

Core is for storing values(tokens) and managing them. It contains two smart contracts, Pair and Factory.

  • Pair
    Smart contract that implements functionality for swapping, minting and burning tokens.
  • Factory
    Creating and tracking pairs

Periphery

Periphery, obviously, contains smart contract to interact with Core. It also contains two smart contracts, Router and Library

  • Router
    Interacting with the core. Provides functionalities such as swapETHForExactTokens, swapExactETHForTokens, etc.
  • Library
    Some functionalities like getReserves, getAmountIn, getAmountOut, etc.

Outro

Now we’ve understand

  • Centralized Exchange/Decentralized Exchange
  • Market Maker/Automated Market Maker
  • Constant Product Formula
  • Architecture of Uniswap smart contracts

In the next article, we will deeply walk through all four main smart contracts. Feel free to contact me for any questions. Thanks for your time.

--

--