javascriptethereumsolidityweb3jstruffle

How to check for negative numbers sent to smart contract?


I don't want my contract to accept negative values being added to it's balance, so I thought I'd add a require value > 0. It seams though as the value sent with the payable function is unsigned and therefore always a positive number. Meaning if you send -1 ether it is accepted and adds about 1 ether instead.

How do I make sure no negative numbers can be sent?

function () external payable {
  require(msg.value > 0); 
  require(msg.data.length == 0);
} 

Solution

  • It is impossible to send a negative value in EVM. So msg.value is always non-negative. You don't need to check that.