0

I know that web3 exposes a function to estimate gas limit, but is it accurate when sending to a contract?

AFAIK the amount of gas I'm gonna need if sending to a contract depends on the contract code, so in general case can be anything.

Is there a way to reliably figure out the gas limit for a transaction if it's to a contract before sending it?

tristantzara
  • 103
  • 3

1 Answers1

1

No there is no way to know it reliably. Even sending the same transaction multiple times may result in different gas amounts. (See for example here Why gas-used are different for same transfer() tx? ). Also other factors make it difficult/impossible to estimate it correctly, such as loops (can we know in advance how many times we will loop?).

The amount can only be estimated. Different tools have different estimates - some probably better than others.

Transactions to EOAs (Externally Owned Account) are trivial to estimate as it depends solely on the amount of data you send plus static transaction cost (21000).

Lauri Peltonen
  • 29,391
  • 3
  • 20
  • 57