0

In hardhat-ethers we have these helper functions:

function deployContract(name: string, constructorArgs?: any[], signer?: ethers.Signer): Promise<ethers.Contract>;

function getContractFactory(name: string, signer?: ethers.Signer): Promise<ethers.ContractFactory>;

function getContractFactory(name: string, factoryOptions: FactoryOptions): Promise<ethers.ContractFactory>;

function getContractFactory(abi: any[], bytecode: ethers.utils.BytesLike, signer?: ethers.Signer): Promise<ethers.ContractFactory>;

function getContractAt(name: string, address: string, signer?: ethers.Signer): Promise<ethers.Contract>;

function getContractAt(abi: any[], address: string, signer?: ethers.Signer): Promise<ethers.Contract>;

function getSigners() => Promise<ethers.Signer[]>;

function getSigner(address: string) => Promise<ethers.Signer>;

function getImpersonatedSigner(address: string) => Promise<ethers.Signer>;

function getContractFactoryFromArtifact(artifact: Artifact, signer?: ethers.Signer): Promise<ethers.ContractFactory>;

function getContractFactoryFromArtifact(artifact: Artifact, factoryOptions: FactoryOptions): Promise<ethers.ContractFactory>;

function getContractAtFromArtifact(artifact: Artifact, address: string, signer?: ethers.Signer): Promise<ethers.Contract>;

My questions: (1) Do I need to pass all parameters stated in function? (2) We use ethers.getcontractFactory like this const sample = await ethers.getcontractFactory("Sample"), if we need to pass all parameters in function as stated then how this given example works?? (3) What is use of deployContract()?? How to use it and when to use? (4) In hardhat-ethers documentation they stated that The Contract s and ContractFactory s returned by these helpers are connected to the first signer returned by getSigners by default. Is that mean we don't need to worry about signer, we don't need to pass signer in funtion's parameter,signer is automatically set by default????

1 Answer 1

0

Wherever you see ? in the arguments definition, it means that this parameter is optional.

If you use const Contract = ethers.getContractFactory("ContractName") for example, this will return an ContractFactory which is something like an instance of the contract in the js.

Then it can be deployed with const contract = await Contract.deploy([contract constructor args]).

As for the signer, you don't really need to worry about it while you are running your code on local hardhat node. In this case it is assigned automatically by hardhat.

Alternatively you can do const [_, account2, account3, account4] = await ethers.getSigners(); and then use any of the other accounts as signer for the contract factory. _ is by convention the first signer which is assigned from hardhat by default.

1
  • For ethers.getContractAt() the address to be passed in it must be the address where the contract was deployed?? like if we do ``` const sample = await deploy("Sample",{....``` so the sample.address will be passed as address or any address i can pass??
    – Hacek
    Commented Feb 7, 2023 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.