Deploy Using Hardhat
Hardhat is a popular smart contract development frameworks. In this tutorial, we will be using Hardhat to deploy a simple Counter smart contract to the Custom Rollup Testnet. We will explore the basics of creating a Hardhat project with a sample contract and a script to deploy it.
For the full instruction on how to use Hardhat, please refer to the official Hardhat documentation.
Create New Project
Start with creating an npm project by going to an empty folder, running npm init, and following its instructions. You can use another package manager, like yarn, but Hardhat recommends you use npm 7 or later, as it makes installing Hardhat plugins simpler.
Hardhat Smart Contract
To create the sample project, run npx hardhat init in your project folder:
Press
<ENTER>choose javascript, typescript or empty projectPress
<ENTER>to set the project rootPress
<ENTER>again to accept addition of.gitignorePress
<ENTER>to installhardhat @nomicfoundation/hardhat-toolbox
Create deployer account
Create the
.envfile in your project root folder and add the following line:
ACCOUNT_PRIVATE_KEY='my private key'Populate the
.envfile with your private key. You can get your private key from Metamask. See the section below on how to get your private key from Metamask.
Do not commit your private key to a public repository!
Verify that your .gitignore file contains .env to prevent your private key from being committed to a public repository.
Configure Hardhat
Open the
hardhat.config.jsfile and paste the code below:
Write Smart Contract
Create a new file, in the contracts folder, named
Counter.sol:
Copy the below code and paste it in the
Counter.solcontract code:
Create Deploy Script
Delete the content of the
scripts/deploy.jsfile and add the code below:
Compile Contract
Install dotenv package:
npm install dotenvCompile your contract code (i.e., go back to the project root in the CLI),
Deploy Contract
Run the deploy script:
Here's an output example: