✨Hello, everyone! I'm Momo. I've recently been learning about Ethereum-related knowledge and have organized some notes from my learning process into articles to share with everyone. This 0x1 series is basic knowledge for beginners, and I hope like-minded friends can learn and discuss together. I also welcome guidance from experts.
# 0x00 Introduction
We have previously seen the principles and underlying logic of Uniswap V2. Interested friends can check out the previous articles first.
Uniswap V2 is divided into two main parts:
Core: Contains 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 core logic and processing of a single swap; Periphery builds services on top of all swaps.
# 0x01 Uniswap V2 Core Analysis
## UniswapV2Factory: Creation and Management of Trading Pairs
`UniswapV2Factory` contract is one of the core components of the Uniswap V2 protocol, responsible for creating and managing all trading pairs (Pairs). It provides several key functions, including `createPair`, `allPairs`, and `getPair`.
Note: You can check the contract function information by querying the UniswapV2Factory contract address on the blockchain explorer.

🦄### allPairs and allPairsLength
`allPairs` is an array used to store all created trading pair contract addresses. 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 get the number of trading pairs, making it easier for developers and users to understand the total number of available trading pairs in the current protocol.
• Usage of `allPairs`
By indexing the `allPairs` array, users can access the trading pair address at a specific index.

This functionality allows users to query specific trading pair addresses by index, facilitating traversal or query operations on the chain.
🦄### getPair
`getPair` is a mapping used for quickly querying the trading pair address between two tokens. It uses the addresses of `tokenA` and `tokenB` as keys and returns the corresponding trading pair contract address.
• `getPair` function

The implementation of this function is based on a two-dimensional mapping, querying the trading pair address using the combination of `tokenA` and `tokenB` addresses. If the trading pair does not exist, it returns `0x0000000000000000000000000000000000000000`.
• Creation logic of `getPair`
In the `createPair` function, `getPair` is used to record the newly created trading pair address.

The benefit of this design is that regardless of the order of `tokenA` and `tokenB`, corresponding trading pair addresses can be quickly queried using `getPair`.
🦄### createPair
`createPair` function is one of the core functions of `UniswapV2Factory`, used to create new trading pairs. It takes two token addresses `tokenA` and `tokenB` as input and returns the created trading pair contract address.
Creation logic

First, the function checks if `tokenA` and `tokenB` are the same and sorts them to ensure that the address of `token0` is less than that of `token1`.
Then, a new trading pair contract is created using the `create2` opcode, and the contract address is recorded in the `getPair` mapping and `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: The Bridge for User Interaction
`UniswapV2Router02` is the main interaction interface between users and the Core contract, encapsulating the functionalities of the Core contract to provide users with more convenient trading and liquidity management functions. Its main functionalities include:
🦄### Liquidity Management
`addLiquidity` and `addLiquidityETH` functions allow users to add liquidity.
`removeLiquidity` and `removeLiquidityETH` functions are used to remove liquidity.
🦄### Trading Functions
`swapExactTokensForTokens` and `swapTokensForExactTokens` functions support users in token swaps.
`swapExactETHForTokens` and `swapTokensForExactETH` functions support swaps between ETH and ERC20 tokens.
🦄### Flash Swaps Support
`UniswapV2Router02` supports Flash Swaps, allowing users to borrow tokens and perform a series of operations in the same transaction, and finally return the tokens.
## Libraries: Function Extension of Utility Libraries
The Periphery part also includes several utility libraries to provide additional functional support:
🦄### UniswapV2Library
Provides a function to compute trading pair addresses called `pairFor`.
Provides price calculation functions `getAmountOut` and `getAmountIn` for calculating the input and output token amounts for trades.
🦄### UniswapV2OracleLibrary
Provides TWAP calculation functionality, allowing users to obtain price data based on time-weighted average prices.
# 0x03 Summary
Uniswap V2 achieves the core functionality of decentralized trading through both Core and Periphery.
`UniswapV2Factory` contract efficiently manages the creation and querying of trading pairs through functions like `createPair`, `allPairs`, and `getPair`.
`UniswapV2Router02` provides users with a convenient interaction interface that encapsulates the core logic of trading and liquidity management. This layered design not only enhances the flexibility of the protocol but also strengthens its scalability.
-- Whether from the code or overall design perspective, it is truly impressive! No wonder it is the soul creator in DeFi!