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