3

I'm executing transactions on mainnet with a NodeJS script:

async function send(web3, transaction, destAddr, gasPrice, privateKey) {
    const options = {
        data    : transaction.encodeABI(),
        to      : destAddr,
        gasPrice: gasPrice,
        gas     : (await web3.eth.getBlock("latest")).gasLimit
    };
    const signed  = await web3.eth.accounts.signTransaction(options, privateKey);
    const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
    return receipt;
}

Every transaction takes ages to complete.

I keep raising the gas-price, but no improvement.

I've finally tried to reduce the gas-limit, and instead of taking the max gas-limit per block, I just wrote "500000", which is far less than the block gas-limit, but far more than the actual gas required for each transaction.

All of the sudden, the network has started to process my transactions much faster.

It feels as if a high gas-limit "scares off" miners or something.

I've tried using await transaction.estimateGas() but it's been giving me a "gas required exceeds allowance or always failing transaction" error every time.

I believe that this is possibly because this function underestimates the actual gas required for the transaction, so I am somewhat reluctant to use it.

Would anyone here be able to shed some light on this?

Thank you!

1

2 Answers 2

9

In short: It's exactly as you suspect, a higher gas limit means your transaction is less likely to be included by miners.

The reasoning: miners seek to maximize their profit within each block. Calls to eth_estimateGas can be computationally heavy, so miners typically do not determine the actual gas each tx will consume. Instead they calculate gasPrice * gasLimit for each given transaction and from that determine the most cost-effective set of transactions to mine.

If you are setting the gas limit to be == the block gas limit, you will need to offer a very high gas price in order for your tx on it's own to be more profitable than all other possible combinations of pending transactions.

1
  • Excellent answer. I just got a very similar explanation from a friend. Thank you very much!!! Oct 7, 2019 at 15:22
0

Common advice would be to choose a comfortable gas limit to ensure your transaction doesn't revert, but don't overdo it. It is rare that you will have a transaction that consumes gas for a whole block.

@iamdefinitelyahuman has written an excellent answer. But it also depends on factors such as how high a gas limit you're using, and what's in the txpool.

Here is an example, 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 and consider 2 scenarios.

Scenario 1: 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.

Scenario 2: You see a transaction with a gas limit of 300.

As miner, would you now choose this transaction and 7 of the other transactions?

You certainly might. One reason could be that there are chances that the single transaction has estimated its gas usage, better than the 3 other transactions it is competing with. For example, the single transactions gas usage might be 270, and the 3 other transactions combined gas usage turns out to be 250.


Perhaps the clearest scenario of what a miner should always choose first, are the 21,000 gas limit transactions because they are known to use 21,000 gas. After that, different miners have different heuristics and algorithms on what they include in a block.

2
  • In the first scenario, can't the miner take the whole gas limit from the big transaction? Apr 3 at 23:17
  • @JoeyYiZhao Gas used can be much lower than the gas limit, for example see ethereum.stackexchange.com/questions/266/…; miner can only get paid for the gas used. The gas limit in this context is a subjective estimate from the user submitting the transaction, so it can be very wrong; gas used is objective and exactly how much gas the transaction used.
    – eth
    Apr 4 at 8:26

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.