create account

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

View this thread on: hive.blogpeakd.comecency.com
· @learncode ·
$5.91
Part 6: Coding on Hive with Python - Interacting with the Hive-Engine Side Chain
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!

πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 23 others
properties (23)
authorlearncode
permlinkpart-6-coding-on-hive-with-python-interacting-with-the-hive-engine-side-chain
categoryprogramming
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"]}"
created2021-07-31 22:11:42
last_update2021-07-31 22:11:42
depth0
children17
last_payout2021-08-07 22:11:42
cashout_time1969-12-31 23:59:59
total_payout_value2.963 HBD
curator_payout_value2.946 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,068
author_reputation682,726,195,093
root_title"Part 6: Coding on Hive with Python - Interacting with the Hive-Engine Side Chain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id105,242,774
net_rshares9,159,188,077,095
author_curate_reward""
vote details (87)
@bashadow ·
$0.03
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.
πŸ‘  , ,
properties (23)
authorbashadow
permlinkre-learncode-r1mikg
categoryprogramming
json_metadata{"tags":["programming"],"app":"peakd/2021.09.1"}
created2021-10-27 06:41:00
last_update2021-10-27 06:41:00
depth1
children2
last_payout2021-11-03 06:41:00
cashout_time1969-12-31 23:59:59
total_payout_value0.015 HBD
curator_payout_value0.013 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length622
author_reputation100,388,692,638,882
root_title"Part 6: Coding on Hive with Python - Interacting with the Hive-Engine Side Chain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id107,319,739
net_rshares26,055,117,953
author_curate_reward""
vote details (3)
@learncode ·
$0.13
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. 
πŸ‘  
properties (23)
authorlearncode
permlinkre-bashadow-20211027t7428632z
categoryprogramming
json_metadata{"tags":["programming"],"app":"ecency/3.0.23-mobile","format":"markdown+html"}
created2021-10-27 14:04:30
last_update2021-10-27 14:04:30
depth2
children1
last_payout2021-11-03 14:04:30
cashout_time1969-12-31 23:59:59
total_payout_value0.064 HBD
curator_payout_value0.065 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length195
author_reputation682,726,195,093
root_title"Part 6: Coding on Hive with Python - Interacting with the Hive-Engine Side Chain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id107,326,499
net_rshares114,813,561,346
author_curate_reward""
vote details (1)
@bashadow ·
$0.03
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.
πŸ‘  ,
properties (23)
authorbashadow
permlinkre-learncode-r1nexx
categoryprogramming
json_metadata{"tags":["programming"],"app":"peakd/2021.09.1"}
created2021-10-27 18:20:12
last_update2021-10-27 18:20:12
depth3
children0
last_payout2021-11-03 18:20:12
cashout_time1969-12-31 23:59:59
total_payout_value0.016 HBD
curator_payout_value0.014 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length225
author_reputation100,388,692,638,882
root_title"Part 6: Coding on Hive with Python - Interacting with the Hive-Engine Side Chain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id107,331,815
net_rshares27,485,483,993
author_curate_reward""
vote details (2)
@cliffagreen ·
Thank you! 
πŸ‘  
properties (23)
authorcliffagreen
permlinkre-learncode-2021731t16335578z
categoryprogramming
json_metadata{"tags":["programming","coding","hive","python","ctp","palnet","proofofbrain","stem","broadhive","hivepizza"],"app":"ecency/3.0.18-vision","format":"markdown+html"}
created2021-07-31 23:33:54
last_update2021-07-31 23:33:54
depth1
children0
last_payout2021-08-07 23:33:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length11
author_reputation41,836,792,579,395
root_title"Part 6: Coding on Hive with Python - Interacting with the Hive-Engine Side Chain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,244,118
net_rshares7,379,607
author_curate_reward""
vote details (1)
@ecency ·
**Yay!** πŸ€—<br>Your content has been **boosted with Ecency Points**, by @torrey.blog. <br>Use Ecency daily to boost your growth on platform! <br><br><b>Support Ecency</b><br>[Vote for Proposal](https://hivesigner.com/sign/update-proposal-votes?proposal_ids=%5B141%5D&approve=true)<br>[Delegate HP and earn more](https://ecency.com/hive-125125/@ecency/daily-100-curation-rewards)
properties (22)
authorecency
permlinkre-202181t64848333z
categoryprogramming
json_metadata{"tags":["ecency"],"app":"ecency/3.0.16-welcome","format":"markdown+html"}
created2021-08-01 06:48:48
last_update2021-08-01 06:48:48
depth1
children0
last_payout2021-08-08 06:48:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length377
author_reputation624,379,752,280,270
root_title"Part 6: Coding on Hive with Python - Interacting with the Hive-Engine Side Chain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,249,997
net_rshares0
@hivebuzz ·
Congratulations @learncode! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@learncode/upvoted.png?202108010834"></td><td>You received more than 500 upvotes.<br>Your next target is to reach 600 upvotes.</td></tr>
</table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@learncode) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



