✨Hello, everyone. I am Momo. I have been learning about Ethereum recently. I have compiled some notes from the learning process into articles to share with you. This 0x1 series is about basic knowledge for beginners. I hope that like-minded friends can study and discuss together. Please give me more advice.
# 0x00 Preface
We have already seen the principles and underlying logic of Uniswap V2 before. Those who are interested can read the previous articles first.
Uniswap V2 is divided into two main parts:
Core: including Factory, Pair, Pair ERC20
Periphery: Contains Routers
Mainnet contract deployment address
Factory Contract Address:0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f
V2Router02 Contract Address:0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
Among them, Core is mainly responsible for the core logic and the processing of a single swap; Periphery is a peripheral service that builds services based on all swaps.
# 0x01 Uniswap V2 Core Analysis
## UniswapV2Factory: Creation and management of trading pairs
The `UniswapV2Factory` contract is one of the core components of the Uniswap V2 protocol, responsible for creating and managing all trading pairs. It provides several key functions, including `createPair`, `allPairs`, and `getPair`.
Note: You can view the contract function information by querying the UniswapV2Factory contract address in the blockchain browser
🦄### allPairs 与 allPairsLength
`allPairs` is an array used to store the contract addresses of all created trading pairs. Through the `allPairs` array, users can query the address information of all trading pairs.
• `allPairsLength` function
This function returns the length of the `allPairs` array, which is the total number of trading pairs currently created.
This function provides users with a quick way to obtain the number of trading pairs, making it easier for developers and users to understand the total number of trading pairs available in the current protocol.
• Use of `allPairs`
By accessing the `allPairs` array through the index, you can get the transaction pair address at a specific index position.
This function allows users to query specific transaction pair addresses through indexes, making it easier to traverse or query on the chain.
🦄### getPair
`getPair` is a mapping used to quickly query the transaction pair address between two tokens. It uses the addresses of `tokenA` and `tokenB` as keys and returns the corresponding transaction pair contract address.
• `getPair` function
The implementation of this function is based on a two-dimensional mapping, and queries the transaction pair address through the address combination of `tokenA` and `tokenB`. If the transaction pair does not exist, it returns `0x000000000000000000000000000000000000000000000`.
• `getPair` creation logic
In the `createPair` function, `getPair` is used to record the newly created transaction pair address.
The advantage of this design is that no matter what the order of `tokenA` and `tokenB` is, the corresponding transaction pair address can be quickly queried through `getPair`.
🦄### createPair
The `createPair` function is one of the core functions of `UniswapV2Factory`, which is used to create a new trading pair. It receives two token addresses `tokenA` and `tokenB` as input and returns the contract address of the created trading pair.
Create Logic
First, the function checks if `tokenA` and `tokenB` are the same, and sorts them, making sure that the address of `token0` is smaller than `token1`.
Then, a new trading pair contract is created through the `create2` opcode, and the contract address is recorded in the `getPair` mapping and the `allPairs` array.
Finally, the `PairCreated` event is triggered to notify external listeners that a new trading pair has been created.
# 0x02 Uniswap V2 Periphery Analysis
UniswapV2Router02: A bridge for user interaction
`UniswapV2Router02` is the main interaction interface between users and the Core contract. It encapsulates the functions of the Core contract and provides users with more convenient trading and liquidity management functions. Its main functions include:
🦄### Liquidity Management
The `addLiquidity` and `addLiquidityETH` functions allow users to add liquidity.
The `removeLiquidity` and `removeLiquidityETH` functions are used to remove liquidity.
🦄### Trading Features
The `swapExactTokensForTokens` and `swapTokensForExactTokens` functions support users to swap tokens.
The `swapExactETHForTokens` and `swapTokensForExactETH` functions support swapping between ETH and ERC20 tokens.
🦄### Flash Swaps support
`UniswapV2Router02` supports Flash Swaps, which allow users to borrow tokens and complete a series of operations in the same transaction and finally return the tokens.
## Libraries: Functional extension of tool libraries
The Periphery part also includes several tool libraries to provide additional functional support:
🦄### UniswapV2Library
Provides the calculation function `pairFor` for transaction pair addresses.
Provides price calculation functions `getAmountOut` and `getAmountIn` to calculate the input and output token amounts of a transaction.
🦄### UniswapV2OracleLibrary
Provides TWAP calculation function, allowing users to obtain price data based on time-weighted average price.
# 0x03 Summary
Uniswap V2 implements the core functions of decentralized trading through Core and Periphery.
The `UniswapV2Factory` contract efficiently manages the creation and query of trading pairs through functions such as `createPair`, `allPairs`, and `getPair`.
`UniswapV2Router02` provides users with a convenient interactive interface that encapsulates the core logic of trading and liquidity management. This layered design not only improves the flexibility of the protocol, but also enhances its scalability.
-- Whether from the code or the overall design, it is really amazing! It is worthy of being the soul of Defi!