首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何访问同一契约的另一个已部署实例中的公共变量?

如何访问同一契约的另一个已部署实例中的公共变量?
EN

Ethereum用户
提问于 2022-01-05 06:10:19
回答 1查看 206关注 0票数 0

契约Escrow中的函数需要访问同一个契约Escrow的另一个部署的公共变量。下面是函数的伪代码(不确定下面的代码是否有效):

代码语言:javascript
复制
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;
    }
}
EN

回答 1

Ethereum用户

回答已采纳

发布于 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)。

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

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

复制
相关文章

相似问题

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