Using Web3.js to Send Transactions on Harmony

Introduction

This guide walks through the process of using web3.js to manually sign and send a transaction to a Harmony network. For this example, we will use Node.js and straightforward JavaScript code.
For this guide we will use Harmony Testnet Network.

Install Dependencies

Next, we can create a directory to store all our relevant files by running:
1
mkdir transaction && cd transaction/
Copied!
And create a simple package.json file:
1
npm init --yes
Copied!
With the package.json file created, we can then install the web3.js package by executing:
1
npm install web3
2
npm install 'bn.js'
Copied!
We recommended to use bn.js for format values
To verify the installed version of web3.js, you can use the ls command:
1
npm ls web3
Copied!
As of the writing of this guide, the version used was 1.3.0.

Sending Transaction

For our example, we only need a single JavaScript file (arbitrarily named transaction.js, which you can find here) to create and send the transaction, which we will run using the node command in the terminal. The script will transfer 1 ONE from the genesis account to another address. For simplicity, the file is divided into three sections: variable definition, create transaction, and deploy transaction.
We need to set a couple of values in the variable definitions:
  1. 1.
    Create our Web3 constructor (Web3).
  2. 2.
    Define the HMY_PRIVATE_KEY variable as the private key of your ONE wallet, what is used to sign the transactions.
  3. 3.
    Create a local Web3 instance and set the provider to connect to Testnet Harmony network.

Setup Account

1
const Web3 = require('web3');
2
const BN = require('bn.js');
3
4
HMY_PRIVATE_KEY = 'YOUR_PRIVATE_KEY';
5
HMY_TESTNET_RPC_URL = 'https://api.s0.b.hmny.io';
6
7
const web3 = new Web3(HMY_TESTNET_RPC_URL);
8
9
let hmyMasterAccount = web3.eth.accounts.privateKeyToAccount(HMY_PRIVATE_KEY);
10
web3.eth.accounts.wallet.add(hmyMasterAccount);
11
web3.eth.defaultAccount = hmyMasterAccount.address
12
13
const myAddress = web3.eth.defaultAccount;
14
console.log('My address: ', myAddress);
Copied!

Check Account Balance

1
const balance = await web3.eth.getBalance(myAddress);
2
console.log('My balance: ', balance / 1e18);
Copied!

Create new account on which we will send tokens

1
const newAccount = web3.eth.accounts.create();
2
console.log('New account created: ', newAccount.address);
Copied!

Sending a Transaction

1
const gasPrice = new BN(await web3.eth.getGasPrice()).mul(new BN(1));
2
const gasLimit = 6721900;
3
4
const value = 1 * 1e18; // 1 ONE
5
6
const from = web3.eth.defaultAccount;
7
const to = newAccount.address; // account was created on prev step
8
9
const result = await web3.eth
10
.sendTransaction({ from, to, value, gasPrice, gasLimit })
11
.on('error', console.error);
12
13
console.log(`Send tx: ${result.transactionHash} result: `, result.status);
14
15
const newAddrBalance = await web3.eth.getBalance(newAccount.address);
16
console.log('New account balance: ', newAddrBalance / 1e18);
Copied!
Full code samples can be found here.
Last modified 7d ago