If I want to check if the payer has enough funds, should I use...





I also see that people usually check balance in the following way:

require((balances[msg.sender] + msg.value) >= balances[msg.sender]);

I think I missing something, because for me the most logical way would be this:

require(msg.sender.balance >= msg.value );

Thanks in advance!

2 Answers 2


I think I'm missing something

You're definitely missing something - those are two different balances:

  • msg.sender.balance is the ether balance of msg.sender
  • balances[msg.sender] is the token balance of msg.sender

The latter is valid (can be compiled) only in a contract which maintains a storage variable:

mapping (address => uint) balances

Or in a contract which inherits such contract (typically, a token contract).

  • Great! So balances[address] is to manage another token balance. It makes completeley sense! Dec 23, 2020 at 0:33

They are two different things.

msg.sender.balance is the protocol-level ethereum balance.

balances[msg.sender would be an element in an array or mapping that contains data, usually a number for internal accounting purposes.

For example if you have an ATM contract that holds ETH for users a mapping like mapping(address => uint) balances can hold balance information for all of the depositors while the total ETH on hand would be all be in address(this).balance.

The users' money outside of the system would be in msg.sender.balance but that would probably not be relevant. It would not be important check that users don't send more money than they have because the protocol handles that.

It would be crucial to check that the user doesn't withdraw money than the contract's internal accounting says they are entitled to because the contract would have more money available than individual users should receive.

Hope it helps.

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.