【问题标题】:Solidity: Can a Parent contract see data updates from a Child contract?Solidity:父合约可以查看子合约的数据更新吗?
【发布时间】:2019-04-12 16:05:20
【问题描述】:

我有一个以太坊游戏的过早实现。我将我的代码分为两个合同,将“游戏”功能与管理员调用的功能分开。

Admin.sol 继承自 Game.sol,如下所示。在 Admin 中创建了一个 struct 对象,但 Game 看不到它。 (Getter 函数不返回任何内容)

import "./Game.sol";

contract Admin is Game

相同的代码,如果不分成两个合约,也能完美运行。

Admin.sol 中创建对象的函数头:

function createJob(string memory _jname, uint _reward, uint _application_period, uint _job_duration) public {

Game.sol中getter函数的头文件:

function getJob(uint _jID) public view returns (string memory, uint, uint, uint, uint)

我从 getter 得到的是:

结果 { '0': '', '1': , '2': , '3': , '4': }

这清楚地表明它只是在映射中向我显示了一个“空白”点。

是否可以让 Game.sol 看到在 Admin.sol 中进行的数据更改?如果有,是怎么做的?

感谢您的帮助。

【问题讨论】:

  • 是的,它将能够看到更新。
  • @Common 我知道它应该,但它目前表现得好像不能

标签: inheritance ethereum solidity smartcontracts


【解决方案1】:

是的,这是可能的。您首先要了解的几件事是,SmartContract 只能访问在 EVM 中分配的自己的存储,因此一个智能合约在继承之前无法访问另一个 SmartContract 的变量。在您的情况下,您可能会单独部署它们,并且在更新使用不同实例的值时。

你要做的就是只部署一个具有继承性的合约合约(在下面的例子中是它的 ContractB)。你也可以在哪里使用父合约的功能。这是示例

pragma solidity >=0.4.22 <0.6.0;

contract ContractA {

      int a;

      function setA(int _a)  public {
           a = _a;
      }
      function getA() view public returns(int){
           return a;
      }
}

而合同B是这样的

pragma solidity >=0.4.22 <0.6.0;
import"./ContractA.sol";

contract ContractB is ContractA {
       function getContractAvalue() pure public returns(int){
           // For reference that you can also access the function of ContractA in ContractB
           return ContractA.a;
       }
}

因此,如果您仅部署合约 B,您可以访问合约 A 的功能,因此您可以使用一个实例进行更改,并将存储在 EVM 的相同存储空间中,可以使用正确的值访问。

你也可以通过部署 ContractB 来检查它,你可以看到 Contract A 的功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2022-08-18
    • 2021-03-06
    • 1970-01-01
    • 2022-06-14
    • 2018-08-13
    • 1970-01-01
    相关资源
    最近更新 更多