Getting Started with Symbol in Python – (Part 1)

This post is based on published articles from @nobu_kyutech and his work on interacting with the Symbol blockchain using Python. You can find his original examples in Japanese on GitHub

Thanks @nobu_kyutech – I hope you don’t mind me translating to English!

Previously I thought I would get my hands dirty and see if I could get anywhere with the Symbol Python SDK but couldn’t find much in the way of documentation and didn’t want to have to try to work everything out myself. Recently I was made aware of @nobu_kyutech‘s work which was recently featured on the Symbol tweet update (09/07/21) and although it doesn’t cover the SDK in the first three examples I think that looking at his GitHub page that this will be coming soon and I will provide translations (and potentially my trials and errors with my own code) in the future. In the meantime I thought it would be great to translate the existing exercises as I think that they are an excellent way for you to have a play and see how easy it is to get information from the Symbol blockchain programmatically.

Just a disclaimer before we start. I am an absolute beginner with Python so any of my own code I might post in future updates using will not be pretty! 😅 I do have several years worth of experience using the Linux command line and can program in Perl so hopefully I will be able to work things out and get them running at least.

In today’s post I will cover the first of @nobu_kyutech‘s current examples on GitHub which provides a beginners guide to using Python to interact with Symbol API nodes in order to retrieve information.

Getting Started

I am going to make some assumptions here, first is that you already have Python installed and second, that you are using the Linux (or like me) Mac command line. You will be able to do this on Windows but I can’t help you with the setup as I haven’t owned a Windows machine since the early 2000s 😁

Like @nobu_kyutech I will use Jupyter Notebook for these, and future, exercises – if you haven’t used Jupyter Notebook before then you can install it using pip (or conda if you have a conda environment set up).

pip install:

pip install jupyterlab

conda install:

conda install -c conda-forge jupyterlab

Example 1

Obtaining XYM balance of an account on the Symbol blockchain

In this example we will fetch the XYM balance from an account in a Python script.

First start a new Jupyter Notebook and the import urllib.request and json modules.

import urllib.request
import json

Next we want to specify the API node that we want to query as well as the account that we want to check the balance of (I just queried my own node in this case and used the same account as in the example).

# Set the node address that you will query
 NODEURL = "http://xymharvesting.net:3000"
 Set the Symbol address that you want to query
 ADDRESS = "NDLS6GYOIPHATATNAVVOUNJXBD6X4BXU6IRBHIY"

Next we need to fetch the account data of ADDRESS from the node NODEURL using urllib.request and save the result it in a json data structure:

# Set the request to the API node the request will look like this:
# http://xymharvesting.net:3000/accounts/NDLS6GYOIPHATATNAVVOUNJXBD6X4BXU6IRBHIY
 req = urllib.request.Request(NODEURL + '/accounts/' + ADDRESS)
 We can save the result in a json object:
 with urllib.request.urlopen(req) as res:
     accountInfo = json.load(res)

This gives us a json object which we store as accountInfo.
We can print the json entry to have a look at its contents

# The json result looks like this:
 print(json.dumps(accountInfo, indent=2))

Which gives the following result:

{
   "account": {
     "version": 1,
     "address": "68D72F1B0E43CE09826D056AEA353708FD7E06F4F22213A3",
     "addressHeight": "143022",
     "publicKey": "5DB471481B3A39AC5CEEE949B64A577C0D5CA67176BE4462145CAD6ABD84CEBC",
     "publicKeyHeight": "143146",
     "accountType": 1,
     "supplementalPublicKeys": {
       "linked": {
         "publicKey": "24120E20A7958457691D15200387F62EA5A3A0FE8349302F9D42C96FF2C233DC"
       },
       "node": {
         "publicKey": "9CBE17EDFC8B333FE6BD3FF9B4D02914D55A9368F318D4CEF0AB4737BA5BB160"
       },
       "vrf": {
         "publicKey": "F7827E7465E72A19BD4169E2D26FC2B02E0DBE6674A4F77FD5999EA294D70A95"
       }
     },
     "activityBuckets": [
       {
         "startHeight": "329040",
         "totalFeesPaid": "0",
         "beneficiaryCount": 0,
         "rawScore": "9721098768"
       },
       {
         "startHeight": "328320",
         "totalFeesPaid": "0",
         "beneficiaryCount": 0,
         "rawScore": "9721243591"
       },
       {
         "startHeight": "327600",
         "totalFeesPaid": "0",
         "beneficiaryCount": 0,
         "rawScore": "9721399517"
       },
       {
         "startHeight": "326880",
         "totalFeesPaid": "0",
         "beneficiaryCount": 0,
         "rawScore": "9721521679"
       },
       {
         "startHeight": "326160",
         "totalFeesPaid": "0",
         "beneficiaryCount": 0,
         "rawScore": "9721627186"
       }
     ],
     "mosaics": [
       {
         "id": "6BED913FA20223F8",
         "amount": "10265366835"
       }
     ],
     "importance": "9721098768",
     "importanceHeight": "329040"
   },
   "id": "6092C1F7DE979C31ACE082DD"
 }

 

This gives us all of the account information. In this case we are only interested in obtaining the XYM account balance. XYM is a mosaic on the Symbol chain and so we can find this in the “mosaics” section of the output above.

"mosaics": [
       {
         "id": "6BED913FA20223F8",
         "amount": "10265366835"
       }
     ],

The mosaic ID 6BED913FA20223F8 represents the XYM token and the “amount” field shows the balance in microXYM.

We can specifically request the account balance using the following code that parses the relevant information from
accountInfo.

You can see in the example below that we divide the result by 1,000,000 to provide the balance in XYM rather than microXYM:

# We can programmatically obtain XYM account balance by pulling the relevant information from the json entry
# Note that we divide the result by one million as the result returned is in microXYM
print(int(accountInfo['account']['mosaics'][0]['amount']) / 1000000)

 

We are obtaining the first entry in the mosaics section (element zero) or [0]. I am not 100% sure if XYM will always be the first mosaic. E.g. if an account has a zero XYM balance but has a balance of another mosaic then this code may return an incorrect result (i.e. the balance of another mosaic). I haven’t tested this so it may not be the case but to be safer to specify which mosaic balance we want to return using the mosaic ID 6BED913FA20223F8.

The result of running this command gives the XYM balance of account we specified in the ADDRESS variable above. So we see that the account balance of NDLS6GYOIPHATATNAVVOUNJXBD6X4BXU6IRBHIY is 10265.366835 XYM.

 

We can obviously adapt this code, to pull out other information that we might be interested in. For example if we wanted to return the account importance we could add some code to do so:

print(int(accountInfo['account']['importance']))

Which will return the account importance of 9721098768.

Final Thoughts

Great – so we have worked our way through @nobu_kyutech‘s first example! I have included a link to the Jupyter Notebook here, feel free to play around and see if you can extract other information on this account or query your own Symbol address and have a look.

Hopefully I will add a translation of @nobu_kyutech‘s second example about retrieving transaction history from an account in the near future. Thanks for reading! And another huge thank you to the original poster! 🙂

Avatar photo
NineLives
admin@symbolblog.com

I'm a Symbol and NEM enthusiast and run this blog to try to grow awareness of the platform in the English-speaking world. If you have any Symbol news you would like me to report on or you have an article that you would like to publish then please let me know!

No Comments

Sorry, the comment form is closed at this time.