65 votes
Accepted

What are the limitations to estimateGas and when would its estimate be considerably wrong?

estimateGas works by pretending the transaction was actually being included in the blockchain, and then returning the exact gas amount that would have been charged if that pretend operation was real. ...
Jeff Coleman's user avatar
31 votes
Accepted

how to estimate gas cost?

Using Truffle and testrpc. It's actually pretty easy to build a development environment and test different use cases. For the gas estimation, it's mostly based on Web3 native functions: You can ...
Greg Jeanmart's user avatar
21 votes

Estimate gas price with ethers.js

There is a method called estimateGas in the Contract class. Example for how to use it: const erc20Abi = [ /* ... */ ]; const address = "TOKEN_ADDRESS_HERE"; const provider = ethers....
Paul Razvan Berg's user avatar
18 votes

How to estimate gas at contract creation/deployment to private ethereum blockchain

Try using .getData(). .getData() returns the encoded parameters of a function in order to send the transaction manually. You can then stick this in web3.eth.estimateGas() (the one on web3.eth, not on ...
Matthew Schmidt's user avatar
18 votes
Accepted

What happens if transaction runs out of gas before transaction completes?

Correct, it rolls back. Except for one thing: it gives the miner all the gas, you don't get that back. That's what keeps attackers from spamming, and it's fair anyway because the miner still had to do ...
Dennis Peterson's user avatar
18 votes
Accepted

Estimate gas price with ethers.js

Found it! https://docs.ethers.io/v5/api/providers/provider/#Provider-getGasPrice Hint: the search functionality on docs.ethers.io isn't great - this method didn't show up by searching for "...
Sam Richards's user avatar
15 votes
Accepted

How to whitelist up to 50k addresses in a single contract?

Quick Lower Bound You can put a lower bound on the cost, because each address will include one SSTORE, at the very least. So the cost to hard-code 50k addresses, at 20 kgas each would be more than 1 ...
carver's user avatar
  • 6,361
12 votes

Composition Over Inheritance - Gas Efficiency

We have two approaches: Inheritance contract Parent { string w; function Set(string _w) public { w = _w; } function Greet(string name) internal view returns(string) { ...
Aquila's user avatar
  • 1,802
11 votes

how to estimate gas cost?

The answer provided by Greg Jeanmart perfectly explained how to estimate the gas cost for a function. Here I want to point out that the design of your function giveAwayDividend() might be vulnerable ...
longcc's user avatar
  • 146
11 votes

Estimate gas price with ethers.js

As of 2022 you should use getFeeData() since getGasPrice() has been deprecated. This accounts for network fee changes introduced in EIP-1559. The creator of Ethers.js talks about this in the above ...
Jasperan's user avatar
  • 251
10 votes

What are the limitations to estimateGas and when would its estimate be considerably wrong?

One limitation I have found, or at least a noteworthy aspect of the eth.estimateGas method, is that it only works as intended if the actual call does not throw an exception. This may well be a bug in ...
Julio Faura's user avatar
9 votes
Accepted

Mist Testnet error "it seems this transaction will fail"

You can't send ether to this constructor or it will fail by design. If you want the opposite behavior then you specify the keyword "payable" in the function. It helps prevent errant transactions ...
Rob Hitchens's user avatar
  • 54.8k
9 votes

Correct syntax for estimating gas cost of a function in Web3?

UPDATE: It is simpler to use contract.methods.methodName.estimateGas with web3 1.0.x: myContract.methods.SetMessage(_message) .estimateGas( { from: _from, ...
Victor Baranov's user avatar
9 votes
Accepted

how to estimate gas limit when using web3.eth.sendSignedTransaction

Include nonce field in estimate gas calculation. web3.eth.estimateGas({ "from" : walletbase, "nonce" : value, "to" : contractAddr, "data" : data ...
Pinank Lakhani's user avatar
9 votes
Accepted

Does using a very high gas limit somehow get your transactions executed slower?

In short: It's exactly as you suspect, a higher gas limit means your transaction is less likely to be included by miners. The reasoning: miners seek to maximize their profit within each block. Calls ...
iamdefinitelyahuman's user avatar
8 votes
Accepted

Infinite gas estimation from solc for simple function

There are many cases when the gas estimator reports infinite gas. However, it doesn't necessarily mean that there is an infinite loop in your code or that your code is incorrect but just the estimator ...
medvedev1088's user avatar
  • 10.9k
8 votes

Getting infinite gas estimates for simple functions

I suspect this is because the cost of those functions is indeed unbounded. Strings can be of any length, so setNewMessage() needs to store an unbounded amount of data, and getMessage() needs to read ...
user19510's user avatar
  • 27.9k
8 votes
Accepted

How can I estimate price of data storage?

Currently the only times you pay for storage are when you write it and when you read it back. There is no ongoing cost for storage, although there have been some discussions about whether there should ...
Edmund Edgar's user avatar
  • 16.8k
8 votes
Accepted

Do intermediate memory variables cost gas?

Here is more information about storing data in solidity https://solidity.readthedocs.io/en/latest/introduction-to-smart-contracts.html#storage-memory-and-the-stack Those variables, which you defined, ...
Aquila's user avatar
  • 1,802
7 votes

Is it possible to calculate the gas costs of a transaction exactly upfront?

The short answer is: it depends, for some transactions state changes might change the pre calculated gas costs. It is possible to calculate the gas costs of every transaction given the current state. ...
mKoeppelmann's user avatar
  • 7,586
7 votes
Accepted

Why ether transfer sometimes use more that 21000 gas?

That's because when the normal transaction took place, there was no contract code at this address (it was created here at block 15158470, but the tx is at block 15152542). So all that was paid was the ...
hroussille's user avatar
  • 7,386
6 votes
Accepted

How do you estimate the gas cost of any arbitrary function in Remix IDE?

If you put the cursor on the name of the function in Remix, on the top right of the screen you will see the required gas. Hope this helps
Jaime's user avatar
  • 8,280
5 votes
Accepted

Gas estimation for mapping updating with web3js

With web3 1.0.x and with contract ABI: const Web3 = require('web3'); const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); estimate(); async function estimate() { const ...
Victor Baranov's user avatar
5 votes
Accepted

Gas, gas price, gas cost

In the transaction object { from:web3.eth.accounts[0], data:tokenCompiled.token.code, gas: 1000000 } the gas property is indeed the maximum to be used for the transaction. The transaction object can ...
eth's user avatar
  • 85.1k
5 votes

Can't estimate gas usage in Mist 0.5.2

The wallet block count always lagged behind the eth.blocknumber as reported by Geth and ethstats. After upgrading to Homestead, this problem disappeared.
Luza's user avatar
  • 181
5 votes
Accepted

Understanding "eth_estimateGas" results estimating gas

I'm too lazy to 100% confirm, but I'm pretty sure that in the example you gave, the key difference is that the cheaper transaction was for the full token balance of the sender. This means they ended ...
user19510's user avatar
  • 27.9k
5 votes
Accepted

Gas cost tests: Sending a transaction to a new address is more expensive?

Yes this is true. If you read the Ethereum Yellow paper you can see in Appendix H that you have to pay extra gas if you send Ether to a new account. This is to discourage addresses sending a load of ...
JBrouwer's user avatar
  • 1,052
5 votes

Composition Over Inheritance - Gas Efficiency

In a 1-1 comparison Composition is more expensive to deploy, and execute. That said, if you need to deploy many instances, you could use the Library pattern and use composition in that way. Libraries ...
Steve Ellis's user avatar
  • 1,357
5 votes

Do intermediate memory variables cost gas?

It depends on the settings used when compiling the Solidity. https://solidity.readthedocs.io/en/v0.4.25/using-the-compiler.html As tested by @Aquila, compiling with the optimizer leads to the same ...
eth's user avatar
  • 85.1k
5 votes

Can I estimate the total gas limit of several dependent transactions?

You can use this solution to sync the state of public ethereum chains into your local private blockchain in truffle (or embark) and then locally simulate the execution of both transactions in the ...
sea212's user avatar
  • 1,820

Only top scored, non community-wiki answers of a minimum length are eligible