以太坊json rpc

卧疾丰暇豫,翰墨时间作。这篇文章主要讲述以太坊json rpc相关的知识,希望能为你提供帮助。


Contents


  • ??Hash List??
  • ??JSON-RPC support??
  • ??HEX value encoding??
  • ??The default block parameter??
  • ??Curl Examples Explained??
  • ??JSON-RPC methods??

Hash List
类型
长度
描述
blockHash
String
0x+64位
区块的哈希
blockNumber
Number
区块的块号
transactionHash
String
0x+64位
交易的哈希值
transactionIndex
Number
交易在区块里面的序号,整数
client addresses
String
0x+40位
账户地址
gas
Number
工作量单位
contractAddress
String
0x+40位
合约地址
JSON-RPC support
cpp-ethereum
go-ethereum
py-ethereum
parity
JSON-RPC 1.0
?
【以太坊json rpc】JSON-RPC 2.0
?
?
?
?
Batch requests
?
?
?
?
HTTP
?
?
?
?
IPC
?
?
?
WS
?
?
HEX value encodingAt present there are two key datatypes that are passed over JSON: unformatted byte arrays and quantities. Both are passed with a hex encoding, however with different requirements to formatting:
When encoding QUANTITIES (integers, numbers): encode as hex, prefix with “0x”, the most compact representation (slight exception: zero should be represented as “0x0”). Examples:

  • 0x41 (65 in decimal)
  • 0x400 (1024 in decimal)
  • WRONG: 0x (should always have at least one digit - zero is “0x0”)
  • WRONG: 0x0400 (no leading zeroes allowed)
  • WRONG: ff (must be prefixed 0x)

When encoding UNFORMATTED DATA (byte arrays, account addresses, hashes, bytecode arrays): encode as hex, prefix with “0x”, two hex digits per byte. Examples:

  • 0x41 (size 1, “A”)
  • 0x004200 (size 3, “\\0B\\0”)
  • 0x (size 0, “”)
  • WRONG: 0xf0f0f (must be even number of digits)
  • WRONG: 004200 (must be prefixed 0x)

Currently ??cpp-ethereum???,??go-ethereum???, and ??parity?? provide JSON-RPC communication over http and IPC (unix socket Linux and OSX/named pipes on Windows). Version 1.4 of go-ethereum and version 1.6 of Parity onwards have websocket support.
The default block parameterThe following methods have an extra default block parameter:

  • ??eth_getBalance??
  • ??eth_getCode??
  • ??eth_getTransactionCount??
  • ??eth_getStorageAt??
  • ??eth_call??

When requests are made that act on the state of ethereum, the last default block parameter determines the height of the block.
The following options are possible for the defaultBlock parameter:

  • ??HEX String?? - an integer block number
  • ??String "earliest"?? for the earliest/genesis block
  • ??String "latest"?? - for the latest mined block
  • ??String "pending"?? - for the pending state/transactions

