Use external RPCs in ChainGate
ChainGate lets you connect to any EVM-compatible network by providing an external JSON-RPC endpoint. This is useful for chains not natively supported by ChainGate, for using your own infrastructure, or for connecting to third-party providers like Infura or Alchemy.
Create a custom network
Use cg.networks.evmRpc() to register any EVM chain:
import { newWallet, ChainGate } from 'chaingate'
const { wallet } = newWallet()
const cg = new ChainGate({ apiKey: 'your-api-key' })
// Your own RPC endpoint
const bsc = cg.networks.evmRpc({
rpcUrl: 'https://bsc-dataseed.binance.org',
chainId: 56,
name: 'BNB Smart Chain',
symbol: 'BNB',
})
// A third-party provider
const optimism = cg.networks.evmRpc({
rpcUrl: 'https://optimism-mainnet.infura.io/v3/YOUR_KEY',
chainId: 10,
name: 'Optimism',
symbol: 'ETH',
})
Configuration parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
rpcUrl | string | Yes | JSON-RPC endpoint URL |
chainId | number | Yes | EVM chain ID (EIP-155) |
name | string | Yes | Human-readable network name |
symbol | string | Yes | Native coin symbol (e.g., 'BNB', 'ETH') |
decimals | number | No | Decimal places (defaults to 18) |
Connect and transact
Once registered, use the network with cg.connect() exactly like any built-in network:
const connector = cg.connect(bsc, wallet)
// Get address
const address = await connector.address()
// Check balance
const { confirmed } = await connector.addressBalance()
console.log('Balance:', confirmed.base().toString(), confirmed.symbol)
// Transfer
const amount = bsc.amount('0.1')
const tx = await connector.transfer(amount, '0xRecipient...')
tx.setFee('normal')
const broadcasted = await tx.signAndBroadcast()
Explore without a wallet
You can query data from custom networks without connecting a wallet:
const explorer = cg.explore(bsc)
const balance = await explorer.getBalance('0x...')
const receipt = await explorer.getTransactionReceipt('0xtxhash...')
const feeData = await explorer.getFeeData()
Custom RPC networks use EvmRpcExplorer, which wraps standard Ethereum JSON-RPC methods (balances, gas, receipts). Natively supported networks (like Ethereum or Polygon) use EvmExplorer, which provides richer data through the ChainGate API -- token balances, transaction history, NFT metadata, and more.
Mix and match with ChainGate RPCs
You can also point evmRpc() to ChainGate's own RPC proxy via cg.rpcUrls. This is useful for networks that ChainGate supports via RPC but that you want to interact with through the custom network API:
const polygon = cg.networks.evmRpc({
rpcUrl: cg.rpcUrls.polygon,
chainId: 137,
name: 'Polygon',
symbol: 'POL',
})
const connector = cg.connect(polygon, wallet)
Key points
- Any EVM chain: Connect to any network that speaks JSON-RPC -- BNB, Optimism, zkSync, Gnosis, or your own local node.
- Same API:
connect(),transfer(),address(), and all connector methods work identically. - EIP-1559 support: Transaction format (legacy or EIP-1559) is auto-detected based on the chain.
- Combine with ChainGate RPCs: Use
cg.rpcUrls.*as therpcUrlto route through ChainGate's infrastructure.