0

Two Uniswap txns calling swapExactETHForTokens that are almost identical (even same timestamp) except for the value and gas price, but the one with higher value and gas price runs out of gas and uses much more gas than the other one. The only other difference is of course a proportionally different amountOutMin.

Given the value on the failing swap is less than x2 of the other one while its gas price is x10, why is it failing?

I've linked above, but these are the txns:

This has happened to me twice now. The other time was a lot less value but still x10 gas price over the txn that was fully mined (although that txn failed for a different reason - insufficient output amount).

Any idea what could be causing the failing txns to run out of gas (and even use much more gas) given that they're virtually identical to a successful swap at the same time, but with much higher gas price?

The internal txns are also identical between the txns in each case.

7
  • 1
    'Out of gas' has nothing to do with gas price. Try to increase the gas limit. Nov 4 '20 at 14:55
  • @goodvibration I never said it does, but that makes my question all the more valid. Why are the failing txns running out of gas (and even using more gas) for no apparent reason (especially as gas price should not cause any difference like you said)? Increasing the gas limit ad infinitum will obviously improve the likelihood of not running out of gas, but the real issue is that the failing txns are using more more gas than the succeedings txns.
    – Neo
    Nov 4 '20 at 15:09
  • 1
    The title of your question actually says it very explicitly IMO. Nov 4 '20 at 15:23
  • As to the rest of your question - gas usage of a function is not constant. For one, it depends on the operations within that function, which may differ from one execution to another, depending on the input to the function as well as the current value of any state-variable (in this contract or in another contract) which the function changes during its execution. For example, changing a state-variable from zero to non-zero costs 20K, while changing it from non-zero to non-zero costs only 10K (or 5K, can't remember for sure). Nov 4 '20 at 15:25
  • @goodvibration That's all very well and understood, but it doesn't explain what happened in my two cases where the inputs are virtually identical except for the value, amountOutMin and gas price. Are you suggesting that those particular inputs could cause such a huge disparity between two txns that are otherwise identical?
    – Neo
    Nov 4 '20 at 17:53
1

Transactions have a gas limit and a gas price field. If your transaction is running out of gas, you need to increase the gas limit field, not the gas price field.

The amount of gas consumed by a transaction can vary based on which code executes and how much data is written to or removed from the blockchain. You can find the details in the yellow paper, but one simple example: if a previous transaction sets some value to zero, and your transaction sets it to non-zero, your transaction will require 15000 more gas than if the previous transaction had not happened.

Your wallet cannot predict how the contract state will change between the time you send the transaction and the time it gets confirmed, so it uses the current contract state to compute the amount of gas required.

If you're sending a transaction to a busy contract with many other users, you should increase the gas limit beyond what your wallet suggests. If the transaction ends up consuming less gas than your gas limit, the difference is automatically refunded to you. Besides broken contracts, there is no harm in simply increasing your gas limit by 100k to help your transaction get confirmed.

I hope this helps

1
  • Thanks for the explanation, but I'm more interested in why my particular txns ran out of gas with respect to the almost-identical txns at the exact same time that required much less gas. Re the automatic refund, are you saying the net gas used on my txns may have been less than what they actually used if the gas limit was higher? In other words, because they ran out of gas, the txn just cancelled, and any automatic refund that would've taken place didn't. So if I set the gas limit to 200k, and it got confirmed at 190k, I may have been refunded 70k. But as my limit was 137k, it cost me 137k?
    – Neo
    Nov 5 '20 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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