solana web3 confirmTransaction @deprecated 使用 TransactionConfirmationConfig 示例

solana web3 confirmTransaction @deprecated using a TransactionConfirmationConfig example

使用此代码 VS 显示弃用警告:

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

有人可以给我一个新语法的例子吗?

新方法签名看起来像

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

所以现在需要 BlockheightBasedTransactionConfirmationStrategy

而不是早先的 TransactionSignature

哪里

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

因此你需要的是

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