Pair Address

🔍 Getting a Pair Address

To interact with a liquidity pool on Zealous Swap, you first need the address of the pair contract that manages the pool between two tokens. There are two ways to get this address:


✅ Method 1: On-Chain via getPair

The simplest way is to use the factory contract’s getPair() function:

address pair = IZealousSwapFactory(factory).getPair(tokenA, tokenB);
  • If the pair exists, it returns the pair contract address.

  • If the pair does not exist, it returns address(0).

Note: The tokens must be passed in any order. The factory handles the sorting internally.


⚡ Method 2: Off-Chain via CREATE2 (No RPC Needed)

Zealous Swap uses CREATE2 to deploy pairs at deterministic addresses. This means you can pre-compute a pair’s address without interacting with the blockchain.

To do this, you’ll need:

  • factory: the Zealous Factory address

  • token0, token1: the two token addresses, sorted in ascending order

  • initCodeHash: the hash of the pair creation bytecode used by the factory


🧮 Solidity Example

address factory = 0xZealousSwapFactoryAddress;
address token0 = 0xCAFE...; // lower address
address token1 = 0xF00D...; // higher address

bytes32 salt = keccak256(abi.encodePacked(token0, token1));
bytes32 initCodeHash = hex'...'; // Zealous Swap Pair init code hash

address pair = address(uint160(uint(keccak256(abi.encodePacked(
  hex'ff',
  factory,
  salt,
  initCodeHash
)))));

Make sure token0 < token1. If not, sort them before hashing.


🔐 Pair Sorting Logic

Zealous Swap enforces a consistent token order in every pair. Before calling keccak256(abi.encodePacked(token0, token1)), you must ensure:

(token0 < token1)

This guarantees consistent pair addresses across clients and tools.


✅ Summary

Method
Lookup
Requires RPC
Use Case

getPair()

On-chain

✅ Yes

In-contract logic

CREATE2

Offline

❌ No

Precomputing or indexing

Last updated