Fetching Pair Price

AgoraStableSwapPair exposes two read functions for price discovery:

FunctionWhen to use
getPrice() (no args)Current on-chain price at block.timestamp.
getPrice(uint256 timestamp)Future price, using the pair’s base price + interest-rate oracle. Pass a deadline to model worst-case execution when one token accrues interest.

1. Pick the pair address

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

2. Helper to read the price

1/**
2 * Returns the price token0 / token1 at a given timestamp.
3 * Pass at=`undefined` to get the current block price.
4 */
5async function getPairPrice(
6 pair: `0x${string}`,
7 at?: number, // UNIX seconds
8) {
9 const args: readonly [] | readonly [bigint] = at ? [BigInt(at)] : [];
10 return await client.readContract({
11 address: pair,
12 abi: stableSwapAbi,
13 functionName: "getPrice",
14 args,
15 });
16}

3. Run the query (with a 5-minute deadline)

1(async () => {
2 // 5 minutes from now → worst-case price for interest-bearing pairs
3 const deadline = Math.floor(Date.now() / 1000) + 300;
4
5 const priceRaw = await getPairPrice(TESTNET_PAIR_AUSD_CTK, deadline);
6
7 console.log("Raw price token0/token1 (18-dec):", priceRaw);
8})();
Raw price token0/token1 (18-dec): 1000000n
Token order in the pair

Deadline logic

For pairs where token1 is interest-bearing, passing a future timestamp guards against receiving fewer tokens than expected if block processing is delayed. For non-yield token pairs, deadline simply replays the current price.

Understanding Pricing

Since token0 and token1 might have different decimals, the pricing of the pair follows:

Price=10decToken010decToken1×1018Price=\frac{ 10^{\text{decToken}_0}} {10^{\text{decToken}_1}} \times 10^{18}