1/5
We're going to step through the `getAmountsOut` function in the Uniswap V2 library contract, step by step. This function helps us calculate the amount of tokens we'll receive after a series of swaps along a specified path. Let's start with an example. We'll input 1 WETH and specify a path that goes through DAI and then to MKR. The `getAmountsOut` function takes three inputs: * `amountIn`: The amount of the initial token we're starting with. * `path`: The list of tokens involved in the swap. In our example, this is `[WETH, DAI, MKR]`. * `factory`: The Uniswap factory contract. This contract is used to create and manage the liquidity pools for the different tokens on Uniswap. The first step is to initialize a `uint` array called `amounts` with the length of the path, which in this case is 3. The first element of this array will be set to our input amount, which is `1e18` (representing 1 WETH). ```javascript amounts = new uint[](path.length); amounts[0] = amountIn; ``` Next, we'll use a for loop to iterate through the path and calculate the output amounts for each swap. The loop will start at index 0 and continue until it reaches the end of the path. For each iteration, the loop will: 1. Determine the current pair contract by accessing `path[i]` and `path[i + 1]`. This pair contract represents the pool where we'll be swapping our current token. 2. Get the reserve balances for the two tokens in the pair contract. The `getReserves` function on the factory contract returns these reserve balances. 3. Calculate the amount of tokens we'll receive in the swap. This calculation is done using the `getAmountOut` function on the pair contract. 4. Store the calculated output amount in the `amounts` array at the `i + 1` index. ```javascript for (uint i = 0; i < path.length - 1; i++) { (uint reserveIn, uint reserveOut) = getReserves(factory, path[i], path[i + 1]); amounts[i + 1] = getAmountOut(amounts[i], reserveIn, reserveOut); } ``` The output of this function is the `amounts` array, which contains the calculated output amounts for each step in the swap path. In our example, the `amounts` array will end up containing the following values: * `amounts[0]`: 1e18 (1 WETH) * `amounts[1]`: 2,500.3397 * 1e18 (amount of DAI received after swapping 1 WETH) * `amounts[2]`: 1.2427 * 1e18 (amount of MKR received after swapping the DAI) The `getAmountsOut` function plays a crucial role in Uniswap V2, enabling users to calculate the potential outcomes of swaps and make informed trading decisions.
A technical walkthrough of the Uniswap V2 library 'getAmountsOut' function. The video steps through an example of how this function works by showing how each iteration of the code populates an array of amounts with the expected values, given an initial amount of 1 ETH and a path of WETH, DAI, and MKR.
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