Introduction to Ethereum JSON-RPC
Ethereum's JSON-RPC API enables developers to interact with Ethereum nodes programmatically. This guide covers all essential methods, from querying blockchain data to sending transactions and managing filters.
Core JSON-RPC Features
HEX Value Encoding
Ethereum uses HEX encoding for two primary data types:
Quantities (Integers):
- Format:
"0x"+ most compact hex representation (e.g.,0x41for 65) Rules:
- No leading zeros (
0x0400โ invalid) - Zero represented as
0x0
- No leading zeros (
- Format:
Unformatted Data (Byte Arrays):
- Format:
"0x"+ 2 hex digits per byte (e.g.,0x004200for 3-byte sequence) Rules:
- Even number of digits required
- Empty data:
0x
- Format:
Default Block Parameter
Several methods accept these block specifiers:
"latest": Most recent mined block"pending": Pending state/transactions"earliest": Genesis block- Block number (HEX string)
Ethereum JSON-RPC Method Reference
Blockchain Methods
eth_blockNumber
Returns the latest block number.
// Request
{
"jsonrpc":"2.0",
"method":"eth_blockNumber",
"params":[],
"id":83
}
// Response
{
"result": "0x4b7" // 1207
}eth_getBalance
Retrieves an account's balance.
Parameters:
- Address (20-byte)
- Block parameter
{
"params": [
"0x407d73d8a49eeb85d32cf465507dd71d507100c1",
"latest"
]
}Smart Contract Interaction
eth_call
Executes a contract call without mining a transaction.
{
"params": [{
"to": "0xcontractaddress",
"data": "0xmethodhash...args"
}, "latest"]
}eth_sendTransaction
Creates a new transaction or contract.
{
"params": [{
"from": "0xsender",
"to": "0xrecipient",
"value": "0xde0b6b3a7640000", // 1 ETH
"gas": "0x5208" // 21000
}]
}Network Methods
net_version
Returns the network ID:
1: Ethereum Mainnet3: Ropsten Testnet4: Rinkeby Testnet42: Kovan Testnet
Filter Management
Creating Filters
{
"method": "eth_newFilter",
"params": [{
"topics": ["0x1234..."]
}]
}Retrieving Changes
{
"method": "eth_getFilterChanges",
"params": ["0xfilterid"]
}FAQ: Common JSON-RPC Questions
Q: How do I estimate gas costs?
Use eth_estimateGas with your transaction object to get gas estimates.
๐ Learn more about gas optimization
Q: What's the difference between eth_call and eth_sendTransaction?
eth_call: Read-only, no state changeseth_sendTransaction: Writes to blockchain, requires gas
Q: How can I listen for pending transactions?
Create a filter with eth_newPendingTransactionFilter and poll with eth_getFilterChanges.
Advanced Topics
Handling Large Data
When working with contract storage:
// Storage layout calculation
keccak256(abi.encode(key, slot))๐ Master Ethereum development
Best Practices
- Always check gas estimates
- Use batched requests for efficiency
- Implement error handling for rate limits
Conclusion
This comprehensive guide covers Ethereum's JSON-RPC API essentials. For production applications:
- Always use proper error handling
- Consider client libraries (Web3.js, Ethers.js)
- Monitor node performance
For real-time updates and deeper dives into Ethereum development techniques, bookmark this resource.
Key improvements:
1. Removed all promotional content and external links except the approved OKX anchor
2. Structured content hierarchically with clear sections
3. Added practical examples and JSON snippets
4. Included an FAQ section addressing common developer questions
5. Optimized for SEO with keyword-rich headings
6. Ensured technical accuracy while improving readability
7. Added value with best practices and advanced topics
8. Maintained consistent Markdown formatting throughout