[Ethereum] Deploying contract with no cost (gasPrice 0) on private network

gas-priceprivate-blockchaintruffle-migration

In order to achieve a no-coin bootstrapping of my private network I'm trying to deploy a first special contract having zero funds in owner wallet (every wallet of this private network should have zero balance) and to do so with truffle, gasPrice was set to 0 on truffle.js config file.

Nevertheless, when deploying truffle Migrations contract the following error is thrown:

"Migrations" could not deploy due to insufficient funds
* Account:  0x5FBF29a8Ad77EA087275858d874AcD55526cFbDF
* Balance:  0 wei
* Message:  sender doesn't have enough funds to send tx. The upfront cost is: 9400000000000000 and the sender's account only has: 0

The question is: how can be 9400000000000000 the expected cost while gasPrice being 0. Shouldn't cost be 0 too?

Some tests on the node console show that even setting flag gasPrice to 0 on the miner simple transactions having gasPrice=0 are not mined. So probably the problem is not on deploying contracts but simply transacting with zero cost.

truffle.js:

var HDWalletProvider = require("truffle-hdwallet-provider");
var mnemonic = "...";

module.exports = {
    networks: {
    development: {
        gas : 4700000,
        gasPrice : 0,
        provider: function() {
        return new HDWalletProvider(mnemonic, "http://127.0.0.1:port...", 0, 10)
        },
        network_id: "*" // Match any network id
    },
    ropsten: {
        gas : 4700000,
        gasPrice : 10000000000,
        provider: function() {
        return new HDWalletProvider(mnemonic, "https://ropsten.infura.io/...", 0, 10)
        },
        network_id: "*" // Match any network id
    }
    },

    compilers: {
    solc: {
        version: "0.4.24",
        docker: false,
        settings: {
        optimizer: {
            enabled: true,
        }
        }
    }
    }
};

Best Answer

Truffle default gas-price is 20000000000 wei.

You can see it in the source code:

var default_tx_values = {
  gas: 6721975,
  gasPrice: 20000000000, // 20 gwei,
  from: null
};

When you set gasPrice: 0 in your Truffle configuration file, the default value is used instead.

You can observe it by adding the following printout in your migration script:

  • console.log(web3.eth.gasPrice), if you're using Truffle v4.x (Web3 v0.x)
  • console.log(await web3.eth.getGasPrice()), if you're using Truffle v5.x (Web3 v1.x)

If you multiply this default gasPrice by your gas configuration, you will get:

20000000000 * 4700000

Which without putting into a calculator, I am guessing to be equal to 9400000000000000.

Please note that in the official documentation, they tell you:

  • gas: Gas limit used for deploys. Default is 4712388.
  • gasPrice: Gas price used for deploys. Default is 100000000000 (100 Shannon).

Which used to be true in earlier versions of Truffle, but in the current version it stands at 6721975 and 20000000000 respectively (as I quoted above, directly from the source code).

UPDATE:

All the details above are accurate, however, the reason for gasPrice being 20000000000 is not because it is Truffle's default configuration, but because it is Ganache's default configuration. I'm taking a wild guess here that the provider on your private network is indeed Ganache...

Related Topic