首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么address.call函数失败?

为什么address.call函数失败?
EN

Ethereum用户
提问于 2022-02-10 17:42:30
回答 3查看 841关注 0票数 1

我试着在混合中逐个运行以下示例:

代码语言:javascript
复制
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

contract Payable {
// Payable address can receive Ether
address payable public owner;

// Payable constructor can receive Ether
constructor() payable {
    owner = payable(msg.sender);
}

// Function to deposit Ether into this contract.
// Call this function along with some Ether.
// The balance of this contract will be automatically updated.
function deposit() public payable {}

// Call this function along with some Ether.
// The function will throw an error since this function is not payable.
function notPayable() public {}

// Function to withdraw all Ether from this contract.
function withdraw() public {
    // get the amount of Ether stored in this contract
    uint amount = address(this).balance;

    // send all Ether to owner
    // Owner can receive Ether since the address of owner is payable
    (bool success, ) = owner.call{value: amount}("");
    require(success, "Failed to send Ether");
}

// Function to transfer Ether from this contract to address from input
function transfer(address payable _to, uint _amount) public {
    // Note that "to" is declared as payable
    (bool success, ) = _to.call{value: _amount}("");
    require(success, "Failed to send Ether");
}

}

部署之后,当我使用任意一个混合的javascript VM地址作为_to,使用number 1作为_amount调用"transfer“函数时,它将使用以下方式进行还原:

还原事务已恢复到初始状态。合同提供的理由是:“未能发送以太”。

我认为这与访问无关,而是与我如何引入属性有关。有人能给我指点正确的方法吗?提前谢谢你!

EN

回答 3

Ethereum用户

发布于 2022-02-10 23:01:20

一个简单的解决方案是将此函数添加到代码中,以便正确地接收契约的令牌:

代码语言:javascript
复制
receive() external payable { }

我发现将ETH发送到合同中会造成错误,不管使用何种气体,如果没有上述功能,这是可以预料的。

其次,您应该考虑到,当您输入发送的ETH数量时,除非代码(即_amount * 10**18)中另有规定,否则发送的ETH数量没有十进制。这意味着"1“的输入实际上意味着"0.000000000000000001 ETH”。

票数 1
EN

Ethereum用户

发布于 2022-11-11 06:19:22

测试了这个智能混合协议(0.8.11+)能够

  1. 使用沉积函数沉积1醚
  2. 将1乙醚转移到EOA。在您的情况下,_to地址是一个契约吗?如果没有接收器回退功能,_to地址契约就无法接收以太。

另外,_to地址不需要支付,

代码语言:javascript
复制
function transfer(address  _to, uint _amount) public {}

由于调用调用不需要支付地址

票数 1
EN

Ethereum用户

发布于 2022-02-10 18:23:34

您的合同可能没有足够的乙醚发送到另一个帐户。在部署时,确保它收到了足够的数量,以便以后调用传输函数时,它可以执行传输。

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/121338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档