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)

            from: address,
            gasPrice: "35000000000"
        }).on('confirmation', () => {

            if(confirmationCounter == 5) {
                console.log("Transaction confirmed 5 times.")
        }).on('error', function(error) {

I then call the aforementioned function with:

const init = async () => {
    await contractInteract()

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.


I fixed the issue by assigning a variable to const swap = contractInstance.methods.contractFunction(0).send(...

And then in the if statement I run swap.removeAllListeners().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.