以下契约使用接口方法(更改代码)调用另一个契约:
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 (代码不能更改):
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。我搞不懂为什么。任何帮助都是非常感谢的。
发布于 2022-02-25 05:08:31
问题是,StorageFactory.initStorage调用setStorageValue(uint256),并且在您的契约中将其声明为setStorageValue(int256) (输入类型不同)。
对于稳固性,它们是完全不同的函数,因此Storage契约调用回退函数,因为setStorageValue(uint256)不存在。
https://ethereum.stackexchange.com/questions/122512
复制相似问题