Deploy Smart Contracts Using Hardhat

in blockchain •  3 years ago 

Introduction
Smart contracts are just programs kept on a blockchain. That runs when preset conditions are met. They normally are used to automate the performance of an agreement. Therefore, that all participants may be directly sure of the outcome, deprived of any mediator’s participation or time loss.

Hardhat plugin enhances a mechanism to deploy contracts to any network. It makes available an environment to compile, test and deploy Solidity smart contracts. In this post, we will understand that how to deploy a smart contract with Hardhat.

Description
Hardhat as well improves a mechanism to associate names to addresses. Therefore, test and deployment scripts may be reconfigured by merely shifting the address a name points to, permitting different configurations per network. This also consequences in much clearer tests and deployment scripts.

Main features
Hardhats trust ethers.js, the 2nd generation Ethereum JavaScript API library.
It mixes well with TypeScript — a second-generation JavaScript.
It brings a better debugging ability with console.log functionality in Solidity code.
Deterministic deployment through networks.
Ability to access deployment from mate networks.
Introducing products from external sources like npm packages, with truffle help.
Chain configuration export.
Listing deployed contracts’ addresses and their abis.
Library linking at time of deployment.
Set-Up
Make ready the project and install Hardhat as a dependency.

$ npm init --yes

$ npm install --save-dev hardhat
After installation, we may run npx hardhat. This will make a Hardhat config file (hardhat.config.js) in the project directory.

$ 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.2.1

✔ What do you want to do? · Create an empty hardhat.config.js

Config file created
Solidity source files (.sol) are stored in a contacts directory. This is the same as the src directory we may be familiar with from other languages.
We may now write our first simple smart contract, named Box.
It would allow people to store a value that can be later saved.
We will save this file as contracts/Box.sol.
Each .sol file should have the code for a single contract, and be called after it.
/ contracts/Box.sol

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract Box {

uint256 private _value;

// Emitted when the stored value changes

event ValueChanged(uint256 value);

// Stores a new value in the contract

function store(uint256 value) public {

    _value = value;

    emit ValueChanged(value);

}

// Reads the last stored value

function retrieve() public view returns (uint256) {

    return _value;

}

}
We now will generate a script to deploy our Box contract. We would save this file as scripts or deploy.js.

// scripts/deploy.js

async function main () {

// We receive the contract to deploy

const Box = await ethers.getContractFactory('Box');

console.log('Deploying Box...');

const box = await Box.deploy();

await box.deployed();

console.log('Box deployed to:', box.address);

}

main()

.then(() => process.exit(0))

.catch(error => {

console.error(error);

process.exit(1);

});
Now, install ethers in the script by using the below command.

$ npm install --save-dev @nomiclabs/hardhat-ethers ethers
Then, we need to add in the configuration.

/ hardhat.config.js

require('@nomiclabs/hardhat-ethers');

...

module.exports = {

...

};
We can deploy the Box contract to the local network (Hardhat Network) by using the run command:

$ npx hardhat run --network localhost scripts/deploy.js

Deploying Box...

Box deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
Contract
We will import a Solidity contract from Open Zeppelin.
Create a file named ERC20.sol under a new folder contract.
The ERC20 contract would take name and symbol arguments at deployment.
/SPDX-License-Identifier: Unlicense

pragma solidity 0.8.3;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract _ERC20 is ERC20 {

constructor (string memory _name, string memory _symbol) ERC20(_name, _symbol) {}

}
Script Deployment
We will control the hardhat-deploy plugin to deploy the contracts.
It permits us to perform and track deployments, besides a range of features.
The first deployment script, located in the deploy folder, would be the following:
The deployer constant remains the namedAccounts feature in action. It enables us to associate custom names to addresses based on their index in the accounts array of the given network in hardhat.config.js

In our example, therefore, setting deployer to zero permits us to access the private key using const { deployer } = await getNamedAccounts().

Otherwise, the args must be in the order expected by the smart contract, and tags can be used to identify or group contracts for deployment.

Deployment
We can compile our contract with the following command:

$ npx hardhat deploy --network kovan --tags ERC20
For more details visit:https://www.technologiesinindustry4.com/2021/11/deploy-smart-contracts-using-hardhat.html
OIP.jpg

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!