Truffleでdeployする前にガス代を見積もる方法

2022/11/17に公開

https://github.com/trufflesuite/truffle/issues/3367

$ truffle migrate --dry-run

のようにdeploy時に --dry-run オプションを付ければガス代をシミュレーションできます。

Migrations dry-run (simulation)
===============================
> Network name:    'ethereum_live-fork'
> Network id:      1
> Block gas limit: 30000000 (0x1c9c380)

2_deploy_contracts.js
=====================

   Deploying 'MyContract'
   ---------------------
   > block number:        13587948
   > block timestamp:     1636539921
   > account:             0x0123
   > balance:             1.106993597391617736
   > gas used:            3721437 (0x38c8dd)
   > gas price:           100 gwei
   > value sent:          0 ETH
   > total cost:          0.3721437 ETH

Contract deployed at address: 0x0456
   -------------------------------------
   > Total cost:           0.3721437 ETH


Summary
=======
> Total deployments:   1
> Final cost:          0.3721437 ETH

ただし、多少の誤差はあるようです。

https://ethereum.stackexchange.com/questions/113260/truffle-dry-run-on-ethereum-main-reports-different-gas-cost-less-than-actual-d

↓こちらの記事も参考になりました。

https://ethereum.stackexchange.com/questions/35539/what-is-the-real-price-of-deploying-a-contract-on-the-mainnet

Discussion