JSON RPC API

The Ethereum RPC API

MetaMask uses the ethereum.sendAsync() API to wrap an RPC API which is based on an interface exposed by all Ethereum clients, with some extra methods that are provided by MetaMask, as a key-holding signer.

This document is a cross-post of EIP 1474, which aims to standardize the declaration of this interface.

eip: 1474
title: Remote procedure call specification
author: Paul Bouchon <mail@bitpshr.net>
discussions-to: https://ethereum-magicians.org/t/eip-remote-procedure-call-specification/1537
status: Draft
type: Standards Track
category: Interface
created: 2018-10-02

Simple Summary

This proposal defines a standard set of remote procedure call methods that an Ethereum node should implement.

Abstract

Nodes created by the current generation of Ethereum clients expose inconsistent and incompatible remote procedure call (RPC) methods because no formal Ethereum RPC specification exists. This proposal standardizes such a specification to provide developers with a predictable Ethereum RPC interface regardless of underlying node implementation.

Specification

Concepts

RFC-2119

The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in this document are to be interpreted as described in RFC-2119.

JSON-RPC

Communication with Ethereum nodes is accomplished using JSON-RPC, a stateless, lightweight remote procedure call protocol that uses JSON as its data format. Ethereum RPC methods MUST be called using JSON-RPC request objects and MUST respond with JSON-RPC response objects.

Error codes

If an Ethereum RPC method encounters an error, the error member included on the response object MUST be an object containing a code member and descriptive message member. The following list contains all possible error codes and associated messages:

Code Message Meaning Category
-32700 Parse error Invalid JSON standard
-32600 Invalid request JSON is not a valid request object standard
-32601 Method not found Method does not exist standard
-32602 Invalid params Invalid method parameters standard
-32603 Internal error Internal JSON-RPC error standard
-32000 Invalid input Missing or invalid parameters non-standard
-32001 Resource not found Requested resource not found non-standard
-32002 Resource unavailable Requested resource not available non-standard
-32003 Transaction rejected Transaction creation failed non-standard
-32004 Method not supported Method is not implemented non-standard

Example error response:

{
    "id": 1337
    "jsonrpc": "2.0",
    "error": {
        "code": -32003,
        "message": "Transaction rejected"
    }
}

Value encoding

Specific types of values passed to and returned from Ethereum RPC methods require special encoding:

Quantity

  • A Quantity value MUST be hex-encoded.
  • A Quantity value MUST be “0x”-prefixed.
  • A Quantity value MUST be expressed using the fewest possible hex digits per byte.
  • A Quantity value MUST express zero as “0x0”.

Examples Quantity values:

Value Valid Reason
0x Parse error empty not a valid quantity
0x0 Invalid request interpreted as a quantity of zero
0x00 Method not found leading zeroes not allowed
0x41 Invalid params interpreted as a quantity of 65
0x400 Internal error interpreted as a quantity of 1024
0x0400 Invalid input leading zeroes not allowed
ff Resource not found values must be prefixed

Data

  • A Data value MUST be hex-encoded.
  • A Data value MUST be “0x”-prefixed.
  • A Data value MUST be expressed using two hex digits per byte.

Examples Data values:

Value Valid Reason
0x valid interpreted as empty data
0x0 invalid each byte must be represented using two hex digits
0x00 valid interpreted as a single zero byte
0x41 true interpreted as a data value of 65
0x004200 true interpreted as a data value of 16896
0xf0f0f false bytes require two hex digits
004200 false values must be prefixed

Proposing changes

New Ethereum RPC methods and changes to existing methods MUST be proposed via the traditional EIP process. This allows for community consensus around new method implementations and proposed method modifications. RPC method proposals MUST reach “draft” status before being added to this proposal and the official Ethereum RPC specification defined herein.

Methods

