Ethereum: Json-RPC with C ++ Linux
In this article we help you to use the process of the JSON RPC protocol C ++ to interact with Ethereum Blockchain. We will also see how to use Bitcoin RPC libraries and specify examples of electrum api.
What is JSON-RPC?
The JSON -RPC (JavaScript -Berekript -Remote Procedure Call) is a remote procedure call standard with which developers can interact with Remote servers via HTTP. In this context, we will use the JSON RPC protocol to interact with Ethereum Blockchain.
Which library do I have to use JSON?
There are several libraries that work with JSON C ++. There are several options here:
- JSONCPP: A light and modern JSON analyzer and generator.
- Boost :: JSON: A popular and rich feature JSON analysis and library generation.
- GLIB-2.X: Modern C ++ bonds to the GLIB JSON library.
In this example we will use the JSONCPP library. You can install it with your package driver or add this line to the CMAkelist.txt file:
`cmake
Add_libry (jsoncpp_json cpp_json.cpp)
`
How can I use Bitcoin or Bitcoin Cli-RPC?
Here is an example of how you can use the JSON RPC protocol with Bitcoin:
* Bitcoind : You can use Bitcoin Cli-JsonrPC option when starting. For example:
`Bash
./bitcoin -Coin -jsonrpc = 1 -Url http: // localhost: 8332
This makes a new connection with the Bitcoin node under http: // localhost: 8332
and you can send JSON-RPC inquiries to you.
* Bitcoin-Cli : You can also use the “-jsonrpc” option if you run Bitcoin-Cli. For example:
`Bash
Bitcoin -cli -jsonrpc = 1 -Url http: // localhost: 8332
How can you use electrium -API -RPC?
Here is an example of how you can use the JSON RPC protocol with electrium:
* Electrum : You can manufacture a new connection to the electrical unit under http: // localhost: 8090
and then send it to the JSON RPC inquiries with the following code:
`CPP
#include
#include
int main () {
// Start a new connection with electricity nodes
Std :: cout << "connected with electricity ..." << std :: endl;
// send JSON-RPC request to receive all expected operations
Std :: String Json = "{\" Method \ ": \" Listransection \ ", \" for days \ ": [], \" jsonrpc \ ": \" 1.0 \ "," id ": null}";
It results = json.cpp2json (JSON, JSON);
If (result == 0) {
For (int i = 0; i
Std :: cout << "Operation" << i + 1 << ":" << json.cpp2string (result [i]) << std :: endl;
}
}
Return 0;
}
This code sends a JSON RPC requirement to the listeners without parameters and prints out a list of the waiting processes.
Example application cases
* Ethereum : To interact with Ethereum Blockchain, you can use the JSON-RPC protocol to send inquiries to the two (program linear interface) to carry out the functions.
* Bitcoin : You can use the JSON-RPC protocol to obtain information about the Bitcoin network such as Block Time Tags and operations.
* Electrum : With the JSON RPC protocol you can scan the list of waiting processes in the electrium node.
Diploma
In this article we examined how the JSON RPC protocol C ++ is used to interact with Ethereum Blockchain. We also delivered examples of Bitcoin and Electrium -API -RPC libraries. With this knowledge you can create your own programs that use the JSON RPC protocol to interact with these blockchains.