0

On official solidity page , there is an example Safe remote purchase, in that example there is a payable function Purchase, and function firsts checks if msg.value is even or odd. I want to know if that is just for purpose of demonstration or is there any significance of that code.

  • msg.value can be even/odd. I guess that's only for demonstration. If you need even value in msg.value (like in case where you want to distribute 2 equal parts t2 addresses), you can have a check like that else if you don't need that check, skip this. – Prashant Prabhakar Singh Nov 15 '17 at 9:08
  • Note that in practice if you need to split ETH into two parts, checking that msg.value is even is somewhat unnecessary. The difference is 1 Wei, which is an incredibly small amount – Tjaden Hess Nov 15 '17 at 16:05
  • @TjadenHess Yeah obviously that's negligible, but if we have to do it million times, it will matter, so I guess if we can handle the case, we should. :) – Prashant Prabhakar Singh Nov 16 '17 at 4:00
  • Honestly, even if you do it a million times, were talking about 10^-12 ETH, or around 1 trillionth of a cent – Tjaden Hess Nov 16 '17 at 5:17
1

Should msg.value always be even number?

Not necessarily. msg.value is amount send in Wei (1 ether = 10^18 Wei). So there is no such rule for msg.value to be even.

It all depends on your requirement. If you want your msg.value be an even number. You can check for that else just go ahead with whatever value you receive in msg.value.

There are a number of ways to check for even values. One being:

function checkEven(uint testNo) constant returns(bool){
        uint remainder = testNo%2;
        if(remainder==0)
            return true;
        else
            return false;
    }

Note: You are referring develop documentation of solidity. For latest version see : latest solidity documentation

0

msg.value gives you total number of wei sent with the message.So not necessarily it should be even. In that example function first checks if msg.value is even or odd based on requirement.

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.