Introduction
In this article, let’s discuss about low level transaction and call data and also fallback and receive function in depth.
Example contract for low level contract
In this contract we have function to deposit money and function to withdraw the money. Let's deposit some Ethers in finney.
Here we can see that the amount has been deposited and the balance 0.1 Ether.
What is low level transactions and call data
If we look at this transaction, we can see details like from address, to address, transaction cost, gas fee, and many more. There is also a parameter called input which is an hexcode.
This input is the low-level code that is sent to the EVM.
If you copy the input and past it in the low-level interface section and then hit transact, then you would receive an error called "fallback function is not defined".
What are fallback functions?
The reason for the above error is that we have not declared the fallback function. When we deposit 100 finney, the compiler calls the deposit function and adds the balance to the account. But when the input is copied and passed in the low-level interactions section the compiler doesn’t know which function to call or where to go and therefore it is looking for a fallback function. So, if there is no function to be called, then fallback function would be called.
What are receive function?
The other side of the fallback function is receive function. The receive function provides a payable contract function where that will be the first function when you transfer Ether to the smart contract.
Here we added a new fallback function to the contract and now let’s deploy the contract.
After deploying the contract, again add some Ether to the contract and calling the deposit money function, we copy the input field and paste in the low-level interaction section. If we hit on transact, the transaction would be executed successfully. But there will be a warning shown in the left corner of the toolbox.
The fallback function cannot deployed separately, it needs a receive function. So let's declare a receive function.
We have added a string to know in which function the compiler executes.
When we set the value section to 100 and paste the copied input value in low-level interactions section and hit transact, we can see that the balance gets increased too.
When you just hit the transact button and see the ‘currentInWhichFunction ‘ function, the string will be in receive function. The string will be in the receive function when you hit on any function in the contract. So, for fallback function to occur, we give a false hexcode in the calldata column
You can see that we have given a false hexcode but the transaction is successful. But if you see the currentInWhichFunction you can see that the string is in the fallback function. Which means that the compiler does not have any function to execute for the above hexcode.