Using Web3

This guide walks you through the process of using the Solidity compiler and web3.js to deploy and interact with a Solidity-based smart contract on Harmony. Given Harmony's Ethereum compatibility features, the web3.js library can be used directly with a Harmony node.

Completed code can be found here.

Installation

npm init
npm i dotenv solc @truffle/hdwallet-provider web3

Create .env in root

MNEMONIC=<YOUR_MNEMONIC>
RPC_ENDPOINT=https://api.harmony.one

Create a file Counter.sol in root

pragma solidity >=0.4.22;

contract Counter {
    uint256 private count = 0;
    uint256 moneyStored = 0;

    function incrementCounter() public {
        count += 1;
    }
    function decrementCounter() public {
        count -= 1;
    }

    function addMoney() payable public {
        moneyStored += msg.value;
    }

    function getCount() public view returns (uint256) {
        return count;
    }

    function getMoneyStored() public view returns (uint256){
        return moneyStored;
    }
}

Create compile.js

The only purpose of the compile.js file, is to use the Solidity compiler to output the bytecode and interface of our contract.

Create deploy.js

Finally

Congratulations

You just deployed your first smart contract on Harmony using web3.js.

Last updated

Was this helpful?