Loading...

How to specify the deployer in a Smart Contract deployment in Hardhat?

question solidity hardhat
Ram Patra Published on August 22, 2024
Hardhat 6+

In Hardhat 6, deployment is done using the Ignition module, which introduces a declarative way to manage deployments. The process is different from the Hardhat 5 approach. With Ignition, you define your deployment logic using modules, which then handle the deployment of contracts. To specify which wallet to use for the deployment of your smart contract, you can follow the below steps.

Ignition Deployment in Hardhat 6

With Hardhat 6 and Ignition, the deployment process uses the deployer specified in the ignition module, or it defaults to the first account provided by the Hardhat network if no deployer is explicitly set.

Example: Deploying Contracts Using an Ignition Module

import { buildModule, Account } from "@nomicfoundation/hardhat-ignition";

export const myModule = buildModule("MyModule", (m) => {
  const deployer: Account = m.getAccount(0); // Use the first account by default

  const myContract = m.contract("MyContract", {
    from: deployer,
    args: [/* constructor arguments here */],
  });

  return { myContract };
});

In this code:

  • m.getAccount(0): Retrieves the first account provided by Hardhat as the deployer. This account will be the owner of the deployed contract if Ownable or similar logic is used.
  • from: deployer: Specifies that the contract should be deployed from the deployer account.

Steps to Deploy Using the Ignition Module

  1. Create an Ignition Module: Define a module that specifies how your contracts should be deployed.

  2. Configure Deployment Accounts:
    • By default, the first account (m.getAccount(0)) is used.
    • You can specify a different account by using m.getAccount(1), m.getAccount(2), etc.
  3. Run the Deployment: You deploy the module using Hardhat’s Ignition system:

    npx hardhat ignition deploy ./ignition/modules/MyContract.ts
    

    Note: You can specify the --network node in the above command if you want your changes to persist.

Setting a Custom Deployer Account

To change the deployer, simply select a different account in your module:

import { buildModule, Account } from "@nomicfoundation/hardhat-ignition";

export const myModule = buildModule("MyModule", (m) => {
  const deployer: Account = m.getAccount(1); // Use the second account as deployer

  const myContract = m.contract("MyContract", {
    from: deployer,
    args: [/* constructor arguments here */],
  });

  return { myContract };
});

Summary

In Hardhat 6 with Ignition, the account used to deploy contracts is determined by the account specified in the ignition module. If no specific account is chosen, the default is the first account (m.getAccount(0)). The owner of the deployed contract is typically this deployer account unless overridden in the contract’s constructor logic.

This method allows more structured and controlled deployments, integrating well with the modern practices encouraged by Hardhat 6.

Ram Patra Published on August 22, 2024
Image placeholder

Keep reading

If this article was helpful, others might be too

question solidity blockchain August 16, 2024 Data Types in Solidity and how to initialise each

In Solidity, variables can be categorized based on their data types, and each type has specific ways to initialize them. Here’s an overview of different variable types in Solidity and how to initialize them:

question solidity blockchain August 16, 2024 What is the difference between external and public functions, and when should you use each?

In Solidity, both external and public functions can be called from outside a contract, but there are key differences in how they are used, accessed, and their gas efficiency. Understanding these differences is crucial for writing optimized and secure smart contracts.

question ethereum solidity August 17, 2024 How can you make a smart contract able to receive Ether in Solidity?

To make a smart contract able to receive Ether in Solidity, you need to implement specific functions and ensure that the contract is properly configured to accept incoming Ether transfers. Here’s how you can do it: