Ethers.js – Estimating Gas Cost to Deploy Smart Contract

contract-deploymentethers.jssolidity

I want to know how much gas it would cost to deploy my smart contract using Ethers.js.

I know how to estimate it for a transaction with contract.estimateGas.functionName(), but I can't seem to figure out how to do it for a smart contract.

Please help.
Thank you!

Best Answer

You'll first need to get the encoded contract deployment data as follows:

const deploymentData = contract.interface.encodeDeploy([<constructor_arguments>])

Then, you could use the data to get the estimated gas limit as follows:

const estimatedGas = await ethers.provider.estimateGas({ data: deploymentData });