Fetching Pair Price

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

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.

Pick the pair address

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

Helper function 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}

Example use of the helper function (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})();

Returned Data

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}