1

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?

1 Answer 1

2

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.

4
  • That's great, contract.method.estimateGas(arg, object) works great, thanks! I guess with web3.eth.estimateGas, you need the contract's bytecode which you may not have access to, unless you can get that dynamically? (sorry newb)
    – joe
    Commented Jan 9, 2018 at 23:33
  • 1
    The contract's bytecode is publicly available, but you would have to manually construct the data field, which is much easier if you know the contract's ABI, which describes the public methods and their arguments.
    – user19510
    Commented Jan 9, 2018 at 23:35
  • Thanks for that. Could you confirm if estimateGas an estimate of gas required, or is it a total of (gas * gas price)?
    – joe
    Commented Jan 10, 2018 at 0:55
  • 1
    Just the amount of gas required.
    – user19510
    Commented Jan 10, 2018 at 1:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.