简体   繁体   中英

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

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

您还可以尝试将连接的承诺级别设置为“最终确定”

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM