How to find out if a transaction is finalized on Solana

Viewed 185

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);
})();
3 Answers

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

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

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);
})();
Related