Curl Examples ExplainedThe curl options below might return a response where the node complains about the content type, this is because the --data option sets the content type to application/x-www-form-urlencoded . If your node does complain, manually set the header by placing -H “Content-Type: application/json” at the start of the call.
The examples also do not include the URL/IP & port combination which must be the last argument given to curl e.x. 127.0.0.1:8545
JSON-RPC methods

  • ??web3_clientVersion 当前客户端版本??
  • ??web3_sha3 给定字符串的sha3结果??
  • ??net_version 当前的网络ID??
  • ??net_peerCount 客户端数量??
  • ??net_listening 客户端连接状态??
  • ??eth_protocolVersion 当前EUTHUM协议版本??
  • ??eth_syncing 当前同步情况??
  • ??eth_coinbase 客户端钱包的地址??
  • ??eth_mining 挖矿状态??
  • ??eth_hashrate 每秒节点挖掘数量??
  • ??eth_gasPrice 当前gas价格??
  • ??eth_accounts 客户拥有的地址列表??
  • ??eth_blockNumber 客户端当前区块号??
  • ??eth_getBalance 当前余额??
  • ??eth_getStorageAt 存储位置的值??
  • ??eth_getTransactionCount 账户地址的交易数量??
  • ??eth_getBlockTransactionCountByHash 区块哈希值的交易数量??
  • ??eth_getBlockTransactionCountByNumber 区块号的交易数量??
  • ??eth_getUncleCountByBlockHash 通过区块哈希值获取叔块??
  • ??eth_getUncleCountByBlockNumber 通过区块号获取叔块??
  • ??eth_getCode 获取链上指定地址的代码??
  • ??eth_sign 账户数据加签??
  • ??eth_sendTransaction 发送一个交易到合约??
  • ??eth_sendRawTransaction 发送加签的交易??
  • ??eth_call??
  • ??eth_estimateGas 合约部署所花费的gas??
  • ??eth_getBlockByHash 区块哈希值所对应的区块??
  • ??eth_getBlockByNumber 区块号所对应的区块??
  • ??eth_getTransactionByHash 交易哈希值查交易信息??
  • ??eth_getTransactionByBlockHashAndIndex 区块哈希值的指定序号的交易??
  • ??eth_getTransactionByBlockNumberAndIndex 区块号的指定序号的交易??
  • ??eth_getTransactionReceipt 智能合约记录查询??
  • ??eth_getUncleByBlockHashAndIndex 区块哈希值返回叔块??
  • ??eth_getUncleByBlockNumberAndIndex 区块号返回叔块??
  • ??eth_getCompilers 可用合约编译器??
  • ??eth_compileLLL 编译LLL源代码??
  • ??eth_compileSolidity 编译智能合约??
  • ??eth_compileSerpent 编译serpent源代码??
  • ??eth_newFilter 创建一个filter对象监听区块或交易的变化??
  • ??eth_newBlockFilter 创建一个filter监听新区块的创建??
  • ??eth_newPendingTransactionFilter 创建一个filter监听发起一个交易??
  • ??eth_uninstallFilter 取消指定filter??
  • ??eth_getFilterChanges 获取filter的变化日志??
  • ??eth_getFilterLogs 获取指定filter日志??
  • ??eth_getLogs 返回所有日志??
  • ??eth_getWork 挖矿应用程序??
  • ??eth_submitWork 守护进程通信??
  • ??eth_submitHashrate 提交挖矿率哈希值??
  • ??db_putString 写入字符串到数据库??
  • ??db_getString 在数据库读取字符串??
  • ??db_putHex 写入十六进制到数据库??
  • ??db_getHex 在数据库读取十六进制??
  • ??shh_post??
  • ??shh_version??
  • ??shh_newIdentity??
  • ??shh_hasIdentity??
  • ??shh_newGroup??
  • ??shh_addToGroup??
  • ??shh_newFilter??
  • ??shh_uninstallFilter??
  • ??shh_getFilterChanges??
  • ??shh_getMessages??

JSON RPC API Reference web3_clientVersionReturns the current client version.
Parametersnone
Returns??String?? - The current client version
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":67

// Result

"id":67,
"jsonrpc":"2.0",
"result": "Mist/v0.9.3/darwin/go1.4.1"

web3_sha3Returns Keccak-256 (not the standardized SHA3-256) of the given data.
Parameters
  1. ??DATA?? - the data to convert into a SHA3 hash
params: [
"0x68656c6c6f20776f726c64"
]

Returns??DATA?? - The SHA3 result of the given string.
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"web3_sha3","params":["0x68656c6c6f20776f726c64"],"id":64

// Result

"id":64,
"jsonrpc": "2.0",
"result": "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"

net_versionReturns the current network id.
Parametersnone
Returns??String?? - The current network id.

  • ??"1"??: Ethereum Mainnet
  • ??"2"??: Morden Testnet (deprecated)
  • ??"3"??: Ropsten Testnet
  • ??"4"??: Rinkeby Testnet
  • ??"42"??: Kovan Testnet

Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"net_version","params":[],"id":67

// Result

"id":67,
"jsonrpc": "2.0",
"result": "3"

net_listeningReturns ??true?? if client is actively listening for network connections.
Parametersnone
Returns??Boolean??? - ??true??? when listening, otherwise ??false??.
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"net_listening","params":[],"id":67

// Result

"id":67,
"jsonrpc":"2.0",
"result":true

net_peerCountReturns number of peers currently connected to the client.
Parametersnone
Returns??QUANTITY?? - integer of the number of connected peers.
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"net_peerCount","params":[],"id":74

// Result

"id":74,
"jsonrpc": "2.0",
"result": "0x2" // 2

eth_protocolVersionReturns the current ethereum protocol version.
Parametersnone
Returns??String?? - The current ethereum protocol version
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"eth_protocolVersion","params":[],"id":67

// Result

"id":67,
"jsonrpc": "2.0",
"result": "54"

eth_syncingReturns an object with data about the sync status or ??false??.
Parametersnone
Returns??Object|Boolean???, An object with sync status data or ??FALSE??, when not syncing:

  • ??startingBlock???: ??QUANTITY?? - The block at which the import started (will only be reset, after the sync reached his head)
  • ??currentBlock???: ??QUANTITY?? - The current block, same as eth_blockNumber
  • ??highestBlock???: ??QUANTITY?? - The estimated highest block

Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"eth_syncing","params":[],"id":1

// Result

"id":1,
"jsonrpc": "2.0",
"result":
startingBlock: 0x384,
currentBlock: 0x386,
highestBlock: 0x454


// Or when not syncing

"id":1,
"jsonrpc": "2.0",
"result": false

eth_coinbaseReturns the client coinbase address.
Parametersnone
Returns??DATA??, 20 bytes - the current coinbase address.
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"eth_coinbase","params":[],"id":64

// Result

"id":64,
"jsonrpc": "2.0",
"result": "0x407d73d8a49eeb85d32cf465507dd71d507100c1"

eth_miningReturns ??true?? if client is actively mining new blocks.
Parametersnone
Returns??Boolean??? - returns ??true??? of the client is mining, otherwise ??false??.
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"eth_mining","params":[],"id":71

// Result

"id":71,
"jsonrpc": "2.0",
"result": true

eth_hashrateReturns the number of hashes per second that the node is mining with.
Parametersnone
Returns??QUANTITY?? - number of hashes per second.
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"eth_hashrate","params":[],"id":71

// Result

"id":71,
"jsonrpc": "2.0",
"result": "0x38a"

eth_gasPriceReturns the current price per gas in wei.
Parametersnone
Returns??QUANTITY?? - integer of the current gas price in wei.
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"eth_gasPrice","params":[],"id":73

// Result

"id":73,
"jsonrpc": "2.0",
"result": "0x09184e72a000" // 10000000000000

eth_accountsReturns a list of addresses owned by client.
Parametersnone
Returns??Array of DATA??, 20 Bytes - addresses owned by the client.
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"eth_accounts","params":[],"id":1

// Result

"id":1,
"jsonrpc": "2.0",
"result": ["0x407d73d8a49eeb85d32cf465507dd71d507100c1"]

eth_blockNumberReturns the number of most recent block.
Parametersnone
Returns??QUANTITY?? - integer of the current block number the client is on.
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":83

// Result

"id":83,
"jsonrpc": "2.0",
"result": "0x4b7" // 1207

eth_getBalanceReturns the balance of the account of given address.
Parameters

  1. ??DATA??, 20 Bytes - address to check for balance.
  2. ??QUANTITY|TAG??? - integer block number, or the string ??"latest"???, ??"earliest"??? or ??"pending"???, see the ??default block parameter??

params: [
0x407d73d8a49eeb85d32cf465507dd71d507100c1,
latest
]

Returns??QUANTITY?? - integer of the current balance in wei.
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"eth_getBalance","params":["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"],"id":1

// Result

"id":1,
"jsonrpc": "2.0",
"result": "0x0234c8a3397aab58" // 158972490234375000

eth_getStorageAtReturns the value from a storage position at a given address.
Parameters

  1. ??DATA??, 20 Bytes - address of the storage.
  2. ??QUANTITY?? - integer of the position in the storage.
  3. ??QUANTITY|TAG??? - integer block number, or the string ??"latest"???, ??"earliest"??? or ??"pending"???, see the ??default block parameter??

Returns??DATA?? - the value at this storage position.
ExampleCalculating the correct position depends on the storage to retrieve. Consider the following contract deployed at ??0x295a70b2de5e3953354a6a8344e616ed314d7251??? by address ??0x391694e7e0b0cce554cb130d723a9d27458f9298??.
contract Storage
uint pos0;
mapping(address => uint) pos1;

