Development
EVM Smart Contracts
Hardhat: Deploy Smart Contract

Hardhat: Deploy EVM Smart Contract

Hardhat (opens new window)is a flexible development environment for building Ethereum-based smart contracts. It is designed with integrations and extensibility in mind.

Setting up the development environment

There are a few technical requirements before we start. Please install the following:

npm install --save-dev hardhat

Create Hardhat Project

Let’s create the sample project and go through these steps to try out the sample task and compile, test and deploy the sample contract.

  • Following the prompts should create a new project structure in your directory
    cd project_folder
    npx hardhat
     
    888    888                      888 888               888
    888    888                      888 888               888
    888    888                      888 888               888
    8888888888  8888b.  888d888 .d88888 88888b.   8888b.  888888
    888    888     "88b 888P"  d88" 888 888 "88b     "88b 888
    888    888 .d888888 888    888  888 888  888 .d888888 888
    888    888 888  888 888    Y88b 888 888  888 888  888 Y88b.
    888    888 "Y888888 888     "Y88888 888  888 "Y888888  "Y888
     
    Welcome to Hardhat v2.9.9
     
    ? What do you want to do? ...
    > Create a basic sample project
    Create an advanced sample project
    Create an advanced sample project that uses TypeScript
    Create an empty hardhat.config.js
    Quit
  • Configure network options in hardhat.config.js
```javascript
module.exports = {
    defaultNetwork: "aioz_testnet",
    networks: {
        hardhat: {
        },
        aioz_testnet: {
            url: "https://eth-ds.testnet.aioz.network",
            accounts: [privateKeyDeployer] // Add your private key of deployer in here
        }
    },
    solidity: {
        version: "0.8.4",
    }
}
```

You need to configure Network RPC and owner account private key to deploy smart contract

Write a Smart Contract

Create a new smart contract HelloAIOZ.sol in folder /contracts with below source code:

// SPDX-License-Identifier: Unlicense
// Specifies that the source code is for a version of Solidity greater than 0.6.8
pragma solidity ^0.8.0;
 
contract HelloAIOZ {
 
    // The public variables, which can be accessible from outside a contract
    string public status;
 
    constructor(string memory initStatus) {
        // setting `status` to initial value
        status = initStatus;
    }
 
    // A publicly accessible function to updates `status`
    function update(string memory newStatus) public {
        status = newStatus;
    }
}

This is the most basic smart contract on Solidity, the smart contract stores the status message and let everyone on the blockchain to read/update status.

Compile Smart Contract

This command will compile all smart contracts in /contracts folder.

npx hardhat compile

Deploying on AIOZ Network Testnet

Create deploy script file deploy_helloaioz.js in scripts folder, source code as below:

const hre = require("hardhat");
 
async function main() {
  // We get the contract to deploy
  const Contract = await ethers.getContractFactory("HelloAIOZ");
  const dContract = await Contract.deploy("Hello, Hardhat!");
 
  await dContract.deployed();
 
  console.log("HelloAIOZ deployed to:", dContract.address);
}
 
main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });
 

Run this command in root of the project directory:

npx hardhat run scripts/deploy_helloaioz.js --network aioz_testnet

Contract will be deployed successfully on AIOZ Network Testnet, it will display output as follow:

HelloAIOZ deployed to: 0xBd3065C31bfDC8AA2983E41ef02AdDF12DfE6710

You can check the deployment status here: https://testnet.explorer.aioz.network/ (opens in a new tab)