Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

145
Vistas
web3.eth.contracts.method await function never returns

I'm working on a project to interact with a smart contract in ethereum. Everything was working fine, the transactions was working properly and so on... but I realized that when I tried to run some code after an specifically await function, that code never ran and I really don't know why. Others contract methods that I invoked is working well and the code is executed normally.

The code:

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


}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It's likely that is because the transaction wasn't mined within 750 seconds and .send threw an error, but wasn't caught. You can prove this by by wrapping it with a try/catch block and inspect the error.

  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);
  }

A better way is to subscribe to either receipt, transactionHash or confirmation event, which is exposed by send.

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

In your case it would be

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

Either of these answers https://ethereum.stackexchange.com/a/58936/6814 and https://ethereum.stackexchange.com/a/59114/6814 are correct.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda