Create and Execute Smart Contracts Locally with Hardhat and Web3Client.app

Create and Execute Smart Contracts Locally with Hardhat and Web3Client.app

Smart contracts have gained widespread popularity in recent years due to their ability to automate business processes, eliminate intermediaries, and reduce costs. They are self-executing contracts with the terms of the agreement between buyer and seller directly written into lines of code. This blog will guide you through writing, compiling, deploying, and executing a smart contract using Hardhat and Web3Client.app.

Write a Smart Contract

First, you need to write a smart contract in the Solidity language. Solidity is a contract-oriented programming language used for creating smart contracts on the Ethereum blockchain. Below is a sample smart contract called "Greetings" that can be used to greet users.

// SPDX-License-Identifier: MITpragma 
solidity >=0.7.0 <0.9.0;
contract Greetings {
string public name;
string public gPrefix = "Hello ";

constructor(string memory initName) {
    name = initName;
}

function setName(string memory newName) public {
    name = newName;
}

function getGreeting() public view returns(string memory) {
    return string(abi.encodePacked(gPrefix, name));
}
}

Compile the Smart Contract

After writing the smart contract, you need to compile it. To compile the smart contract, navigate to the root directory of your Hardhat project and run the following command:

npx hardhat compile

Create a Deployment Script

To deploy the smart contract, you need to write a deployment script. Here is an example of a deployment script that deploys the "Greetings" contract on the local Hardhat network.

// We require the Hardhat Runtime Environment explicitly here. This is optional
// but useful for running the script in a standalone fashion through `node <script>`.
//
// You can also run a script with `npx hardhat run <script>`. If you do that, Hardhat
// will compile your contracts, add the Hardhat Runtime Environment's members to the
// global scope, and execute the script.
const hre = require("hardhat");


async function main() {
 const Greet = await hre.ethers.getContractFactory("Greetings");
 const lock = await Greet.deploy("World");


 await lock.deployed();


 console.log(`Greet with 1 ETH in ${lock.address}`);
}


// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
 console.error(error);
 process.exitCode = 1;
});

Start the Local Hardhat Network

To start the local Hardhat network, run the following command in your terminal:

npx hardhat node

Deploy the Smart Contract

With the deployment script ready, you can deploy the smart contract on the local Hardhat network by running the following command in your terminal:

npx hardhat run scripts/deployGreet.js --network localhost

Execute the Smart Contract with Web3Client.app

To execute the smart contract, you can use Web3Client.app, a web-based platform that enables interaction with the Ethereum blockchain. Follow these steps to execute the smart contract using Web3Client.app:

Open Web3Client

Select "Create new ABI."

Enter the ABI name and Contract ID obtained when you deployed the smart contract.

Select "Add Network Manually" in the "Add Network" section and fill in the Chain Name, Chain ID, and Local RPC URL.

Example:

Click "Submit" to create a new ABI.

Click on the created ABI and select "New Method."

Enter the method name and click "Send" to execute.

Example:

Method Name : setName

In this greetings example contract, setName method is used to set current name in contract.

Method Name : getGreeting

In this greetings example contract, getGreeting method is used to getGreeting from contract, it returns string

Conclusion

This blog has provided a step-by-step guide on how to write, compile, deploy, and execute a smart contract using Hardhat and Web3Client.app. By following these steps, you can easily create and interact

Github Link:

https://github.com/Poriyaalar/hardhat-example