create account

Tx 62bb5d983eb7ecfa7a7db83ea0b8c43ed1cf1202@56134844

Included in block 56,134,844 at 2021-07-31 22:11:45 (UTC)

62bb5d98 comment options: 0.0% HBD, allow votes: true, allow curation rewards: true

Raw transaction

ref_block_num36,004
ref_block_prefix822,050,240
expiration2021-07-31 22:21:39
operations
0.
0.comment
1.
parent_author""
parent_permlinkprogramming
authorlearncode
permlinkpart-6-coding-on-hive-with-python-interacting-with-the-hive-engine-side-chain
title"Part 6: Coding on Hive with Python - Interacting with the Hive-Engine Side Chain"
body"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](https://peakd.com/@learncode) first.
![image.png](https://files.peakd.com/file/peakd-hive/learncode/23u6X11XGstWpCyLXh5YWefTaaXY4MGUL3jtNv2sd1ou9HSNGSTg3xVn2VZiVspkg5hNg.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](https://pypi.org/project/hiveengine/).
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](https://he.dtools.dev/@learncode) to see the transaction. The summary looks like this:
![image.png](https://files.peakd.com/file/peakd-hive/learncode/23uRJ9mRuBW96fS2VJDN7oEcuiyK3hmYLMWHYgv6QvWxaScv5A6eTqpYR53eCttWRHGNh.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](https://files.peakd.com/file/peakd-hive/learncode/48FmV6aVsydEcdwQjkVayq882TeSyH3PYBK1buFLmqMwb5kCUaqYx4Q2goxJRoea1m.png)
---
We can also use a [Hive base chain block explorer tool](https://hiveblocks.com/@learncode) 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](https://files.peakd.com/file/peakd-hive/learncode/23tHbKgySHhPSNWyDsmT7TP9gmyLXAXR1tEX3tZepJD4MyRs9LtKbvNys3Mk4yEyL1gLk.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](https://files.peakd.com/file/peakd-hive/learncode/23u5ZX2czd4dwvPTAxnpNvXoYYWUL23xvbe8hF6Kts2Qnc6598MEYjZuSEG8bCR159T3N.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](https://vote.hive.uno/@pizza.witness) of approval for our witness (@pizza.witness). Here's a convenient way to [vote](https://vote.hive.uno/@pizza.witness) using HiveKeychain or HiveSigner: https://vote.hive.uno/@pizza.witness. Thanks for your support!
"
json_metadata"{"app":"peakd/2021.07.5","format":"markdown","author":"hive.pizza","description":"Using Python to check Hive-Engine token balances and transfer tokens between wallets.","tags":["programming","coding","hive","python","ctp","palnet","proofofbrain","stem","broadhive","hivepizza"],"users":["learncode","Hive.Pizza","hivetrending","thebeardflex","pizza.witness","pizza.witness."],"image":["https://files.peakd.com/file/peakd-hive/learncode/23u6X11XGstWpCyLXh5YWefTaaXY4MGUL3jtNv2sd1ou9HSNGSTg3xVn2VZiVspkg5hNg.png","https://files.peakd.com/file/peakd-hive/learncode/23uRJ9mRuBW96fS2VJDN7oEcuiyK3hmYLMWHYgv6QvWxaScv5A6eTqpYR53eCttWRHGNh.png","https://files.peakd.com/file/peakd-hive/learncode/48FmV6aVsydEcdwQjkVayq882TeSyH3PYBK1buFLmqMwb5kCUaqYx4Q2goxJRoea1m.png","https://files.peakd.com/file/peakd-hive/learncode/23tHbKgySHhPSNWyDsmT7TP9gmyLXAXR1tEX3tZepJD4MyRs9LtKbvNys3Mk4yEyL1gLk.png","https://files.peakd.com/file/peakd-hive/learncode/23u5ZX2czd4dwvPTAxnpNvXoYYWUL23xvbe8hF6Kts2Qnc6598MEYjZuSEG8bCR159T3N.png"]}"
1.
0.comment_options
1.
authorlearncode
permlinkpart-6-coding-on-hive-with-python-interacting-with-the-hive-engine-side-chain
max_accepted_payout1,000,000.000 HBD
percent_hbd0
allow_votestrue
allow_curation_rewardstrue
extensions[]
extensions[]
signatures
0.203f7af0996e783b9101fb398730b781843742b61f8b62f09aa67269c9437ae1a13f5f2e5cd32fcdcc1594540079e7ddc84759e6daff8842571e67fa5ece301601
transaction_id62bb5d983eb7ecfa7a7db83ea0b8c43ed1cf1202
block_num56,134,844
transaction_num19