Solidity smart contract deployment using hardhat

Deployment Using Hardhat

There are three steps involved in the contract deployment using Hardhat,

  1. Create a new Hardhat project

  2. Configure Hardhat project with targeted blockchain

  3. Compile and deploy

Requirement specification

  1. Visual Studio - VS code

  2. Hardhat

Software Installation

A) Setup the Hardhat environment in the VS code:

Follow the step-by-step procedure explained in the tutorial provided by Hardhat,

Now, the VS code will be installed with Hardhat framework and ready to begin with smart contract deployment.

Solidity Smart contract Deployment using Hardhat

The steps to deploy smart contract using Hardhat is explained below,

  1. Login to the VS code environment which has the Hardhat framework installed with necessary files and folders to access.

  2. Next, OpenZeppelin is an external library which should be present in VS code for contract deployment and it can be installed using below command:

npm install @openzeppelin/contracts

The TEST_ERC20.sol is the script provided by OpenZeppelin library to provide the desired minting value.

  1. There are two files used for contract deployment

A. hardhat_config.ts - Config file

B. deploy_config.ts - Deploy script

Example script structure is provided below:

The user has to provide the account's private key for accounts value in the hardhat_config file.

// Hardhat_config
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";

const config: HardhatUserConfig = {
  solidity: "0.8.19",
  defaultNetwork: 'peerplays',
  networks: {
    peerplays: {
      url: 'http://96.46.48.200:9933',
      accounts: [`0x` + process.env.PRIVATE_KEY], // Account's private key is added
      chainId: 33,
    },
  }
};

export default config;

Ethers library is provided by Ethereum and it has the necessary functions required for calls. Deploy script is required to deploy smart contract.

// Deploy_config
import { ethers } from "hardhat";

async function main() {

  const myToken = await ethers.deployContract("MyToken");

  await myToken.waitForDeployment();

  console.log(`MyToken deployed to: ${myToken.target}`) // Mention the Contract name used
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });
  1. The command to deploy contract is given below:

npx hardhat run scripts/deploy.ts

The contract address will be generated with deployment successful message.

Solidity smart contract deployment using Hardhat is successful! ✅

Last updated