API ReferenceReact provider and hooksFunction
useTokenPrice()
function useTokenPrice(tokenAddress?): UseTokenPriceResult;Fetches a token’s USD Price from the oracle.
An undefined address leaves the hook idle and fetches nothing, so a form can mount it before its
token is chosen. Once an address is supplied, the price is re-read every 30 seconds while the page
is in the foreground.
Parameters
| Parameter | Type | Description |
|---|---|---|
tokenAddress? | `0x${string}` | ERC-20 to price. Omit to leave the hook idle. |
Returns
UseTokenPriceResult
A UseAnvilQueryResult holding the Price. 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> );}