receive & fallback
What happens when someone sends ether to a contract without calling a function, or calls a function that doesn't exist? Two special functions decide: receive and fallback.
receive() external payable { … } // plain ether, empty call data
fallback() external [payable] { … } // everything else that matches no functionNeither has the function keyword, a name or arguments, and both must be external.
Which one runs?
call arrives
│
does call data match a function? ── yes ──▶ that function
│ no
is call data empty?
│yes │no
receive() exists? │
│yes │no │
receive() fallback() ◀┘ (if neither exists: revert)| Situation | Runs |
|---|---|
call{value: 1 ether}("") and receive exists |
receive |
empty call data, no receive, payable fallback |
fallback |
| unknown function selector | fallback (must be payable if ether is attached) |
| neither exists | the call reverts |
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Catcher {
event Received(address from, uint256 amount);
event Fallback(address from, uint256 amount, bytes data);
receive() external payable {
emit Received(msg.sender, msg.value);
}
fallback() external payable {
emit Fallback(msg.sender, msg.value, msg.data);
}
}
contract Main {
constructor() payable {}
function main() external {
Catcher c = new Catcher();
address target = address(c);
(bool ok1, ) = target.call{value: 1 ether}(""); // empty data
(bool ok2, ) = target.call{value: 2 wei}(abi.encodeWithSignature("doesNotExist()"));
(bool ok3, ) = target.call(hex"c0ffee"); // junk data
console.log("all ok?", ok1 && ok2 && ok3);
console.log("Catcher balance:", target.balance);
}
}all ok? true
Catcher balance: 1000000000000000002
event Received(from: 0x5FbDB2315678afecb367f032d93F642f64180aa3, amount: 1000000000000000000)
event Fallback(from: 0x5FbDB2315678afecb367f032d93F642f64180aa3, amount: 2, data: 0x1dcc85ae)
event Fallback(from: 0x5FbDB2315678afecb367f032d93F642f64180aa3, amount: 0, data: 0xc0ffee)
The Fallback events show the raw msg.data: the 4-byte selector of doesNotExist() and the 3 junk bytes.
Rejecting ether by default
A contract with neither function (or with a non-payable fallback) refuses plain ether. That's a safety feature: a contract that has no way to spend ether shouldn't accept it.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Plain {
function hello() external pure returns (string memory) { return "hi"; }
}
contract NonPayableFallback {
fallback() external {} // accepts unknown calls, but no ether
}
contract Main {
constructor() payable {}
function main() external {
(bool a, ) = address(new Plain()).call{value: 1 ether}("");
(bool b, ) = address(new NonPayableFallback()).call{value: 1 ether}("");
(bool c, ) = address(new NonPayableFallback()).call("");
console.log("ether to Plain :", a);
console.log("ether to non-payable fallback:", b);
console.log("no ether to the fallback :", c);
}
}ether to Plain : false
ether to non-payable fallback: false
no ether to the fallback : true
Note: A contract can still end up with ether it never accepted: a block reward, or
selfdestructof another contract pointing at it. Never assumeaddress(this).balanceequals the sum of what your functions recorded.
Keep receive cheap
When ether is sent with transfer or send, receive gets only 2,300 gas: enough to emit an event, not enough to write storage. If your receive updates storage, senders must use call:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Tracker {
uint256 public deposits;
receive() external payable {
deposits += 1; // a storage write: needs more than 2,300 gas
}
}
contract Main {
constructor() payable {}
function main() external {
Tracker t = new Tracker();
bool sent = payable(address(t)).send(1 ether);
console.log("send (2300 gas) ok?", sent);
(bool called, ) = address(t).call{value: 1 ether}("");
console.log("call (all gas) ok?", called);
console.log("deposits:", t.deposits());
}
}send (2300 gas) ok? false
call (all gas) ok? true
deposits: 1
A fallback that forwards calls
fallback is also the heart of proxy contracts: a proxy has almost no functions of its own and forwards every unknown call to an implementation contract. Its fallback can take the call data and return raw bytes:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Calculator {
function double(uint256 x) external pure returns (uint256) { return x * 2; }
}
contract Forwarder {
address public immutable target;
constructor(address target_) { target = target_; }
fallback(bytes calldata input) external returns (bytes memory) {
(bool ok, bytes memory output) = target.call(input);
require(ok, "forwarded call failed");
return output;
}
}
contract Main {
function main() external {
Calculator calc = new Calculator();
Forwarder fwd = new Forwarder(address(calc));
// talk to the forwarder as if it were a Calculator
uint256 result = Calculator(address(fwd)).double(21);
console.log("double(21) via forwarder:", result);
}
}double(21) via forwarder: 42
The Proxies lesson builds on this idea with delegatecall.
Test yourself
Ether arrives with empty call data. The contract has both receive and a payable fallback. Which runs?
fallbackreceive- Both
- Neither; it reverts
What happens when plain ether is sent to a contract with no receive and no fallback?
- The ether is accepted
- The call reverts
- The ether is burned
- It is forwarded to the deployer
Why might receive() external payable { count++; } fail when ether is sent with transfer?
transfersends no ether- Only 2,300 gas is forwarded, too little for a storage write
receivecan't modify statecount++overflows