**Check out the last post from @hivebuzz:**
<table><tr><td><a href="/hivebuzz/@hivebuzz/pud-202108"><img src="https://images.hive.blog/64x128/https://i.imgur.com/805FIIt.jpg"></a></td><td><a href="/hivebuzz/@hivebuzz/pud-202108">Hive Power Up Day - August 1st 2021 - Hive Power Delegation</a></td></tr></table>
properties (22)
authorhivebuzz
permlinkhivebuzz-notify-learncode-20210801t085646
categoryprogramming
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2021-08-01 08:56:45
last_update2021-08-01 08:56:45
depth1
children0
last_payout2021-08-08 08:56:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length942
author_reputation369,390,339,508,590
root_title"Part 6: Coding on Hive with Python - Interacting with the Hive-Engine Side Chain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,251,784
net_rshares0
@javivisan ·
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,
properties (22)
authorjavivisan
permlinkre-learncode-rchi94
categoryprogramming
json_metadata{"tags":["programming"],"app":"peakd/2022.05.8"}
created2022-05-26 09:55:51
last_update2022-05-26 09:55:51
depth1
children6
last_payout2022-06-02 09:55:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length428
author_reputation276,953,032,680,683
root_title"Part 6: Coding on Hive with Python - Interacting with the Hive-Engine Side Chain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id113,530,039
net_rshares0
@learncode ·
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.
properties (22)
authorlearncode
permlinkre-javivisan-rcmdvf
categoryprogramming
json_metadata{"tags":["programming"],"app":"peakd/2022.05.8"}
created2022-05-29 01:09:18
last_update2022-05-29 01:09:18
depth2
children5
last_payout2022-06-05 01:09:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length641
author_reputation682,726,195,093
root_title"Part 6: Coding on Hive with Python - Interacting with the Hive-Engine Side Chain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id113,597,619
net_rshares0
@javivisan ·
I'm going to see the documentation to find a solution.
Your post helped me a lot, thanks!!!
properties (22)
authorjavivisan
permlinkre-learncode-rcnixf
categoryprogramming
json_metadata{"tags":["programming"],"app":"peakd/2022.05.8"}
created2022-05-29 15:56:03
last_update2022-05-29 15:56:03
depth3
children4
last_payout2022-06-05 15:56:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length91
author_reputation276,953,032,680,683
root_title"Part 6: Coding on Hive with Python - Interacting with the Hive-Engine Side Chain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id113,614,190
net_rshares0
@pizzabot · (edited)
RE: Part 6: Coding on Hive with Python - Interacting with the Hive-Engine Side Chain
<center>PIZZA! 


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


<sub>Join us in <a href="https://discord.gg/hivepizza">Discord</a>!</sub></center>
πŸ‘  
properties (23)
authorpizzabot
permlinkre-part-6-coding-on-hive-with-python-interacting-with-the-hive-engine-side-chain-20210731t222524z
categoryprogramming
json_metadata"{"app": "beem/0.24.26"}"
created2021-07-31 22:25:24
last_update2022-06-01 20:02:00
depth1
children0
last_payout2021-08-07 22:25:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length225
author_reputation7,563,984,529,447
root_title"Part 6: Coding on Hive with Python - Interacting with the Hive-Engine Side Chain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,243,250
net_rshares0
author_curate_reward""
vote details (1)
@poshtoken ·
https://twitter.com/HiveTrending/status/1421594951821840386
<sub> 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.</sub>
properties (22)
authorposhtoken
permlinkre-learncode-part-6-coding-on-hive-with-python-interacting-with57957
categoryprogramming
json_metadata"{"app":"Poshtoken 0.0.1"}"
created2021-07-31 22:17:48
last_update2021-07-31 22:17:48
depth1
children0
last_payout2021-08-07 22:17:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length247
author_reputation5,425,560,732,756,377
root_title"Part 6: Coding on Hive with Python - Interacting with the Hive-Engine Side Chain"
beneficiaries
0.
accountreward.app
weight10,000
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id105,242,863
net_rshares0
@stickupboys ·
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](https://files.peakd.com/file/peakd-hive/stickupboys/23tw9tsKHZ697q3P9AspA2Z4iCXEo7PdnUHYHooxVbmR1M6Nq3yVbfiYebDB56JNNudzR.png)

Also I still not worked out who Jason is.....
properties (22)
authorstickupboys
permlinkre-learncode-qz1brj
categoryprogramming
json_metadata{"tags":["programming"],"app":"peakd/2021.08.2"}
created2021-09-06 22:57:21
last_update2021-09-06 22:57:21
depth1
children1
last_payout2021-09-13 22:57:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length376
author_reputation488,834,890,932,513
root_title"Part 6: Coding on Hive with Python - Interacting with the Hive-Engine Side Chain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id106,071,169
net_rshares0
@pizzabot ·
<center><p>@learncode! I sent you a slice of <b>$PIZZA</b> on behalf of @stickupboys.</p>
<p>Did you know you can now buy <a href="https://www.risingstargame.com/?referrer=hive.pizza">Rising Star</a> packs with $PIZZA?
<sub>(10/20)</sub></p></center>
properties (22)
authorpizzabot
permlinkre-re-learncode-qz1brj-20210906t225817z
categoryprogramming
json_metadata"{"app": "beem/0.24.26"}"
created2021-09-06 22:58:18
last_update2021-09-06 22:58:18
depth2
children0
last_payout2021-09-13 22:58:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length250
author_reputation7,563,984,529,447
root_title"Part 6: Coding on Hive with Python - Interacting with the Hive-Engine Side Chain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id106,071,180
net_rshares0