繁体   English   中英

无法弄清楚为什么在处理事务时出现 VM 异常:恢复错误

[英]Cannot figure out why I am getting VM Exception while processing transaction: revert error

我是区块链编程的初学者,我无法理解为什么在尝试使用 Web3 和 Ganache 巩固 function 时出现错误。

 const PetList = require('./build/contracts/PetList.json') const fs = require('fs') const Web3 = require('web3') const abi = fs.readFileSync("build/contracts/PetList.json").toString().trim(); // Ganache Blockchain const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545")); web3.eth.net.isListening().then(() => console.log('Web3 is connected')).catch(e => console.log('Wow. Something went wrong')); var setUpContract = async function( ){ await web3.eth.net.getId().then((networkId) => { const networkData = PetList.networks[networkId] console.log("Setting up contract...") if(networkData){ return petList = new web3.eth.Contract(PetList.abi, networkData.address) } }) acc = await web3.eth.getAccounts() currentAccount = acc[0] console.log("Account: ", acc[0]) } async function start(){ await setUpContract() await addPet() } var addPet = async function(){ console.log(await getPetCount()) return results = await petList.methods.addPet('Zippo', 'Dog').send({from: currectAccount}) } start()
 pragma solidity ^0.5.0; contract PetList{ uint public petCount = 0; mapping (uint => Pet) public pets; struct Pet{ uint id; string name; string tag; } event PetAdded( uint id, string name, string tag ); function addPet(string memory _name, string memory _tag) public { require(bytes(_name).length > 0, "INVALID TAG"); require(bytes(_tag).length > 0, "INVALID NAME"); petCount++; pets[petCount] = Pet(petCount, _name, _tag); emit PetAdded(petCount, _name, _tag); } }

我还使用了没有任何参数的.send(),它给了我一个错误,说在给定选项和默认选项中都没有指定“发件人”地址。

要使 function 接收以太币,您必须有一个后备 function或添加payable 修饰符

因此,您可以将代码更改为以下

function addPet(string memory _name, string memory _tag) public payable

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM