Skip to main content

Access blockchain utilities with useUtils()

The useUtils() hook provides access to the initialized blockchain utilities from anywhere within your React component tree. It is a thin context wrapper that lets you interact with ChainGate utility functions without managing state manually.

Quick setup

1. Wrap your app with the provider

Start by wrapping your application or a subtree with the UtilsContext, providing your API key:

import { UtilsContext } from 'chaingate'

function App() {
return (
<UtilsContext apiKey="your-api-key">
<YourComponent />
</UtilsContext>
)
}

2. Use the hook in any component

You can now access the utilities from any component inside the provider:

import { useUtils } from 'chaingate'

function Dashboard() {
const { utils } = useUtils()

// Example usage
const fetchLatestBlock = async () => {
const block = await utils?.currency('bitcoin').latestBlock()
console.log('Latest block:', block)
}
}

What it gives you

Use this to access blockchain utility methods for any supported currency. For a full list of available features, see the blockchain utils documentation.

utils?.currency('bitcoin').latestBlock()
utils?.currency('polygon').estimateGas(...)

Error handling

If useUtils() is called outside the <UtilsContext> provider, it will throw an error:

Error: useUtils must be used within <UtilsContext>

Make sure your component is inside the provider tree.

Key points

  • Clean and simple access to currency utility methods
  • Abstracts client/market initialization
  • Works with all supported currencies through .currency(id)