Arashtad Blog
  • MARKET
  • SERVICES
  • COURSES
  • DOCUMENTS
  • BLOG
  • FORUM
  • JOBS
  • ACCOUNT
      • Back
      • Register
      • Login

Harmony Blockchain: Getting Familiar with It Using Python

4 Comments
Harmony Blockchain: Getting Familiar with It Using Python

In this article, we are going to get familiar with the Harmony blockchain. and install the dependencies such as Docker to be able to connect to the Harmony blockchain. After installing everything and connecting to the blockchain, using the Python scripts, we will get some data from the shard of the Testnet.

What Is Harmony Blockchain?

Harmony is a powerful blockchain platform designed to ease and facilitate the development of Dapps. The innovation of Harmony in decentralized applications is based on random state sharding. The random state sharding allows creating blocks faster. This blockchain is EVM compatible with staking features as well. The good news for Python developers is that all the smart contracts in Harmony are written in Python. Developing on Harmony looks very familiar for Ethereum developers because it is fully Ethereum compatible.

Installing the Dependencies for Harmony Blockchain

To get started with the harmony blockchain, follow the instructions below:
For Windows and Linux:

Coppied to clipboard. pip install pyhmy

For Mac Os and Linux:

Coppied to clipboard. sudo pip3 install pathlib
sudo pip3 install pyhmy

Docker

Docker is the most important dependency for Harmony blockchain. However, it's different to install it based on the operating system and we are going through on Linux

Installation Guide on Linux

1. Depedencies

Enter the following commands one by one in the terminal to install the docker dependencies:

Coppied to clipboard. sudo apt update
sudo apt -y install apt-transport-https ca-certificates curl software-properties-common
sudo apt -y remove docker docker-engine docker.io containerd runc

2. Add Docker’s official GPG key

Enter the following 2 commands one by one in the terminal:

Coppied to clipboard. curl -fsSL https://download.docker.com/linux/ubuntu/gpg |
sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

3. Add the Docker repository to Linux:

To add the docker repository to Linux, enter the 2 following commands separately in the terminal:

Coppied to clipboard. echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu bionic stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu bionic stable

4. Install Docker Engine and Docker Compose:

The 2 separate commands should install the Docker engine and Docker Compose for you:

Coppied to clipboard. sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io

5. Checking the installation:

To check the installation, first install the 2 following commands in the terminal:

Coppied to clipboard. sudo usermod -aG docker
USER newgrp docker

And then, check the version of Docker:

Coppied to clipboard. docker version

Result:

Coppied to clipboard. Client: Docker Engine - Community Version: 20.10.14 API version: 1.41 Go version: go1.16.15 Git commit: a224086 Built: Thu Mar 24 01:47:57 2022 OS/Arch: linux/amd64 Context: default Experimental: trueServer: Engine: Version: 20.10.14 API version: 1.41 (minimum version 1.12) Go version: go1.16.15 Git commit: 87a90dc Built: Thu Mar 24 17:15:03 2022 OS/Arch: linux/amd64 Experimental: false containerd: Version: v1.5.11 GitCommit: 3df54a852345ae127d1fa3092b95168e4a88e2f8 runc: Version: 1.0.3 GitCommit: docker-init: Version: 0.19.0 GitCommit: de40ad0

After installation for every use, you need to first sign in to docker and run the following commands in terminal:

Coppied to clipboard. sudo usermod -aG docker
docker login

Now, let’s git clone the harmony blockchain repository by using the following command in the terminal:

Coppied to clipboard. git clone https://github.com/harmony-one/pyhmy

Then, install it using this command in the terminal of the same directory:

Coppied to clipboard. make install

Finally, the whole set of the following commands altogether will provide you the connection to the Harmony blockchain:

Coppied to clipboard. mkdir -p $(go env GOPATH)/src/github.com/harmony-one
cd $(go env GOPATH)/src/github.com/harmony-one
git clone https://github.com/harmony-one/mcl.git
git clone https://github.com/harmony-one/bls.git
git clone https://github.com/harmony-one/harmony.git
cd harmony
make test-rpc

Once you faced the "=== FINISHED RPC TESTS ===" message and also passed all tests, you can be sure that everything has gone the right way.

Connecting to Harmony Testnet:

Using the below scripts, we will at first to a Testnet of shard0 in the Harmony blockchain and then use a test address to check the balance, balance by shard, latest balance, and the account nonce:


from pyhmy import account

test_net = 'https://api.s0.b.hmny.io'	# this is shard 0
test_address = 'one18t4yj4fuutj83uwqckkvxp9gfa0568uc48ggj7'

balance = account.get_balance(test_address, endpoint=test_net)				
total_balance = account.get_total_balance(test_address, endpoint=test_net)	
balance_by_shard = account.get_balance_on_all_shards(test_address, endpoint=test_net)												
genesis_balance = account.get_balance_by_block(test_address, block_num=0, endpoint=test_net)
latest_balance = account.get_balance_by_block(test_address, block_num='latest', endpoint=test_net)						
account_nonce = account.get_account_nonce(test_address, block_num='latest', endpoint=test_net)									

print(balance)
print(total_balance)
print(balance_by_shard)
print(genesis_balance)
print(latest_balance)
print(account_nonce)


Result:

Coppied to clipboard. 10000000000000000000 1010000000000000000000 [{'shard': 0, 'balance': 10000000000000000000}, {'shard': 1, 'balance': 0}, {'shard': 2, 'balance': 1000000000000000000000}, {'shard': 3, 'balance': 0}] 0 10000000000000000000 10169

And by using the following scripts, we can get the data of the number out of transactions that have been sent and received on shard 0 of the Testnet.


from pyhmy import account

test_net = 'https://api.s0.b.hmny.io'	# this is shard 0
test_address = 'one18t4yj4fuutj83uwqckkvxp9gfa0568uc48ggj7'

tx_count = account.get_transactions_count(test_address, tx_type='ALL', endpoint=test_net) 											
sent_tx_count = account.get_transactions_count(test_address, tx_type='SENT', endpoint=test_net)							
received_tx_count = account.get_transactions_count(test_address, tx_type='RECEIVED', end-point=test_net)									
legacy_tx_count = account.get_transaction_count(test_address, block_num='latest', endpoint=test_net)	
# API is legacy
legacy_tx_count_pending = account.get_transaction_count(test_address, block_num='pending', end-point =        
       test_net)
       
print(tx_count)
print(sent_tx_count)
print(received_tx_count)
print(legacy_tx_count)
print(legacy_tx_count_pending)


Result:

Coppied to clipboard. 10211 10162 49 10169 10169

Using the code below, we can get the number of staking transactions in total, sent and received on shard 0 of the Harmony Testnet:


from pyhmy import account

test_net = 'https://api.s0.b.hmny.io'	# this is shard 0
test_net_shard_1 = 'https://api.s1.b.hmny.io'
test_address = 'one18t4yj4fuutj83uwqckkvxp9gfa0568uc48ggj7'

stx_count = account.get_staking_transactions_count(test_address, tx_type='ALL', end-point=test_net)
sent_stx_count = account.get_staking_transactions_count(test_address, tx_type='SENT', end-point=test_net)
received_stx_count = account.get_staking_transactions_count(test_address, tx_type='RECEIVED', 
       endpoint=test_net)							

print(stx_count)
print(sent_stx_count)
print(received_stx_count)


Result:

Coppied to clipboard. 0 0 0

Using the following code, we can get the first 100 transaction hashes of the shard 0:


from pyhmy import account

test_net = 'https://api.s0.b.hmny.io'	# this is shard 0
test_net_shard_1 = 'https://api.s1.b.hmny.io'
test_address = 'one18t4yj4fuutj83uwqckkvxp9gfa0568uc48ggj7'

first_100_tx_hashes = account.get_transaction_history(test_address, page=0, page_size=20, 
       include_full_tx=False, endpoint=test_net)

print(first_100_tx_hashes)


Result:

