Fallback Functions in Solidity
Shlok Kumar
Posted on February 21, 2023
Fallback function is executed when the contract receives plain Ether without any data.
If none of the other functions match the function identifier, the solidity fallback function is called. It is run if the call didn't get any data. Most of the time, a contract has only one unnamed task. It is run whenever the contract gets just Ether, with no data. To receive Ether and add it to the contract's total balance, the fallback function must be set to "payable." Fallback is a function that takes no arguments and returns nothing back.
Fallback function is executed when:
If a function that doesn't exist is called
Ether is sent directly to a contract but receive() doesn't exist or msg.data isn't empty, fallback has a 2300 gas limit when called by transfer or send.
There can only be one fallback function in a contract. It is declared with fallback () external [payable]. No arguments can be passed to this function. It can't give anything back and can't be hidden from the outside world. If none of the functions in the opposite direction match the given function signature, it is run when the contract is called. It can be run even if there was no data at all and there is no receive Ether function. The fallback function is still used to get information. But if you want to also get Ether, you have to mark it as payable.
Like any other function, the fallback function can do complicated things as long as it is given enough gas. If a payable fallback function is also used instead of a receive function, it can only think that 2300 gas is available in the worst case.
Features:
It is called when the contract names a function that doesn't exist.
Must be marked as external.
It has no name.
It has no arguments
It can't return anything back.
It's usually defined together with contracts.
If the contract is not marked as payable, it will throw an exception if it gets just plain ether without any data.
If a contract only gets plain Ether and no other information about the transaction, fallback functions are called.
This choice for the default design is smart and helps protect users.
But for our use case, it will be very important that our smart contract has a fallback function that lets it receive plain Ether.
Example:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Fallback {
event Log(uint gas);
// Fallback function must be declared as external.
fallback() external payable {
// send / transfer (forwards 2300 gas to this fallback function)
// call (forwards all of the gas)
emit Log(gasleft());
}
// Helper function to check the balance of this contract
function getBalance() public view returns (uint) {
return address(this).balance;
}
}
contract SendToFallback {
function transferToFallback(address payable to) public payable {
to.transfer(msg.value);
}
function callFallback(address payable to) public payable {
(bool sent, ) = to.call{value: msg.value}("");
require(sent, "Failed to send Ether");
}
}
For more content, follow me at - https://linktr.ee/shlokkumar2303
Posted on February 21, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.