In my project I have a contractInteract function that interacts with a blockchain contract returning a promise. I add a "confirmation" event. This event logs "Confirmed!" every confirmation and once it reaches 5 confirmations it resolves. This is the function:
function contractInteract() {
return new Promise((resolve, reject) => {
var confirmationCounter = 0
const contractInstance = new web3.eth.Contract(contract.ABI, contract.address)
contractInstance.methods.contractFunction(0).send({
from: address,
gasPrice: "35000000000"
}).on('confirmation', () => {
confirmationCounter++
console.log("Confirmed!")
if(confirmationCounter == 5) {
console.log("Transaction confirmed 5 times.")
resolve()
}
}).on('error', function(error) {
reject(error)
})
})
}
I then call the aforementioned function with:
const init = async () => {
await contractInteract()
console.log("Finished")
}
The function executes correctly. Once it reaches 5 confirmations it first logs Transaction confirmed 5 times.
And then logs Finished
. Only issue is that the .on('confirmation')
event keeps on running, meaning that it keeps on printing Confirmed!
. I also tried returning the resolve() but it still continued running. How can I exit from the event? Thanks.