Skip to content

API ReferenceReact provider and hooksFunction

useTokenPairPrice()

function useTokenPairPrice(inputToken?, outputToken?): UseTokenPairPriceResult;

Fetches the oracle exchange rate between two tokens.

Both addresses are required: while either is undefined the hook stays idle and writes no cache entry. Once both are present the rate stays fresh for 30 seconds and is re-read on that interval while the window is focused.

Unlike useCollateralFactor, this read does not fail closed. After one successful read a later failed refresh keeps TanStack’s retained value rather than blanking data.

Parameters

ParameterTypeDescription
inputToken?`0x${string}`ERC-20 the rate is quoted from. Omit to stay idle.
outputToken?`0x${string}`ERC-20 the rate is quoted in. Omit to stay idle.

Returns

UseTokenPairPriceResult

The read envelope. data is undefined until the first read settles; isLoading and error report request state, and retry re-runs the latest completed or failed request.

Throws

Error when rendered outside <AnvilProvider>, by way of useAnvilSDK.

Example

export function CollateralPerCredit() {
// Both addresses are required; one `undefined` leaves the hook idle.
const { data: price, error } = useTokenPairPrice(WETH, USDC);
if (error) return <p>{error.message}</p>;
return <span>{price ? price.price.toString() : 'Loading…'}</span>;
}