1/5
## The Uniswap V2 Pair Contract In this lesson, we will be walking through the code for the Uniswap V2 Pair Contract, which is responsible for managing the exchange of tokens. The Pair contract is located inside the V2-core repo under the contracts folder. We can access it by navigating to the following directory: ```bash Uniswap/v2-core/contracts/UniswapV2Pair.sol ``` The `swap` function is the core functionality of this contract, responsible for swapping tokens. We will focus our attention on the `swap` function and explore its different parts. The first part of the `swap` function checks for the invariant: *x* *y* after the swap must be greater than or equal to *x* *y* before the swap. This check ensures that the price of the tokens does not change drastically due to a swap. Let's break down how this check is implemented in the code. The variables `x0` and `y0` represent the amount of tokens in the contract before the swap. The calculation of the amount of tokens that came in, `dx` and `dy`, is also important. This is calculated by taking the difference between the actual balance of the tokens and the internal balance of the tokens. The code snippet demonstrates the calculation of `balance0Adjusted`, which is a key component of the invariant check: ```javascript uint balance0Adjusted = balance0.mul(1000).sub(amount0In.mul(3)); ``` The `balance0Adjusted` is calculated by multiplying the actual balance of token0, `balance0`, by 1,000 and then subtracting the amount of token0 that came in, `amount0In`, multiplied by 3. This step adjusts the balance to account for the swap fees. Next, the code checks if the product of `balance0Adjusted` and `balance1Adjusted` is greater than or equal to the product of `reserve0` and `reserve1`, multiplied by 1,000 squared. This verifies the invariant condition. ```javascript require(balance0Adjusted.mul(balance1Adjusted) >= reserve0.mul(reserve1).mul(1000 ** 2), 'UniswapV2: INSUFFICIENT_INPUT_AMOUNT'); ``` This step ensures that the price of the tokens remains stable after the swap. We will cover more details about the Uniswap V2 Pair Contract, including its implementation of flash swaps and the time-weighted average pricing, in future lessons.
A detailed code walkthrough of the Uniswap V2 Pair smart contract's swap function. The lesson covers how the swap function calculates the amount of tokens that came in, transfers one token for the other, and avoids stack too deep errors by wrapping code inside curly braces.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
How to use Uniswap v2 dex and contracts
Interacting with the Uniswap v2 router and factory
How to create Uniswap v2 liquidity pools
How to add liquidity to Uniswap v2 pools
Swaps, flash swaps, flash swap arbitrage, and time-weighted average price (TWAP)
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on October 9, 2024
Solidity Developer
Uniswap V2Duration: 14min
Duration: 1h 20min
Duration: 10min
Duration: 54min
Duration: 25min
Duration: 26min
Duration: 1h 03min
Duration: 59min
Course Overview
About the course
How to use Uniswap v2 dex and contracts
Interacting with the Uniswap v2 router and factory
How to create Uniswap v2 liquidity pools
How to add liquidity to Uniswap v2 pools
Swaps, flash swaps, flash swap arbitrage, and time-weighted average price (TWAP)
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on October 9, 2024
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar