ReasonJun

Solidity : fallback() vs receive() 본문

Blockchain/Solidity

Solidity : fallback() vs receive()

ReasonJun 2023. 10. 18. 18:29
728x90

The main difference between fallback() and receive() in Solidity is that fallback() is called when a contract receives Ether without any calldata, while receive() is called when a contract receives Ether with empty calldata.

 

In other words, fallback() is called when a user sends Ether to a contract directly, while receive() is called when a user calls a function on a contract without passing any arguments.

 

Here is a table that summarizes the key differences between fallback() and receive():

Feature fallback() receive()
Called when Contract receives Ether without any calldata Contract receives Ether with empty calldata
Can have arguments? No No
Can return a value? No No

Here are some examples of when you might use fallback() and receive():

  • You would use fallback() if you want to handle Ether that is sent to the contract directly, without having to call a specific function. For example, a crowdfunding platform might use a fallback() function to allow users to contribute to a campaign without having to call a contribute() function.
  • You would use receive() if you want to handle Ether that is sent to the contract with empty calldata. For example, a contract that implements a decentralized exchange might use a receive() function to allow users to deposit Ether into the exchange.

It is important to note that you can only have one fallback() function and one receive() function in a contract. If you have both functions, the fallback() function will be called first.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ReceiveEther {
    /*
    Which function is called, fallback() or receive()?
           send Ether
               |
         msg.data is empty?
              / \\
            yes  no
            /     \\
receive() exists?  fallback()
         /   \\
        yes   no
        /      \\
    receive()   fallback()
    */

    // 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 {
        // This function is no longer recommended for sending Ether.
        to.transfer(msg.value);
    }

    function sendViaSend(address payable to) public payable {
        // Send returns a boolean value indicating success or failure.
        // This function is not recommended for sending Ether.
        bool sent = to.send(msg.value);
        require(sent, "Failed to send Ether");
    }

    function sendViaCall(address payable to) public payable {
        // Call returns a boolean value indicating success or failure.
        // This is the current recommended method to use.
        (bool sent, bytes memory data) = to.call{value: msg.value}("");
        require(sent, "Failed to send Ether");
    }
}

Conclusion

Understanding the payable, fallback, and receive functions in Solidity are crucial for developing effective and secure smart contracts. These functions provide the means for your contracts to interact with Ether in a controlled and safe manner.

 

Remember, the payable modifier allows a function to receive Ether, the fallback the function provides a catch-all mechanism for non-existent function calls or plain Ether transfers, and the receive the function offers a more gas-efficient way to handle plain Ether transfers.

 

Solidity — Part 2- Payable, Fallback, and Receive

 

Solidity — Part 2- Payable, Fallback, and Receive

Solidity is a programming language used to write smart contracts on the Ethereum blockchain. Smart contracts are self-executing contracts…

shishirsingh66g.medium.com

 

728x90

'Blockchain > Solidity' 카테고리의 다른 글

Solidity : sol2uml  (0) 2023.10.20
Solidity : deposit code example  (0) 2023.10.18
Solidity : receive()  (0) 2023.10.18
Solidity : call() vs delegatecall()  (0) 2023.10.18
Solidity : Delegatecall  (0) 2023.10.18
Comments