【问题标题】:calling method failed in another contract when create an instance by new通过 new 创建实例时调用方法在另一个合约中失败
【发布时间】:2019-02-23 22:05:03
【问题描述】:

我正在尝试将一个合约实例用作另一个合约的变量,例如下面的示例。

pragma solidity ^0.4.23;

contract basic {
    uint num1 = 10;
    
    function getNum1() public view returns(uint) {
        return num1;
    }
    function setNum1(uint _num) public returns(uint) {
         num1 = _num;
    }
}

contract parent {
    uint public num2;
    basic public b;

    constructor() public {
        b = new basic();
        num2 = 20;
    }
        
    function getNum1() public constant returns(uint) {
        return b.getNum1();
    }
    
}

当我在 remix 和 truffle 中测试合约时,它运行良好。 enter image description here 但是我在我的私人网络上部署了合约“父”,parent.getNum1() 返回了“0”而不是“10”。

此外,我尝试了其他类型的构造函数,例如将“基本”地址作为参数,但效果不佳。

我也尝试了一些将另一个合约实例作为变量的合约,它们在私有网络上都不能很好地工作。

有人遇到过这个问题吗?救命!!!

【问题讨论】:

  • 你的代码没问题,所以部署的时候可能会失败。您能否分享有关您的专用网络的详细信息、您如何部署到它、您看到的输出是什么?
  • @smarx { "coinbase": "0x0000000000000000000000000000000000000000", "config": { "homesteadBlock": 5 }, "difficulty": "0x20000", "extraData": "0x", "gasLimit" : “0x2FEFD8”, “mixhash”: “0x00000000000000000000000000000000000000647572616c65787365646c6578”, “随机数”: “为0x0”, “parentHash”: “0x0000000000000000000000000000000000000000000000000000000000000000”, “时间戳”: “0×00”, “黄金”:{ “0x84f4a56a217c882a20d8d8c83e3d2cfc9bb87fc6”:{ “平衡” :"100000000000000000000000000000" } } }
  • @smarx 上面是我的 genesis.json,我通过 remix 给出的 web3deploy 脚本部署了我的合约。我还部署了一些合约,例如 erc20 代币,它们都运行良好....

标签: ethereum solidity smartcontracts remix private-network


【解决方案1】:

现在来结束这个问题! 我在 ropsten 测试网络上部署了我的合约,并且合约运行良好。 看来我的专用网络不支持从另一个合约调用。有兴趣的可以去看看。

【讨论】:

    猜你喜欢
    • 2018-02-27
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    相关资源
    最近更新 更多