【问题标题】:Problem sending eth from contract to contract将 ETH 从合约发送到合约的问题
【发布时间】:2022-11-25 08:17:02
【问题描述】:
pragma solidity ^0.8.7;
// SPDX-License-Identifier: MIT


contract Client  {
 
    address payable private hub;
    address payable public owner;
    uint256 public balance;

    
   
    constructor(address payable _hub) {
        hub = _hub;
        owner = payable(msg.sender);
    }
    
    receive() payable external {
        balance += msg.value;
    }    

  
    
    function withdraw(address payable destAddr) public {
        require(msg.sender == owner, "Only owner can withdraw funds"); 
        uint amount = address(this).balance;
        destAddr.transfer(amount);
    }
    
    function start() public payable {
        require(msg.sender == owner, "Only owner can start the process"); 
        uint amount = address(this).balance;
        hub.transfer(amount);
        balance = 0;
    }  

    function setHub(address payable  _new) public {
        require(msg.sender == owner, "Only owner can change address");
        hub = _new;   
    }  
}

嗨,我有一个问题,当我部署这个合同并将另一个合同作为输入(集线器),然后将 eth 发送到这个合同时,我调用“开始”函数并抛出一个气体估计错误。 有人可以帮助我...

我期望调用 start 函数 fund 将被发送到另一个也有接收 eth 函数的合约

receive() payable external {
        balance += msg.value;
    }    

【问题讨论】:

    标签: solidity


    【解决方案1】:

    你得到 gas 估计错误是因为你在第二个合同上的 receive 函数不为空并且交易没有足够的 gas 来执行代码。 transfer 仅发送执行以太币传输所需的气体量(21,000 气体),仅此而已。

    您可以使用 call 而不是 transfer 来发送以太币并设置您要发送的气体量。 transfer 实际上不再推荐用于发送以太币。

    【讨论】:

      猜你喜欢
      • 2021-11-14
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2022-08-12
      • 2020-10-25
      • 2018-10-18
      相关资源
      最近更新 更多