Fetching Purchase Fees

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

The AgoraStableSwapPair contract stores the current purchase-fee rate :

  • token0PurchaseFee – fee charged when buying token0 (i.e., swapping into token0).
  • token1PurchaseFee – fee charged when buying token1.

The returned rates are both using 18 decimal “fee precision” units.

Pick a pair address

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

Helper function to read the purchase fees

1/**
2 * Returns raw purchase-fee rates (18-dec precision).
3 */
4async function getPairPurchaseFees(
5 pair: `0x${string}`,
6): Promise<{ token0PurchaseFee: bigint; token1PurchaseFee: bigint }> {
7 const token0PurchaseFee = await client.readContract({
8 address: pair,
9 abi: stableSwapAbi,
10 functionName: "token0PurchaseFee",
11 args: [],
12 });
13
14 const token1PurchaseFee = await client.readContract({
15 address: pair,
16 abi: stableSwapAbi,
17 functionName: "token1PurchaseFee",
18 args: [],
19 });
20
21 return { token0PurchaseFee, token1PurchaseFee };
22}

Example use of the helper function

1(async () => {
2 const { token0PurchaseFee, token1PurchaseFee } = await getPairPurchaseFees(
3 TESTNET_PAIR_AUSD_CTK,
4 );
5
6 console.log("Purchase fee (token0 → token1):", token0PurchaseFee);
7 console.log("Purchase fee (token1 → token0):", token1PurchaseFee);
8})();

Returned Data

1Purchase fee (token0 → token1): 100000000000000n // 0.01% (1 bps)
2Purchase fee (token1 → token0): 200000000000000n // 0.02% (2 bps)

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.