处理事务时获取 VM 异常:还原错误

时间:2021-04-15 13:55:08

标签: solidity web3 truffle contract

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

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"
  }
}

你能帮帮我吗?我是新来的。

1 个答案:

答案 0 :(得分:1)

web3 不同,Truffle 不使用 sorted_test_dict = dict(sorted(test_dict.items(), key=lambda kv: kv[1]['fruit'])) 关键字。调用失败,因为它试图访问合同的(不存在的)methods 属性。

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

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

有关详细信息,请参阅 docs

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

.call()
function getIsVoting() public view returns (bool) { // added `view`
    return isVoting;
}