1/5
In this lesson, we'll walk through how the `getAmountOut` function in the UniswapV2 library calculates the amount of tokens coming out of a swap. Let's start by looking at the `getAmountOut` function. ```javascript // given an input amount of an asset and pair reserves, returns an equivalent amount of the other asset function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) internal pure returns (uint amountOut) { require(amountIn > 0 && reserveIn > 0 && reserveOut > 0, 'UniswapV2Library: INSUFFICIENT_INPUT_AMOUNT'); // NOTE: // x = token in // y = token out // dx = amountIn * 997 / 1000 // dy = dx * 997 / 1000 * yo // ---------- // xo * 1000 + dx * 997 // NOTE: dx * 997 uint amountInWithFee = amountIn.mul(997); // NOTE: dx * 997 * yo uint numerator = amountInWithFee.mul(reserveOut); // NOTE: xo * 1000 uint denominator = reserveIn.mul(1000).add(amountInWithFee); // NOTE: // dy = dx * 997 / 1000 * yo // ---------- // xo * 1000 + dx * 997 amountOut = numerator / denominator; } ``` In this function, we calculate the amount out, `amountOut`, using a formula that we previously derived. We denote `amountIn` as `dx` and `reserveOut` as `yo`, the amount of tokens that are coming out of the swap. We calculate the `amountInWithFee` by multiplying `amountIn` by 997 and dividing by 1000. We also have the numerator, which is `amountInWithFee` multiplied by `reserveOut` or `dx * 997 * yo`. The denominator is `reserveIn` multiplied by 1000 added to `amountInWithFee`. ```javascript // NOTE: xo * 1000 uint denominator = reserveIn.mul(1000).add(amountInWithFee); ``` We denote `reserveIn` as `xo`, the current reserve for the token going into the swap. The denominator translates to `xo * 1000 + dx * 997`. The `amountOut` is then calculated as the `numerator` divided by the `denominator`. ```javascript amountOut = numerator / denominator; ``` We can see that the code reflects the equation we derived: ```javascript // dx * 997 / 1000 * yo // ---------- // xo * 1000 + dx * 997 ``` We'll cover the rest of the code in our next lesson.
A detailed explanation of the `getAmountOut` function from the UniswapV2Library Solidity code. The lesson covers how the function works, what calculations are being performed, how the code is structured, and how it relates to other UniswapV2 functions like `getAmountIn` and `getReserves`.
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