We will now learn how to use JSON-RPC to interact with Ethereum:
- Build a basic JSON-RPC body with four parameters. The parameters are version, method, parameters, and ID:
{
"jsonrpc":"2.0",
"method":"", // method identifier
"params":[], // list of parameters
"id":0 // id
}
- Use the net_peerCount method to return the number of peers connected to a node:
curl -X POST
--data '{"jsonrpc":"2.0","method":"net_peerCount","params":[],"id":74}'
http://localhost:8545
- You will get a response like this for all the requests you make. Some requests will return a hex value instead of a decimal/string. You may have to convert it to the required format manually:
{
"id":74,
"jsonrpc": "2.0",
"result": "0x2" // 2
}
- Create the same request in Node.js to get the peer count:
var request = require('request');
request({
url: 'http://localhost:8545',
method: 'POST',
body: '{"jsonrpc":"2.0","method":"net_peerCount","params":[],"id":74}'
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
});
- Use this in Python:
import requests
data = '{"jsonrpc":"2.0","method":"net_peerCount","params":[],"id":74}'
response = requests.post('http://localhost:8545/', data=data)
- And use this in Go:
body := strings.NewReader(`{"jsonrpc":"2.0","method":"net_peerCount","params":[],"id":74}`)
req, err := http.NewRequest("POST", "http://localhost:8545", body)
if err != nil {
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
}
defer resp.Body.Close()
- Use any programming language that can make HTTP requests for interacting through JSON-RPC.
Let's look into a few more important examples.
- To get the current Ether base of the connected node, use eth_coinbase:
// Request
curl -X POST
--data '{"jsonrpc":"2.0","method":"eth_coinbase","params":[],"id":64}'
http://localhost;8545
// Response
{
"id":64,
"jsonrpc": "2.0",
"result": "0x824e470cCac64CC5fa4Abe953e64FA360EA11366"
}
- To view the list of accounts in the node, use eth_accounts:
// Request
curl -X POST
--data '{"jsonrpc":"2.0","method":"eth_accounts","params":[],"id":1}'
http://localhost:8545
// Response
{
"id":1,
"jsonrpc": "2.0",
"result": ["0x824e470cCac64CC5fa4Abe953e64FA360EA11366"]
}
- To get the current block height, use eth_blockNumber:
// Request
curl -X POST
--data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":83}'
http://localhost:8545
// Response
{
"id":83,
"jsonrpc": "2.0",
"result": "0x53D390" // 5493648
}
- To get the balance of an account in the network, use eth_getBalance along with parameters:
// Request
curl -X POST
--data '{"jsonrpc":"2.0",
"method":"eth_getBalance",
"params":["0x824e470cCac64CC5fa4Abe953e64FA360EA11366", "latest"],
"id":1
}'
http://localhost:8545
// Response
{
"id":1,
"jsonrpc": "2.0",
"result": "0x5AF3107A4000" // 100000000000000
}
- To send a transaction from an account, use eth_sendTransaction:
// Request
curl -X POST
--data '{
"jsonrpc":"2.0",
"method":"eth_sendTransaction",
"params": <Trnsaction_Object>,
"id":1
}'
http://localhost:8545
// Response
{
"id":1,
"jsonrpc": "2.0",
"result": "0xf456c56efe41db20f32853ccc4cbea3d2ab011b2c11082150f29c36212345dbd"
}
- To get the transaction receipt, use eth_getTransactionReceipt:
// Request
curl -X POST
--data '{
"jsonrpc":"2.0",
"method":"eth_getTransactionReceipt",
"params":["<TransactionHash>"],
"id":1
}'
http://localhost:8545
// Result
{ "id":1, "jsonrpc":"2.0", "result": <Receipt> }