【问题标题】:Getting VM Exception while processing transaction: revert error处理事务时出现 VM 异常:还原错误
【发布时间】:2021-07-10 13:43:22
【问题描述】:

我是区块链新手。我想调用一个函数来从合约中读取一个变量。 我的合同

bool public isVoting = false;
function getIsVoting() public returns (bool) {
        return isVoting;
    }

在客户端,我这样打电话

const isVoting = async () => {
        const _isVoting = await ElectionInstance.methods
            .getIsVoting()
            .call()
            .then(console.log);
    };

然后我得到了错误但不知道为什么:

{
  "message": "VM Exception while processing transaction: revert",
  "code": -32000,
  "data": {
    "0xdbe5e039374fdc83fe873f5e55d91f05ec5d19e2e3c88351130c3f3672644e08": {
      "error": "revert",
      "program_counter": 130,
      "return": "0x"
    },
    "stack": "RuntimeError: VM Exception while processing transaction: revert\n    at Function.RuntimeError.fromResults (/tmp/.mount_ganachnMw5dG/resources/static/node/node_modules/ganache-core/lib/utils/runtimeerror.js:94:13)\n    at /tmp/.mount_ganachnMw5dG/resources/static/node/node_modules/ganache-core/lib/blockchain_double.js:568:26",
    "name": "RuntimeError"
  }
}

你能帮我吗,请。我是新人。

【问题讨论】:

    标签: solidity web3 truffle contract


    【解决方案1】:

    web3 不同,Truffle 不使用methods 关键字。调用失败,因为它试图访问合约的(不存在的)methods 属性。

    如果您想使用链接的call(),您应该使用方法名称作为属性(不带括号)。

    您还结合了await 和回调函数then,这可能会导致一些意外结果(但不会导致事务恢复)。

    请参阅docs 了解更多信息。

    您可以选择将 Solidity 函数标记为 view,因为它不会将任何数据写入存储。这也将允许您将其用作 ElectionInstance.getIsVoting() 函数,而无需在 Truffle 中使用 .call()

    function getIsVoting() public view returns (bool) { // added `view`
        return isVoting;
    }
    
    const _isVoting = await ElectionInstance
        .getIsVoting // no parenthesis
        .call();
        // no `then`
    

    【讨论】:

      猜你喜欢
      • 2021-11-23
      • 2022-08-08
      • 1970-01-01
      • 2022-08-11
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      相关资源
      最近更新 更多