In Stylus smart contracts, fallback and receive functions are special handlers for incoming transactions when no other function matches the call signature. They’re essential for flexible contracts that can handle direct Ether transfers and unknown function calls.
Specifically handles plain Ether transfers—transactions that send Ether with no calldata.
Key characteristics:
fallbackfallback when both existCatch-all handler for transactions that don’t match any function signature.
Key characteristics:
receive is not definedWhen a transaction targets your contract, Stylus follows this decision tree:
Has calldata?
receive() (if defined), otherwise call fallback()Signature matches?
fallback() (if defined), otherwise revertThis example demonstrates both functions and includes tests that verify:
receive/fallback are invokedbytes4) when calldata is presentNotes:
bytes4 selector is logged as FixedBytes<4>; we derive it from the first 4 bytes of calldata.fallback, the example emits three logs (in order): EtherReceived → FallbackTriggered → UnknownFunctionCalled.fallback emits two logs: FallbackTriggered → UnknownFunctionCalled.1Loading...1Loading...1Loading...1Loading...1Loading...1Loading...