Solidity Steps
  • Learning Solidity
  • Step 1
    • 1: Introduction
    • 2: Data Types
    • 3: Functions
    • 4: Control Structures
    • 5: State Variable
    • 6: Local Variables
    • 7: Global Variables
    • 8: View Keyword
    • 9: Pure Keyword
  • STEP 2
    • 10: Immunable Keyword
    • 11: Events
    • 12: Condition
    • 13: While Loop
    • 14: Do While Loop
    • 15: For Loop
    • 16: Required
    • 17: Assert
    • 18: Revert
    • 19: Modifier
  • STEP 3
    • 20: Constructor
    • 21: Mapping
    • 22: Array
    • 23: Enum
    • 24: Structs
    • 25: Data Location
    • 26: Inheritance
    • 27: The Shadowing Effect
    • 28: Super Keyword
    • 29: Visibility
  • STEP 4
    • 30: Interface
    • 31: Abstract Contract
    • 32: Payable
    • 33: Using type()
    • 34: Sending Ether
    • 35: Receive
    • 36: Fallback
    • 37: Call
    • 38: DelegateCall
    • 39: Calling Other Contracts
  • STEP 5
    • 40: Factory Contract
    • 41: Proxy Contract
    • 42: Create2
    • 43: Try and Catch
    • 44: Solidity Library
    • 45: ABI Encoded
    • 46: ABI Decoded
    • 47: Keccak256
    • 48: Function Signature Hash
  • TIPS
    • Tips: Solidity by "Immunable"
    • Tips: Truffle Tutorial
    • Tips: Microblog Dapp
    • Tips: Reentrancy
    • Tips: Slither Tutorial
    • Tips: Remix Tutorial
    • Tips: Hardhat Tutorial
  • CAREER
    • 💲Cover Letter
    • 💲Resume
  • ABOUT
    • Contact me
Powered by GitBook
On this page
  • Fallback()
  • fallback() Hack - by Some Examples
  1. STEP 4

36: Fallback

Fallback()

The fallback() function is a special function in Solidity that is used to define a default behavior if no other functions have been called. It is triggered when a contract receives a transaction or message call and no other functions match the given signature.

  • Example 1 The following example shows a fallback() function that logs a warning message when called.

contract MyContract {   
    event LogWarning(string message);
    function () public {        // Log a warning        
        emit LogWarning("You called the fallback function!");    
    }
}
  • Example 2 The following example shows a fallback() function that reverts any calls to it with a warning message.

contract MyContract {    
    function () public {        // Revert and log a warning        
        revert("You called the fallback function!");    
    }
}
  • Example 3 The following example shows a fallback() function that accepts any incoming funds and stores them in an account balance.

contract MyContract {    
    uint public balance; 
    function () public payable {        // Store incoming funds        
        balance += msg.value;    
    }
}

fallback() Hack - by Some Examples

The fallback() function is a special function in Solidity that can be used to catch any incoming transaction or call to a contract that hasn't been defined or declared by a user. It is a powerful tool, and can be used to execute malicious code if used without proper caution.

Here are some examples of how it can be used:

  • Example 1 In this example, we will use the fallback function to send any Ether that is sent to the contract to the calling address.

contract FallbackExample {    
    address public owner; 
    constructor() public {        owner = msg.sender;    } 
    fallback() external payable {        
        require(msg.sender != address(0));        
        owner.transfer(msg.value);    
    }
}
  • Example 2 In this example, we will use the fallback function to increase the balance of the calling address by 10% of the amount that was sent to the contract.

contract FallbackExample {    
    address public owner; 
    constructor() public {        owner = msg.sender;    } 
    fallback() external payable {        
        require(msg.sender != address(0));        
        owner.transfer(msg.value + (msg.value * 0.1));    
    }
}
  • Example 3 In this example, we will use the fallback function to execute an arbitrary function when a transaction is sent to the contract.

contract FallbackExample {    
    address public owner; 
    constructor() public {        owner = msg.sender;    } 
    fallback() external payable {        
        require(msg.sender != address(0));        
        executeArbitraryFunction();    
    } 
    function executeArbitraryFunction() internal { // Arbitrary code here }
}

Tips: In addition, developers should also audit the code within the 'fallback()' function to ensure that it is secure and does not contain any exploitable vulnerabilities.

That's it for the lesson 36! In the next lesson, Call Function

Previous35: ReceiveNext37: Call

Last updated 1 year ago