Diving deeper into Cellframe: Python SDK and Plugins pt. 2

Category: Guide

Title image, read title

This article was originally written by Mika Hyttinen. Medium link: https://medium.com/@mika.hyttinen

Hi, it’s me again!

In this article we will be creating another simple plugin for Cellframe node. I honestly had to think a bit that what type of plugin would be great to do, so I decided to do something which shows some data from a wallet.

As Cellframe mainnet is now (about) released, it can be useful for node owners in the future to check their wallets from time to time.

I know, I know, Cellframe node has a command line tool for checking you wallet. I even created a small Python script to do that also which gets your wallet info via Telegram bot.

But let’s do this differently this time, we’ll use strictly Cellframe Python API to achieve this. And we’ll show it neatly on your web browser so no need to SSH to you Raspberry Pi / VPS.

Let’s go!

. . .

About Cellframe

Cellframe is an ambitious third generation blockchain project and few people (me included) have already started to talk about it as the next Polkadot or Kusama.

This project is, however, more service oriented and has some advantages compared to Polkadot/Kusama (eg. post-quantum cryptography, 2-level sharding, P2P cross-chain operations).

Cellframe is written in C (which will make it FAST), and it has an SDK for C and Python at the moment. More supported languages are coming in the future.

I really recommend visiting their website (https://cellframe.net) and read their whitepaper!

. . .

1. Create manifest.json and the Python file

So you have probably read my first tutorial of how to create a simple plugin for Cellframe node. If you haven’t, you should check that article and see how the plugin file / directory structure should be done.

In this tutorial, we’ll be using this type of manifest.json:

Therefore we have also a directory named tutorial2 and Python script with a name tutorial2.py and the directory / file structure looks like this:

2. Start coding tutorial2.py file

Luckily Cellframe gitlab has some sample plugins (here and here), which might give you some clues about how to get some data from a specific wallet.

Their wiki pages also have lots of resources for you to explore. Or if you have trouble understanding something, you can visit their Telegram development channel!

So after a bit of researching and testing, I came up with this:

We created a function getwalletBalance() which takes address as an input string.

I try to keep this as simple as possible. First we check that the address is valid by checking it’s length. It should be (AFAIK) always 104 characters.

addr = CF.ChainAddr.fromStr(address)
Converts our address to ChainAddr object.

net_id = addr.getNetId()
Gets our NetID with our wallet address

chain_net = CF.ChainNet.byId(net_id)
Gets ChainNet with our NetID

ledger = chain_net.getLedger()
Gets ledger from our chain

tokens = ledger.addrGetTokenTickerAllFast(addr)
Gets all token tickers available in that ledger with provided address.

After that, we loop through the tokens which are available and print the data to our terminal after converting them to a string. If there are no tokens available, the provided address is then empty and we print that to our terminal. Easy, right? (Yeah, right!)

Then it’s time to test it. We’ll just insert the init function to our plugin and call our function with addresses:

I’m testing this one address that has tCELLs in it, one is not valid address and one is my own which has no any tokens in it.

Then we should see something like this in our terminal when we start Cellframe node manually with /opt/cellframe-node/bin/cellframe-node.

Address: mJUUJk6Yk2gBSTjcCakotMsh4DQMr3Kuw1FhV7GUrDF8zLQfS1KPMATRpWADQdXnw9CAAz8ronw2sBUukDiqq7CMwnqUuwmyuHVK35tm
Token: tCELL
Balance: 100.000000000
Datoshi: 100000000000

Provided address is wrong! Address length should be 104 characters, provided address has 103 characters!
Provided address is empty!

Success! Now we need to slightly modify the getwalletBalance()function as we don’t want it to print the data to a console. We want it to return the data. So after modifying, it should look something like this:

Cellframe node has a built-in HTTP server so let’s use that to display that data on web browser. We need to add a process HttpSimple to init()function and also handler function which handles the requests from a web browser.

After modifying, this part of the file should look something like this:

I’m using ? as a delimiter in queries. When using it, you can check multiple wallets at the same time.

I also added some logging which will be shown in logs when someone is querying wallet addresses from a web browser.

And let’s look the whole plugin file now and actually run some tests with it!

Ok, let’s test it with curl (you can use a web browser if you wish). Remember to change your IP address to a correct host!

mika@cellframe:~$ curl http://192.168.1.10:8079/wallet?mJUUJk6Yk2gBSTjcCakotMsh4DQMr3Kuw1FhV7GUrDF8zLQfS1KPMATRpWADQdXnw9CAAz8ronw2sBUukDiqq7CMwnqUuwmyuHVK35tm

Address: mJUUJk6Yk2gBSTjcCakotMsh4DQMr3Kuw1FhV7GUrDF8zLQfS1KPMATRpWADQdXnw9CAAz8ronw2sBUukDiqq7CMwnqUuwmyuHVK35tm
Token: tCELL
Balance: 100.000000000
Datoshi: 100000000000

It works! How about with multiple addresses?

mika@cellframe:~$ curl http://192.168.1.10:8079/wallet?mJUUJk6Yk2gBSTjcCakotMsh4DQMr3Kuw1FhV7GUrDF8zLQfS1KPMATRpWADQdXnw9CAAz8ronw2sBUukDiqq7CMwnqUuwmyuHVK35tm?mJUUJk6Yk2gBSTjcCakotMsh4DQMr3Kuw1FhV7GUrDF8zLQfS1KPMATRpWADQdXnw9CAAz8ronw2sBUukDiqq7CMwnqUuwmyuHVK35tm?mJUUJk6Yk2gBSTjcCakotMsh4DQMr3Kuw1FhV7GUrDF8zLQfS1KPMATRpWADQdXnw9CAAz8ronw2sBUukDiqq7CMwnqUuwmyuHVK35tm

Address: mJUUJk6Yk2gBSTjcCakotMsh4DQMr3Kuw1FhV7GUrDF8zLQfS1KPMATRpWADQdXnw9CAAz8ronw2sBUukDiqq7CMwnqUuwmyuHVK35tm
Token: tCELL
Balance: 100.000000000
Datoshi: 100000000000

Address: mJUUJk6Yk2gBSTjcCakotMsh4DQMr3Kuw1FhV7GUrDF8zLQfS1KPMATRpWADQdXnw9CAAz8ronw2sBUukDiqq7CMwnqUuwmyuHVK35tmToken: tCELL
Balance: 100.000000000
Datoshi: 100000000000

Address: mJUUJk6Yk2gBSTjcCakotMsh4DQMr3Kuw1FhV7GUrDF8zLQfS1KPMATRpWADQdXnw9CAAz8ronw2sBUukDiqq7CMwnqUuwmyuHVK35tm
Token: tCELL
Balance: 100.000000000
Datoshi: 100000000000

I’m using the same wallet address here for all the queries. Let’s change them a bit and add a wrong and also an empty wallet:

curl http://192.168.1.10:8079/wallet?mJUUJk6Yk2gBSTjcCakotMsh4DQMr3Kuw1FhV7GUrDF8zLQfS1KPMATRpWADQdXnw9CAAz8ronw2sBUukDiqq7CMwnqUuwmyuHVK35tm?mJUUJk6Yk2gBSTjcDwPQqT61ApF9ELT?mJUUJk6Yk2gBSTjcDwPQqT61ApF9ELTboYPnZgLDBXbRukqda4hNyWbBSJU876Qf9yWGbJzFcgiZiRsx8M2Rr3pXYdTLViXPgUc3Phh2
Address: mJUUJk6Yk2gBSTjcCakotMsh4DQMr3Kuw1FhV7GUrDF8zLQfS1KPMATRpWADQdXnw9CAAz8ronw2sBUukDiqq7CMwnqUuwmyuHVK35tm
Token: tCELL
Balance: 100.000000000
Datoshi: 100000000000

Provided address is wrong! Address length should be 104 characters, provided address has 31 characters!Provided address is empty!

And in the logs:

[04/07/22-16:05:00] [ * ] [libdap-python] Getting wallet info for mJUUJk6Yk2gBSTjcCakotMsh4DQMr3Kuw1FhV7GUrDF8zLQfS1KPMATRpWADQdXnw9CAAz8ronw2sBUukDiqq7CMwnqUuwmyuHVK35tm
[04/07/22-16:05:00] [ * ] [libdap-python] Getting wallet info for mJUUJk6Yk2gBSTjcDwPQqT61ApF9ELT
[04/07/22-16:05:00] [ * ] [libdap-python] Getting wallet info for mJUUJk6Yk2gBSTjcDwPQqT61ApF9ELTboYPnZgLDBXbRukqda4hNyWbBSJU876Qf9yWGbJzFcgiZiRsx8M2Rr3pXYdTLViXPgUc3Phh2

Success!

Conclusion

I don’t in any way consider myself as a professional programmer, my specialities are in hardware and networking issues 😉.

With that said, I would really like to see real programmers exploring the SDK, building different plugins and t-dApps. Cellframe SDK is super powerful and fun to use!

There is also a possibility to run a node if you’re interested!

You can find the sources for this tutorial @ https://github.com/CELLgainz/Cellframe/tree/main/cellframe-plugins/tutorial2

NOTE: I have also made a version with 5.1 API which will work with the new version of Cellframe node. Sources are available here: https://github.com/CELLgainz/Cellframe/tree/main/cellframe-plugins/tutorial2_5.1

If you’re interested building something on the future of blockchains, join their development Telegram channel: https://t.me/cellframe_dev_en