Hooks for interacting with blockchain networks and chain data.
BuilderKit provides hooks for interacting with blockchain networks and managing chain-related data.
The useChains
hook provides functions for accessing and managing chain information.
import { useChains } from '@avalabs/builderkit';
const {
getChains,
getChain,
getProvider,
getBlock
} = useChains();
// Get all configured chains
const chains = getChains();
// Get specific chain info
const chain = getChain(43114); // Avalanche C-Chain
// Get provider for a chain
const provider = getProvider(43114);
// Get block information
const block = await getBlock(43114, 12345);
getChains()
: Get list of all configured chains
getChain(chain_id: number)
: Get chain information by ID
getProvider(chain_id: number)
: Get ethers provider for a chain
getBlock(chain_id: number, block_number: number)
: Get block information
function ChainSelector() {
const { getChains, getChain } = useChains();
const [selectedChainId, setSelectedChainId] = useState<number>();
const chains = getChains();
const selectedChain = selectedChainId ? getChain(selectedChainId) : undefined;
return (
<div>
<h2>Current Chain: {selectedChain?.name}</h2>
<select
value={selectedChainId}
onChange={(e) => setSelectedChainId(Number(e.target.value))}
>
{chains.map(chain => (
<option key={chain.id} value={chain.id}>
{chain.name}
</option>
))}
</select>
</div>
);
}