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

in #programming3 years ago
Authored by

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.


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. 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']))


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:


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


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.


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


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: Thanks for your support!


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.

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


PIZZA Holders sent $PIZZA tips in this post's comments:
@javivisan(2/5) tipped @learncode (x1)

Join us in Discord!

Yay! 🤗
Your content has been boosted with Ecency Points, by
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 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

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)

Dear @learncode, thank you very much for your post, it has been very helpful.
Is it possible to get the data of members who have delegated a token to an account?
I need their account name and the delegated amount.

We have a clan account in Splinterlands where all members delegate tokens and I want to send the generated earnings to each member based on the amount delegated.

Looking forward to hearing from you.
Kind regards,


I have not found a simple way to get a list of incoming delegations for a token.

This snippet of code will tell the total amount of incoming or outgoing delegation.

from hiveengine.wallet import Wallet

{'_id': 254860, 'account': 'learncode', 'symbol': 'SWAP.HIVE', 'balance': '0.00000000', 'stake': '0', 'pendingUnstake': '0', 'delegationsIn': '0', 'delegationsOut': '0', 'pendingUndelegations': '0'}

It may be possible to query the data you want my making a JSON RPC request. A good reference is the documentation here:

I'm going to see the documentation to find a solution.
Your post helped me a lot, thanks!!!

Thank you for the feedback.

Just following up on your question about getting the list of incoming delegations to an account. I found a simple way to request this data using the hiveengine API object.

Here's what it looks like:

from hiveengine.api import Api
api = Api()
api.find("tokens", "delegations", query={"to": "learncode"})

In this case, the "tokens" string is the contract name, the "delegations" string is the table name, and the query is like a search filter for MongoDB.

There are additional filters you can use, as documented at

from = account that initiated the delegation
to = account that received the delegation
symbol = symbol of the token delegated
quantity = quantity of tokens delegated

So you can request the list of delegations based on the account delegated to or from, the symbol for the token, and the quantity. I believe you can include logical statements in the query also, to do things like only return results where the quantity is greater than N.


api.find("tokens", "delegations", query={"to": "learncode", "from": "javivisan", "symbol": "BEE"})

The rewards earned on this comment will go directly to the people(@hivetrending) sharing the post on Twitter as long as they are registered with @poshtoken. Sign up at

The code works great!!!! Thank you so much, I had been looking for a solution to the problem for months.

Sent 0.1 PGM - 0.1 LVL- 1 STARBITS - 0.01 MOTA - 0.05 DEC - 15 SBT tokens to @learncode

remaining commands 1


The tokens that the command sends are: 0.1 PGM-0.1 LVL-2.5 BUDS-0.01 MOTA-0.05 DEC-15 SBT-1 STARBITS-0.00000001 BTC (SWWAP.BTC)

Discord image.png

Support the curation account @ pgm-curator with a delegation 10 HP - 50 HP - 100 HP - 500 HP - 1000 HP

Get potential votes from @ pgm-curator by paying in PGM, here is a guide

I'm a bot, if you want a hand ask @ zottone444