'sendTransaction v sendAndConfirmTransaction

So in my code I am doing the following in my backend and wondering which I should use?

const sig = await web3.sendAndConfirmTransaction(connection, createMetadataTx, [mint_authority], {
      skipPreflight: false
    })
const sig = await connection.sendTransaction(
      createMetadataTx,
      [mint_authority],
      {
        skipPreflight: false,
      }
    );

I am guessing that the sendAndConfirmTransaction takes a little longer but confirms that the trx has been accepted for processsing, but not necessarily finalized?

And what bearing does my connection 'commitment' have on this?:

const connection = new Connection(tokenType.cluster, "processed");


Solution 1:[1]

sendTransaction just broadcasts the transaction and does not wait for it to confirm on the network. You can then use confirmTransaction separately to check it the transaction was confirmed on the network.

sendAndConfirmTransaction does both and doesn't return until the transaction is either confirmed on the network or dropped.

You would:

  • Use sendTransaction if you are ok with send and forget or manual confirm later
  • Use sendAndConfirmTransaction if you want to know the transaction status before any further processing

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jacob Creech