0

I make a contract method call using .send() method:

    contractInstance.methods.pay(itemId).send({from: defaultAccount, value: web3.utils.toWei(String(price*1.0000001)), gas: '1000000'},
                                              function(error, transactionHash) {
        if(error)
            alert("You tried to pay below the price or payment failure! " + err);
    })

How to be notified when the transaction fully happened (cannot be canceled by clicking Cancel in MetaMask anymore)? Is it called a confirmation?

If I just add .on('confirmation'):

    contractInstance.methods.pay(itemId).send({from: defaultAccount, value: web3.utils.toWei(String(price*1.0000001)), gas: '1000000'},
                                              function(error, transactionHash) {
        if(error)
            alert("You tried to pay below the price or payment failure! " + err);
    })
        .on('confirmation', function(receipt){ showFilesWithMessage(); });

it does nothing (showFilesWithMessage() is not called).

web3.js 1.2.9.


Addition:

When I "follow the example on the official docs":

    contractInstance.methods.pay(itemId).send({from: defaultAccount, value: web3.utils.toWei(String(price*1.0000001)), gas: '1000000'})
        .on('transactionHash', function(hash){
            console.log('transactionHash');
        })
        .on('receipt', function(receipt){
            console.log('receipt');
        })
        .on('confirmation', function(confirmationNumber, receipt) {
            console.log('confirmation', confirmationNumber);
            showFilesWithMessage();
        })
        .on('error', console.error); // If a out of gas error, the second parameter is the receipt.

the confirmation event is not issued either! Instead I get only transactionHash event.

How to get the confirmation event for .send()?

  • Is this the right answer to my question? ethereum.stackexchange.com/a/67234/36438 – porton Jul 7 at 12:18
  • 1
    If I just add... - how exactly do you add it? Please share a complete piece of code, not fragments of it. Otherwise, it is impossible to tell you what exactly you are doing wrong. – goodvibration Jul 7 at 12:29
  • @goodvibration Done. – porton Jul 7 at 12:32
  • Well you can't use both .on and a callback function passed as one of the parameter. If you wanna catch all 4 events (transaction hash, receipt, error and 24 confirmations), then get rid of the fallback and follow the example on the official docs. – goodvibration Jul 7 at 12:43
  • @goodvibration But which of the events I need to catch to know when the transaction has become irreversible (not anymore cancelable by the user)? – porton Jul 7 at 12:55

Your Answer

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

Browse other questions tagged or ask your own question.