Intermediate
How to deploy smart contract using web3
This tutorial will walk you through the process of deploying a smart contract using web3.js on the Harmony blockchain. It will cover the use of the Solidity compiler, installing necessary dependencies, creating the smart contract code, compiling the contract, and deploying it using web3.js. The guide will also provide sample code for the contract and the deployment script, with instructions on how to run the script and deploy the contract. The tutorial assumes that you have a basic understanding of smart contracts and web3.js. By the end of this tutorial, you will have deployed your first smart contract on Harmony using web3.js.
Installation
npm i dotenv npm i solc npm i @truffle/hdwallet-provider web3
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.
const path = require("path"); const fs = require("fs"); const solc = require("solc"); // Compile contract const contractPath = path.resolve(__dirname, "Counter.sol"); const source = fs.readFileSync(contractPath, "utf8"); const input = { language: "Solidity", sources: { "Counter.sol": { content: source, }, }, settings: { outputSelection: { "*": { "*": ["*"], }, }, }, }; const tempFile = JSON.parse(solc.compile(JSON.stringify(input))); const contractFile = tempFile.contracts["Counter.sol"]["Counter"]; module.exports = contractFile;
Create Deploy.js
require("dotenv").config(); const Web3 = require("web3"); const contractFile = require("./compile"); const HDWalletProvider = require("@truffle/hdwallet-provider"); const bytecode = contractFile.evm.bytecode.object; const abi = contractFile.abi; const provider = new HDWalletProvider( process.env.mneumonic, process.env.rpcEndpoint ); const web3 = new Web3(provider); const deploy = async () => { console.log("Deploying...."); I; const accounts = await web3.eth.getAccounts(); const result = await new web3.eth.Contract(abi) .deploy({ data: "0x" + bytecode }) .send({ gas: "3000000", from: accounts[0] }); console.log("Contract deployed to", result.options.address); process.exit(); }; deploy();
Finally
node deploy.js
Congratulations
You just deployed your first smart contract on Harmony using web3.js