VM out of Gas Exception 试图调用属于其他合约的函数

VM out of Gas Exception trying to call a function belonging to an other contract

我正在尝试从不同的合约调用一个函数,但将 运行 保持在 VM 耗尽 gas 异常中。

我正在使用 Oraclize 提供的 IDE 来测试以下代码。

pragma solidity ^0.4.22;

contract ContractA {
    ContractB contractB;

    constructor() public {
        contractB = new ContractB();
    }

    function saySomething() external returns(string) {
        return contractB.retunsAString();
    }
}

contract ContractB {
    function retunsAString() public pure returns(string) {
        return "Hello to you all!";
    }
}

如果我尝试使函数 saySomething() 成为视图,则在编译时会发生此错误。

我在询问之前尝试搜索但找不到 post 解释它(至少不是我理解的方式)。

为什么会发生这种情况,有什么方法可以解决它,让代码按照我的预期运行吗?

您的代码没问题(只需在 saySomething 函数中添加 view)。 所以,首先,从以太坊转到官方 IDE - http://remix.ethereum.org

右上角转到 Run 选项卡,然后选择:环境 - JavaScript VM(如果你有 metamask - 注入 web3)。粘贴此代码并按 deploy

pragma solidity ^0.5.1;

contract ContractA {
    ContractB contractB;

    constructor() public {
        contractB = new ContractB();
    }

    function saySomething() external view returns(string memory) {
        return contractB.retunsAString();
    }
}

contract ContractB {
    function retunsAString() public pure returns(string memory) {
        return "Hello to you all!";
    }
}