Fetching Pair Reserves

Fetching live reserves is useful for knowing the liquidity on each side of the pair before you trade.

If you want to run this example, be sure that you have followed the steps here to configure your Wallet Client.

Pick a pair address

1// AUSD / CTK pair on Sepolia
2const TESTNET_PAIR_AUSD_CTK = '0x1Aa8958Aa34cEC8096EF4381cb335effe977b0ae';

In order to query the reserves from the contract, we’ll call the methods reserve0 and reserve1

Helper function to read raw reserves

1/**
2 * Returns the raw on-chain reserves (no decimal conversion).
3 */
4async function getPairReserves(
5 pair: `0x${string}`,
6): Promise<{ reserve0: bigint; reserve1: bigint }> {
7 const reserve0 = await client.readContract({
8 address: pair,
9 abi: stableSwapAbi,
10 functionName: "reserve0",
11 });
12
13 const reserve1 = await client.readContract({
14 address: pair,
15 abi: stableSwapAbi,
16 functionName: "reserve1",
17 });
18
19 return { reserve0, reserve1 };
20}

Example use of the helper function

1(async () => {
2 const { reserve0, reserve1 } = await getPairReserves(TESTNET_PAIR_AUSD_CTK);
3
4 console.log(`Reserves token0 (AUSD – 6 dec):`, reserve0);
5 console.log(`Reserves token1 (CTK – 18 dec):`, reserve1);
6})();

Returned Data

1Reserves token0 (AUSD – 6 dec): 1000000000000n
2Reserves token1 (CTK – 18 dec): 1000000000000000000000000n

This is the value stored by the contract. In order to convert these units to human readable values, we’ll need to divide the output by the ERC20 token decimals.