Deploying a Smart Contract

This tutorial explains how to create, compile, deploy, and verify a simple smart contract on the Nero modular chain Testnet using Hardhat.

Hardhat

Hardhat is a toolset that allows you to compile, deploy, test, and verify your smart contracts.

Setting Up Your Workspace

Node.js v18+ LTS (which comes with Node)

Starting a New Project

  • Initiate a new project by running npm init --yes (Recommend npm 7 or higher).

  • Install Hardhat in your project by running npm install --save-dev hardhat or yarn add --dev hardhat.

  • To create a Hardhat project by running npx hardhat in your project directory.

  • Install the Hardhat toolbox by running npm install @nomicfoundation/hardhat-toolbox.

  • Then, you will be presented with several options for project creation. Select Create an empty hardhat.config.js.

Create Smart Contract

You can create your own smart contract or use the Openzeppelin token smart contract template, and place it in the contracts directory of your project.

Modify Hardhat for Nero

The updates to the hardhat.config.js and scripts/deploy.js files should resemble the following example (specific content should be adjusted according to requirements).

You'll need a .env with the following contents:

L2_PROVIDER_URL=https://testnet.nerochain.io
L2_PRIVATE_KEY=add your own private key

You'll need a hardhat.config.js with the following contents:

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: "0.8.18",
  defaultNetwork:"l2_testnet"
  networks: {
    l2_testnet: {
      url: L2_PROVIDER_URL,
      accounts: [L2_PRIVATE_KEY],
    },
  },
etherscan: {
  apiKey: {
    nero: ETHERSCAN_API_KEY
  },
  customChains: [
    {
      network: "nero",
      chainId: 6660001, // L2: 6660001
      urls: {
        apiURL: "https://testnetscan.nerochain.io/api",
        browserURL: "https://testnetscan.nerochain.io"|
      }
    }
  ]
},

Note:

  • You can obtain test coins from this faucet and refer to this page.

  • For further information on how to add Nero Chain (Layer-2) on MetaMask, please refer to this link.

Deploy Smart Contract on Nero Network

  1. Compile the contract by running npx hardhat compilein the terminal.

  2. Run npx hardhat run scripts/deploy.js --network l2_testnet in the root of the project directory.

  3. Visit the Testnet Explorer to view the deployed contract.

Congratulations! You have successfully deployed your own Smart Contract.

Verify with Hardhat

The Hardhat Etherscan plugin supports contract verification(requires v3.0.0+).

Install the plugin

npm

npm install --save-dev @nomiclabs/hardhat-etherscan

yarn

yarn add --dev @nomiclabs/hardhat-etherscan

Configure the file

  1. Addrequire("@nomiclabs/hardhat-etherscan") in hardhat.config.js.

2. Then, apply for an API key from Etherscan and add it to your .envfile. Add the relevant settings hardhat.config.js as shown in the content.

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: "0.8.18",
  defaultNetwork:"l2_testnet"
  networks: {
    l2_testnet: {
      url: L2_PROVIDER_URL,
      accounts: [L2_PRIVATE_KEY],
    },
  },
etherscan: {
  apiKey: {
    nero: ETHERSCAN_API_KEY
  },
  customChains: [
    {
      network: "nero",
      chainId: 6660001, // L2: 6660001
      urls: {
        apiURL: "https://testnetscan.nerochain.io/api",
        browserURL: "https://testnetscan.nerochain.io"|
      }
    }
  ]
},

3. Go back to Testnet Explorer and confirm the successful verification.

Conclusion

This tutorial has walked you through creating and deploying a basic smart contract using Hardhat. Although the tutorial used the testnet, you can follow the same process for the mainnet.​​

Last updated