ethereum-jsonrpc
Explore essential Ethereum JSON RPC methods like eth_syncing, eth_chainId, eth_blockNumber, and eth_getBalance. Learn how to interact with the Ethereum blockchain using JSON RPC calls.
Ethereum JSON RPC Guide
Ethereum JSON RPC Methods Overview
This guide provides examples of common Ethereum JSON RPC methods used to interact with the Ethereum blockchain. These methods allow developers to query blockchain data, manage accounts, and submit transactions.
Key Ethereum JSON RPC Resources
Common JSON RPC Methods and Examples
eth_syncing
Checks if the node is currently syncing and returns information about the sync process. This is useful for understanding the node's current state relative to the network.
curl -XPOST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"eth_syncing","params":[],"id":1}' http://127.0.0.1:8545
eth_chainId
Returns the current chain ID of the Ethereum network the node is connected to. This is crucial for signing transactions correctly.
curl -s -XPOST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"eth_chainId","params":[],"id":1}' localhost:8545 | jq -r '.result' | tr -d '\n' | xargs -0 printf "%d"
eth_blockNumber
Returns the number of the most recent block in the blockchain. This is a fundamental method for tracking the latest state.
curl -s -XPOST -H "Content-type: application/json" -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' localhost:8545 | jq -r ".result" | tr -d '\n' | xargs -0 printf "%d"
eth_getBlockByNumber
Retrieves a block by its number. You can specify a block number or use keywords like "latest" or "earliest". The second parameter determines if full transaction objects are included.
By Block Number:
curl -s -H "Content-type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x3a5d74", false],"id":1}' localhost:8545 | jq -r '.result.number' | tr -d '\n' | xargs -0 printf "%d"
Latest Block:
curl -s -H "Content-type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", false],"id":1}' localhost:8545 | jq -r '.result.number' | tr -d '\n' | xargs -0 printf "%d"
Timestamp of Latest Block
Get the timestamp of the latest block by fetching the block details.
curl -s -H "Content-type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", false],"id":1}' localhost:8545 | jq -r '.result.timestamp' | tr -d '\n' | xargs -0 printf "%d"
personal_newAccount
Creates a new Ethereum account. Note: This method is generally not recommended for production environments due to security concerns. It's better to create accounts offline using private keys.
# This is not recommended - rather create an account offline with a private key
curl -s -XPOST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"personal_newAccount","params":["securepassword"],"id":1}' localhost:8545
eth_getBalance
Returns the balance of an account at a specific block. The balance is returned in Wei.
curl -XPOST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x9a070e582ef891ead3e9b92478df38dd17b4489e", "latest"],"id":1}' localhost:8545
Convert Wei to Ether Units
To display the balance in Ether, you need to divide the Wei value by 10^18.
# Example for testnet
printf %.2f $(echo $(curl -s -XPOST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x746C9474d98C8A99280fC0E9DA7d706B647163DE", "latest"],"id":1}' http://localhost:8545 | jq -r '.result' | tr -d '\n' | xargs -0 printf "%d") / 1000000000000000000 | bc -l)
Further Reading
- JSON-RPC 2.0 Specification
- MDN Web Docs - Fetch API (for making HTTP requests)