2022 Developer Survey is open! Take survey.
62 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. ...
user avatar
  • 20.9k
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 ...
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 ...
user avatar
17 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 ...
user avatar
14 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 ...
user avatar
  • 6,051
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 ...
user avatar
  • 146
11 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....
user avatar
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 ...
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 ...
user avatar
  • 50.7k
9 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) { ...
user avatar
  • 1,702
9 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 "...
user avatar
8 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, ...
user avatar
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 ...
user avatar
  • 26.2k
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 ...
user avatar
  • 16.3k
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,...
user avatar
  • 1,702
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. ...
user avatar
  • 7,176
7 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 ...
user avatar
7 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 ...
user avatar
6 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 ...
user avatar
  • 10.2k
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
user avatar
  • 7,735
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 ...
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 ...
user avatar
  • 78k
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.
user avatar
  • 181
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 ...
user avatar
  • 1,328
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 ...
user avatar
  • 78k
5 votes
Accepted

How can I calculate the gas fee of a swap on Uniswap?

To my knowledge swap gas fee is 21000 or so A transfer between two EOAs (i.e. normal wallet accounts) is 21,000 gas. As soon as you include smart contracts in the equation the gas cost increases: ...
user avatar
4 votes

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

One limitation (from my own observation, hopefully someone will correct me if I'm miunderstanding) is that even if estimateGas estimates correctly, that doesn't give you the gas limit that you need to ...
user avatar
  • 16.3k
4 votes
Accepted

How to know, inside the contract function, the amount of gas that will be refunded in the end

There's no way to know the value of the EVM's refund counter from inside the EVM; if you want to account for refunds, you'll have to do them yourself, manually.
user avatar
  • 7,549
4 votes
Accepted

How to estimate the cost to call a Smart Contract method?

The documentation is skimpy on details and it took me forever to figure out the correct way with the help of your links, anyways, here is what works for me: Tested on Geth : 1.6.7 -stable , web3.js :...
user avatar
  • 204
4 votes
Accepted

What is the approximate cost to store a mapping to be used in a contract?

In the simplest implementation I can think of: pragma solidity ^0.4.6; contract Mapping { mapping(address => address) public matches; function writeMapping(address addr) { ...
user avatar
  • 50.7k

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