0

I am trying to deploy my smart contract with truffle and an Infura HDWalletProvider. Here's my dry run (I edited/shortened it for brevity):

Migrations dry-run (simulation)
===============================
> Network name:    'live-fork'
> Network id:      1
> Block gas limit: 29999802 (0x1c9c2ba)


1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------
   > account:             <hidden>
   > balance:             0.365066926
   > gas used:            138922 (0x21eaa)
   > gas price:           32 gwei
   > value sent:          0 ETH
   > total cost:          0.004445504 ETH

   -------------------------------------
   > Total cost:         0.004445504 ETH


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

   Deploying 'MyFirstContract'
   -------------------------
   > account:             <hidden>
   > balance:             0.131909166
   > gas used:            7258890 (0x6ec30a)
   > gas price:           32 gwei
   > value sent:          0 ETH
   > total cost:          0.23228448 ETH

Adding the two migrations gas units up, I get 7397812 units of gas.

and my truffle config:

    live: {
      network_id: 1,
      provider: () => new HDWalletProvider(privatekey, infuraUrl),
      gasPrice: web3.utils.toWei('32', 'gwei'),
      gas: 7500000,
      // skipDryRun: true
    }

I have set the gas to be just higher than the 7397812 units.

I'm trying to understand a couple things, so I don't lose ETH in a failed transaction. Full disclosure, I just lost 1/3 of an ETH because my settings were wrong. ("Migrations" ran out of gas (using a value you set in your network config or deployment parameters.))

  1. Do I need to set the gas value? Is this just a ceiling, to prevent me spending too much?
  2. Can I count on the 7397812 units from the dry run? Will this be the same number of units during the live deploy, or is there potentially more gas units I'll need to account for?
  3. Why is the balance under 2_deploy_contracts.js not the same as the balance under 1_initial_migration.js. At the very least, I would expect it to be the difference between the 1st migrations balance and it's total cost (0.365066926-0.004445504), but it is not.
  4. Most importantly: given funds in my wallet and the gasPrice and gas settings in truffle-config.js, how can I ensure that the settings protect me from losing ETH in the migration, by failing before the transaction is created?

The price of gas fluctuates wildly and I want to reduce my costs by setting a mid-to-low range gas price, but not at the expense of losing all my gas in a failed transaction.

1 Answer 1

0

The gas specified in the configuration file is the maximum gas any transaction will pay.

It is possible to set it to the maximum gas used. A problem with this approach is that transaction fees are in advance. The deployer account has to have enough ether before execution or the transaction will be pending.

The value of the gas doesn't depends on the blockchain, testnet and mainnet should have the same costs. BUT it depends on the configuration parameters and the contract it interacts with. For example if a parameters is an address and it is an EOA in testnet and a multisig wallet in mainnet the cost might be different. Idem if there's interactions with contracts, the cost might change between mainnet and testnet.

To test deployments again mainnet it is possible to use ganache forking mainnet: How does the ganache-cli --fork work?. It should give more realistic values.

I'd suggest to use a small number for gas in the configuration file, something like 200k, an override the gas parameter in every transaction that uses more than 200k gas.

Moreover the Migrations file isn't needed at all in mainnet. I'd suggest if possible write your own deploying scripts to skip truffle antiquated procedures.

2
  • Thank you @ismael definitely useful ideas in here. I will try the ganache mainnet fork. I did run some numbers in Remix directly and though I'm not sure how accurate they are, they were higher than ganache, which I interpret as "better" or hopefully closer to the real values. It sounds like as long as I estimate gas well enough to set the limit above the estimated value (with a buffer) and this number times the gas price is less than the amount in my wallet, I shouldn't lose the transaction fee for not having enough ETH. In this scenario, a miner would likely skip the low gas transaction.
    – wrightcode
    Feb 24, 2022 at 4:00
  • My question now, which I may ask in a new post, is what's the best env for getting the most realistic estimate. Probably not truffle and ganache. :-)
    – wrightcode
    Feb 24, 2022 at 4:02

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.