[Ethereum] Truffle and Ganache – do I need to set the same gas-price and gas-limit in both of them

ganachegas-limitgas-pricetruffle

Or can I set different values?

If I can set different values, then how will they be used in the system (i.e., what gas-price and gas-limit will apply in every transaction that I execute)?

Here is my present configuration (using the same values for Truffle and for Ganache):

Truffle:

module.exports = {
    networks: {
        development: {
            host: "127.0.0.1",
            port: 8545,
            network_id: "*",
            gasPrice: 100000000000,
            gas: 6721975 // gas limit
        }
    }
};

Ganache:

--port=8545
--gasPrice=100000000000
--gasLimit=6721975

Thank you!!!

Best Answer

TL;DR: Yes you can use the same value.

Here is why:

Ganache

Ganache is a local test Blockchain.

  • -l or --gasLimit is the block gas limit (total amount in gas unit that can fit in a block). Let's say for example your block gasLimit is 1000000, you would be able to fit only 10 transactions of 100000 gas units each.

  • -g or --gasPrice is the default price per gas unit in wei. If a transaction doesn't set the gasPrice, this default price is used to calculate the gas fee [fee=gasUsed*gasPrice].

Truffle

In truffle configuration, this is different:

module.exports = {
    networks: {
        development: {
            host: "127.0.0.1",
            port: 8545,
            network_id: "*",
            gasPrice: 100000000000,
            gas: 6721975 // gas limit
        }
    }
};
  • gasPrice represents the price you willing to pay per gas unit to deploy the contracts you have under this Truffle project. Using Ganache, you can set the almost any value I would imagine.

  • gas is the maximum number of gas unit the EVM can use to process the contract deployment transaction. If it's too low, the transaction would fail, if it's too high, the excess will be refunded but if the effective gas used is higher than the block gas limit, the transaction will fail.

Metamask

When a user interacts with the contract deployed above (using Truffle), he can choose the gasLimit and the gasPrice:

  • gasLimit: is the maximum number of gas unit the EVM can use to process the transaction. It is recommended to use the function estimateGas to setup the limit (that's what Metamask is doing by default).

  • gasPrice: represents the price you willing to pay per gas unit. It is recommended to check EthGasStation to estimate how long it would take to get a transaction mined. Of course higher is the price, higher is the chance to be mined quickly.

Same concept here as Truffle except it is not only for contract deployment and migration script but any kind of transaction (transfer, contract methods, contract deployment)

Summary

gas fee estimation (wei) = gasEstimation (or gasLimit) * gasPrice

gas fee effective (wei) = gasUsed * gasPrice

gasEstimation >= gasUsed

gasUsed <= blockGasLimit
Related Topic