我想通过node.js和web3.js调用某些Solidity的合约方法,但是它不起作用,我想知道为什么。
我正在关注这两个教程:https ://cryptozombies.io/en/lesson/6/chapter/9和http://demystifyblockchain.com/2018/06/29/getting-started-with-ethereum-blockchain-development /
我在我的项目中使用 truffle、ganache-cli、node.js、web3.js 和solidity。
该方法工作了一段时间,但由于某种未知原因不再有效。
我使用 node 来调用这个 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)