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.

Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

ToDoBar

Your to-dos on your menu bar.

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 Storage, memory, and calldata in Solidity, and when to use each?

In Solidity, understanding the differences between storage, memory, and calldata is crucial for efficient smart contract development. Each data location serves a different purpose and has its own characteristics, affecting gas costs and data persistence. Here’s a breakdown of each data location with practical examples highlighting when to use each.

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 Inheritance and Overriding in Solidity

In Solidity, overriding allows a derived (child) contract to modify or extend the behavior of functions defined in a base (parent) contract. This is a key feature in object-oriented programming and enables the implementation of polymorphism, where a child contract can provide a specific implementation of a function defined in the parent contract.