Smart Contracts - HardHat

Hardhat is a development environment for Ethereum that allows you to edit, compile, debug and deploy your smart contracts and dApps. The main use of this tool is automatic deployment and unit test execution, similar to Truffle. For this toolbox, we have created a custom Javascript Provider and Typescript Provider, in order to work with the LACChain Networks orchestrated by LACNet.

Pre-Requisites

Before deploying your smart contracts in the LACChain Networks, see this overview that explains the minor changes you will need to incorporate to them.

Install

First, let’s install hardhat.

				
					npm install --save-dev hardhat
npx hardhat
				
			

Now your can create your project folder, which you can name MyDapp.

 
				
					mkdir MyDapp
cd MyDapp
				
			

Contract Compilation

Before anything else, let’s create a very simple smart contract named Greeter.sol and store it in the contracts folder. All smart contracts you create should be stored there. 

 

Greeter.sol:

				
					//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import "hardhat/console.sol";

contract Greeter {
    string private greeting;

    constructor(string memory _greeting) {
        console.log("Deploying a Greeter with greeting:", _greeting);
        greeting = _greeting;
    }

    function greet() public view returns (string memory) {
        return greeting;
    }

    function setGreeting(string memory _greeting) public {
        console.log("Changing greeting from '%s' to '%s'", greeting, _greeting);
        greeting = _greeting;
    }
}
				
			

To compile the smart contract, execute the command:

				
					npx hardhat compile
				
			

Hardhat Configuration

First, we need to extends the Hardhat Runtime Environment (HRE), by importing the corresponding extender in the hardhat.config.js:

				
					import "./extender";

				
			

This will include a lacchain property to the HRE.

To show the accounts defined in the config, use:

				
					task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
    const accounts = await hre.lacchain.getSigners();
    
    for (const account of accounts) {
      console.log(account.address);
    }
});
				
			

Now, you need to define the nodeAddress and expiration parameters that will be sent in every transaction to the node in the hardhat.config.js file. For example:

				
					{"lacchain":{"url":"http:\/\/34.69.22.82","nodeAddress":"0xd00e6624a73f88b39f82ab34e8bf2b4d226fd768","expiration":1736394529,"gasPrice":0,"privateKeys":["0x8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63","0xc87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3","0xae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f"]}}
				
			

Deploy contract & interact

1. In the test file include the lacchain deployContract function as follows:

				
					import { lacchain } from "hardhat";
				
			

2. In your test case use the function as follows:

				
					const accounts = lacchain.getSigners();
const Greeter = await ethers.getContractFactory("Greeter", accounts[0]);
const greeter = await lacchain.deployContract(Greeter, "Hello, world!");
				
			

3. Now you will be able to call functions to the greeter smart contract as usual:

				
					expect(await greeter.greet()).to.equal("Hello, world!");
				
			

Copyright 2024 © All rights Reserved. Designed by LACNet