To my understanding, at the beginning of a payable function in a contract, an amount of msg.value ether is added to the contract's balance.

Therefore, the expression address(this).balance >= msg.value is always true.

Therefore, I can compute address(this).balance - msg.value without any risk of underflow.

Is that correct?

Thank you!


Yes, you are right. When a payable function is invoked the contract balance address(this).balance will be increased by msg.value before any of your code is executed. Therefore both statements of yours are correct:

address(this).balance >= msg.value // Always true in payable function
address(this).balance - msg.value  // Never underflows in payable function

Another answer is confirming it too.

  • Just tried it, but I'll try to get some evidence in the docs..
    – Bruno Zell
    Jul 3 '18 at 8:05

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.