Get the pair's price
AgoraStableSwapPair exposes two read functions for price discovery:
| Function | When 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
// AUSD / CTK pair on Avalanche Fuji Testnet
const TESTNET_PAIR_AUSD_CTK =
'0x237591AaF2FCCb34464Ceae9EeA1eb6f375843AF';
2. Helper to read the price
/**
* Returns the price token0 / token1 at a given timestamp.
* Pass at=`undefined` to get the current block price.
*/
async function getPairPrice(
pair: `0x${string}`,
at?: number, // UNIX seconds
) {
const args: readonly [] | readonly [bigint] = at ? [BigInt(at)] : [];
return await client.readContract({
address: pair,
abi: stableSwapAbi,
functionName: "getPrice",
args,
});
}
3. Run the query (with a 5-minute deadline)
(async () => {
// 5 minutes from now → worst-case price for interest-bearing pairs
const deadline = Math.floor(Date.now() / 1000) + 300;
const priceRaw = await getPairPrice(TESTNET_PAIR_AUSD_CTK, deadline);
console.log("Raw price token0/token1 (18-dec):", priceRaw);
})();
Raw price token0/token1 (18-dec): 1000000n
info
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 pricing of the pair follows: