Skip to main content

Deploy a Smart Contract using Hardhat

Prerequisites​

Before you begin, ensure you:

  1. Set up your wallet and acquired Testnet tokens.
  2. Set up your Hardhat environment.
Concrete values for UniFi Testnet
  • rpc_https_endpoint = https://testnet-unifi-rpc.puffer.fi/
  • block_explorer_url = https://testnet-unifi-explorer.puffer.fi/
  • chain_id = 2092151908

Full list in the RPC Endpoints Reference.

Create a Hardhat project​

To create an empty Hardhat project:

  1. Create your project directory:

    mkdir unifi-smart-contract-tutorial
    cd unifi-smart-contract-tutorial
  2. Initialize your Node.js project:

    npm init
  3. Install Hardhat:

    npm install --save-dev hardhat
  4. 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.

note

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:

  1. 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>
    warning

    Please do not commit your private keys into source control. Double check that .env is contained in your .gitignore

  2. Download and install dotenv

    npm i -D dotenv
  3. 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,
    chainId: 2092151908,
    accounts: [PRIVATE_KEY],
    },
    },
    };
  4. 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 from hardhat.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.

  1. In your project directory, install hardhat-verify

    npm install --save-dev @nomicfoundation/hardhat-verify
  2. Add block explorer url and chain id to the .env file

    BLOCK_EXPLORER_URL=https://testnet-unifi-explorer.puffer.fi
    CHAIN_ID=2092151908
  3. Import hardhat-verify in your hardhat.config.js:

    require("@nomicfoundation/hardhat-verify");
    require("@nomicfoundation/hardhat-toolbox");
    require("dotenv").config();
    const { PRIVATE_KEY, RPC_ENDPOINT, BLOCK_EXPLORER_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: `${BLOCK_EXPLORER_URL}/api`,
    browserURL: BLOCK_EXPLORER_URL,
    },
    },
    ],
    },
    sourcify: {
    enabled: false,
    },
    };
  4. 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.