Solid - fallback function

catalogue Cause of event Problem description problem ana...

catalogue

Cause of event

Problem description

problem analysis

Problem exploration

Cause of event

The fallback function was involved in the training content of the project. My professional teacher (and senior engineer = =) had a problem. At that time, he only left one sentence, "this is the gap between you and me!" (despised < - <), and then I worked all night and studied it well

Problem description

  1. Why is the fallback function needed and what does it do?
  2. There is only fallback function in the contract. How to call the test?
  3. Must the receive function be written? When will it be called? (0.6.x introduction)

problem analysis

With these numbing questions, my brain cells are ready to fight.

According to the official explanation, fallback is simply a function that does not accept any parameters and returns nothing.

Its execution scenario has the following two aspects:

  • Triggered when a function that does not exist is called.
  • Send Ether to a contract address, but receive() does not exist or msg.data is not empty.

In addition, when called through the transfer or send method, the fallback function has a limit of 2300 gas.

Problem exploration

Here, in order to reflect the creative concept of keeping pace with the times, the latest 0.8.x compiled version is selected for testing. The contract editor uses Remix ide desktop app version.

// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0; contract fallbackTest { fallback() payable external {} }

In the fallbackTest contract, only a fallback function is written. After successful compilation and deployment, the screenshot is as follows:

  You can see that after deployment, we do not have corresponding method buttons to call. What should we do... Don't worry. Here is a different way to call CALLDATA (low level interaction). So here's the question. What should I fill in the box under CALLDATA? (interested students can study it. Here we just test the fallback function without filling anything in it.) at the same time, in order to reflect the function of the payable modifier, we find the value input box in the deployment interface, such as the following figure:

Along with calling the fallback function, send an Ethernet (the default unit is Wei), and then just click the Transact button on the right side of the call data box. So who did the money go to? In fact, it is deposited into the current contract account by default.

Advanced exploration of problems

The following content is more brain burning. It doesn't hurt. I'll throw a brick first.

Which function is called, fallback() or receive()?

send Ether
|
msg.data is empty?
/ \
yes no
/ \
receive() exists? fallback()
/ \
yes no
/ \
receive() fallback()

Reference contract example:

// SPDX-License-Identifier: MIT pragma solidity ^0.8.3; contract ReceiveEther { // Function to receive Ether. msg.data must be empty receive() external payable {} // Fallback function is called when msg.data is not empty fallback() external payable {} function getBalance() public view returns (uint) { return address(this).balance; } } contract SendEther { function sendViaTransfer(address payable _to) public payable { _to.transfer(msg.value); } function sendViaSend(address payable _to) public payable { bool sent = _to.send(msg.value); require(sent, "Failed to send Ether"); } function sendViaCall(address payable _to) public payable { (bool sent, bytes memory data) = _to.call(""); require(sent, "Failed to send Ether"); } }

Students with strong curiosity can test this contract and experience the call timing of fallback and receive functions. Because I'm so sleepy that I can't write a word ~ well, if there are questions or improper explanations in the learning process, I hope all Taoist friends can criticize and correct, 3Q!!!:)

6 November 2021, 08:13 | Views: 7481

Add new comment

For adding a comment, please log in
or create account

0 comments