5. Deploy a smart contract using Hardhat
This tutorial discusses the compilation, testing, and deployment of a smart contract through Hardhat. For more information, check theofficial Hardhat documentation, https://hardhat.org/getting-started

Deploy a smart contract using Hardhat

Hardhat is a development environment for compiling, deploying, testing, and debugging Ethereum based smart contracts. It helps developers manage and automate the repetitive tasks inherent in building smart contracts and dApps, and easily introduce more functionality around this workflow. This means compiling, running, and testing smart contracts on the core. Much of Hardhat's functionality comes from plugins, and as a developer, you are free to choose which plugins you want to use. From version 2.1 of ChainIDE, the Terminal function is supported, and the background can be used to test, compile and deploy the Hardhat framework. The purpose of this tutorial is to teach how to use Hardhat in chainIDE, and please refer to its related documentation for specific hardhat usage. The steps are following:

1. Open Command line

Click "Output" in the lower right corner, then click "Command Line" in the middle, and then click "npm-hardhat" to switch to the command-line interface.

2. Create a Project

To create a new project, use npx hardhat, and a new Hardhat project will be added to your 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.0.8
? What do you want to do? …
❯ Create a sample project
Create an empty hardhat.config.js
Quit
Enter~ Let's create a sample project and go through these steps to try out sample tasks and compile, test, and deploy sample contracts. The example project will require you to install hardhat-waffle and hardhat-ethers, which makes Hardhat compatible with tests built with Waffle.
Hardhat will let you know how, but, if you missed them, you can install them with npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers
To first get a quick overview of what's available and what's going on, run npx hardhat in your t:
$ npx hardhat
Hardhat version 2.0.8
Usage: hardhat [GLOBAL OPTIONS] <TASK> [TASK OPTIONS]
GLOBAL OPTIONS:
--config A Hardhat config file.
--emoji Use emoji in messages.
--help Shows this message, or a task's help if its name is provided
--max-memory The maximum amount of memory that Hardhat can use.
--network The network to connect to.
--show-stack-traces Show stack traces.
--tsconfig A TypeScript config file.
--verbose Enables Hardhat verbose logging
--version Shows hardhat's version.
AVAILABLE TASKS:
accounts Prints the list of accounts
check Check whatever you need
clean Clears the cache and deletes all artifacts
compile Compiles the entire project, building all artifacts
console Opens a hardhat console
flatten Flattens and prints contracts and their dependencies
help Prints this message
node Starts a JSON-RPC server on top of Hardhat Network
run Runs a user-defined script after compiling the project
test Runs mocha tests
To get help for a specific task run: npx hardhat help [task]
Here is a list of built-in tasks and a sample accounts task. Going a step further, as you start adding more functionality with plugins, the tasks defined by these will also show up here. This is your starting point for finding out which tasks can run. If you look at the hardhat.config.js file, you will find accounts defined:
require("@nomiclabs/hardhat-waffle");
// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
const accounts = await hre.ethers.getSigners();
for (const account of accounts) {
console.log(account.address);
}
});
// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more
/**
* @type import('hardhat/config').HardhatUserConfig
*/
module.exports = {
solidity: "0.8.4",
};
To run it, try npx hardhat accounts
$ npx hardhat accounts
0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
0x70997970C51812dc3A010C7d01b50e0d17dc79C8
0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC
0x90F79bf6EB2c4f870365E785982E1f101E93b906
0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65
0x9965507D1a55bcC2695C58ba16FB37d819B0A4dc
0x976EA74026E726554dB657fA54763abd0C3a0aa9
0x14dC79964da2C08b23698B3D3cc7Ca32193d9955
0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f
0xa0Ee7A142d267C1f36714E4a8F75612F20a79720
0xBcd4042DE499D14e55001CcbB24a551F3b954096
0x71bE63f3384f5fb98995898A86B02Fb2426c5788
0xFABB0ac9d68B0B445fB7357272Ff202C5651694a
0x1CBd3b2770909D4e10f157cABC84C7264073C9Ec
0xdF3e18d64BC6A983f673Ab319CCaE4f1a57C7097
0xcd3B766CCDd6AE721141F452C550Ca635964ce71
0x2546BcD3c84621e976D8185a91A922aE77ECEc30
0xbDA5747bFD65F08deb54cb465eB87D40e51B197E
0xdD2FD4581271e230360230F9337D5c0430Bf44C0
0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199

3. Compile your smart contract

Next, if you look at contracts/ you should be able to find 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 it, just run:
npx hardhat compile

4. Test your smart contract

