Skip to content
function useTokenPrice(tokenAddress?): UseTokenPriceResult;

Fetches a token’s USD price through the SDK’s oracle read.

An undefined address leaves the hook idle: no query is created and no cache entry is written, so a form may mount this before its token is chosen. Once an address is supplied the price stays fresh for 30 seconds and is re-read on that interval while the window is focused; TanStack pauses interval polling in the background.

Parameters

ParameterTypeDescription
tokenAddress?`0x${string}`ERC-20 to price. Omit to leave the hook idle.

Returns

UseTokenPriceResult

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 TokenUsdPrice({ token }: { token: Address }) {
// Polls every 30s while the tab is visible.
const { data: price, isLoading } = useTokenPrice(token);
if (isLoading || !price) return <span>—</span>;
return (
<span>
{price.price.toString()}e{price.exponent.toString()} USD
</span>
);
}