function Storage()
pos0 = 1234;
pos1[msg.sender] = 5678;


Retrieving the value of pos0 is straight forward:
curl -X POST --data "jsonrpc":"2.0", "method": "eth_getStorageAt", "params": ["0x295a70b2de5e3953354a6a8344e616ed314d7251", "0x0", "latest"], "id": 1 localhost:8545

"jsonrpc":"2.0","id":1,"result":"0x00000000000000000000000000000000000000000000000000000000000004d2"

Retrieving an element of the map is harder. The position of an element in the map is calculated with:
keccack(LeftPad32(key, 0), LeftPad32(map position, 0))

This means to retrieve the storage on pos1[“0x391694e7e0b0cce554cb130d723a9d27458f9298”] we need to calculate the position with:
keccak(decodeHex("000000000000000000000000391694e7e0b0cce554cb130d723a9d27458f9298" + "0000000000000000000000000000000000000000000000000000000000000001"))

The geth console which comes with the web3 library can be used to make the calculation:
> var key = "000000000000000000000000391694e7e0b0cce554cb130d723a9d27458f9298" + "0000000000000000000000000000000000000000000000000000000000000001"
undefined
> web3.sha3(key, "encoding": "hex")
"0x6661e9d6d8b923d5bbaab1b96e1dd51ff6ea2a93520fdc9eb75d059238b8c5e9"

Now to fetch the storage:
curl -X POST --data "jsonrpc":"2.0", "method": "eth_getStorageAt", "params": ["0x295a70b2de5e3953354a6a8344e616ed314d7251", "0x6661e9d6d8b923d5bbaab1b96e1dd51ff6ea2a93520fdc9eb75d059238b8c5e9", "latest"], "id": 1 localhost:8545

"jsonrpc":"2.0","id":1,"result":"0x000000000000000000000000000000000000000000000000000000000000162e"

eth_getTransactionCountReturns the number of transactions sent from an address.
Parameters

  1. ??DATA??, 20 Bytes - address.
  2. ??QUANTITY|TAG??? - integer block number, or the string ??"latest"???, ??"earliest"??? or ??"pending"???, see the ??default block parameter??

params: [
0x407d73d8a49eeb85d32cf465507dd71d507100c1,
latest // state at the latest block
]

Returns??QUANTITY?? - integer of the number of transactions send from this address.
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"eth_getTransactionCount","params":["0x407d73d8a49eeb85d32cf465507dd71d507100c1","latest"],"id":1

// Result

"id":1,
"jsonrpc": "2.0",
"result": "0x1" // 1

eth_getBlockTransactionCountByHashReturns the number of transactions in a block from a block matching the given block hash.
Parameters
  1. ??DATA??, 32 Bytes - hash of a block
params: [
0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238
]

Returns??QUANTITY?? - integer of the number of transactions in this block.
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"eth_getBlockTransactionCountByHash","params":["0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"],"id":1

// Result

"id":1,
"jsonrpc": "2.0",
"result": "0xb" // 11

eth_getBlockTransactionCountByNumber

Returns the number of transactions in a block matching the given block number.
Parameters
  1. ??QUANTITY|TAG??? - integer of a block number, or the string ??"earliest"???, ??"latest"??? or ??"pending"???, as in the ??default block parameter??.
params: [
0xe8, // 232
]

Returns??QUANTITY?? - integer of the number of transactions in this block.
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"eth_getBlockTransactionCountByNumber","params":["0xe8"],"id":1

// Result

"id":1,
"jsonrpc": "2.0",
"result": "0xa" // 10

eth_getUncleCountByBlockHashReturns the number of uncles in a block from a block matching the given block hash.
Parameters
  1. ??DATA??, 32 Bytes - hash of a block
params: [
0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238
]

Returns??QUANTITY?? - integer of the number of uncles in this block.
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"eth_getUncleCountByBlockHash","params":["0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"],"id":1

// Result

"id":1,
"jsonrpc": "2.0",
"result": "0x1" // 1

eth_getUncleCountByBlockNumberReturns the number of uncles in a block from a block matching the given block number.
Parameters
  1. ??QUANTITY|TAG??? - integer of a block number, or the string “latest”, “earliest” or “pending”, see the ??default block parameter??
params: [
0xe8, // 232
]

