Lesson 11 of 21
In Progress

Deploying Contracts with Hardhat

Once you have written, tested, and prepared your Solidity contracts for deployment, you’ll need to deploy them to the Ethereum blockchain. In this article, we’ll provide a step-by-step guide for deploying contracts with Hardhat, a popular development tool for Ethereum.

Prerequisites

Before you begin, you’ll need to install and set up Hardhat. To install Hardhat, you’ll need Node.js and npm. To set up Hardhat, you’ll need to create a hardhat.config.js file in the root directory of your project.

Once you have installed and set up Hardhat, you’ll also need to have your contracts compiled and ready for deployment. You can use the hardhat compile command to compile your contracts.

Connecting to a Network

To deploy your contracts, you’ll need to connect to a network. Hardhat supports several networks, including the Ethereum mainnet, testnets, and local networks.

To connect to a network, you’ll need to specify the network’s provider in your hardhat.config.js file. For example, to connect to the Ethereum mainnet, you can use the following provider configuration:

module.exports = {
  networks: {
    mainnet: {
      url: "https://mainnet.infura.io/v3/YOUR_PROJECT_ID",
      accounts: [
        "0xYOUR_ACCOUNT_ADDRESS",
        "0xANOTHER_ACCOUNT_ADDRESS",
      ],
    },
  },
};

To connect to a testnet, you’ll need to use the testnet’s provider URL and specify your testnet accounts. You can use a tool like Metamask to manage your testnet accounts.

To connect to a local network, you’ll need to start a local node and specify the local network’s provider URL. You can use a tool like Ganache to start a local node.

Deploying Contracts

Once you have connected to a network, you can deploy your contracts using the hardhat deploy command.

To deploy a contract, you’ll need to specify the contract’s name and any arguments that it requires. For example, to deploy a contract called MyContract with two arguments arg1 and arg2, you can use the following command:

hardhat deploy --network mainnet MyContract arg1 arg2

If you have multiple contracts to deploy, you can deploy them all at once using the --all flag:

hardhat deploy --network mainnet --all

Deployment Options

Hardhat provides several options for customizing your contract deployments. Some options you may find useful include:

  • --gasPrice: Specifies the gas price to use for the deployment.
  • --deploy-dependencies: Deploys any dependencies of
  • --skip-compile: Skips contract compilation and uses existing contract artifacts.
  • --skip-dependencies: Skips deployment of contract dependencies.
  • --force: Forces the deployment of the contract, even if it has already been deployed. This is useful if you want to update an existing contract.
  • --dry-run: Performs a dry run of the deployment, without actually deploying the contract. This is useful for testing the deployment process without incurring real costs.
  • --network: Specifies the network to deploy to. You can specify a predefined network like mainnet or rinkeby, or you can specify a custom network by providing a provider URL.

Deployment Hooks

Hardhat also provides deployment hooks that allow you to execute custom code before or after a contract deployment. For example, you can use a deployment hook to log the contract’s address or to store the contract’s address in a database.

To use a deployment hook, you’ll need to specify the hook in your hardhat.config.js file. For example, to log the contract’s address after it is deployed, you can use the following hook:

module.exports = {
  hooks: {
    "after:deploy:contract": async (task, contract) => {
      console.log(`Contract deployed at address: ${contract.address}`);
    },
  },
};

Conclusion

In this article, we provided a step-by-step guide for deploying contracts with Hardhat. By following these steps and using the available options and hooks, you can easily deploy your contracts to the Ethereum blockchain.

Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

Write a Solidity contract called MyContract that has a constructor that takes a uint256 a and a uint256 b, and sets them as the contract’s state variables A and B.

pragma solidity ^0.7.0;

contract MyContract {
  uint256 public A;
  uint256 public B;

  constructor(uint256 a, uint256 b) public {
    A = a;
    B = b;
  }
}

Use the hardhat compile command to compile the MyContract contract.

hardhat compile

Use the hardhat deploy command to deploy the MyContract contract to the rinkeby testnet, with the arguments 123 and 456.

hardhat deploy --network rinkeby MyContract 123 456

In your hardhat.config.js file, add a deployment hook that logs the contract’s address after it is deployed.

module.exports = {
  networks: {
    rinkeby: {
      url: "https://rinkeby.infura.io/v3/YOUR_PROJECT_ID",
      accounts: [
        "0xYOUR_ACCOUNT_ADDRESS",
        "0xANOTHER_ACCOUNT_ADDRESS",
      ],
    },
  },
  hooks: {
    "after:deploy:contract": async (task, contract) => {
      console.log(`Contract deployed at address: ${contract.address}`);
    },
  },
};

Use the hardhat deploy command with the --gasPrice option to specify a custom gas price for the deployment.

hardhat deploy --network rinkeby --gasPrice 50000000000 MyContract 123 456