The sample project uses Waffle Test and Ethers.js. You can use other libraries if needed. If you look at test/, you should be able to find sample-test.js:
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("Greeter", function () {
it("Should return the new greeting once it's changed", async function () {
const Greeter = await ethers.getContractFactory("Greeter");
const greeter = await Greeter.deploy("Hello, world!");
await greeter.deployed();
expect(await greeter.greet()).to.equal("Hello, world!");
const setGreetingTx = await greeter.setGreeting("Hola, mundo!");
// wait until the transaction is mined
await setGreetingTx.wait();
expect(await greeter.greet()).to.equal("Hola, mundo!");
});
});
you can run your test npx hardhat test
$ npx hardhat test
Compiling 1 file with 0.7.3
Compilation finished successfully
Greeter
Deploying a Greeter with greeting: Hello, world!
Changing greeting from 'Hello, world!' to 'Hola, mundo!'
✓ Should return the new greeting once it's changed (803ms)
1 passing (805ms)

5. Deploy your smart contract

Next, in order to deploy the contract, we will use the Hardhat script. Inside scripts/ you will find the following code in sample-script.js:
// We require the Hardhat Runtime Environment explicitly here. This is optional
// but useful for running the script in a standalone fashion through `node <script>`.
//
// When running the script with `npx hardhat run <script>` you'll find the Hardhat
// Runtime Environment's members available in the global scope.
const hre = require("hardhat");
async function main() {
// Hardhat always runs the compile task when running scripts with its command
// line interface.
//
// If this script is run directly using `node` you may want to call compile
// manually to make sure everything is compiled
// await hre.run('compile');
// We get the contract to deploy
const Greeter = await hre.ethers.getContractFactory("Greeter");
const greeter = await Greeter.deploy("Hello, Hardhat!");
await greeter.deployed();
console.log("Greeter deployed to:", greeter.address);
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Run it in npx hardhat run scripts/sample-script.js:
$ npx hardhat run scripts/sample-script.js
Deploying a Greeter with greeting: Hello, Hardhat!
Greeter deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3

6. Connect a wallet or Dapp to the Hardhat network

By default, Hardhat will always start an in-memory instance of the Hardhat Network at boot. It is also possible to run Hardhat Network in standalone mode so that external clients can connect to it, maybe MetaMask, your Dapp frontend, or some script. To run Hardhat Network this way, run npx hardhat node:
$ npx hardhat node
Started HTTP and WebSocket JSON-RPC server at http://127.0.0.1:8545/
This will expose a JSON-RPC interface to the hardhat network. Due to the particularity of the chainIDE port, port forwarding is required first.

7. Using a local node on ChainIDE requires port mapping first

  1. 1.
    First run npx hardhat node to start the hardhat network, this will provide 20 accounts, we choose one of the accounts and save its private key
$ npx hardhat node
Started HTTP and WebSocket JSON-RPC server at http://0.0.0.0:8545/
Accounts
========
WARNING: These accounts, and their private keys, are publicly known.
Any funds sent to them on Mainnet or any other live network WILL BE LOST.
Account #0: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266 (10000 ETH)
Private Key: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
Account #1: 0x70997970c51812dc3a010c7d01b50e0d17dc79c8 (10000 ETH)
Private Key: 0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d
Account #2: 0x3c44cdddb6a900fa2b585dd299e03d12fa4293bc (10000 ETH)
Private Key: 0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a
Account #3: 0x90f79bf6eb2c4f870365e785982e1f101e93b906 (10000 ETH)
Private Key: 0x7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6
Account #4: 0x15d34aaf54267db7d7c367839aaf71a00a2c6a65 (10000 ETH)
Private Key: 0x47e179ec197488593b187f80a00eb0da91f1b9d0b13f8733639f19c30a34926a
Account #5: 0x9965507d1a55bcc2695c58ba16fb37d819b0a4dc (10000 ETH)
Private Key: 0x8b3a350cf5c34c9194ca85829a2df0ec3153be0318b5e2d3348e872092edffba
...
2. Next, perform port mapping, click the port, select npm-hardhat for image, port 8545, and click Add:
And copy the address of the port mapping
3. Open hardhat.config.js in the editor, delete the original content and use the following content, fill in the mapped address and the private key just saved:
require("@nomiclabs/hardhat-waffle");
module.exports = {
solidity: "0.8.0",
networks: {
localhost: {
url: "***",// 8545
accounts: ["**"],
},
},
};
4. Create a new npm-hardhat terminal and enter the following command to deploy the contract to the local node:
npx hardhat run scripts/sample-script.js --network localhost
5. if you need to deploy to the main network and test network, please configure the relevant network fields and deploy.