0

I have a smart contract lottery that requires people to send exactly 0.2 ethers to its address in order to participate.

In the constructor:

    ante = 0.2 ether;

And in the payable function

require(msg.value == ante);

If someone sends the wrong amount, not only do do they lose the 0.2 ethers, but they also get penalized with extra ether. I tried sending 0.3 ethers on purpose from Metamask and I lost 0.45 ethers from my test account.

How do I avoid this?

Thanks

1

Before require() was introduced, the customary approach was:

if(msg.value != ante) throw; // give it back, revert state changes, abnormal stop

They do not lose the ether. It get's sent back as part of reverting all state changes with throw;. More precisely, since a transaction that leads to throw is invalid overall, the ether never gets sent in the first place.

Hope it helps.

0

You could try safeSending a refund (less gas fees). Ethereum Alarm Clock does a refund, for example: http://www.ethereum-alarm-clock.com/source/v0.7.0/

0

You could use throw after a condition requiring your specified ante value. This would be detected in the wallet and alert the user, preventing any Ether to be spent at all. If the user chooses to ignore the warning, the Ether sent for gas will still be eaten.

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.