Coppied to clipboard. ['0xa5bf23cbaf63fa16cebc18d13fef77172c9e49fd2eaf866e7fb2c50529a9af42', '0xedd0dc457976b853de8434c6dbf47be465debe4f5cbd8c9ff1f5e0f920044dfb', '0x16bb8b5df5e19ac35c1f908f48915899c5b02ce97bdc030400b31444e296093b', '0x128dc419a805f8147ad827f4f4da2f5f5eaaf5a42f8515ba3001519f4a4b1758', '0xf36dfee42ccefae3977536b25eaff426fa8e2fc0513ee41e01547320db03e015', '0x9a570499ce34c02bde2b124f1fd75c0aded1e84ec9960477ab0f1854bd8ecefd', '0x4e982c8f493a15f0edfe88221b1ff6d359650cd4273397a5ee057ba1df847551', '0x8a5ed5f6e183da30da74dee5f373b9fdea6e0c3c8b0dbbf4e0c218c637aaf0cf', '0x04b76e4f75bc51607b283fd1e0e75df999e7aac679e7b4a2aadfb96bb7ac39b2', '0x512003037125cbde3ae7af5495eca93c335ce84e181692612853d0f014819e45', '0x113efecb62251356b4c3620c2f79ffdfe5905a8616ea7094edd428dbf4f326ad', '0x09dcf5b49ef74c6f5f159c4168dd9573afcc927e1b45d26c22ee7e4d33e1b152', '0x11bb90e10da359246fe8a026f919b5a6ec6641093c1026967313a843b9bb2b64', '0xd8a792e8e918ff307c2234cef0327fc64977143101f6b20c52b4eb1d7ec80deb', '0xf44a7a632f27709dc0faaa4f9fa59a65d575cd11b904f61515d2e33e1f9f8360', '0xc004838024c304de93e67a930a4667e59a9d011e2049d11bf0bbab3ca1972d9b', '0x95c149c43ed6ca4a206798901289f56832575b953b3fd82c41d65215a6807b46', '0x298b8d589d2e78c55e30f699d2617aa25dab647bbfd442f7b6d313140be90089', '0x3ad6e7bef84a7b8cb6d5322e4f13e9f39e40d1536843388251f32825734ef56d', '0xf49b9467a6cc689a389ccf60b1e69e4688e0d2d84e3238eea9dfbc5aaf1aa2b2']

Wrapping Up

In this tutorial, we have managed to install the dependencies for connecting to the Harmony blockchain. Then, by using docker, we connected to the Harmony blockchain. In addition to that, by using some Python script and harmony library, we got some data from the shard 0 of the Testnet.

Download this Article in PDF format

metaverse

Care to Know About Metaverse?

In Arashtad, we are providing custom services on 3d developments such as 3d websites, 3d models, metaverses and all 3d applications.

Arashtad Services
Drop us a message and tell us about your ideas.
Tell Us What You Need
Blockchain Development

4 Comments

  1. bambu4d login
    Commented on March 19, 2023

    I truly appreciate your technique of writing a blog. I added it to my bookmark site list and will

    Reply
  2. demo slot
    Commented on March 19, 2023

    Great information shared.. really enjoyed reading this post thank you author for sharing this post .. appreciated

    Reply
  3. demo slot
    Commented on March 19, 2023

    Pretty! This has been a really wonderful post. Many thanks for providing these details.

    Reply
  4. demo slot
    Commented on March 19, 2023

    For the reason that the admin of this site is working, no uncertainty very quickly it will be renowned, due to its quality contents.

    Reply

Leave a Reply

Click here to cancel reply.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

David Icke Book

Categories

  • 3D (6)
  • Aave (3)
  • Affiliate Marketing (1)
  • Agile (1)
  • Apache (1)
  • Back-end Developer (1)
  • Blender (10)
  • Blockchain (14)
  • Blog (164)
  • Brownie (10)
  • CMS (2)
  • Content Marketing (1)
  • Data bases (2)
  • Django (1)
  • Ethereum (6)
  • Front-end (1)
  • Frontend (1)
  • Game Development (1)
  • Git (2)
  • Google Ads (1)
  • Harmony (2)
  • Joomla (4)
  • Kubernetes (1)
  • Laravel (1)
  • Linux (1)
  • Linux (5)
  • Metaverse (6)
  • Moralis (1)
  • Network Marketing (1)
  • Node.JS (1)
  • Online Business (1)
  • Online Store (1)
  • SEO (1)
  • Terra (5)
  • ThreeJS (18)
  • UI/UX (1)
  • Web Servers (10)
  • Web Servers (11)
  • WebGL (1)
  • WordPress (5)

Newsletter

Get informed about the latest updates, products, and solutions.
RSS

Subscribe to our free newsletter

To make sure you won't miss any valuable content we share with our community.

Company

  • About
  • How it Works
  • FAQ

Legal

  • Privacy Policy
  • Terms of Use
  • Licensing

Business

  • Affiliate Programs
  • Portfolios
  • Business Inquiries

Explore

  • Sitemap
  • Feed
  • Newsletter

Follow Us

All Arashtad Social Profiles

Arashtad Blog
© Arashtad 2013 - 2023
All Rights Reserved.
TOP