Hive Developer Portal
How to convert your HBD to HIVE using Python.
Full, runnable src of Convert HBD To HIVE can be downloaded as part of: tutorials/python (or download just this tutorial: devportal-master-tutorials-python-32_convert_hbd_to_hive.zip).
In this tutorial we will explain and show you how to convert some or all of your available HBD balance into HIVE on the Hive blockchain using the
commit class found within the beem library.
It should be noted that, using this process, the converted HIVE will not be available instantly as it takes 3.5 days for the transaction to be processed. It is also not possible to stop a conversion once initialized. During the 3.5 days for it to be converted, if the conversion price fluctuates above the target, you will likely receive much less HIVE than expected. Because of this, the method in this tutorial is NOT the preferred and often NOT the most efficient way of converting HBD to HIVE. This tutorial just illustrates that it can be done in this manner.
Note: This is not a market process and will often result in unfavorable outcomes, if used bindly.
There is an internal market on Hive that allows you to sell your HBD. You should use the internal market for routine trades between HBD and HIVE. With the market process you can get your HIVE (sometimes immediately) and at the exact price that you expect. The market place is almost always the better way to exchange your HBD. This article provides more information on using the market to exchange your HBD for HIVE.
The purpose of this process is for experts to help maintain the target price of HBD, not to provide convenience to the end-user.
The Hive python library has built-in functionality to transmit transactions to the blockchain. We are using the
convert method found within the
Account instance. Before we do the conversion, we check the current balance of the account to check how much HBD is available. This is not strictly necessary as the process will automatically abort with the corresponding error, but it does give some insight into the process as a whole. We use the
Account module to check for this. The
convert method has 2 parameters:
- amount - The amount of HBD that will be converted
- request-id - An identifier for tracking the conversion. This parameter is optional
- App setup - Library install and import. Connection to testnet
- User information and Hive node - Input user information and connection to Hive node
- Check balance - Check current HIVE and HBD balance of user account
- Conversion amount and commit - Input of HBD amount to convert and commit to blockchain
In this tutorial we only use 1 package:
beem- hive library and interaction with Blockchain
from pick import pick import getpass from beem import Hive from beem.account import Account
Because this tutorial alters the blockchain we connect to the testnet so we don’t create spam on the production server.
We require the
private active key of the user in order for the conversion to be committed to the blockchain. This is why we have to specify this alongside the
testnet node. The values are supplied via the terminal/console before we initialize the beem class.
# capture user information account = input('Enter username: ') wif_active_key = getpass.getpass('Enter private ACTIVE key: ') # connect node and private active key client = Hive('http://127.0.0.1:8090', keys=[wif_active_key])
In order to give the user enough information to make the conversion we check the current balance of the account using the
# get account balance for HIVE and HBD account = Account(account, blockchain_instance=client) total_hive = account['balance'] total_hbd = account['hbd_balance'] print('CURRENT ACCOUNT BALANCE:' + '\n' + str(total_hive) + '\n' + str(total_hbd) + '\n')
The result of the query is displayed in the console/terminal.
The final step before we can commit the transaction to the blockchain is to assign the
amount parameter. We do this via a simple input from the terminal/console.
# get recipient name convert_amount = float(input('Enter the amount of HBD to convert to HIVE: ') or '0') if (convert_amount <= 0): print("Must be greater than zero.") exit()
This value must be greater than zero in order for the transaction to execute without any errors. Now that we have all the parameters we can do the actual transmission of the transaction to the blockchain.
# parameters: amount, request_id account.convert(convert_amount) print('\n' + format(convert_amount, '.3f') + ' HBD has been converted to HIVE')
If no errors are encountered a simple confirmation is printed on the UI.
As an added confirmation we check the balance of the user again and display it on the UI. This is not required at all but it serves as a more definitive confirmation that the conversion has been started correctly.
# get remaining account balance for HIVE and HBD account.refresh() total_hive = account['balance'] total_hbd = account['hbd_balance'] print('\n' + 'REMAINING ACCOUNT BALANCE:' + '\n' + str(total_hive) + '\n' + str(total_hbd))
The HIVE balance will not yet have been updated as it takes 3.5 days to settle. The HBD will however show the new balance.
To Run the tutorial
Before running this tutorial, launch your local testnet, with port 8090 mapped locally to the docker container:
docker run -d -p 8090:8090 inertia/tintoy:latest
For details on running a local testnet, see: Setting Up a Testnet
- review dev requirements
git clone https://gitlab.syncad.com/hive/devportal.git
pip install -r requirements.txt
- After a few moments, you should see a prompt for input in terminal screen.