Hardhat Tests – How to Deploy Contracts with Arguments Using TypeScript

ethers.jshardhathardhat-deploytypescriptwaffle

I am writing tests for my contract in typescript. I am deploying the contracts like this:

import { Contract } from "../typechain-types/index";
import ContractArtifact from "../artifacts/contracts/Contract.sol/Contract.json";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { ethers, waffle } from "hardhat";


let contractOwner: SignerWithAddress;
let deployedContract: Contract;

const { deployContract } = waffle;
[contractOwner] = await ethers.getSigners();
deployedContract = (await deployContract(
        contractOwner,
        ContractArtifact
      )) as Contract;

Is it possible to set constructor arguments when deploying with typescript? If so, how?

Best Answer

From your code snippet I see you are using waffle's function deployContract, it receives 3 arguments:

  • wallet to send the deploy transaction

  • contract information (abi and bytecode)

  • contract constructor arguments

Just include the array of arguments at the end:

deployedContract = (await deployContract(
        contractOwner,
        ContractArtifact,
        [argument1, argument2,...,last argument]
      )) as Contract;
Related Topic