15

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?

Arjun
  • 3,248
  • 18
  • 35
user3887366
  • 2,226
  • 4
  • 28
  • 41
  • Hey. If you haven't already done so, please commit to the Solana Stackexchange. area51.stackexchange.com/proposals/126615/solana You could greatly help us reach our goal! – Jacob Creech Jun 14 '22 at 21:55
  • Sadly it looks like [the docs still use the deprecated method too](https://solanacookbook.com/references/local-development.html#getting-test-sol). – mikemaccana Jun 15 '22 at 17:30

1 Answers1

30

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);
    }
  };
Arjun
  • 3,248
  • 18
  • 35
  • 3
    Hey. If you haven't already done so, please commit to the Solana Stackexchange. area51.stackexchange.com/proposals/126615/solana You could greatly help us reach our goal! – Jacob Creech Jun 14 '22 at 21:55