0

Let's say we call the function:

 function nice(uint a) public returns(uint256){
        
        
        k = 100;
        z = 100;
        f = 100;
        
        
        require(a == 5);
        
        o = 100;
        s = 100;
        
        return 170;
        
 }

Let's say we passed in a = 15. So, what will happen is transaction is going to be reverted due to require. Now, the transaction cost for the first 3 lines of code(assigning 100 to k,z,f) takes 81000 (this includes transaction data) too. It seems like after transaction is done, from my account, only 81000*price of gas is taken from my balance, which is correct.

What I am curious about is how this happens. Specifically,

  • does it take all my gaslimit * gas price from my account, executes the code, and whenever it's done, it checks if there's any remaining and if there's, it gives back the remaining value. In above case, that could happen, since because of require , it wouldn't make us spend all gas. But if this is how it does, why don't I see the balance reduction and then increase again between pending transaction and finished transaction states ?

OR

  • I have no idea how else it could do. Any idea ?

1 Answer 1

1

The first option is close.

  1. Before execution the transaction gasLimit * gasPrice are removed from the sender account
  2. Transaction is executed
  3. unusedGas * gasPrice is returned to sender
  4. usedGas * gasPrice is send to miner

The state changes in 1, 3 and 4 are unusual in that there's only one part involved.

See here https://github.com/ethereum/go-ethereum/blob/v1.9.25/core/state_transition.go#L223-L228 for the exact details of geth's implementation.

3
  • 1
    I think, the actual reason why some eth doesn't disappear when making a transaction(while it's in pending still) is that the transaction is not included in the block, hence balance still shows unchanged. When the transaction is included in the block, it already includes both(what was returned and what wasn't) and it gets updated atomically like in a blink of an eye. Commented Jan 24, 2021 at 10:12
  • Hi any chance you might know this - ethereum.stackexchange.com/questions/92497/… ? Commented Jan 24, 2021 at 21:24
  • I explained the question better and even made the bounty on it. Would appreciate you taking a look since there's a high chance it might left unanswered :( Commented Jan 24, 2021 at 22:33

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.