Search code examples
ethereumhardhatether

hardhat ethers: deploy vs deployed


In following code:

console.log("Deploying contract....");
const simpleStorage = await simpleStorageFactory.deploy();
await simpleStorage.deployed();

Line 2 deploys the contract and we get hold of it.

Why do we need to call deployed method in Line3?


Solution

  • calling deploy() will create the transaction, and you can get the contract address immediately, however this does not mean the transaction has been processed and included within a block.

    deployed() will wait until it has been. Under the hood it will poll the blockchain until the contract has been succesfully processed. See: https://github.com/ethers-io/ethers.js/blob/master/packages/contracts/src.ts/index.ts#L824

    I don't think you technically have to call deployed(), but if you need to do anything after the contract has been deployed and need to make sure it has been included in a mined block, then waiting for deployed() is advised.