Skip to main content

Getting a pair's purchase fees

The AgoraStableSwapPair contract stores the current purchase-fee rate (in 18-dec "fee precision" units) for each token direction:

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

1. Pick a pair address

// AUSD / CTK pair on Avalanche Fuji Testnet
const TESTNET_PAIR_AUSD_CTK = '0x237591AaF2FCCb34464Ceae9EeA1eb6f375843AF';

2. Helper to read the purchase fees

/**
* Returns raw purchase-fee rates (18-dec precision).
*/
async function getPairPurchaseFees(
pair: `0x${string}`,
): Promise<{ token0PurchaseFee: bigint; token1PurchaseFee: bigint }> {
const token0PurchaseFee = await client.readContract({
address: pair,
abi: stableSwapAbi,
functionName: "token0PurchaseFee",
args: [],
});

const token1PurchaseFee = await client.readContract({
address: pair,
abi: stableSwapAbi,
functionName: "token1PurchaseFee",
args: [],
});

return { token0PurchaseFee, token1PurchaseFee };
}

3. Run the query

(async () => {
const { token0PurchaseFee, token1PurchaseFee } = await getPairPurchaseFees(
TESTNET_PAIR_AUSD_CTK,
);

console.log("Purchase fee (token0 → token1):", token0PurchaseFee);
console.log("Purchase fee (token1 → token0):", token1PurchaseFee);
})();
Purchase fee (token0 → token1): 100000000000000n   // 0.01% (1 bps)
Purchase fee (token1 → token0): 200000000000000n // 0.02% (2 bps)

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