🔁Deploying a Smart Contract

Deploying contracts on Layer4 Network.

Using Hardhat

Here is a comprehensive tutorial outlining the process of deploying a contract to the Layer4 testnet via Hardhat. Additionally, you have the flexibility to opt for an alternative Ethereum contract development environment of your preference!

Make sure you have Node.js installed on your system before moving forward.

Deploying the contract

First, let's install Hardhat in your local project environment. Create a test project directory and navigate into it in your command line. Next, run the following command to install Hardhat.

npm install -g hardhat

Now, run Hardhat by using the npx [email protected] command, and create a new JavaScript project to get started. This will initialize your project directory with all the necessary files and a sample contract to work with. You can go ahead and select all the default options during this process if you're doing this for the first time.

We recommend initializing your project with npx [email protected] to ensure you can deploy your contracts without running into any issues since newer versions of Hardhat might require you to explicitly set the gasLimit when deploying a contract.

Now, navigate to the /contracts folder, create a new Solidity file and name it Tester.sol. This is the sample contract we'll be testing out by deploying it to the Mantle Testnet. Update the file to include the following code.

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

import "hardhat/console.sol";

contract Tester {
    string private testing;

    constructor(string memory _testing) {
        console.log("Deploying a Tester with testing:", _testing);
        testing = _testing;
    }

    function test() public view returns (string memory) {
        return testing;
    }

    function setTesting(string memory _testing) public {
        console.log("Changing testing from '%s' to '%s'", testing, _testing);
        testing = _testing;
    }
}

Upon examination, it's evident that this contract carries out a variety of functions. The setTesting() function is responsible for altering the value of the testing variable, whereas the test() function retrieves the string value stored within this variable. At the commencement of the contract, the constructor function logs the testing string that was utilized during the contract's deployment.

Following this, we require a script that can be executed to deploy our Tester.sol contract. To accomplish this, we can make use of the provided sample script found in the /scripts folder. Renaming this script to deploy.js (to reflect our intention to execute it) and making the necessary adjustments in its contents as indicated below will serve our purpose.

const hre = require("hardhat");

async function main() {
    const Tester = await hre.ethers.getContractFactory("Tester");
    const tester = await Tester.deploy("Hello, Layer4!");

    await tester.deployed();
    console.log("Tester deployed to:", tester.address); // Records the deployed contract's address
}

main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error);
        process.exit(1);
    });

The subsequent task involves adjusting the hardhat.config.js file to incorporate the network setup for Layer4 Testnet within this Hardhat project. Additionally, we will generate a .env file where the private key of the wallet designated for contract deployment will be defined. Begin by navigating to the hardhat.config.js file in your project directory, and proceed to modify the configuration to align with the provided specifications.

require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config()

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: "0.8.17",
  networks: {
    "layer4-testnet": {
      url: "https://rpc.testnet.layer4.network/",
      accounts: [process.env.PRIV_KEY] // Utilizes the private key from the .env file
    }
  }
};

Now create a .env file in the main project directory, and specify your wallet private key like so.

PRIV_KEY=0x[your-wallet-private-key]

Before proceeding, ensure that you have a sufficient amount of test $LAYER4 tokens in the wallet linked to the private key specified in the .env file. These tokens will cover the (extremely low) gas fees associated with deploying your contract. Acquiring test $LAYER4 tokens can be done through various methods. Further information can be found in the faucet section.

With the contract code in place, a script written to deploy your contract, and the network configuration ready, you can deploy the Tester.sol contract by running the following command in the command line.

npx hardhat run scripts/deploy.js --network layer4-testnet

Once your configuration is set up accurately, you'll receive a success response as defined in the deploy.js script, similar to this:

Tester deployed to: [contract address]

Congratulations! Your contract is successfully deployed and prepared for invocation on the Layer4 Network. You can easily locate it by using the contract address on the Explorer.

Let's progress and initiate a Hardhat console session to commence making function calls to the contract.

Testing Contract

Initiate a Hardhat console session using the command provided below:

npx hardhat console --network layer4-testnet
   
//Initiate the Tester.sol contract by executing the following line within the console:
const Tester = await ethers.getContractFactory("Tester");

// Establish a connection to the deployed contract
const tester = await Tester.attach("");

// Invoke the test() function
await tester.test();

// Modify the stored value using setTesting() function
await tester.setTesting("Hello, Blockchain!");

By following these steps, you can effectively interact with your deployed contract on the Layer4 Network using the Hardhat console.

You can choose to write a script, import the Hardhat library by including require("hardhat"), and make the same function calls.

Last updated