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:
mkdir transaction && cd transaction/
And create a simple package.json file:
npm init --yes
With the package.json file created, we can then install the web3.js package by executing:
To verify the installed version of web3.js, you can use the ls command:
npm ls web3
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:
Create our Web3 constructor (Web3).
Define the HMY_PRIVATE_KEY variable as the private key of your ONE wallet, what is used to sign the transactions.
Create a local Web3 instance and set the provider to connect to Testnet Harmony network.