Skip to main content
TypeScript-first: In Zig, build JSON-RPC payloads with std.json and POST via std.http.Client.

Network Methods

Network information methods for detecting chain ID, checking sync status, and querying node/account state.

Overview

// {"method":"eth_chainId","params":[]}
// {"method":"eth_syncing","params":[]}
// {"method":"eth_coinbase","params":[]}
// {"method":"eth_accounts","params":[]}

Methods

eth_chainId

Get the chain ID.
// Response is hex quantity (e.g., "0x1"); parse to integer
Returns: Chain ID as hex quantity (e.g., 0x1 for Ethereum mainnet) Usage:
// After parsing hex, match on known chain IDs (1, 11155111, 17000, etc.)

eth_syncing

Get sync status (false if not syncing).
// Response is either false or an object with startingBlock/currentBlock/highestBlock
Returns:
  • false if node is fully synced
  • SyncStatus object if syncing:
    • startingBlock: Quantity - Block where sync started
    • currentBlock: Quantity - Current synced block
    • highestBlock: Quantity - Highest known block
Usage:
// Compute progress: current/highest from hex quantities

eth_coinbase

Get the coinbase address (miner/validator).
// Returns coinbase address as hex (may be null/zero for non-mining nodes)
Returns: Address that receives mining/validation rewards Usage:
const response = await provider.eth_coinbase();
console.log(`Coinbase address: ${response.data}`);
Returns null or zero address if not mining/validating

eth_accounts

List available accounts (if wallet is connected).
// Usually empty from public nodes; populated when connected to a wallet
Returns: Array of available account addresses Usage:
const response = await provider.eth_accounts();

if (response.data.length === 0) {
  console.log('No accounts available');
} else {
  console.log(`Found ${response.data.length} account(s):`);
  response.data.forEach((address, i) => {
    console.log(`  ${i + 1}. ${address}`);
  });
}
Most public RPC nodes return empty array. Only works with personal/wallet connections.

Common Patterns

Network Detection

async function detectNetwork(provider: Provider) {
  const response = await provider.eth_chainId();
  const chainId = parseInt(response.data, 16);

  const networks: Record<number, string> = {
    1: 'Ethereum Mainnet',
    11155111: 'Sepolia',
    17000: 'Holesky',
    137: 'Polygon',
    10: 'Optimism',
    42161: 'Arbitrum One',
  };

  return networks[chainId] || `Unknown (${chainId})`;
}

Check Node Readiness

async function isNodeReady(provider: Provider): Promise<boolean> {
  const syncResponse = await provider.eth_syncing();

  // Node is ready if not syncing
  if (syncResponse.data === false) {
    return true;
  }

  // Check if syncing is near completion
  const { currentBlock, highestBlock } = syncResponse.data;
  const current = parseInt(currentBlock, 16);
  const highest = parseInt(highestBlock, 16);

  // Consider ready if within 10 blocks
  return (highest - current) < 10;
}

Verify Connection

async function verifyConnection(provider: Provider) {
  try {
    // Quick check - chainId is fast and always available
    const response = await provider.eth_chainId();
    return {
      connected: true,
      chainId: parseInt(response.data, 16),
    };
  } catch (error) {
    return {
      connected: false,
      error: error.message,
    };
  }
}

See Also