eth_chainId
Returns the chain ID of the Optimism network, used for transaction signing and network identification.
When to Use This Method
eth_chainId
is essential for:
- Transaction Signing - Required for EIP-155 compliant transactions
- Network Verification - Confirm you're connected to Optimism (Chain ID: 10)
- Multi-chain Applications - Distinguish between different networks
- Wallet Integration - Verify correct network before operations
Parameters
None. This method takes no parameters.
{
"jsonrpc": "2.0",
"method": "eth_chainId",
"params": [],
"id": 1
}
Returns
QUANTITY
- The chain ID as a hexadecimal string.
- Optimism Mainnet:
0xa
(10 in decimal) - Type: Hexadecimal string
- Format:
0x
prefixed
Implementation Examples
- cURL
- JavaScript
curl -X POST https://api-optimism-mainnet-archive.n.dwellir.com/YOUR_API_KEY \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "eth_chainId",
"params": [],
"id": 1
}'
// Using ethers.js
import { JsonRpcProvider } from 'ethers';
const provider = new JsonRpcProvider('https://api-optimism-mainnet-archive.n.dwellir.com/YOUR_API_KEY');
async function verifyOptimismNetwork() {
const network = await provider.getNetwork();
console.log('Network name:', network.name);
console.log('Chain ID:', network.chainId.toString());
if (network.chainId === 10n) {
console.log('✅ Connected to Optimism Mainnet');
} else {
console.log('⚠️ Not connected to Optimism Mainnet');
}
return network;
}
// Using raw JSON-RPC
async function getChainId() {
const response = await fetch('https://api-optimism-mainnet-archive.n.dwellir.com/YOUR_API_KEY', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
method: 'eth_chainId',
params: [],
id: 1
})
});
const data = await response.json();
const chainId = parseInt(data.result, 16);
console.log('Optimism Chain ID:', chainId);
return chainId;
}
// Network validation helper
async function validateOptimismConnection() {
try {
const chainId = await getChainId();
const networks = {
10: 'Optimism Mainnet',
11155420: 'Optimism Sepolia Testnet'
};
const networkName = networks[chainId] || 'Unknown Network';
return {
chainId,
networkName,
isOptimism: chainId === 10 || chainId === 11155420,
isMainnet: chainId === 10
};
} catch (error) {
console.error('Failed to validate network:', error);
return null;
}
}
Response Example
{
"jsonrpc": "2.0",
"id": 1,
"result": "0xa"
}
Note: Chain ID 0xa
(10 in decimal) identifies Optimism Mainnet, the Layer 2 scaling solution for Ethereum.
Optimism Network Information
Mainnet Configuration
- Chain ID:
10
(0xa
in hex) - Network Name: Optimism Mainnet
- Currency: ETH
- Type: Layer 2 (Optimistic Rollup)
Testnet Configuration
- Chain ID:
11155420
(0xaa37dc
in hex) - Network Name: Optimism Sepolia
- Currency: ETH
- Type: Layer 2 Testnet
Common Use Cases
1. Transaction Signing Verification
async function prepareOptimismTransaction(to, value, data) {
const chainId = await provider.getNetwork().then(n => n.chainId);
if (chainId !== 10n) {
throw new Error(`Expected Optimism (10), got chain ID: ${chainId}`);
}
const transaction = {
to,
value,
data,
chainId: chainId,
type: 2 // EIP-1559 transaction
};
return transaction;
}
2. Multi-chain Application Setup
const SUPPORTED_NETWORKS = {
1: { name: 'Ethereum Mainnet', type: 'L1' },
10: { name: 'Optimism', type: 'L2' },
137: { name: 'Polygon', type: 'L2' },
42161: { name: 'Arbitrum One', type: 'L2' }
};
async function detectNetwork() {
const chainId = await getChainId();
const network = SUPPORTED_NETWORKS[chainId];
if (!network) {
throw new Error(`Unsupported network: ${chainId}`);
}
console.log(`Connected to ${network.name} (${network.type})`);
return { chainId, ...network };
}
3. L2-Specific Operations
async function setupOptimismOperations() {
const chainId = await getChainId();
if (chainId === 10) {
// Optimism Mainnet - production operations
return {
sequencerUrl: 'https://mainnet-sequencer.optimism.io',
bridgeContract: '0x...',
gasOracle: '0x420000000000000000000000000000000000000F',
isTestnet: false
};
} else if (chainId === 11155420) {
// Optimism Sepolia - test operations
return {
sequencerUrl: 'https://sepolia-sequencer.optimism.io',
bridgeContract: '0x...',
gasOracle: '0x420000000000000000000000000000000000000F',
isTestnet: true
};
} else {
throw new Error('Not connected to Optimism network');
}
}
4. Wallet Network Switching
async function addOptimismNetwork(ethereum) {
try {
// Request network switch
await ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: '0xa' }], // Optimism mainnet
});
} catch (switchError) {
// Network not added, try adding it
if (switchError.code === 4902) {
try {
await ethereum.request({
method: 'wallet_addEthereumChain',
params: [{
chainId: '0xa',
chainName: 'Optimism',
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18,
},
rpcUrls: ['https://api-optimism-mainnet-archive.n.dwellir.com/YOUR_API_KEY'],
blockExplorerUrls: ['https://optimistic.etherscan.io'],
}],
});
} catch (addError) {
console.error('Failed to add Optimism network:', addError);
}
}
}
}
Need help? Contact our support team or check the Optimism documentation.