viem
Version:
29 lines • 918 B
JavaScript
import { hexToNumber, } from '../../utils/encoding/fromHex.js';
/**
* Returns the chain ID associated with the current network.
*
* - Docs: https://viem.sh/docs/actions/public/getChainId
* - JSON-RPC Methods: [`eth_chainId`](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_chainid)
*
* @param client - Client to use
* @returns The current chain ID. {@link GetChainIdReturnType}
*
* @example
* import { createPublicClient, http } from 'viem'
* import { mainnet } from 'viem/chains'
* import { getChainId } from 'viem/public'
*
* const client = createPublicClient({
* chain: mainnet,
* transport: http(),
* })
* const chainId = await getChainId(client)
* // 1
*/
export async function getChainId(client) {
const chainIdHex = await client.request({
method: 'eth_chainId',
}, { dedupe: true });
return hexToNumber(chainIdHex);
}
//# sourceMappingURL=getChainId.js.map