【Solidity】支付

以太比的单位

wei、gwei、finney 和 ether 是以太币(Ether)的不同单位。

  1. wei 是以太币的最小单位,1 ether = 10^18 wei;这是 Solidity 中的默认单位
solidity 复制代码
uint public oneWei = 1 wei;
  1. gwei 是 wei 的 10^9 倍,1 ether = 10^9 gwei;常用于表示交易费用 (gas price)
solidity 复制代码
uint public oneGwei = 1 gwei;
  1. finney 是 wei 的 10^15 倍,1 ether = 10^3 finney
solidity 复制代码
uint public oneFinney = 1 finney;
  1. ether 是 wei 的 10^18 倍;是以太坊的基本单位
solidity 复制代码
uint public oneEther = 1 ether;

payable 修饰符

以下函数需要使用 payable 修饰:① 需要接收以太币的函数;② 需要使用 msg.value / callvalue() 且可被外部访问的函数。

solidity 复制代码
contract Demo {
    // 使用 payable 修饰构造函数,使合约可以在部署时接收以太币
    constructor() payable {}

    // 接收以太币的函数, 必须使用 payable 修饰符
    function receiveEther() external payable {
        require(msg.value > 0, "Must send some Ether");
    }

    // 提现函数, 将合约中的以太币发送给调用者
    function withdraw() external {
        uint amount = address(this).balance;
        require(amount > 0, "No Ether to withdraw");
        payable(msg.sender).transfer(amount);
    }

    // 获取合约的以太币余额
    function getBalance() external view returns (uint) {
        return address(this).balance;
    }
}
  1. 设置以太币的数量,部署 Demo 合约

  2. 调用 getBalance 函数,查看合约的以太币余额

  3. 设置以太币的数量,调用 receiveEther 函数

  4. 调用 getBalance 函数,查看合约的以太币余额

  5. 调用 withdraw 函数,将合约中的以太币发送给调用者

  6. 再次调用 getBalance 函数,查看合约的以太币余额

receive & fallback 方法

在 Solidity 中,receive & fallback 是一种特殊的函数。它不需要 function 关键字、没有参数、也没有返回值。

特性:

  • receive & fallback 函数必须声明为 external,表示只能通过外部调用来触发。
  • receive 必须使用 payable 修饰符;fallback 可以使用 payable 修饰符。

receive & fallback 的区别:

  • receive:专门用于处理没有附加数据的以太币转账。
  • fallback:用于处理所有其他情况,包括调用不存在的函数或接收带有附加数据的以太币转账。如果合约没有定义 receive 方法,但定义了 payable 的 fallback 方法,那么在接收以太币时会调用 fallback 方法。
solidity 复制代码
contract Demo {
    event Log(string message, address sender, uint value, bytes data);

    // 定义 receive 方法
    receive() external payable {
        emit Log("receive", msg.sender, msg.value, "");
    }

    // 定义 fallback 方法
    fallback() external payable {
        emit Log("fallback", msg.sender, msg.value, msg.data);
    }
}

以太币的发送与接收

接收以太币的 3 种形式:

  1. 编写 payable receive / fallback 方法,以支持直接传入以太币

  2. 用 payable 修饰 constructor 方法,以支持在部署时传入以太币

  3. 用 payable 修饰其他方法,以支持在调用方法时传入以太币

solidity 复制代码
contract ReceiveEther {
    event Received(address, uint, uint);

    // 接收部署时传入的以太币
    constructor() payable {}

    // 接收直接传入的以太币
    receive() external payable {
        // 打印发送者、接收的以太币数量、剩余的 gas
        emit Received(msg.sender, msg.value, gasleft());
    }

    // 接收调用方法时传入的以太币
    function receiveEther() external payable {
        // 打印发送者、接收的以太币数量、剩余的 gas
        emit Received(msg.sender, msg.value, gasleft());
    }
}
  1. 设置以太币的数量,部署 ReceiveEther 合约;查看事件,可以看到接收的以太币数量

  2. 设置以太币的数量,调用 receiveEther 函数;查看事件,可以看到接收的以太币数量

用 payable 修饰的 address 变量有 3 个方法发送以太币:

  1. transfer:有 2300 gas 的限制;如果转账失败,会回滚交易

  2. send:有 2300 gas 的限制;如果转账失败,不会回滚交易,而是返回 false

  3. call:没有 gas 限制,可以指定 gas 量;如果转账失败,不会回滚交易,而是返回 false

solidity 复制代码
contract SendEther {
    // 使用 transfer 发送以太币
    function sendEtherViaTransfer(address payable recipient) external payable {
        recipient.transfer(msg.value);
    }

    // 使用 send 发送以太币
    function sendEtherViaSend(address payable recipient) external payable {
        bool success = recipient.send(msg.value);
        require(success, "Send failed");
    }

    // 使用 call 发送以太币 (推荐)
    function sendEtherViaCall(address payable recipient) external payable {
        (bool success, ) = recipient.call{value: msg.value}("");
        require(success, "Call failed");
    }
}
  1. 部署 SendEther 合约

  2. 设置以太币的数量,调用 SendEther 合约的 sendEtherViaTransfer 函数,传入 ReceiveEther 合约的地址;可以看到 ReceiveEther 合约的以太币余额变化

  3. 设置以太币的数量,调用 SendEther 合约的 sendEtherViaSend 函数,传入 ReceiveEther 合约的地址;可以看到 ReceiveEther 合约的以太币余额变化

  4. 设置以太币的数量,调用 SendEther 合约的 sendEtherViaCall 函数,传入 ReceiveEther 合约的地址;可以看到 ReceiveEther 合约的以太币余额变化

相关推荐
Joy T20 小时前
【Web3】深度解析 NFT 跨链智能合约开发:原生资产与衍生包装合约架构实战
git·架构·web3·区块链·node·智能合约·hardhat
普通网友1 天前
数据加密与零知识证明在区块链中的应用解析
区块链·零知识证明
御坂100571 天前
区块链智能合约AI化:链下计算+TensorRT验证
区块链· 智能合约· tensorrt
BlockChain8881 天前
区块链入门【一】:揭开“信任机器”的神秘面纱
区块链·ai编程
QQ5110082851 天前
基于区块链的个人医疗咨询挂号信息系统vue
前端·vue.js·区块链
BlockChain8881 天前
区块链的组件:从数据结构到去中心化共识
数据结构·去中心化·区块链
Jerry.张蒙2 天前
大语言模型(LLM)的核心逻辑理解
大数据·人工智能·学习·语言模型·自然语言处理·区块链
Joy T2 天前
【Web3】智能合约质量保障工程:从单元测试到 Gas 效能优化
单元测试·log4j·web3·智能合约·hardhat
Joy T2 天前
【Web3】NFT 元数据去中心化存储与智能合约集成实战
开发语言·web3·去中心化·区块链·php·智能合约·hardhat
亚历克斯神3 天前
Flutter 三方库 eip55 的鸿蒙化适配指南 - 在鸿蒙系统上构建极致、严谨、符合 Web3 标准的以太坊地址校验与防串改引擎
flutter·web3·harmonyos