Home > Enterprise >  solana web3 confirmTransaction @deprecated using a TransactionConfirmationConfig example
solana web3 confirmTransaction @deprecated using a TransactionConfirmationConfig example

Time:05-23

With this code VS show a deprecated warning:

(method) Connection.confirmTransaction(strategy: string, commitment?: Commitment): Promise<RpcResponseAndContext> ( 1 overload) @deprecated — Instead, call confirmTransaction using a TransactionConfirmationConfig

The signature '(strategy: string, commitment?: Commitment): Promise<RpcResponseAndContext>' of 'connection.confirmTransaction' is deprecated

const airDropSol = async () => {
  try {
    const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
    const airdropSignature = await connection.requestAirdrop(
      publicKey,
      2 * LAMPORTS_PER_SOL
    );
    await connection.confirmTransaction(airdropSignature);
  } catch (error) {
    console.error(error);
  }
};

Could anyone make me an example with the new syntax, please?

CodePudding user response:

The new method signature looks like

confirmTransaction(
  strategy: BlockheightBasedTransactionConfirmationStrategy,
  commitment?: Commitment,
): Promise<RpcResponseAndContext<SignatureResult>>;

So instead of earlier TransactionSignature it now expects BlockheightBasedTransactionConfirmationStrategy

where

type BlockheightBasedTransactionConfirmationStrategy = {
    signature: string;
} & Readonly<{
    blockhash: string;
    lastValidBlockHeight: number;
}>

Hence what you need is

  const airDropSol = async () => {
    try {
      const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
      const airdropSignature = await connection.requestAirdrop(
        keypair.publicKey,
        2 * LAMPORTS_PER_SOL
      );

      const latestBlockHash = await connection.getLatestBlockhash();

      await connection.confirmTransaction({
        blockhash: latestBlockHash.blockhash,
        lastValidBlockHeight: latestBlockHash.lastValidBlockHeight,
        signature: airdropSignature,
      });
    } catch (error) {
      console.error(error);
    }
  };
  • Related