Part 6: Coding on Hive with Python - Interacting with the Hive-Engine Side Chain

in #programming4 months ago

Part 6 of this series discusses how to interact with the Hive-Engine side chain, to check a wallet token balance, and to send tokens to another wallet.

The examples here require some knowledge from parts 1 - 3. If you’re new to coding on Hive with Python, it’s strongly recommended to review the earlier posts first.

image.png


Background - What is the Hive-Engine Side Chain and how can Python talk to it?

Hive has a really interesting system called Hive-Engine, which operates as a separate chain on top of Hive (Layer 2). This decentralized system runs with its own set of witnesses, and its governance token is WORKERBBEE. Hive-Engine allows individuals to mint tokens for various purposes, such as Tribe sites. Those are blogging communities centered around their own token, i.e. leofinance.io and the LEO token.

There's a convenient Python module called hiveengine for doing basic Hive-Engine operations like checking wallet balances and making transfers. The module has some limited documentation here.

Using Python and the hiveengine library (which uses beem under-the-hood), it’s easy to check for Hive-Engine wallet balances. The hiveengine module is installed the same as other modules in Part 1 of this tutorial series: python3 -m pip install hiveengine.

Step 1 - Checking wallet balances for Hive-Engine tokens

In step 1, hiveengine is called upon to check a wallet balance for the PIZZA token. Yum! First import hiveengine.wallet, then ask the user for a wallet name, then instantiate a Wallet() object and call its get_token() function with a token name. Last, print() out the wallet's balance value.

Code snippet:

import hiveengine.wallet

name = input('Enter wallet name: ')

wallet = hiveengine.wallet.Wallet(name).get_token('PIZZA')

print('@%s has %s $PIZZA' % (name, wallet['balance']))

Expected output:

Enter wallet name: learncode
@learncode has 1.08 $PIZZA

Note you may see a scary error message about a Hive API node issue like Lost connection or internal error on node. This can be safely ignored. The code will auto-switch to a working API node.


Step 2 - Making a Hive-Engine token transfer

In this step, we will use hiveengine module to transfer tokens to another wallet. Doing this requires our wallet's active key. If active key wasn't required Hive-Engine side chain would have a massive security issue because unauthorized apps could transfer our tokens! Like we did in part3, getpass module is used to mask the input of the key like a password, to protect against shoulder-surfing.

This time we need to import some extra modules. The activeKey is passed into instantiate the Hive() blockchain object, similar to what was done with the posting key in LearnCode part 3. Then this blockchain object is used to instantiate a new hive engine Wallet() object. Then we use the wallet's transfer() function to send the tokens.

Code snippet:

import beem
import getpass
import hiveengine.wallet

name = input('Enter wallet name: ')
wallet = hiveengine.wallet.Wallet(name).get_token('PIZZA')
print('@%s has %s $PIZZA' % (name, wallet['balance']))

print('---')

sendTo = input('Who do you want to send $PIZZA to? ')
amount = input('How much $PIZZA do you want to send? ')
activeKey = getpass.getpass(prompt='What is your wallet active private key? ')

HIVE = beem.Hive(keys=[activeKey])
wallet = hiveengine.wallet.Wallet(name, blockchain_instance=HIVE)
wallet.transfer(sendTo, amount, 'PIZZA', memo='Hello from LearnCode6!')

Expected output:

Enter wallet name: learncode
@learncode has 1.08 $PIZZA
---
Who do you want to send $PIZZA to? learncode
How much $PIZZA do you want to send? 0.01
What is your wallet active private key? 

Confirming the Result using Hive-Engine block explorer and Hive Block Explorer Tools

We can immediately use a Hive-Engine block explorer tool to see the transaction. The summary looks like this:

image.png

The same tool allows drilling down into the details of the side-chain transaction. The below screenshot shows all the gorey details.

image.png


We can also use a Hive base chain block explorer tool to see the transaction. In this case, the tool displays the raw Custom_JSON operation. The contents of this operation were filled out for us by the hiveengine Python module.

image.png


Lastly, if we wait a minute and run the example code again, it shows the correct updated wallet balance:

Enter wallet name: learncode
@learncode has 1.07 $PIZZA

image.png

p.s. Thanks for reading. Please let me know if anything is unclear or incorrect. This is intended to be a living document to be improved and maintained over time.


This blog is produced by the @Hive.Pizza team (@hivetrending and @thebeardflex). PIZZA Crew is a Hive-powered social group empowering content creators, gamers, and YOU. We host game servers, a DLUX node, a Hive-Engine node, and now a Hive witness node! Please help us out and give your vote of approval for our witness (@pizza.witness). Here's a convenient way to vote using HiveKeychain or HiveSigner: https://vote.hive.uno/@pizza.witness. Thanks for your support!

Sort:  

Thank you I found that pretty useful. I am trying to learn a bit more about python, and more importantly learn how to get information such as you presented in the Step 1 - Checking wallet balances for Hive-Engine tokens I had trouble installing the hiveengine but with the links you provided and two ways of installing, it finally worked. I was able to just change the token name in the script and it worked.

It is getting pretty late here, so will try doing a few other things tomorrow. One thing when you have time would be getting token history, but I will eventually figure that out also, just have to keep looking.

Glad you found this blog. Happy to help.

The HiveEngine module documentation is pretty light, but the code is well organized and you can easily find the function for fetching the token history.

That is what I am hoping, I am still very new to python and that is why I bought my raspberrypi in November last year, I played with it during winter, and winter is here again, so hoping to re catch up on it some this winter.

Thank you!


The rewards earned on this comment will go directly to the person sharing the post on Twitter as long as they are registered with @poshtoken. Sign up at https://hiveposh.com.

Connect

Trade


@learncode! This post has been manually curated by the $PIZZA Token team!

Learn more about $PIZZA Token at hive.pizza. Enjoy a slice of $PIZZA on us!

Yay! 🤗
Your content has been boosted with Ecency Points, by @torrey.blog.
Use Ecency daily to boost your growth on platform!

Support Ecency
Vote for Proposal
Delegate HP and earn more

Congratulations @learncode! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You received more than 500 upvotes.
Your next target is to reach 600 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out the last post from @hivebuzz:

Hive Power Up Day - August 1st 2021 - Hive Power Delegation

Good work, I love what you do so creativly....it is a very creative thing and you do it so well !PIZZA ...also it did say to let you know what bit is unclear....well this bit in particular
image.png

Also I still not worked out who Jason is.....

@learncode! I sent you a slice of $PIZZA on behalf of @stickupboys.

Did you know you can now buy Rising Star packs with $PIZZA? (10/20)