Create multiple-choice questions with an answer key after the last question for the following text. (Create as many questions and answers as necessary to cover all the important information in the text.):
165
7.4.1 Tutorial 1 - Get your balance
This program will connect to the daemon via RPC, then query and
print the account balance. From the section introducing RPC, you
might remember the getbalance function (also responds to get_
balance).
We’ll start by importing two Python libraries that are very useful
for making POST requests in python: ‘requests’ and ‘json’.
Let’s preemptively store some of the information in variables to
avoid cluttering up the requests:
Mastering Monero Tutorial. This is a comment
import requests
import json
Import Setup variables
Url for JSON RPC interface. We assume that your RPC
interface is running on localhost port 18082
url = “http://localhost:18082/json_rpc”
JSON headers . Required
headers = {‘content-type’: ‘application/json’}
RPC input . Adding method name , at the moment we don’t
need variables.
rpc_fields = {
“method” : “get_balance”
}
166
Recall the standard JSON fields that should be included in an RPC call:
Now everything is prepared, so there’s only one thing left to do!
Send all the variables to the JSON RPC interface using the POST
HTTP method:
Save all the above code as tutorial.py (or any name of your choice)
and execute:
Your script should print the output of the getbalances call into
your terminal:
Your script should print the output of the getbalances call into
your terminal:
Adding the JSON RPC version and id. Id is a int variable
which should be incremented each request. First request
is 0 , second is one and …
rpc_fields.update({“jsonrpc”: “2.0”, “id”: “0”})
execute the rpc request
response = requests.post(url,data=json.dumps(rpc_input),-
headers=headers)
print the response as JSON
print(json.dumps(response.json()))
$ python tutorial.py
{
“id”: “0”,
“jsonrpc”: “2.0”,
“result”: {
“balance”: 0,
“multisig_import_needed”: false,
“unlocked_balance”: 0 }
}
167
While this contains all of the information we need, the output
RPC syntax is not formatted for optimal human readability. Staring
at too many {}'s can become disorienting after a while!
For a cleaner output, we can add a few lines of code to the bottom
of the tutorial script, so that it only prints the balance (or unlocked
balance, if you’d prefer).
Now, running
Should simply return:
You can use RPC methods like this to develop your own personal
client for your Monero wallet!