Method Description
web3_clientVersion Returns the version of the current client
web3_sha3 Hashes data using the Keccak-256 algorithm
net_listening Determines if this client is listening for new network connections
net_peerCount Returns the number of peers currently connected to this client
net_version Returns the chain ID associated with the current network
eth_accounts Returns a list of addresses owned by this client
eth_blockNumber Returns the number of the most recent block seen by this client
eth_call Executes a new message call immediately without submitting a transaction to the network
eth_coinbase Returns the coinbase address for this client
eth_estimateGas Estimates the gas necessary to complete a transaction without submitting it to the network
eth_gasPrice Returns the current price of gas expressed in wei
eth_getBalance Returns the balance of an address in wei
eth_getBlockByHash Returns information about a block specified by hash
eth_getBlockByNumber Returns information about a block specified by number
eth_getBlockTransactionCountByHash Returns the number of transactions in a block specified by block hash
eth_getBlockTransactionCountByNumber Returns the number of transactions in a block specified by block number
eth_getCode Returns the contract code stored at a given address
eth_getFilterChanges Returns a list of all logs based on filter ID since the last log retrieval
eth_getFilterLogs Returns a list of all logs based on filter ID
eth_getLogs Returns a list of all logs based on a filter object
eth_getStorageAt Returns the value from a storage position at an address
eth_getTransactionByBlockHashAndIndex Returns information about a transaction specified by block hash and transaction index
eth_getTransactionByBlockNumberAndIndex Returns information about a transaction specified by block number and transaction index
eth_getTransactionByHash Returns information about a transaction specified by hash
eth_getTransactionCount Returns the number of transactions sent from an address
eth_getTransactionReceipt Returns the receipt of a transaction specified by hash
eth_getUncleByBlockHashAndIndex Returns information about an uncle specified by block hash and uncle index position
eth_getUncleByBlockNumberAndIndex Returns information about an uncle specified by block number and uncle index position
eth_getUncleCountByBlockHash Returns the number of uncles in a block specified by block hash
eth_getUncleCountByBlockNumber Returns the number of uncles in a block specified by block number
eth_getWork Returns a list containing relevant information for proof-of-work
eth_hashrate Returns the number of hashes-per-second this node is mining at
eth_mining Determines if this client is mining new blocks
eth_newBlockFilter Creates a filter to listen for new blocks that can be used with eth_getFilterChanges
eth_newFilter Creates a filter to listen for specific state changes that can then be used with eth_getFilterChanges
eth_newPendingTransactionFilter Creates a filter to listen for new pending transactions that can be used with eth_getFilterChanges
eth_protocolVersion Returns the current Ethereum protocol version
eth_sendRawTransaction Sends and already-signed transaction to the network
eth_sendTransaction Creates, signs, and sends a new transaction to the network
eth_sign Calculates an Ethereum-specific signature in the form of keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))
eth_signTransaction Signs a transaction that can be submitted to the network at a later time using with eth_sendRawTransaction
eth_signTypedData Calculates an Ethereum-specific signature in the form of keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))
eth_submitHashrate Submit a mining hashrate
eth_submitWork Submit a proof-of-work solution
eth_syncing Returns information about the status of this client’s network synchronization
eth_uninstallFilter Destroys a filter based on filter ID

web3_clientVersion

web3_sha3

net_listening

net_peerCount

net_version

eth_accounts

eth_blockNumber

eth_call

eth_coinbase

eth_estimateGas

eth_gasPrice

eth_getBalance

eth_getBlockByHash

eth_getBlockByNumber

eth_getBlockTransactionCountByHash

eth_getBlockTransactionCountByNumber

eth_getCode

eth_getFilterChanges

eth_getFilterLogs

eth_getLogs

eth_getStorageAt

eth_getTransactionByBlockHashAndIndex

eth_getTransactionByBlockNumberAndIndex

eth_getTransactionByHash

eth_getTransactionCount

eth_getTransactionReceipt

eth_getUncleByBlockHashAndIndex

eth_getUncleByBlockNumberAndIndex

eth_getUncleCountByBlockHash

eth_getUncleCountByBlockNumber

eth_getWork

eth_hashrate

eth_mining

eth_newBlockFilter

eth_newFilter

eth_newPendingTransactionFilter

eth_protocolVersion

eth_sendRawTransaction

eth_sendTransaction

eth_sign

eth_signTransaction

eth_signTypedData

eth_submitHashrate

eth_submitWork

eth_syncing

eth_uninstallFilter