Lesson 2 of 4 Advanced Solidity

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.

  1. Calldata matches a function

    That function runs; it must be payable if ether is sent.

  2. Calldata is empty and receive() exists

    receive() external payable runs. This is the plain ether transfer case.

  3. Otherwise, fallback() exists

    fallback() runs; it must be payable to accept ether.

  4. 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 when a contract receives a call or ether. If the calldata matches a function, that function runs, and it must be payable if ether is sent. If the calldata is empty and receive() exists, receive runs. Otherwise fallback() runs if it exists, and it must be payable to accept ether. If nothing matches, the call reverts and the ether is returned.
Empty calldata and a receive function: receive runs.
Scenario questions

Which function runs?

Exam skills 2.2.2

Predict which function handles each incoming call to a contract.

Log in to try this activity Interactive activities are free with an account.

Sending ether

The following comparison shows the three ways a contract can send ether.

transfer
Gas forwarded2,300
On failureReverts
Works with smart contract walletsOften not
RecommendationAvoid
send
Gas forwarded2,300
On failureReturns false
Works with smart contract walletsOften not
RecommendationAvoid
call{value: x}("")
Gas forwardedAll remaining (or a set amount)
On failureReturns false
Works with smart contract walletsYes
RecommendationUse, with a check and reentrancy protection

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.

Match the pairs

Ways to send ether

Exam skills 2.2.2

Match each way of sending ether with how it behaves when the transfer fails.

Log in to try this activity Interactive activities are free with an account.

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.

True or false

selfdestruct after Cancun

Exam skills 2.2.2

Check what selfdestruct does on Ethereum today.

Log in to try this activity Interactive activities are free with an account.
Worked example

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).

Worked example

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.

Check yourself

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).