第123篇 笔记-sendEther合约

Posted wonderBlock

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第123篇 笔记-sendEther合约相关的知识,希望对你有一定的参考价值。

How to send Ether?
You can send Ether to other contracts by

  1. transfer (2300 gas, throws error)
  2. send (2300 gas, returns bool)
  3. call (forward all gas or set gas, returns bool)  

How to receive Ether?
A contract receiving Ether must have at least one of the functions below

  1. receive() external payable
  2. fallback() external payable
  3. receive() is called if msg.data is empty, otherwise fallback() is called.

以下为示例合约:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;

contract ReceiveEther 
    /*
    Which function is called, fallback() or receive()?

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

以上是关于第123篇 笔记-sendEther合约的主要内容,如果未能解决你的问题,请参考以下文章