json-rpc api
JSON-RPC is a lightweight remote procedure call (RPC) protocol that uses JSON for encoding messages.
It is transport agnostic, meaning it can work over HTTP, WebSocket, or other transports.
JSON-RPC Protocol Basics
Methods and Procedures
- JSON-RPC methods are specific to the Ethereum protocol, such as
eth_call
,eth_sendTransaction
, andeth_getBalance
. - Methods usually start with prefixes like
eth_
,web3_
, ornet_
, categorizing them by functionality.
- It is the primary interface for interacting with Ethereum nodes.
- Used for tasks like querying blockchain data, sending transactions, or interacting with smart contracts.
Endpoints and Transport Protocols
- Nodes expose JSON-RPC endpoints, typically over HTTP (e.g.,
http://localhost:8545
) or WebSocket (e.g.,ws://localhost:8546
). - Endpoints can be local (self-hosted node) or remote (via providers like Infura or Alchemy).
Request and Response Format
Each request includes:
jsonrpc
: The version of the JSON-RPC protocol (always"2.0"
).method
: The name of the method to be invoked.params
: A list of parameters for the method.id
: A unique identifier for matching responses to requests.
Responses include the result for successful calls or an error field.
Commonly Used JSON-RPC Methods for EVM
- Account-related: eth_getBalance, eth_getTransactionCount.
- Blockchain Data: eth_blockNumber, eth_getBlockByNumber.
- Smart Contract Interaction: eth_call, eth_estimateGas.
- Transactions: eth_sendTransaction, eth_getTransactionReceipt.
Interfacing with Smart Contracts
- Use eth_call for reading contract state and eth_sendTransaction for writing to a contract.
- ABI encoding/decoding is required for packing and unpacking data when interacting with smart contracts.