4

Say, I want to send a transaction that consumes around ~40,000 gas. Could I just set the gas limit of the transaction to 1 million or is there some disadvantage and I should set a more realistic gas limit? In the end I only pay for what was actually consumed, so a too high limit shouldn't matter, right?

3

2 Answers 2

5

If the transaction executes your on-chain function, which you obviously have 100% confidence of for not spending an excessive amount of gas, then the only drawback is that the gas limit that you set may exceed the block gas limit, in which case your transaction will not be executed.

If the transaction executes someone else's on-chain function, whose gas consumption you are possibly unaware of, then another drawback is that it might cost you a lot more then you anticipate.

In short, the gas-limit parameter is designated in order for you to protect yourself when executing someone else's code.


UPDATE:

There is yet another drawback in setting the gas limit too high.

As it gets higher, miners become more reluctant to execute the transaction.

I'm not sure about the general mining algorithm, but I believe that it opts for the highest gas price and the lowest gas limit (the reason for the highest gas price is obvious, the reason for the lowest gas limit is that more transactions can be squeezed into the same block).

I learned that out of bitter experience, when I set the gas limit to the maximum (i.e., the block gas limit), and noticed that it took forever for the transaction to execute.

0

Yes, there is a drawback by setting gas limits too high.

Common advice would be to choose a comfortable gas limit to ensure your transaction doesn't revert, but don't overdo it.

Here is an example to explain the drawback, which you need to consider from the miner perspective. Suppose the block gas limit is 1,000.

The txpool only has 10 transactions with a gas limit of 100 each.

You're the miner you see a transaction with a gas limit of 1,000.

Would you choose the 10 transactions, or the single "big" one?

You would probably choose the 10 transactions, because you will likely get more profit.

There is too much risk in choosing the "big" one, because the actual gas it might use might only be 500 (or even less) and you only get paid by actual gas used by transactions.

Your Answer

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

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