I am building a sniper bot which continually estimating the gas for buying one token. Once the gas is successfully estimated, a transaction to buy the token is sent. But for many times it estimated the gas and sent the transaction, then the transactio failed because no liquidity was found. I checked the transaction history and found that my transaction was in front of the transaction which added the liquidity due the the difference of gas price.

So the question is, does that means the estimateGas method estimates gas according to the transactions being handled which may have not been included in a block already added to the chain?

Any information is highly appreciated!

1 Answer 1


The estimateGas has nothing to do with gas prices. It estimates gas consumption.

I'm not quite sure of the specifics, but my understanding is that it sends a local static call to your node, where it pretends the transaction is part of the newest block (or perhaps one after it). Any state changes it makes are temporary and are discarded at the end of the estimation.

I'm not sure whether it assumes that the block is otherwise empty, or whether it uses the newest block and pretends to be somewhere among the other transaction in the block. I doubt it would take the mempool into account, so I don't think other non-executed transactions play any role here.

If you want to estimate optimal gas price (or gas fee) for your transaction, you have to use other means (don't ask me what).

  • Thanks for answering! In my case, if liquidity is added, buying transaction will succeed, otherwise it will fail. But after gas was successfully estimated, the actual transaction failed, which indicates gas was estimated with the context where a transaction still in the mempool has been executed. But I want to know is it that always the case? Can I find some official docs about that? Aug 27, 2021 at 15:16

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.