首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Solity-为什么即使address.call{value:msg.value}("")没有数据也会被调用?

Solity-为什么即使address.call{value:msg.value}("")没有数据也会被调用?
EN

Stack Overflow用户
提问于 2022-02-24 17:08:33
回答 1查看 385关注 0票数 1

以下契约使用接口方法(更改代码)调用另一个契约:

代码语言:javascript
复制
pragma solidity 0.8.7;

interface MyStorage {
    function setStorageValue(uint256) external;
}

contract StorageFactory {
  uint256 storageValue;

  constructor(uint256 _storageValue) {
    storage = _storageValue;
  }

  function initStorage(MyStorage store) public payable {
    store.setStorageValue(storageValue);
    address payable storeAddress = payable(address(store));
    storeAddress.call{value: msg.value}("");
  }
}

以下是StorageContract (代码不能更改):

代码语言:javascript
复制
pragma solidity 0.8.7;

contract Storage {

  int _storageValue;

  function setStorageValue(int storageValue) public {
    _storageValue = storageValue;
  }

  receive() external payable {
    require(_storageValue == -1 || address(this).balance <= uint(_storageValue), "Invalid storage value");
  }

  fallback() external {
    _storageValue = -1;
  }
}

通过传递一个存储对象,我使用一个测试来调用第一个契约的initStorage,在这个对象中,由于值设置为大量,所以测试将失败。但是不知怎么的,fallback()函数似乎被调用了,将值设置为-1。我搞不懂为什么。任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-25 04:34:40

由于坚固医生

代码语言:javascript
复制
The fallback function is executed on a call to the contract if none of the other functions match the given function signature, or if no data was supplied at all and there is no receive Ether function. The fallback function always receives data, but in order to also receive Ether it must be marked payable.

您的函数被调用,因为函数没有重载

代码语言:javascript
复制
function setStorageValue(uint256 storageValue) public

因此,将storageValueint更改为uint256将有帮助。

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

https://stackoverflow.com/questions/71255699

复制
相关文章

相似问题

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