eth_chainId
Returns the chain ID used for signing replay-protected transactions.
When to Use This Method
Use eth_chainId
to:
- Network Verification - Ensure connected to the correct network
- Transaction Signing - Include chain ID for EIP-155 replay protection
- Multi-Chain Support - Switch between networks in dApps
- Configuration Validation - Verify network settings
TRON Chain ID
The TRON mainnet chain ID is: 0x2b67540c
(728126428 in decimal)
Parameters
This method accepts no parameters.
{
"jsonrpc": "2.0",
"method": "eth_chainId",
"params": [],
"id": 1
}
Returns
QUANTITY
- The chain ID as a hexadecimal string.
- Type: Hexadecimal string
- Format:
0x
prefixed - Value:
0x2b67540c
for TRON mainnet
Implementation Examples
- cURL
- JavaScript
- Python
curl -X POST https://api-tron-jsonrpc.dwellir.com/YOUR_API_KEY \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "eth_chainId",
"params": [],
"id": 1
}'
// Using fetch
const response = await fetch('https://api-tron-jsonrpc.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('Chain ID:', chainId); // 728126428
// Using ethers.js
import { ethers } from 'ethers';
const provider = new ethers.JsonRpcProvider('https://api-tron-jsonrpc.dwellir.com/YOUR_API_KEY');
const network = await provider.getNetwork();
console.log('Network chain ID:', network.chainId);
// Using web3.js
import Web3 from 'web3';
const web3 = new Web3('https://api-tron-jsonrpc.dwellir.com/YOUR_API_KEY');
const chainId = await web3.eth.getChainId();
console.log('TRON chain ID:', chainId);
import requests
import json
# Using requests
url = 'https://api-tron-jsonrpc.dwellir.com/YOUR_API_KEY'
headers = {'Content-Type': 'application/json'}
payload = {
'jsonrpc': '2.0',
'method': 'eth_chainId',
'params': [],
'id': 1
}
response = requests.post(url, headers=headers, data=json.dumps(payload))
result = response.json()
chain_id = int(result['result'], 16)
print(f'Chain ID: {chain_id}') # 728126428
# Using web3.py
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('https://api-tron-jsonrpc.dwellir.com/YOUR_API_KEY'))
chain_id = w3.eth.chain_id
print(f'TRON network chain ID: {chain_id}')
Example Response
{
"jsonrpc": "2.0",
"id": 1,
"result": "0x2b67540c"
}
Network Information
TRON Networks
Network | Chain ID (Hex) | Chain ID (Decimal) | Description |
---|---|---|---|
Mainnet | 0x2b67540c | 728126428 | TRON main network |
Testnet (Shasta) | 0x94a9059e | 2494104990 | TRON test network |
Testnet (Nile) | 0xcd8690dc | 3448148188 | TRON test network |
Common Use Cases
1. Network Verification
async function verifyNetwork() {
const expectedChainId = 728126428; // TRON mainnet
const chainId = await provider.send('eth_chainId', []);
const actualChainId = parseInt(chainId, 16);
if (actualChainId !== expectedChainId) {
throw new Error(`Wrong network! Expected ${expectedChainId}, got ${actualChainId}`);
}
console.log('Connected to TRON mainnet');
return true;
}
2. Multi-Chain dApp Support
const NETWORK_CONFIG = {
728126428: {
name: 'TRON Mainnet',
symbol: 'TRX',
rpcUrl: 'https://api-tron-jsonrpc.dwellir.com/YOUR_API_KEY',
explorer: 'https://tronscan.org'
},
2494104990: {
name: 'TRON Shasta Testnet',
symbol: 'TRX',
rpcUrl: 'https://api.shasta.trongrid.io/jsonrpc',
explorer: 'https://shasta.tronscan.org'
}
};
async function getNetworkConfig() {
const chainId = await web3.eth.getChainId();
const config = NETWORK_CONFIG[chainId];
if (!config) {
throw new Error(`Unsupported network: ${chainId}`);
}
return config;
}
3. EIP-155 Transaction Signing
async function signTransaction(tx) {
const chainId = await provider.send('eth_chainId', []);
// Include chain ID for replay protection
const signedTx = await wallet.signTransaction({
...tx,
chainId: parseInt(chainId, 16)
});
return signedTx;
}
4. Wallet Network Switch
async function switchToTRON() {
try {
await window.ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: '0x2b67540c' }],
});
} catch (switchError) {
// Network not added to wallet
if (switchError.code === 4902) {
await window.ethereum.request({
method: 'wallet_addEthereumChain',
params: [{
chainId: '0x2b67540c',
chainName: 'TRON Mainnet',
nativeCurrency: {
name: 'TRON',
symbol: 'TRX',
decimals: 6
},
rpcUrls: ['https://api-tron-jsonrpc.dwellir.com/YOUR_API_KEY'],
blockExplorerUrls: ['https://tronscan.org']
}],
});
}
}
}
Best Practices
- Always verify chain ID before sending transactions
- Cache chain ID to reduce RPC calls
- Handle network switches gracefully
- Use chain ID for transaction replay protection
- Validate against expected network
Error Codes
Code | Message | Description |
---|---|---|
-32600 | Invalid Request | The JSON sent is not valid |
-32601 | Method not found | Method doesn't exist |
-32602 | Invalid params | Invalid parameters |
-32603 | Internal error | Internal JSON-RPC error |
Related Methods
- net_version - Get network version
- eth_getBlockByNumber - Get block information
- eth_sendRawTransaction - Send signed transaction