5

The below is right from the solidity readTheDocs page. Its from the Safe Remote Purchase section on https://solidity.readthedocs.io/en/develop/solidity-by-example.html#micropayment-channel.

function Purchase() payable {
seller = msg.sender;
value = msg.value / 2;
require((2 * value) == msg.value);
}

What are the last two lines doing? Dividing the value by two and then requiring that the inverse must be equal to the original number. What's the point? I must be missing something.

1 Answer 1

4

Solidity only allows for integer arithmetic. So if you have an odd number and divide it by 2, it will truncate the decimal. Re-multiplying by 2 at that point would not give you back the same number. So the require statement essentially verifies that the number of wei sent is an even number.

1
  • ok, so it's no wonder it doesn't make sense in the context of this contract. It didn't mention anywhere that only even payments are accepted.
    – jyapx
    Commented Jun 17, 2017 at 19:16

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.