UniswapV2Library
Last updated
Last updated
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
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:
compute pair (liquidity pool) addresses without any on-chain lookups because of CREATE2. The following values are required for this technique:
address
salt
keccak256(abi.encodePacked(token0, token1))
keccak256(init_code)
0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f
token0
must be strictly less than token1
by sort order.
UniswapV2Factory
is deployed at 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f
factory creates liquidity pools via createPair
The