Deploy a Smart Contract using Hardhat
Prerequisites
Before you begin, ensure you:
Create a Hardhat project
To create an empty Hardhat project:
-
Create your project directory:
mkdir unifi-smart-contract-tutorial
cd unifi-smart-contract-tutorial -
Initialize your Node.js project:
npm init
-
Install Hardhat:
npm install --save-dev hardhat
-
Initialize the Hardhat project:
npx hardhat init
In the menu that appears, select Create a JavaScript project and press Enter. Accept all the default values in the questionnaire.
You should now have a sample HardHat project with a Lock
contract, that locks funds for a set time, and a few smart contract tests.
The default script in ignition/modules/Lock.js
locks 1 gwei in the contract upon deployment, you can modify this
value in the script.
Deploy the contract
The sample project already includes the deployment script. To deploy on UniFi, you'll just need to make
a few modifications to the hardhat.config.js
file:
-
Create a
.env
file in the root folder with your wallet's private key and RPC Endpoint.PRIVATE_KEY=<your_private_key>
RPC_ENDPOINT=<rpc_https_endpoint>warningPlease do not commit your private keys into source control. Double check that
.env
is contained in your.gitignore
-
Download and install
dotenv
npm i -D dotenv
-
Add UniFi to your
hardhat.config.js
file. The following example shows how to add a RPC endpoint to the configuration file.require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
const { PRIVATE_KEY, RPC_ENDPOINT } = process.env;
module.exports = {
solidity: "0.8.27",
networks: {
unifi_testnet: {
url: RPC_ENDPOINT,
accounts: [PRIVATE_KEY],
},
},
}; -
Deploy your contract.
npx hardhat ignition deploy ./ignition/modules/Lock.js --network <network_name>
In the command:
<network_name>
is the name of the network fromhardhat.config.js
you want to deploy on f.e.unifi_testnet
.
Your output should look something like this:
Deployed Addresses
LockModule#Lock - 0xA72022A83654E794B8e9FD7217ADF7378f3e985d
Verify your smart contract
Optionally, you can verify your contract on the network. This makes the source code publicly available.
Verify an already existing contract
To verify a contract, you need to make a few modifications the project.
-
In your project directory, install
hardhat-verify
npm install --save-dev @nomicfoundation/hardhat-verify
-
Add blockscout url and chain id to the .env file
BLOCKSCOUT_URL=<blockscout_homepage_explorer_url>
CHAIN_ID=<chain_id> -
Import
hardhat-verify
in yourhardhat.config.js
:require("@nomicfoundation/hardhat-verify");
require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
const { PRIVATE_KEY, RPC_ENDPOINT, BLOCKSCOUT_URL, CHAIN_ID } = process.env;
module.exports = {
solidity: "0.8.27",
networks: {
unifi_testnet: {
url: RPC_ENDPOINT,
accounts: [PRIVATE_KEY],
},
},
etherscan: {
apiKey: {
unifi_testnet: "empty",
},
customChains: [
{
network: "unifi_testnet",
chainId: CHAIN_ID,
urls: {
apiURL: `${BLOCKSCOUT_URL}/api`,
browserURL: BLOCKSCOUT_URL,
},
},
],
},
sourcify: {
enabled: false,
},
}; -
Execute the
hardhat verify
command:npx hardhat verify --network <network_name> <contract_address> "constructor_argument_1" "constructor_argument_2" ...
In our example, the full command should look something like this:
npx hardhat verify --network unifi_testnet 0x545D79cAace91bB8A710Ad2ee4e50B01d87bB6Ff 123456
In the command:
<network_name>
is the name of the network you want to verify on.<contract_address>
is the address where your contract was deployed.constructor_argument_1
,constructor_argument_2
, etc., are the arguments passed to your contract's constructor (if any).
For more contract verification options, refer to the Hardhat verification documentation.