【问题标题】:Solidity 'out of gas' exception when trying to transfer Ether尝试转移 Ether 时,Solidity 出现“气体不足”异常
【发布时间】:2019-06-02 23:06:13
【问题描述】:

我尝试将以太币从合约转移到一个地址,但它给出了交易已用完gas的错误。我认为这是一个小问题,但我找不到它。我必须专门使用solidity 0.4.24版本。

The warning from Remix

The error from MetaMask

我尝试了不同的方法,例如:

address.transfer(amount);
address.send(amount);
address.call.value(amount)( );

所有方法都会给出相同的 out of gas 异常。并且 send 和 call 方法也会给出一个警告,它已经过时了,我应该使用 transfer 方法。

我也尝试过调整gas,但没有成功,我还尝试了docs上列出的转账所需的2,300。

代码:

pragma solidity ^0.4.24;

contract TestContract {        
    function payAddress(address _address) external payable {
         _address.transfer(msg.value);
    }
}

如果问题是合约没有任何以太币要转移​​,它可以使用我通过函数调用发送的以太币吗?还是有其他问题?

感谢您的阅读。

编辑:

我已经尝试将以太币发送到我的合约并且它有效,我现在的合约上确实有以太币,但该函数仍然给出与以前相同的错误。所以问题是别的。

当前代码:

pragma solidity ^0.4.24;

contract TestContract {

    function() external payable { }

    function payContract() public payable {}

    function paySomeone(address _address, uint256 _amount) external {
         _address.transfer(_amount);
    }

    function getBalance() public view returns (uint256) {
         return address(this).balance;
   }

}

The balance of the contract

The parameters I use

Same MetaMask error as before

正如您在此处看到的,合约的余额是 10 wei,但是当我尝试发送 9 wei 时,它仍然会出现同样的耗气错误。我仍然从 Remix 中得到与以前相同的错误。

【问题讨论】:

  • @GeertBraakman,您能否分享更多有关您如何测试此代码的信息?你是否在 JavaScript VM 中进行测试,通过一些本地网络,在测试网络上等等?你在调用函数时指定了什么气体限制?

标签: solidity transfer ether


【解决方案1】:

我还在 Stack Exchange 上发布了这个问题,并在那里得到了答案。问题是我的 甘纳许版。我切换到 Robsten 测试网络,它工作正常。我会链接帖子here

【讨论】:

    【解决方案2】:

    是的。为了将以太币从合约发送到另一个地址,首先你必须将一些以太币发送到合约地址。看看thisthis

    【讨论】:

    • 您好,感谢您的帮助,我试过了,但还是不行。我用新代码编辑了我的帖子。
    猜你喜欢
    • 2018-01-14
    • 2022-08-24
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    相关资源
    最近更新 更多