1

I try to refund gas to the minimum even after using revert with the following example.

uint A = IERC20(base).balanceOf(address(this));
IERC20(token).transfer(owner,1000000);

then I run swaps functions

IUniswapV2Pair(pairA).swap( amount0Out2, amount1Out2, pairB, new bytes(0));
IUniswapV2Pair(pairB).swap( amount0Out2, amount1Out2, pairC, new bytes(0));
IUniswapV2Pair(pairC).swap( amount0Out2, amount1Out2, address(this), new bytes(0));
uint b = IERC20(base).balanceOf(address(this));

finally I apply a condition

if(a>b){revert(0,0)}

This revert consumes all the gas already used, is it possible to return the gas already used taking into account that the state in the block does not change?

1
  • To avoid using the gas without changing the block, you can choose not to broadcast the transaction at all. Using the gas is designed to punish you for making every node waste its time processing a transaction that failed.
    – user253751
    Mar 23, 2023 at 8:59

1 Answer 1

3

Unfortunately, it is not possible to refund gas that has already been used in a transaction on the Ethereum blockchain. Once gas is consumed in a transaction, it cannot be refunded or reclaimed. This means that if your transaction runs out of gas, the transaction will fail, and any changes made to the state will be reverted.

In your example, the revert statement will consume all the gas that was already used in the transaction, so you will not be able to reclaim that gas. If you want to minimize gas usage, you could consider optimizing your contract code to reduce the number of operations or the complexity of the calculations. This could potentially reduce the amount of gas used in the transaction and save you some costs.

Alternatively, you could try to estimate the required amount of gas more accurately, so that you do not run out of gas during execution. You can use tools such as the gas estimator in Remix or other gas estimation libraries to help you calculate the required gas for your transactions.

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.