Returns??QUANTITY?? - integer of the number of uncles in this block.
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"eth_getUncleCountByBlockNumber","params":["0xe8"],"id":1

// Result

"id":1,
"jsonrpc": "2.0",
"result": "0x1" // 1

eth_getCodeReturns code at a given address.
Parameters

  1. ??DATA??, 20 Bytes - address
  2. ??QUANTITY|TAG??? - integer block number, or the string ??"latest"???, ??"earliest"??? or ??"pending"???, see the ??default block parameter??

params: [
0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b,
0x2// 2
]

Returns??DATA?? - the code from the given address.
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"eth_getCode","params":["0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b", "0x2"],"id":1

// Result

"id":1,
"jsonrpc": "2.0",
"result": "0x600160008035811a818181146012578301005b601b6001356025565b8060005260206000f25b600060078202905091905056"

eth_signThe sign method calculates an Ethereum specific signature with: ??sign(keccak256("\\x19Ethereum Signed Message:\\n" + len(message) + message)))??.
By adding a prefix to the message makes the calculated signature recognisable as an Ethereum specific signature. This prevents misuse where a malicious DApp can sign arbitrary data (e.g. transaction) and use the signature to impersonate the victim.
Note the address to sign with must be unlocked.
Parametersaccount, message

  1. ??DATA??, 20 Bytes - address
  2. ??DATA??, N Bytes - message to sign

Returns??DATA??: Signature
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"eth_sign","params":["0x9b2055d370f73ec7d8a03e965129118dc8f5bf83", "0xdeadbeaf"],"id":1

// Result

"id":1,
"jsonrpc": "2.0",
"result": "0xa3f20717a250c2b0b729b7e5becbff67fdaef7e0699da4de7ca5895b02a170a12d887fd3b17bfdce3481f10bea41f45ba9f709d39ce8325427b57afcfc994cee1b"

An example how to use solidity ecrecover to verify the signature calculated with ??eth_sign??? can be found ??here??. The contract is deployed on the testnet Ropsten and Rinkeby.
eth_sendTransactionCreates new message call transaction or a contract creation, if the data field contains code.
Parameters
  1. ??Object?? - The transaction object

  • ??from???: ??DATA??, 20 Bytes - The address the transaction is send from.
  • ??to???: ??DATA??, 20 Bytes - (optional when creating new contract) The address the transaction is directed to.
  • ??gas???: ??QUANTITY?? - (optional, default: 90000) Integer of the gas provided for the transaction execution. It will return unused gas.
  • ??gasPrice???: ??QUANTITY?? - (optional, default: To-Be-Determined) Integer of the gasPrice used for each paid gas
  • ??value???: ??QUANTITY?? - (optional) Integer of the value sent with this transaction
  • ??data???: ??DATA??? - The compiled code of a contract OR the hash of the invoked method signature and encoded parameters. For details see ??Ethereum Contract ABI??
  • ??nonce???: ??QUANTITY?? - (optional) Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce.

params: [
"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
"to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
"gas": "0x76c0", // 30400
"gasPrice": "0x9184e72a000", // 10000000000000
"value": "0x9184e72a", // 2441406250
"data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
]

Returns??DATA??, 32 Bytes - the transaction hash, or the zero hash if the transaction is not yet available.
Use ??eth_getTransactionReceipt?? to get the contract address, after the transaction was mined, when you created a contract.
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"eth_sendTransaction","params":[see above],"id":1

// Result

"id":1,
"jsonrpc": "2.0",
"result": "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331"

eth_sendRawTransactionCreates new message call transaction or a contract creation for signed transactions.
Parameters
  1. ??DATA??, The signed transaction data.
params: ["0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"]

Returns??DATA??, 32 Bytes - the transaction hash, or the zero hash if the transaction is not yet available.
Use ??eth_getTransactionReceipt?? to get the contract address, after the transaction was mined, when you created a contract.
Example
// Request
curl -X POST --data "jsonrpc":"2.0","method":"eth_sendRawTransaction","params":[see above],"id":1

// Result

"id":1,
"jsonrpc": "2.0",
"result": "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331"

eth_callExecutes a new message call immediately without creating a transaction on the block chain.
Parameters
  1. ??Object?? - The transaction call object