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. 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
import hiveengine.wallet name = input('Enter wallet name: ') wallet = hiveengine.wallet.Wallet(name).get_token('PIZZA') print('@%s has %s $PIZZA' % (name, wallet['balance']))
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.
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!')
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: https://email@example.com. Thanks for your support!