2

How can I revert a transfer of ETH funds to my smart contract?
I would like to instantly revert any ETH being sent to my smart contract.

NowsyMe
  • 1,365
  • 1
  • 19
  • 37

3 Answers3

4

Any Ether transfers to a contract are automatically reverted, unless explicitly allowed by the contract, e.g. by:

  • using a payable function
  • using a receive or fallback function

If a function is called with a non-zero value and it's not payable, the transaction will be reverted. If no function is called (no transaction data), and there is no receive or payable fallback function, the transaction will also be reverted.

It's still possible to send Ether to the contract using the methods described by Lauri Peltonen, but above applies to regular transactions.

Morten
  • 6,017
  • 2
  • 12
  • 26
3

You can't make it impossible. But you can make it hard by just not including any payable functions (including receive).

Even if you revert all Ether transfers with a receive function there are three ways the contract can have Ethers sent to it (https://ethereum.stackexchange.com/a/63988/31933):

  1. selfdestruction. Another contract self destructs (by using the selfdestruct functionality) and sends its remaining Ether to your contract

  2. Target of mining (so called coinbase address). Ether rewarded from mining can't be refused.

  3. Ether sent to the contract before the contract exists.

Lauri Peltonen
  • 29,391
  • 3
  • 20
  • 57
  • 1
    Looking at the linked docs, it looks like Morten is correct that if there are no payable functions, that after Solidity 0.4 any direct transfer of Ether will revert even without a receive explicitly reverting them (with the exceptions you mentioned - self-destruct, coinbase, previously sent) - so wouldn't no payable functions mean you don't need a receive that reverts? – The Renaissance Apr 21 '21 at 11:40
  • 1
    Thanks for the heads up, you are correct. Edited my answer. – Lauri Peltonen Apr 21 '21 at 11:46
  • Does the 2nd approach still apply after in PoS Ethereum, post Merge? – Paul Razvan Berg Sep 29 '23 at 14:25
  • Yes. It works very similarly: validator rewards can't be refused. As far as I know. – Lauri Peltonen Sep 29 '23 at 19:06
-1

When you use require or assert it automatically does the transfer to the calling address you don't need to anything extra

Abhishek
  • 1
  • 1