39

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. In other words, it uses the exact same procedure a miner would use to calculate the actual fee. Given this approach, a casual observer might think that there ...


21

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 retrieve the gas price (in wei) using web3.eth.getGasPrice The function estimateGas will give the gas estimation for a function (with the parameters passed) ...


14

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 a given method.) to simulate sending the transaction. Here's an untested example, but hopefully it can help you on your way: var contractData = ...


14

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 the computation you did pay for, even though you didn't include enough gas to finish.


10

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 Gigagas total. The latest block limit is 6.7 Megagas. It will not fit, by a long shot. Gsset - 20000 gas - Paid for an SSTORE operation when the storage ...


9

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 sending ether to contracts with no hope of recovering it. Hope it helps. UPDATE: Example for clarity. pragma solidity ^0.4.2; contract Example { string ...


8

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 the current version of geth, but what I have found is that a call to a method that throws an exception always yields the same gas estimate result, which is a ...


8

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 to attack. Since each of the for loop is paying ether to a user, and it only executes the next loop after this payment is successfully sent, it could be the ...


7

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. However - since the state can change until the transaction is finally executed the state can change. Consider the following code: if a = true: cheap ...


7

We have two approaches: Inheritance contract Parent { string w; function Set(string _w) public { w = _w; } function Greet(string name) internal view returns(string) { return string(abi.encodePacked(w, name)); } } contract Child is Parent { function Get() public view returns(string) { return Greet("Bob"); ...


6

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 is quite restrictive when making decisions about how much gas can be consumed by the code. In particular, any backward jumps or loops in the assembly code will ...


6

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 an unbounded amount of data. If you want to avoid that warning, you'd have to use a data type with a fixed upper bound on its size.


6

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 be, and it is possible that there will be in future. As with other transaction costs, the cost for storage is set in gas. This means that you need to multiply ...


6

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, will be stored in stack(if there is a space and you keep them in one function). in case if compile will optimize it properly it will be for free, if not, each ...


5

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 contractAddress = "0x..."; const contractABI = [...] const contract = new web3.eth.Contract(contractABI, contractAddress) const estimatedGas = await ...


5

The wallet block count always lagged behind the eth.blocknumber as reported by Geth and ethstats. After upgrading to Homestead, this problem disappeared.


5

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 also have a gasPrice property. Miners determine what gasPrice they are willing to accept. If the gasPrice is too small, the miner will ignore the transaction. ...


4

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.


4

Compiled (binary) contract code can not exceed 24576 bytes (48Kb in HEX encoded). See https://github.com/ethereum/EIPs/issues/170 for details. Thanks to https://github.com/fjl for pointing.


4

Yes if you are transferring to an Externally Owned Account (EOA). No if you are transferring to a contract account. You can specify the gasPrice (x gas) you are willing to pay for your transaction and the miner will decide whether to mine your transaction. For an EOA, the gas is always 21000, so you can specify the fee exactly. For a contract account, ...


4

The gas limit for single send transaction is about 21000 gas which turns out to be 0.000378 ether per 21000 gas, roughly equal to 0.4 US$. The SafeLow cost for a transaction is currently $0.019, which is a lot less than your $0.40. It's possible, however, that the price has fluctuated since you wrote your question, which I'll talk about more below. Now I ...


4

From the perspective of gas usage, composition will be more expensive, mainly because you will have more CALL opcodes and more contracts instantiations. See Appendix G Fee Schedules (G_call, G_create, G_callvalue, ...) in the yellow paper -> https://ethereum.github.io/yellowpaper/paper.pdf


4

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 can lower deployment costs if you're going to be deploying many copies of the same code. If you imagine deploying 100 copies of the same code, you may only ...


3

EthGasStation has some data in Misc. statistics. In the last 2,500 blocks Contracts: Median Gas Per Call 42840 Contracts: Median Gas Fee $0.125 Total Transactions (last 10k blocks) 255090 Total Transfers 146778 (58%) Total Contract Calls 108312 (42%) Perhaps you can suggest them an average per address.


3

If you look at storage layout with "The mapping or the dynamic array itself occupies..." you will see that all actions are O(1), keccak256 calculations and read / writes. I do not understand where you got this increasing cost. It must have been something elsewhere in the function.


3

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 :0.20.2 Parity/v1.7.2 & Kovan testnet. I'll be using the minimal token as example : pragma solidity ^0.4.0; contract minimalToken { mapping (...


3

If you want to know this in development, you can get a good estimate by using testrpc to simulate the blockchain. After sending a transaction, it will tell you the gas usage in the terminal. If you are experiencing out-of-gas errors, you may also consider using testrpc (or browser-solidity) to determine it actually is an out-of-gas error. Other errors (...


3

Given that delete myArray can be dangerous (How to clear large arrays without blowing the gas limit?), you probably want to know exactly how much storage you are clearing. Then you could multiply the number of storage slots cleared by 15,000 and check if the refund needs to be limited. Precise gas calculations can be tricky and could require testing. Feel ...


3

So there is some unexpected behavior in the solidity compiler: https://github.com/ethereum/solidity/issues/2999 Works on pragma experimental "v0.5.0"; with compiler 0.4.18.


3

Truffle 4.0 has optimization turned off by default, so you'll probably want to both increase supplied gas and turn optimization on so the gas used on creation is considerably reduced: module.exports = { networks: { development: { host: "localhost", port: 8545, network_id: "*", // Match any network id gas: 4500000 } }, ...


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