2

I came across published smart contracts (like this one from ConsensSys) that check whether msg.value is strictly larger than zero in their payable functions.

After wondering whether msg.value can be negative, I found this question whose answer says that it's not possible.

These checks, therefore, seem unnecessary to me. Is this to make sure not to fall victim to another negative transaction value bug? Do zero-value transactions pose a threat?

1
  • This kind of check makes sense because the msg.value cannot be negative but it can be zero or a strictly positive (I.e., msg.value is always >= 0). So, msg.value > 0 checks if at least 1 Wei was sent along with the invocation (it is possible to send 0 Weis)..
    – Briomkez
    Apr 30, 2019 at 13:41

2 Answers 2

4

In general, 0 value transactions pose no threat. However, in practice, it is logical to not accept transactions that do not include any value, especially in transactions whose whole purpose is to transfer value.

Many implementations of requiring a value that is greater than 0 do so because of the uniqueness of 0 versus all other numbers. For example, someone my do this because the value may be the divisor in an equation at some point in the logic flow, and this would throw (as you cannot divide by 0).

During the design of the ERC20 standard, there was a long discussion about failing the transaction if no tokens were transferred. The final decision was made that there are no issues with 0 value transactions, and the tokens that folow the spec allow for this.

1

The actual code from the link that you've posted (improperly):

event Deposit(address indexed sender, uint value);

/// @dev Fallback function allows to deposit ether.
function()
    payable
{
    if (msg.value > 0)
        Deposit(msg.sender, msg.value); // should be preceded by `emit` as of v0.4.21
}

The conditional statement in this specific case, is simply in order to avoid emitting the Deposit event if the msg.value is 0. In other cases, there could be other reasons (my point being, one may wonder about "the inside of the if statement", but the condition by itself is "legitimate", since msg.value may well be zero).

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.