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 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:

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.

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

In Solidity, both internal and private functions are restricted to the contract they are defined in, but they differ in terms of inheritance and accessibility. Understanding these differences is important for implementing the right access control and ensuring proper encapsulation within your smart contracts.