Skip to main content
added 443 characters in body
Source Link
Fićo
  • 21
  • 2

You can also do it like this, by specifying number of block confirmations you'd like to wait for before proceeding. Note: You'll have to first instantiate account and target contract with web3. (https://web3js.readthedocs.io/en/v1.2.11/callbacks-promises-events.html):

    const signedTxencoded = await accounttokenContract.signTransactionmethods.approve(txrouterAddr, amountToSend);.encodeABI()
    console.log("Sendingconst transaction...");
tx = {
    web3.eth.sendSignedTransaction(signedTx.rawTransaction)  gas: 100000,
      .once('transactionHash'to: tokenAddr, 
 function (hash) {   data: encoded
    }
     const signedTx = await account.signTransaction(tx);
    console.log("Transaction"Sending hash:transaction...", hash); 

    const targetConfirmations = })3;

    await new .oncePromise('receipt'(resolve, function (receiptreject) => {
      const promiEvent console= web3.logeth.sendSignedTransaction("Transaction receipt received:", receiptsignedTx.rawTransaction); 

      })promiEvent
        .on('confirmation', function (confNumber, receipt) => {
          console.log("Confirmation number:", confNumber);
          if (confNumber ===>= 5targetConfirmations) { // Wait for 
 5 confirmations
          console.log("Received`Received 5${targetConfirmations} confirmations. Transaction is confirmed."`);
            promiEvent.off('confirmation'); // Remove all confirmation handlers
            resolve(receipt); // Resolve the promise after the desired confirmations
          }
        })
        .on('error', function (error) => {
          console.error("Error:", error);
      }    reject(error)
 ; // Reject the promise if .then(functionthere's (receipt)an {error
     console.log("Tx successful")
  });
    });

You can also do it like this (https://web3js.readthedocs.io/en/v1.2.11/callbacks-promises-events.html):

    const signedTx = await account.signTransaction(tx);
    console.log("Sending transaction...");

    web3.eth.sendSignedTransaction(signedTx.rawTransaction)
      .once('transactionHash', function (hash) {
        console.log("Transaction hash:", hash);
      })
      .once('receipt', function (receipt) {
        console.log("Transaction receipt received:", receipt);
      })
      .on('confirmation', function (confNumber, receipt) {
        console.log("Confirmation number:", confNumber);
        if (confNumber === 5) { // Wait for 5 confirmations
          console.log("Received 5 confirmations. Transaction is confirmed.");
        }
      })
      .on('error', function (error) {
        console.error("Error:", error);
      })
       .then(function (receipt) {
     console.log("Tx successful")
      })

You can also do it like this, by specifying number of block confirmations you'd like to wait for before proceeding. Note: You'll have to first instantiate account and target contract with web3. (https://web3js.readthedocs.io/en/v1.2.11/callbacks-promises-events.html):

    const encoded = await tokenContract.methods.approve(routerAddr, amountToSend).encodeABI()
    const tx = {
      gas: 100000,
      to: tokenAddr, 
      data: encoded
    }
     const signedTx = await account.signTransaction(tx);
    console.log("Sending transaction..."); 

    const targetConfirmations = 3;

    await new Promise((resolve, reject) => {
      const promiEvent = web3.eth.sendSignedTransaction(signedTx.rawTransaction); 

      promiEvent
        .on('confirmation', (confNumber, receipt) => {
          console.log("Confirmation number:", confNumber);
          if (confNumber >= targetConfirmations) { 
            console.log(`Received ${targetConfirmations} confirmations. Transaction is confirmed.`);
            promiEvent.off('confirmation'); // Remove all confirmation handlers
            resolve(receipt); // Resolve the promise after the desired confirmations
          }
        })
        .on('error', error => {
          console.error("Error:", error);
          reject(error); // Reject the promise if there's an error
        });
    });
Source Link
Fićo
  • 21
  • 2

You can also do it like this (https://web3js.readthedocs.io/en/v1.2.11/callbacks-promises-events.html):

    const signedTx = await account.signTransaction(tx);
    console.log("Sending transaction...");

    web3.eth.sendSignedTransaction(signedTx.rawTransaction)
      .once('transactionHash', function (hash) {
        console.log("Transaction hash:", hash);
      })
      .once('receipt', function (receipt) {
        console.log("Transaction receipt received:", receipt);
      })
      .on('confirmation', function (confNumber, receipt) {
        console.log("Confirmation number:", confNumber);
        if (confNumber === 5) { // Wait for 5 confirmations
          console.log("Received 5 confirmations. Transaction is confirmed.");
        }
      })
      .on('error', function (error) {
        console.error("Error:", error);
      })
      .then(function (receipt) {
     console.log("Tx successful")
      })