When adding a new or existing liquidity pool, the initial step is typically to verify if the liquidity pool already exists before determining the next course of action. The factory contract provides a method to retrieve the pool address, if it exists, as shown below.

Checking if the liquidity pool exists using SaucerSwapโ€™s REST API is also a suitable alternative. For more information, see Get V1 liquidity pools

Get the Existing Liquidity Pool if it Exists

โ›ฝ No gas cost

Get the existing liquidity poolโ€™s CREATE2 EVM address if it exists. If the pool does not exist on-chain, a zero address will be returned.

Function name: getPair

Parameter NameDescription
address tokenAEVM address of the first token
address tokenBEVM address of the second token
function getPair(
  address tokenA,
  address tokenB
) external view returns (address pair);

The ordering of tokens for tokenA and tokenB does not matter.

When working with HBAR, use the wrapped HBAR token ID (WHBAR) for either tokenA or tokenB.

Code Overview

Resources:

Typescript
import * as ethers from 'ethers'; //V6

//Set one of Hedera's JSON RPC Relay as the provider
const provider = new ethers.JsonRpcProvider(hederaJsonRelayUrl, '', {
  batchMaxCount: 1, //workaround for V6
});

//load ABI data containing the Factory's getPair function
const interfaces = new ethers.Interface(abi);

const factoryContract = new ethers.Contract(factoryEvmAddress, interfaces.fragments, provider);
const result = await factoryContract.getPair(tokenA, tokenB); //(tokenB, tokenA) will give same result
const poolEvmAddress = result; //address pool