首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >solidity中的address.call{}(abi.encodeWithSignature())返回布尔值,其他参数是什么?

solidity中的address.call{}(abi.encodeWithSignature())返回布尔值,其他参数是什么?
EN

Ethereum用户
提问于 2022-06-20 08:16:25
回答 2查看 2.3K关注 0票数 0

我从这个文档中学到了https://docs.soliditylang.org/en/v0.8.15/contracts.html和关于abi.encodeWithSignature()的讨论如何在实体中使用address.call{}()

我对下面这个片段有疑问。

代码语言:javascript
复制
contract TestPayable {
uint x;
uint y;
// This function is called for all messages sent to
// this contract, except plain Ether transfers
// (there is no other function except the receive function).
// Any call with non-empty calldata to this contract will execute
// the fallback function (even if Ether is sent along with the call).
fallback() external payable { x = 1; y = msg.value; }

// This function is called for plain Ether transfers, i.e.
// for every call with empty calldata.
receive() external payable { x = 2; y = msg.value; }
}

然后我们找到了来电者

代码语言:javascript
复制
contract Caller {

function callTestPayable(TestPayable test) public returns (bool) {
    (bool success,) = address(test).call(abi.encodeWithSignature("nonExistingFunction()"));
    require(success);
    // results in test.x becoming == 1 and test.y becoming 0.
    (success,) = address(test).call{value: 1}(abi.encodeWithSignature("nonExistingFunction()"));
    require(success);
    // results in test.x becoming == 1 and test.y becoming 1.
    return true;
}
}
  1. 我想知道这行(bool success,) = address(test).call(abi.encodeWithSignature("nonExistingFunction()"));在逗号(bool success, **What other parameters could fit here?**)之后会发生什么?
  2. 如果这一行只返回bool success,那么为什么我们需要将它放在带有逗号(bool success,)的括号中呢?为什么我们不能把bool success = address(test).call(abi.encodeWithSignature("nonExistingFunction()"));
  3. nonExistingFunction()是一个默认的保留函数名,可以调用其他智能契约中的无名称回退函数吗?

我在官方文件中找不到这方面的信息。

EN

回答 2

Ethereum用户

回答已采纳

发布于 2022-06-20 10:34:33

  1. 这是低级别呼叫。您可以直接调用一个函数,并使用call函数(最好导入契约的接口来调用它上的函数)。建议调用用于调用合同的回退和接收功能。
  2. 因为它返回一个元组。调用函数的返回值是bool和字节数组的一个元组。第一个是调用的状态,字节数组具有名为.(看看这个)的契约函数的返回值。
  3. 不是的。这是一个不存在的函数名:)如果您的msg.data (第一次调用Par业馀者)是空的,那么调用接收()和任何不匹配的数据调用回滚().看看这个
票数 1
EN

Ethereum用户

发布于 2022-06-20 09:36:20

  1. 它也返回字节,但通常编码器对返回数据不感兴趣,因此省略了您的示例中的内容。否则,类似的东西,例如(bool,字节内存retData) = msg.sender.call{ .
  2. 见1。
  3. 不,不是保留的名称,只是示例的有意义的名称。任何与函数名不匹配的东西都同样有效
票数 1
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

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

复制
相关文章

相似问题

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