Interacting with a Witnet Node via a Remote Procedure Call (RPC)
A brief introduction to Witnet
Witnet is a “Decentralised Oracle Network”, transitioning to Mainnet in the next quarter. Witnet aims to be a truly decentralized and permissionless protocol. You can read more about it here.
If you’re not yet running a Witnet instance, please head here: run-a-node. Once the Witnet instance is running, we would like to interact with it. For this, Witnet binary has subcommands like node nodeStats
,node blockchain
etc.
How to interact with your Witnet instance via an RPC?
There are 2 ways of doing this:
- Issue commands to the OS, running Witnet binary as a subprocess like
subprocess
oros.system
in Python. - The Witnet instance has json rpc exposed at default port
21338
. This RPC is a direct TCP stream, without any application layer protocol likeHTTP
over it. So, we can directly interact with it usingnc
.
nc 127.0.0.1 21338
After issuing the above command, it will wait for you to paste request data. Try with the request below:
{"jsonrpc": "2.0","method": "nodeStats", "id": "1"}
Now that we have seen how easy it is to interact with witnet node
, let’s try with code. Below is a python snippet doing the same thing.
One catch though; remember to end the request with \n
otherwise the node will not respond, as it thinks the request hasn’t ended and will wait for more data. CLI#L946 handles this case.
Witnet CLI has a subcommand witnet node raw
, which allows you to query witnet instance
using a raw request string.
Interacting directly with the RPC will prevent the use of OS calls to run the Witnet subcommands when doing automation.
Below I have provided request string for all subcommands of witnet binary
. All of this can also be found at witnet#json_rpc_client.