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?

hardfork
  • 163
  • 1
  • 8

1 Answers1

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.

user19510
  • 27,999
  • 2
  • 30
  • 48
  • 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 Mar 04 '18 at 20:31