2

Is it possible to decline a transaction to a contract in Ethereum (with Solidity) (written here

This is a part of my code:

  function accept() public payable {
    if (msg.value == 1e18) { //1e18 wei = 1 ether
      // do smth. special
    } else {
      // do nothing
    }
  }

I would like to decline a transaction of less than 1 ether, but I can only filter it that way. So if someone does not read that he must send 1 ether and sends more or less, how can I decline it completly?

2

I think you're looking for require:

function accept() public payable {
    require(msg.value == 1 ether);
    // do smth. special
}

If the condition of the require is not met, the transaction is reverted.

  • Completely agree. Just to avoid an omission, there are edge cases such as selfdestruct than can used to forcefully push ether into a contract that doesn't necessarily want or expect it. It's a possible attack vector if the author thinks this is a fail-safe restriction and there is logic based on that assumption. – Rob Hitchens - B9lab Mar 4 '18 at 20:31

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.