Pair Addresses
getPair
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).
- The "canonical" way to determine whether or not a pair exists.
- Requires an on-chain lookup.
CREATE2
Ring Swap pair addresses can also be computed without any on-chain lookups because the factory uses CREATE2. The following values are required for this technique:
address | The factory address |
salt | keccak256(abi.encodePacked(token0, token1)) |
keccak256(init_code) | The Ring Swap Pair Init Code for the target chain from the deployment table. |
token0must be strictly less thantoken1by sort order.
- Can be computed offline.
- Requires the ability to perform
keccak256.
Examples
Solidity
address factory = 0xeb2A625B704d73e82946D8d026E1F588Eed06416; // Ring Swap Factory on Ethereum mainnet
address token0 = 0xCAFE000000000000000000000000000000000000; // change me!
address token1 = 0xF00D000000000000000000000000000000000000; // change me!
bytes32 initCodeHash = 0xa7ae6a5ec37f0c21bbdac560794258c4089b8ae3ffa6e3909b53c6091764a676;
address pair = address(uint160(bytes20(keccak256(abi.encodePacked(
hex'ff',
factory,
keccak256(abi.encodePacked(token0, token1)),
initCodeHash
))));