How to deploy a Solidity smart contract with Hardhat

In this guide, we are going to learn how to deploy a Solidity smart contract build on Hardhat. We're also going to learn how to verify that smart contract on Etherscan.

In this guide, we are going to learn how to deploy a Solidity smart contract build on Hardhat. We're also going to learn how to verify that smart contract on Etherscan.

To deploy a smart contract on the Ethereum blockchain or other EVM networks (chains built with Ethereum's technology like the Binance Smart Chain, Fantom or Polygon) you need:

  • to compile the code of the smart contract
  • have an address that will make the transaction to deploy the smart contract. That address needs to have Ethereum to pay for gas fees associated with the transaction
  • a way to connect to the blockchain and send the contract creation transaction

So first of all, we are going to make the deploy script. The basic code to deploy a smart contract would be:

const hardhat = require("hardhat"); 

async function main() {
  const MyNFT = await hardhat.ethers.getContractFactory("MyNFT");
  const myNft = await MyNFT.deploy();

  await myNft.deployed();

  console.log(`MyNFT deployed to ${myNft.address}`);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

In the example above, we have a smart contract called MyNFT and we deploy it to the network that the script is running on using the Ethers JS library.

First we create an instance of our smart contract and then we call the deploy function. We wait for it to be fully deployed and then we print the address of the smart contract in the terminal.

In the Hardhat config, you can add networks to run your scripts on. Here, we're going to add the Goërli Testnet which is an Ethereum testnet and we're going to add the Ethereum network.

To add networks, you need to update the hardhat.config.js file:

require('@nomicfoundation/hardhat-toolbox');

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: '0.8.9',
  networks: {
    goerli: {
      url: 'BLOCKCHAIN_API_URL',
      accounts: ['YOUR_PRIVATE_KEY'], // never share this
    },
    ethereum: {
      url: 'BLOCKCHAIN_API_URL',
      accounts: ['YOUR_PRIVATE_KEY'], // never share this
    },
  },
  etherscan: {
    apiKey: 'ETHERSCAN_API_KEY',
  },
};

In the config, we add a networks property and add network objects there.

The most simple way to define networks is to set a URL. For that, you'll need to create an account on a blockchain API like Infura or another blockchain API.

It allows you to interact with the blockchain through an HTTP API instead of having a node running locally.

So when you have your account on Infura, go to your dashboard and create a new key if you don't have one already. Then click on "Manage key" and you'll get an API key and a URL on Ethereum and other networks.

You can copy and paste the URLs it gives you in the config of the smart contract. Be careful to paste the correct URL on the correct network otherwise it won't work.

Then, you have to put your API key in the config. If you put your private key there, be careful to never show that code to anybody else and to not push that file to github. Anybody who has your private key can send transactions on your behalf and steal your funds or mess with your smart contracts.

If you're using MetaMask, you can easily get your private key by going to your account details (click on the 3 dots on the top right corner) and clicking on "export private key".

Lastly, if you want to verify your smart contract code on Etherscan (which is crucial for your users to trust your code), you have to create an account on Etherscan. You have similar blockchain explorers for networks like the Binance Smart Chain, Fantom and all other EVM networks.

Next, go to your account settings in the API Keys section. There, you can create an API Key and copy and paste it to your config.

Now, when it's done, you can run the command:

npx hardhat run --network <your-network> scripts/deploy.js

and replace <your-network> with the name of the network in the config (for us, either goerli or ethereum.

By default, you have an extra network called localhost. To have it running, you have to run this command: npx hardhat node.

Lastly, if you want to verify your smart contract, you have to run this command:

npx hardhat verify --network <your-network> <contract-address>

Like before, you have to replace <your-network> with the network you deployed your smart contract on and replace <contract-address> with the address of your smart contract that was displayed in the terminal when you deployed it.

To verify that it was correctly verified, search for your smart contract in Etherscan and then go to the "contract" section. If it's verified correctly, there should be a "Code", a "React contract" and a "Write contract" section and you should be able to see the code of your smart contract.

And that's it! 🎉

Thank you for reading this article, if you want to go further and take shortcuts to get better at blockchain development, join our community of Web3 builders.

Fill in the form below and you'll receive a free guide to learn Web3 JS and be able to work on any dApp and you'll get an access to the private Discord of the community.