0
function burn(uint256 _value) public returns (bool success) { balances[msg.sender] = balances[msg.sender].sub(_value); totalSupply = totalSupply.sub(_value); Burn(msg.sender, _value); require(balances[msg.sender] >= _value); // fail tx return true; } 

after executing the above burn function, if get a fail in require() at the end, Will the gas fee used to run the code before that be fully refunded?

1 Answer 1

1

Gas consumption works like this. The are three scenarios:

  1. Say you set your gas limit to 30 000 ( this is the maximum amount of gas that you are prepaying when you send the transaction ). Say the transaction costs 5000 gas. The transaction is successful. The gas needed for its execution is deducted from the 30 000 gas that you sent. You get back 25 000 gas.

  2. You have set your gas limit again to 30 000. The transaction costs 35000 gas. In this case, the entire 30 000 gas that you sent will be deducted from your account, but the transaction will not be successful and an " Out of gas " revert will occur.

  3. Your gas limit is 30 000 gas. The transaction costs also 30 000 gas, but has a require statement 2 lines deep into the function's body. The transaction reverts due to the require() statement not being met. In this case, the gas, required for executing all the logic before the require () statement (say it is the equivalent to 10 000 gas )is deducted from your account, In this case, only 10 000 gas is consumed, the remaining 20 000 is sent back to you.

In your particular case, the msg.sender will pay for the execution of all operations before the require() statement. If there is any extra gas left from the gas limit, this amount will be refunded. But the gas for computing all the logic before the require() statement will be sent to its miner and burnt.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.