契约Escrow中的函数需要访问同一个契约Escrow的另一个部署的公共变量。下面是函数的伪代码(不确定下面的代码是否有效):
pragma "^0.7.0";
contract Escrow {
uint256 public value;
constructor () payable {
...
}
function getValueFromAnotherInstanceEscrow(address _anotherEscrowAddress) external returns
(bool) {
Escrow _double = Escrow(_anotherEscrowAddress); //legal? instance itself from inside of Escrow
uint256 temp = _double.value();
//do something with _double.value(); //<<== value is a public variable
return true;
}
}发布于 2022-01-05 09:01:41
是的,这是可能的和合法的。通过将地址转换到契约实例,您只需告诉他们您可以使用契约的公共方法来访问它。如果_anotherEscrowAddress实际上不是Escrow的一个实例,那么契约很可能会恢复。此外,如果地址或地址上的契约不受您的控制,您应该注意安全性,因为它可能会影响您的代码(类似于您应该如何清理任何应用程序中的用户输入)。
外部函数调用记录在Solidity:https://docs.soliditylang.org/en/v0.8.11/control-structures.html#external-function-calls中。
编辑:
另一种可能更干净的方法是为您提取一个Interface并使用这个(参见https://docs.soliditylang.org/en/v0.8.11/contracts.html?#interfaces)。
https://ethereum.stackexchange.com/questions/118030
复制相似问题