有些交易有效,有些则无效。处理事务时出现 UnhandledPromiseRejectionWarning 和 VM Exception

Some transactions work and some don't. UnhandledPromiseRejectionWarning and VM Exception while processing transaction

我想通过node.js和web3.js调用某个Solidity的合约方法,但是不行,想知道为什么。

我正在学习这两个教程:https://cryptozombies.io/en/lesson/6/chapter/9 and http://demystifyblockchain.com/2018/06/29/getting-started-with-ethereum-blockchain-development/

我在我的项目中使用了 truffle、ganache-cli、node.js、web3.js 和 solidity。

该方法曾经有用过一段时间,但由于某些未知原因不再有效。

我用节点调用这个JavaScript方法:

   async function main() {
    let mainAccount = await getMainAccount();
    const counter = new web3.eth.Contract(abi, contractAddress);
    counter.methods.setCooldownTime(mainAccount).send({from: mainAccount})
    .on('receipt', function (receipt) {
        if (receipt.status) {
            let events = Object.keys(receipt.events);
            if (events.length > 0) {
            let event = receipt.events[events[0]];
            console.log("event Name  ", event.event);
            let returnValues = event.returnValues;
            let returnedCount = returnValues.count;
            console.log("count ", returnedCount);
        }
        }
    })
    .on('error', console.error);
}

Solidity中应该会触发这段代码:

uint cooldownTime = 36 hours;
function setCooldownTime() public{
    cooldownTime = 1 hours;
}

这给了我一个错误,我不确定如何修复:

Error: Node error: {"message":"VM Exception while processing transaction: revert","code":-32000,"data":{"0xbf9f7df4bb490662840d3f5f3294cf96a00ba22ec3271c0eb1145989589f70c0":{"error":"revert","program_counter":278,"return":"0x"},"stack":"o: VM Exception while processing transaction: revert\n    at Function.o.fromResults (/home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:10:81931)\n    at /home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:47:121973\n    at /home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:47:102384\n    at p (/home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:47:102041)\n    at /home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:47:102094\n    at t.default (/home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:52:497368)\n    at /home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:47:103008\n    at w.n.emit (/home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:1211583)\n    at /home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:1850382\n    at /home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:1850405\n    at /home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:61:500011\n    at /home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:26124\n    at i (/home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:41179)\n    at /home/user/.nvm/versions/node/v12.3.0/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:21647\n    at processTicksAndRejections (internal/process/task_queues.js:82:9)","name":"o"}}
    at Function.validate (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/web3-providers/dist/web3-providers.cjs.js:114:18)
    at HttpProvider._callee$ (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/web3-providers/dist/web3-providers.cjs.js:710:61)
    at tryCatch (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/regenerator-runtime/runtime.js:45:40)
    at Generator.invoke [as _invoke] (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/regenerator-runtime/runtime.js:271:22)
    at Generator.prototype.<computed> [as next] (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/regenerator-runtime/runtime.js:97:21)
    at asyncGeneratorStep (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
    at _next (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
    at processTicksAndRejections (internal/process/task_queues.js:89:5) false 0

当我像前面提到的教程那样写作时建议:

counter.methods.setCooldownTime()... 

而不是:

counter.methods.setCooldownTime(mainAccount)...

也会报错:

(node:5006) UnhandledPromiseRejectionWarning: Error: types/values length mismatch (count={"types":1,"values":0}, value={"types":[{"name":"newOwner","type":"address"}],"values":[]}, version=4.0.28)
    at Object.throwError (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/ethers/errors.js:76:17)
    at AbiCoder.encode (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/ethers/utils/abi-coder.js:922:20)
    at AbiCoder.encodeParameters (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/web3-eth-abi/dist/web3-eth-abi.cjs.js:45:34)
    at MethodEncoder.encode (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/web3-eth-contract/dist/web3-eth-contract.cjs.js:143:45)
    at MethodsProxy.createMethod (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/web3-eth-contract/dist/web3-eth-contract.cjs.js:556:57)
    at MethodsProxy.executeMethod (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/web3-eth-contract/dist/web3-eth-contract.cjs.js:534:23)
    at Function.ContractMethod.send (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/node_modules/web3-eth-contract/dist/web3-eth-contract.cjs.js:507:27)
    at main (/mnt/c/Users/Sloth/Solidity/firstBlockchainApp/interaction/interaction.js:20:39)
    at processTicksAndRejections (internal/process/task_queues.js:89:5)

问题出在我的 JavaScript 代码的前 4 行。由于未知原因,abi 中的数据被弄乱了。可以通过手动写入 abi 数据来解决该问题。

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
const metadata = require('../tokens/slavetoken.sol');
const abi = metadata.abi;