Berry Blue
Berry Blue

Reputation: 16532

How to find out if a transaction is finalized on Solana

How do you get the current status of a transaction on Solana with the Solana Javascript API (@solana/web3.js)?

https://solscan.io/tx/4fwgX16WDwYj5hZ2t5xEHz6UUnuaTovJpMeoWWEBvuA7z1baf1qX1BW2EGZVR9ChSyJZ8akeLX6EDTadFcEcSTdy

This is what I've tried, but it only seems to work for recent transactions.

const { Connection, clusterApiUrl } = require("@solana/web3.js");

(async () => {
    const connection = new Connection(clusterApiUrl('mainnet-beta'));
    const status = await connection.getSignatureStatus("4fwgX16WDwYj5hZ2t5xEHz6UUnuaTovJpMeoWWEBvuA7z1baf1qX1BW2EGZVR9ChSyJZ8akeLX6EDTadFcEcSTdy");
    console.log(status);
})();

Upvotes: 3

Views: 3995

Answers (3)

Jon C
Jon C

Reputation: 8472

You can also use the confirmTransaction function:

const { Connection, clusterApiUrl } = require("@solana/web3.js");

(async () => {
    const connection = new Connection(clusterApiUrl('mainnet-beta'));
    const result = await connection.confirmTransaction({
        signature: "4fwgX16WDwYj5hZ2t5xEHz6UUnuaTovJpMeoWWEBvuA7z1baf1qX1BW2EGZVR9ChSyJZ8akeLX6EDTadFcEcSTdy"
    },
    'finalized');
    console.log(result);
})();

EDIT: this function signature has been deprecated in web3.js and has been moved to this package: https://www.npmjs.com/package/@solana-developers/helpers the connection needs to be passed in as the first parameter:

 await confirmTransaction(connection, transaction);

if you made the transaction yourself you should pass the same blockhash into the confirmation and construction.

const { blockhash, lastValidBlockheight } = await connection.getLatestBlockhash();

const tx = new Transaction({ blockhash, lastValidBlockheight });
const signature = await connection.sendTransaction(tx);

const confirmationStrategy: BlockheightBasedTransactionConfirmationStrategy = {
  blockhash,
  lastValidBlockheight,
  signature,
};
await connection.confirmTransaction(confirmationStrategy);

details: https://github.com/solana-labs/solana-web3.js/pull/2733/files#r1611723082

reason:

Confirming a transaction using a blockhash other than the one that was actually used in the transaction will yield incorrect information, such as ‘your blockhash is now invalid’ when it's either not or it's been invalid for quite some time.

https://github.com/solana-labs/solana-web3.js/issues/2067#issuecomment-1947353265

Upvotes: 1

Kristian Quirapas
Kristian Quirapas

Reputation: 21

You could also try setting your connection's commitment level to "finalized"

new Connection(clusterApiUrl('mainnet-beta'), 'finalized');

Upvotes: 0

dr497
dr497

Reputation: 554

Try adding searchTransactionHistory: true to the SignatureStatusConfig

const getConfirmation = async (connection: Connection, tx: string) => {
  const result = await connection.getSignatureStatus(tx, {
    searchTransactionHistory: true,
  });
  return result.value?.confirmationStatus;
};

https://docs.solana.com/developing/clients/jsonrpc-api#getsignaturestatuses

Upvotes: 3

Related Questions