首页 > 解决方案 > web3.eth.contracts.method 等待函数永远不会返回

问题描述

我正在开发一个与以太坊中的智能合约进行交互的项目。一切工作正常,事务工作正常等等......但我意识到,当我尝试在一个专门的等待函数之后运行一些代码时,该代码从未运行过,我真的不知道为什么。我调用的其他合约方法运行良好,代码正常执行。

编码:

onSubmit = async (event) => {
  event.preventDefault()
  console.log("Submitting the form...")
  var day = new Date(Date.now()).getDate()
  var month = new Date(Date.now()).getMonth() + 1
  var year = new Date(Date.now()).getFullYear()
  var today = new Date(year, month -1, 1)
  var todayTime = today.getTime()

  const ipfsadded = await ipfs.add({path:this.state.fileName, content:this.state.buffer})
  var ipfsHash = ipfsadded.cid.toString()
  this.setState({ipfsHash: ipfsHash})
  console.log('hash:', ipfsHash, " / name =", this.state.fileName )

  console.log("inserindo na blockchain..")

  const accounts = await web3.eth.getAccounts()
  //ERROR HERE!!
  //THIS FUNCTION IS EXECUTED, THE TRANSACTION IS CONFIRMED IN METAMASK BUT AFTER THAT NOTHING HAPPENS
  var result = await contract.methods.add(ipfsHash, this.state.fileName, this.state.fileType, todayTime).send({from:accounts[0], gas:300000});
  console.log("resultado =", result) //IS NEVER EXECUTED EVEN WITH TRANSACTION OK
  console.log("File submitted on blockchain!") //IS NEVER EXECUTED


}

标签: javascriptasync-awaitethereumweb3

解决方案


这很可能是因为交易没有在 750 秒内被挖掘并.send引发错误,但没有被捕获。您可以通过用 try/catch 块包装它并检查错误来证明这一点。

  try {
    var result = await contract.methods.add(ipfsHash, this.state.fileName, this.state.fileType, todayTime).send({from:accounts[0], gas:300000});
  } catch(err) {
    console.error(err);
  }

更好的方法是订阅由 公开receipttransactionHashconfirmation事件send

  contract.methods.add(ipfsHash, this.state.fileName, this.state.fileType, todayTime)
    .send({from:accounts[0], gas:300000})
    .on('transactionHash', resolve)
    .on('error', reject);

在你的情况下,它会是

onSubmit = async (event) => {
  event.preventDefault()
  console.log("Submitting the form...")
  var day = new Date(Date.now()).getDate()
  var month = new Date(Date.now()).getMonth() + 1
  var year = new Date(Date.now()).getFullYear()
  var today = new Date(year, month -1, 1)
  var todayTime = today.getTime()

  const ipfsadded = await ipfs.add({path:this.state.fileName, content:this.state.buffer})
  var ipfsHash = ipfsadded.cid.toString()
  this.setState({ipfsHash: ipfsHash})
  console.log('hash:', ipfsHash, " / name =", this.state.fileName )

  console.log("inserindo na blockchain..")

  const accounts = await web3.eth.getAccounts()
  //ERROR HERE!!
  //THIS FUNCTION IS EXECUTED, THE TRANSACTION IS CONFIRMED IN METAMASK BUT AFTER THAT NOTHING HAPPENS
  const promise = new Promise()
  contract.methods.add(ipfsHash, this.state.fileName, this.state.fileType, todayTime)
  .send({from:accounts[0], gas:300000})
  .on('receipt', (receipt) => {
    console.log('this should be executed now')
    console.log("resultado =", receipt)
    console.log("File submitted on blockchain!")
  })
  .on('error', console.error)
}

这些答案中的任何一个https://ethereum.stackexchange.com/a/58936/6814https://ethereum.stackexchange.com/a/59114/6814都是正确的。


推荐阅读