Enhancing Security with receive() and fallback() in Ethereum Contracts (Part-2)

Enhancing Security with receive() and fallback() in Ethereum Contracts (Part-2)

INTRODUCTION

The fallback function is executed on a call to the contract if none of the other functions match the given function signature, or if no data was supplied at all and there is no receive ether function. The visibility of the fallback function is always external, Why? I already provided the reason in the Part-1 article if you haven't read that click here to read. The fallback function is optionally payable means you can make it payable or leave it doesn't care. A contract can have at most one fallback function. A fallback function can be virtual, can override and can have modifiers. A fallback function always receives data, but in order to receive ether it must be marked as payable. The fallback function is active only when we call functions using low-level interaction. Now you have a question what is low-level interaction? Don't worry I will tell you.

CONTRACT

First, we all need to create a sample/normal contract through which I can able to explain about fallback function.

//SPDX-License-Identifier: MIT
pragma solidity > 0.5.0 < 0.9.0;

contract fall{
    uint public a;
    function add() public {
        a= a+1;
    }
    function sub() public {
        a= a-1;
    }
}

In this above contract, there is no fallback function I assigned. I tried to do low-level interaction through this but it shows the error that the 'Fallback' function is not defined. To do low-level interaction we need to create a fallback function in this contract.

LOW-LEVEL INTERACTION

Let's first understand the low-level interaction. Low-level interaction is an interaction we do with our contract using the hash of the contract functions as the name suggests. But now you are thinking how do we get the hash of the function? First, I will explain in remix and then explain how to get in our own frontend.

In Remix ide first, write your contract after that compile the contract using ctrl+s go to the tabs section on the left-hand side and open the SOLIDITY COMPILER tab. Here you find COMPILATION DETAILS at the bottom and above the ABI and BYTE CODE options click on it and this popup screen will open.

In this above image, you can see the FUNCTIONHASHES option simply open it and here you will get all the hashes of the created functions.

Simply copy the hash of the function that you want to call using low-level interaction. This low-level interaction option you will find when you deploy your compile contract at the bottom you will able to see this option.

START DOING INTERACTION

If we try to do low-level interaction without creating the fallback function in our contract then it shows the same error that you can see in the above image below the TRANSACT button. Let's add a fallback function in our previous contract.

//SPDX-License-Identifier: MIT
pragma solidity > 0.5.0 < 0.9.0;

contract fall{
    uint public a;
    function add() public {
        a= a+1;
    }
    function sub() public {
        a= a-1;
    }
    fallback() external {
        a = 10;
    }
}

Deploy the above contract copy the hash of the add function and paste it on the low-level interaction parameter.

You need to write 0x before pasting the hash of the add function because the hexa decimal number is represented like this you can check the hashes of the transaction in the remix ide terminal. After clicking on the transact button you can see the transaction is successful and the value of the "a" changes from 0 to 1. Now you are saying, that all about this fallback function. Maybe yes but the actual use of the fallback function is seen when I call with the wrong function hash means I am going to paste the hash that is not present in the contract then what happens? Let see.

You can see when I paste the wrong hash the value of "a" is 10 which we define in the fallback function, one thing is when I try to call the function that is not present in the contract this thing needs to send me an error massage but it didn't send me any kind of error and the transaction get success, How?

When you try to do this and your contract has a fallback function then the contract calls the fallback function and executes the code inside the fallback. That's why the value of "a" is 10.

How you can get hashes of functions in your front end? Simply first, create the instance of your contract code and console the contract. Run your project open your browser and open the console by left click of your mouse and then click on the inspect option. Your instance is there click on it and open the methods section where you find all hashes of your function.

useEffect(() =>{
    const init = async() => {
        const provider = new Web3.providers.HttpProvider("HTTP://127.0.0.1:7545");
        const web3 = new Web3(provider);
        const networkId = await web3.eth.net.getId();
        const deployerNetwork = Your_contract_name.networks[networkId];
        const contract = new web3.eth.Contract(
          Your_contract_name.abi,
          deployerNetwork.address
        );
        // console.log(web3);
         console.log(contract);
    }
    init();
  },[]);

Using this code in React JS you can see your contract instance in the browser console. Where you find functions hashes inside methods. This is for the Ganache blockchain if you want to deploy on a test network the code simply changes the URL in the provider variable.

That's all about the fallback function if you missed Part 1 of this article, where I explain about receive function in simple and easy wording click here.

If you like this article then follow me and in last Keep reading, Keep learning and Keep growing.