JSON-RPC API and EVM Interaction
This domain tests exposing the API safely and using the right method for reads, writes and tracing. Besu serves JSON-RPC over HTTP and WebSocket, plus GraphQL.
Core rules
- HTTP is enabled with
--rpc-http-enabledon host 127.0.0.1 and port 8545; WebSocket uses--rpc-ws-enabledon 8546; GraphQL uses 8547. - Only ETH, NET and WEB3 are enabled by default. Add namespaces with
--rpc-http-apior--rpc-ws-api, for exampleETH,NET,WEB3,QBFT,ADMIN,DEBUG. --host-allowlistchecks the HTTP Host header and defaults to localhost entries;--rpc-http-cors-originsgoverns browser access. A remote client can be blocked by either.- Read methods include
eth_chainId,eth_blockNumber,eth_getBalance,eth_call,eth_estimateGasandeth_getCode. State-changing methods areeth_sendRawTransactionfollowed byeth_getTransactionReceipt. eth_subscribeworks only over WebSocket. Tracing methods live in DEBUG or TRACE.- On private networks with zero gas price, set the node's
--min-gas-price=0so zero-priced transactions are accepted.
Worked example
To read an ERC-20 balance, call eth_call with to set to the token address and data set to the 4-byte balanceOf selector followed by the holder address padded to 32 bytes, using block parameter latest. No key, gas or signature is needed because nothing is persisted. To transfer tokens, build and sign the transaction offline with the network chain ID, submit it with eth_sendRawTransaction, then poll the receipt until status is 0x1.
Traps
- Enabling the API but forgetting the host allowlist or CORS origin.
- Signing with the wrong chain ID, which yields an invalid-sender error.
- Expecting Besu to hold unlocked accounts for
eth_sendTransaction. - Using
eth_subscribeover HTTP instead of WebSocket.