What are the fallback() and receive() functions in Solidity
A guide on Solidity's fallback and receive functions
Table of Contents for What are the fallback() and receive() functions in Solidity
Receive function in solidity
receive() external payable {
// .. your logic here
}
- The
receive()
function was added in Solidity 0.6.0. - It is used as a fallback function when eth is sent to a smart contract
- It is used only when there is ether sent to a smart contract, and no calldata was sent.
- (if call data was sent, it will fall back to
fallback()
)
Fallback function in Solidity
fallback() external payable {
// .. your logic here
}
- If you call a function that does not exist on a smart contract (from another contract), then it will attempt to run the
fallback
function (if it was defined).
Further reading
https://ethereum.stackexchange.com/questions/81994/what-is-the-receive-keyword-in-solidity
Spotted a typo or have a suggestion to make this crypto dev article better? Please let me know!
Next post
Previous post
π Solidity Auditing online quiz
Learn how to audit smart contracts by looking at some example code and trying to find the bugs
β½ Solidity Gas Optimizations Guide
How to optimize and reduce gas usage in your smart contracts in Solidity
π§ͺ Guide to testing with Foundry
Guide to adding testing for your Solidity contracts, using the Foundry and Forge tools
π Guide to UTXO
UTXO and the UTXO set (used by blockchains such as Bitcoin) explained
π Solidity Assembly Guide
Introduction guide to using assembly in your Solidity smart contracts
π¦ Ethereum EOF format explained
Information explaining what the upcoming Ethereum EOF format is all about