Guide to Deploy Smart Contract to Harmony Blockchain

Tram Ho

Harmony is introduced as a fast and secure blockchain platform for distributed applications. Their mainnet has 1000 nodes divided into 4 shards and with the ability to close blocks in 8 seconds. This blockchain platform is strangely similar to Ethereum, is it using a different consensus mechanism called Effective Proof-of-Stake (EPoS), according to the developer. improvements to the PoS. In many similarities with Ethereum, one of them is that there are smart contracts, also using solidity to write contracts and deploying can also use Ethereum’s development tools. To demonstrate this, I would like to write this article to guide you to deploy a smart contract to Harmony network

Installation required

We can also use Truffle to code and deploy smart contract so where we need to install Truffle

Project initialization

Installing Harmony Core

To deploy we need install Harmony @ harmony-js / core

Install additional libraries

If the contract is developed on ERC-20 or ERC-721 standard, you need to install additional libraries of open zeppelin libraries.

Create a package.json file

Need to create a package.json file as follows to load libraries:

Then run command: npm install

Configure truffle to use Harmony

To build a contract on Harmony we need to change the file truffle-config.js , to set up the network to Harmony network.

In this there will be some space to use environment variables, so we also need to set up the .env file as follows:

Write smart contract

We will write 2 files deployed on Harmony HRC20 as below:

  • HarmonyMintable.sol
  • HarmonyERC20.sol

Add the migration file

Finally, the migration file for deploying contract:

  • 2_deploy_HarmonyERC20.js

The directory structure will look like this:

Compile and deploy contracts

Use the following commands to compile and deploy:

The results will be as follows

Check out totalSupply

We will try on two environments: test environment and client call environment

  • Test environmentWe need to create a Harmony.js test file in the test directory

    Then run the command:

    Result

    Please pay attention to the file 2_deploy_HarmonyERC20.js , we are configuring like Ethereum’s wei is so decimal = 18 and amount=1000000 so the total return will be: 1000000000000000000000000 (10 ^ (6 + 18))

  • Client environmentJust like in the client environment, we will simulate a js file calling the totalSupply function through the contract instance . We will create a file called Harmony_sdk.js in the test directory:

    Then run the command: node test/Harmony_sdk.js

    Result

    Pay attention to help me here I am using Testnet with Id is 2 . So if in case of using for Mainnet or other Testnet, everyone can change here to get the correct address.

Block exporers

Like Ethereum, Harmony also has a page to block exporers : Mainnet and Testnet

  • As here we deploy it to the testnet environment so we will go to Testnet to test

  • Contract is already on the network

  • But no transactions yet

Conclude

Thus, we have successfully deployed a smart contract on the Harmony blockchain network. Surely everyone now is a bit skeptical, I don’t know if I can deploy to the right network, but it’s like on Ethereum. If you don’t deploy it to the right network, it just looks like Ethereum. Thank you very much for reading and see you in the next article.

 

Source: https://docs.harmony.one/home/developers/smart-contracts
Share the news now

Source : Viblo