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

avatar
Authored by @hive.pizza

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!



0
0
0.000
17 comments
avatar

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
0
0
0.000
avatar

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.....

0
0
0.000
avatar

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.

0
0
0.000
avatar

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.

0
0
0.000
avatar

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.

0
0
0.000
avatar

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,

0
0
0.000
avatar

Hello,

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
Wallet('learncode').get_token('SWAP.HIVE')

{'_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: https://github.com/hive-engine/steemsmartcontracts-wiki.

0
0
0.000
avatar

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

0
0
0.000
avatar

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 https://github.com/hive-engine/steemsmartcontracts-wiki/blob/master/Tokens-Contract.md#delegations

fields:
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.

Example:

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

0
0
0.000
avatar

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

0
0
0.000
avatar

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

remaining commands 1

BUY AND STAKE THE PGM TO SEND A LOT OF TOKENS!

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)

image.png
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


0
0
0.000