0/5
We will be examining the function `getAmountIn` within the UniSwapV2Library. This function is used to calculate the amount of tokens needed for a swap. The input is the desired amount out, the reserves, and a path. We can understand how this function works by looking at a table. Let's say we have a list of amounts and the length of this list is some number N. We will start by listing out the indexes of this array, starting from N-1. The for loop runs from N-1 down to 1. In each iteration, the function `getAmountIn` is called. The input of the function is the current amount out, along with the reserves. The first iteration of the for loop will assign the amount out to the last element of the array, `amounts[N-1]`. The output of `getAmountIn` is then assigned to `amounts[N-2]`, effectively working its way backward down the list of amounts. The function `getAmountIn` uses an equation we derive from the Uniswap swap. ```javascript amountOut = numerator / denominator ``` The denominator in this equation is: ```javascript denominator = reserveOut - amountOut * 997 / 1000 ``` This equation is derived from the Uniswap swap equation where we solve for `dy`. ```javascript dy = reserveOut * amountOut * 997 / (amountOut * 997 + reserveIn * 1000) ``` The numerator in this equation is: ```javascript numerator = reserveIn * amountOut * 1000 / (amountOut * 997 + reserveIn * 1000) ``` The result of this calculation is then stored in the amounts array. We will keep looping through the array until we get to `amounts[0]`, which will contain the amount of tokens that are needed to execute the swap. This entire process is performed inside the for loop.
A detailed code walkthrough of the function getAmountIn in Uniswap V2 library - The lesson covers how the function calculates the required amount of tokens to get a certain amount of token out after a swap, along with its mathematical derivation.
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