Gas Estimate – How to Estimate Gas for a Transaction with Custom Data

gas-estimate

How can I estimate the gas I need for a string which may be quite long?

I've found an example here, but in this estimate, it doesn't include any custom data.

In my app I would like to save a string on-chain, though the estimateGas's data property appears to require the bytecode for the contract.

How would I go about dealing with this?

Best Answer

You can use contract.method.estimateGas(...) instead of contract.method.sendTransaction(...).

web3.eth.estimateGas also works fine, but you'll have to first compute the data field. (It should be whatever data you'd send in the transaction, which is the contract bytecode only for contract deployment. For most transactions, it's the encoded function arguments.) The estimateGas function that hangs off of your contract's functions does the right encoding for you.

Related Topic