Skip to content

Latest commit

 

History

History

uniswap-v2

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

What does uniswap do?

  • Basically there are two types of users liquidity providers and traders

  • Liquidity providers provide liquidity to the pool and in return they get third token that represents the partial ownership of the pool called liquidity token.

  • Traders can swap tokens means then can provide a token and receive another token. The exchange rate is determined by the relative number of tokens in the pool e.g. pool has 8 USDC & 10 DAI then the value of USDC will be high. The pool takes a small percent as a reward for the liquidity pool.

  • When liquidity providers want their assets back they can burn the liquidity token and receive back their assets, including the share of reward.

Contracts

  • Uniswap v2 is divided into 2 contracts, a core and a periphery.
  • The division allow the core contract, which holds all assets and therefore it needed to be secure, to be simpler and easier audit.
  • All the extra functionality required by traders can then be provided by periphery contracts.

DATA AND CONTROL FLOWS

  • Trader/liquidity provider interacts with periphery contract not with main core contract directly

Swap

Caller

  1. Approve periphery contract to use trader's tokens
  2. Trader uses router contract to swap tokens

In the periphery contract (UniswapV2Router02.sol)

  1. Sends tokenA to core contract
  2. Calculates tokenOut
  3. Then iterate over path array and swap tokens untill desired token come

In the core contract (UniswapV2Pair.sol)

  1. Then we sends the token to trader
  2. Update the reserves with _update function

Add Liquidity

Caller

  1. Approve periphery contract to use liquidity provider's tokens
  2. Liquidity provider uses router contract to add liquidity

In the periphery contract (UniswapV2Router02.sol)

  1. Sends assets of liquidity provider to core contract

In the core contract (UniswapV2Pair.sol)

  1. Calculate liquidity tokens to be minted
  2. Mint liquidity tokens for liquidity provider
  3. Update the reserves with _update function

Remove Liquidity

Caller

  1. Approve periphery contract to use liquidity provider's tokens
  2. Liquidity provider uses router contract to withdraw liquidity

In the periphery contract (UniswapV2Router02.sol)

  1. Sends liquidity tokens of liquidity provider to core contract

In the core contract (UniswapV2Pair.sol)

  1. Burn liquidity tokens
  2. Send assets back to liquidity provider
  3. Update the reserves with _update function

Resources I used -