The most obvious way to get the address for a pair is to call getPair on the factory. If the pair exists, this function will return its address, else address(0)
(0x0000000000000000000000000000000000000000
).
Thanks to some fancy footwork in the factory, we can also compute pair addresses without any on-chain lookups because of CREATE2. The following values are required for this technique:
address | The factory address |
salt | keccak256(abi.encodePacked(token0, token1)) |
keccak256(init_code) | 0xbc919ae6f6f95dca1e223fc957286afa1da81529418e9f187db8a0b2d2e963bc |
token0
must be strictly less than token1
by sort order.keccak256
.address factory = 0xdD9EFCbDf9f422e2fc159eFe77aDD3730d48056d;address token0 = 0xCAFE000000000000000000000000000000000000; // change me!address token1 = 0xF00D000000000000000000000000000000000000; // change me!address pair = address(uint(keccak256(abi.encodePacked(hex'ff',factory,keccak256(abi.encodePacked(token0, token1)),hex'96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f'))));