Solidity Fallback Function: Everything You Need to Know

This article aims to provide clear understanding regarding solidity fallback function in order to provide valuable concepts to the learner regarding the fallback functions and their working respectively.



What Is Solidity Fallback Function?

Solidity fallback functions lack a name, parameters, or return values.

The fallback function is a special function in Solidity contracts that is called when the contract receives funds or when an invalid function call is made to the contract.

It is a catch-all function that allows contracts to receive ether and also handles unrecognized function calls.

The following scenarios can lead to its execution:

  • The function identifier is not one of the available functions in smart contracts.
  • If no data was provided with the function call.
  • It is possible to assign only one unnamed function to a contract.

Solidity Fallback Function Properties

  1. It is an unnamed function.
  2. It is impossible for them to accept arguments.
  3. It is impossible to return anything.
  4. In a smart contract, only one fallback function can exist.
  5. Marking it as external is mandatory.
  6. You should mark it as payable. The contract will throw an exception if ether is received without any data.
  7. If invoked by another function, it will be limited to 2300 gas.

The fallback function is called in two cases:

  • When a user sends ether to a contract without specifying a function call.
  • When a user sends ether to a contract and specifies an invalid function call.

In the first case, the fallback function is called automatically. In the second case, the fallback function is called explicitly by the EVM (Ethereum Virtual Machine).


How to Use Fallback Function?

The fallback function can be used in various ways, depending on the requirements of the contract.

Here are some examples of how the fallback function can be used:

 

Use CasesOverview
Receive EtherThe fallback function can be used to receive ether from external accounts or contracts.
Reject EtherThe fallback function can be used to reject ether from external accounts or contracts.
Redirect EtherThe fallback function can be used to redirect ether to another contract.
Execute CodeThe fallback function can be used to execute code when an invalid function call is made to the contract.

 

Important: Fallback function should be used with caution, as it can lead to unexpected behavior if not implemented correctly.
Recommendation: Avoid using the fallback function unless it is necessary for the contract’s functionality.

The given programme shows the declaration of a fall back function. The output of this method can only be visible in the log section:

Example: 

// SPDX-License-Identifier: 3.0 pragma solidity ^0.8.0;contract SolidityTest{event fallback_Function_calling(string msg);fallback() external payable { emit fallback_Function_calling("This is a fall ball function having no name,parameters of return type"); }}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>
You will better understand the working of the fallback function by considering the example below:

Example: 

pragma solidity ^0.8.17;contract Fallback { event my_Log(string func, 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 my_Log("fallback", gasleft()); }// Receive is a variant of fallback that is triggered when msg.data is empty receive() external payable { emit my_Log("receive", gasleft()); }// Helper function to check the balance of this contract function show_Balance() 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"); } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>
We can use the fallback() function to process the ether transaction without providing any data to them:

Example: 

pragma solidity ^0.5.0;contract SolidityTest { uint public mrx ; function() external { mrx = 1; } } contract SolidityTest1 { function() external payable { } } contract SolidityTest2 { function callSolidityTest(SolidityTest test) public returns (bool) { (bool success,) = address(test).call(abi.encodeWithSignature("nonExistingFunction()")); require(success); // test.mrx is now 1address payable testPayable = address(uint160(address(test)));// Sending ether to SolidityTest contract, // the transfer will fail, i.e. this returns false here. return (testPayable.send(2 ether)); } function callSolidityTest1(SolidityTest1 sink) public returns (bool) { address payable sinkPayable = address(sink); return (sinkPayable.send(2 ether)); } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Conclusion

Solidity Fallback functions are essential to Solidity smart contracts. With this feature, the contract can process incoming Ether transactions and function calls that are not explicitly defined in the contract.

Contract users may use fallback functions when dealing with custom payment and refund logic, handling unexpected inputs gracefully, or providing a fallback mechanism in case of errors.

The use of fallback functions should, however, be implemented properly to avoid introducing security vulnerabilities. A contract should avoid sending Ether or making external calls from the fallback function, since this can potentially be exploited by attackers.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *