Uniswap v2 review
The essence of Uniswap is a series of solidity smart contracts that builds a decentralised trading platform. This article will talk about version 2 of Uniswap.
Firstly, let’s ask a question: What is good about decentralised trading platform?
Answer: There will not be a centralised entity that decides which stock can be purchased by you. Think about the Robinhood and GME (Gamestop) case.
The Uniswap comes with core smart contracts and peripheral smart contracts. In uniswap-v2-core, which contains the core smart contracts, contracts/UniswapV2ERC20.sol, it implements the IUniswapV2ERC20 interface. The key functions of the Uniswap v2 erc20 are :
- mint : increase liquidity
- burn : decrease liquidity
- approve: approve the amount to be sold
- transfer: transfer the amount from you to buyer
- transferFrom: transfer the amount from third party to buyer
- permit: sign an approval off-chain, reduce 2 step process to 1 step
In uniswap-v2-core, contracts/UniswapV2Factory.sol, it implements the IUniswapV2Factory interface. The key functions of the Uniswap v2 factory are:
- createPair: create the token pair that is sold and bought
- setFeeTo: set the transaction fee
In uniswap-v2-core, contracts/UniswapV2Pair.sol, it implements the IUniswapV2Pair interface and UniswapV2ERC20. The key functions of the Uniswap v2 pair are:
- mint: update the balance after liquidity is increased
- burn: update the balance after liquidity is reduced
- swap: adjust the balance of tokens so that the invariant stays constant (The product of pair x * y = k is invariant)
In uniswap-v2-periphery, contracts/UniswapV2Migrator.sol, it implements the IUniswapV2Migrator interface. The key functions of the Uniswap v2 migrator are :
- migrate: support migration from v1 exchange
In uniswap-v2-periphery, contracts/UniswapV2Router01.sol, it implements the IUniswapV2Router01 interface. The key functions of the Uniswap v2 router01 are :
- removeLiquidity: called by frontend to remove liquidity
- addLiquidity: called by frontend to increase liquidity
- a series of swap functions
In addition, in uniswap-v2-periphery, there is another contracts/UniswapV2Router02.sol, it implements the IUniswapV2Router02 interface. It builds on top of IUniswapV2Router01.
There is also uniswap-lib which provides library to the Uniswap v2 core. For example, there is Babylonian library that implements the square root computation efficiently.
Besides that, Uniswap comes with a ReactJS frontend example (uniswap-interface) that interfaces to the Uniswap smart contract. In the uniswap-interface, utils/index.ts, getContract() function, it calls:
new Contract(address, ABI, getProviderOrSigner(library, account) as any)
to create a contract object so that frontend can interact with on-chain smart contract. The Contract is a class from @ethersproject/contracts.
To show how the frontend calls the methods in smart contract, let us look at uniswap-interface, pages/RemoveLiquidity/index.ts. In the frontend typescript, the solidity method “removeLiquidity” is called to remove liquidity in the blockchain. Basically, the typescript function RemoveLiquidity sets the methodNames, args, and then sends a transaction.
There is Uniswap SDK, as in uniswap-sdk. It is written in typescript used in the uniswap-interface. For example, the sdk exports Token, Currency, Pair, TokenAmount.
In the frontend uniswap-interface, it depends on token-list to show a list of tokens. The token-list defines the functions for token version management, and interfaces for tokens.