Sending and Receiving Ether
Contracts that hold ether must receive it safely and pay it out without getting stuck or being exploited. This lesson explains payable functions, receive and fallback, the three ways to send ether and the risks of each.
Learning outcomes
After completing this lesson, you will be able to:
- Determine whether receive, fallback or a payable function runs for a call.
- Compare transfer, send and call for sending ether.
- Explain why ether can arrive without running code.
- Describe the current behaviour of selfdestruct.
Receiving ether
A contract can receive ether in three ways: through a payable function, through its receive function, or through its fallback function. Which one runs depends on the call, as the following sequence shows.
-
Calldata matches a function
That function runs; it must be
payableif ether is sent. -
Calldata is empty and receive() exists
receive() external payableruns. This is the plain ether transfer case. -
Otherwise, fallback() exists
fallback()runs; it must bepayableto accept ether. -
No match
The call reverts, and the ether is returned to the sender.
Ether can also arrive without running any code: as the fee recipient of a block or as the beneficiary of selfdestruct. A contract therefore must not assume that address(this).balance equals the sum of deposits it has recorded.
The following diagram shows the order in which a contract decides which function runs.

Which function runs?
Sending ether
The following comparison shows the three ways a contract can send ether.
The 2,300-gas stipend of transfer and send was once considered a reentrancy defence, but gas costs change between upgrades and many recipients, such as multisig wallets, need more gas to accept ether. The recommended form is:
(bool ok, ) = payable(recipient).call{value: amount}("");
if (!ok) revert TransferFailed();Because call forwards all remaining gas, the recipient's code can do almost anything, including calling back into the sender. Ether must therefore be sent only after the contract's own state has been updated.
Ways to send ether
selfdestruct
selfdestruct(recipient) used to delete a contract and send its ether to the recipient. Since the Cancun upgrade (March 2024, EIP-6780), it deletes the contract only if called in the same transaction that created it; otherwise it just sends the ether and the code remains. The compiler warns that it is deprecated, and new code should not rely on it.
selfdestruct after Cancun
A payment that keeps failing
An escrow contract pays sellers with payable(seller).transfer(amount). Payments to sellers using a Safe multisig wallet always revert. Why, and how should the contract change?
The multisig's receive logic needs more than the 2,300 gas that transfer forwards, so it runs out of gas and the payment reverts. The contract should use call{value: amount}("") with a success check, update the escrow's state before the call, and ideally let sellers withdraw their own funds (the pull pattern covered in the security modules).
Which function runs?
A contract defines receive() external payable and fallback() external (not payable). What happens when (a) a wallet sends 1 ETH with no data, and (b) someone sends 1 ETH with data 0x1234?
(a) The calldata is empty and receive exists, so receive runs and the ether is accepted. (b) The data matches no function, so fallback runs; because it is not payable, the call reverts and the ether stays with the sender.
Why are transfer and send discouraged for sending ether?
Show answer
They forward only 2,300 gas, which smart contract wallets often need to exceed, so payments fail; and gas costs can change in upgrades. call{value: x}("") with a checked result and checks-effects-interactions is recommended.
Exam Tips
Remember for the exam
- receive() handles plain ether; fallback() handles unmatched calls; both need payable to accept ether.
- Use call{value: ...}("") and check the result; transfer and send forward only 2,300 gas.
- Update state before sending ether, because the recipient can run code.
- selfdestruct no longer deletes code except in the creating transaction (EIP-6780).