eth_chainId
Returns the chain ID of the current network. For Linea mainnet, this returns 0xe708
(59144 in decimal).
Parameters
None
Returns
QUANTITY
- The chain ID as a hexadecimal string.
Request Example
{
"jsonrpc": "2.0",
"method": "eth_chainId",
"params": [],
"id": 1
}
Response Example
{
"jsonrpc": "2.0",
"id": 1,
"result": "0xe708"
}
Implementation Examples
- JavaScript
- Python
import { JsonRpcProvider } from 'ethers';
const provider = new JsonRpcProvider('https://api-linea-mainnet-archive.n.dwellir.com/YOUR_API_KEY');
// Get chain ID
async function getChainId() {
const chainId = await provider.getNetwork().then(n => n.chainId);
console.log('Chain ID (decimal):', chainId);
console.log('Chain ID (hex):', '0x' + chainId.toString(16));
// Verify it's Linea
if (chainId === 59144n) {
console.log('Connected to Linea mainnet');
}
return chainId;
}
// Verify network before transactions
async function verifyNetwork() {
const chainId = await provider.send('eth_chainId', []);
const expectedChainId = '0xe708'; // Linea mainnet
if (chainId !== expectedChainId) {
throw new Error(`Wrong network! Expected Linea (${expectedChainId}), got ${chainId}`);
}
return true;
}
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('https://api-linea-mainnet-archive.n.dwellir.com/YOUR_API_KEY'))
def get_chain_id():
"""Get the chain ID"""
chain_id = w3.eth.chain_id
print(f"Chain ID (decimal): {chain_id}")
print(f"Chain ID (hex): {hex(chain_id)}")
# Verify it's Linea
if chain_id == 59144:
print("Connected to Linea mainnet")
else:
print(f"Connected to unknown network: {chain_id}")
return chain_id
def verify_network():
"""Verify connected to correct network"""
chain_id = w3.eth.chain_id
expected = 59144 # Linea mainnet
if chain_id != expected:
raise ValueError(f"Wrong network! Expected {expected}, got {chain_id}")
return True
Network Information
Network | Chain ID (Decimal) | Chain ID (Hex) |
---|---|---|
Linea Mainnet | 59144 | 0xe708 |
Common Use Cases
1. Multi-Network Support
// Handle multiple networks
async function getNetworkConfig() {
const chainId = await provider.send('eth_chainId', []);
const networks = {
'0x1': { name: 'Ethereum Mainnet', explorer: 'https://etherscan.io' },
'0xe708': { name: 'Linea Mainnet', explorer: 'https://lineascan.build' },
'0x89': { name: 'Polygon', explorer: 'https://polygonscan.com' }
};
const network = networks[chainId] || { name: 'Unknown', explorer: null };
return {
chainId: chainId,
decimal: parseInt(chainId, 16),
...network
};
}
2. Wallet Connection Validation
// Validate wallet is on correct network
async function validateWalletNetwork(expectedChainId = 59144) {
if (typeof window.ethereum === 'undefined') {
throw new Error('No wallet detected');
}
const chainId = await window.ethereum.request({
method: 'eth_chainId'
});
const currentChainId = parseInt(chainId, 16);
if (currentChainId !== expectedChainId) {
// Request network switch
try {
await window.ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: '0xe708' }]
});
} catch (switchError) {
// Network not added to wallet
if (switchError.code === 4902) {
await addLineaNetwork();
}
}
}
}
// Add Linea network to wallet
async function addLineaNetwork() {
await window.ethereum.request({
method: 'wallet_addEthereumChain',
params: [{
chainId: '0xe708',
chainName: 'Linea',
nativeCurrency: {
name: 'ETH',
symbol: 'ETH',
decimals: 18
},
rpcUrls: ['https://api-linea-mainnet-archive.n.dwellir.com/YOUR_API_KEY'],
blockExplorerUrls: ['https://lineascan.build']
}]
});
}
Need help? Contact our support team or check the Linea documentation.