UniswapV2Library
pairFor -> calculates the CREATE2 address of the liquidity pool
getReserves → fetches and sorts the reserves for a pair
getAmountsOut -> Iterates over the path of exchanges needed and performs chained getAmountOut calculations on any number of pairs
getAmountOut → given an input amount of an asset and pair reserves, returns the maximum output amount of the other asset considering a 0.3% fee
sortTokens
If the tokens weren't sorted, it would result in a distinct pair address for each sort order:
Tokens: [A,B] -> Pair address: X
Tokens: [B,A] -> Pair address: Y
By sorting the tokens first, the address of any given pair is pre-determined, and can be computed without any on-chain lookups (using CREATE2).
You have to sort the tokens first, otherwise you will get 2 different pair addresses for each pair.
Regardless of scenario, token0 will be the smaller address:
pairFor
compute pair (liquidity pool) addresses without any on-chain lookups because of CREATE2. The following values are required for this technique:
| The factory address |
|
|
|
|
token0
must be strictly less thantoken1
by sort order.UniswapV2Factory
is deployed at0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f
factory creates liquidity pools via
createPair
Last updated