Solana Web3 confirmTransaction已过时,使用TransactionConfirmationConfig示例。

19

使用这段代码,VS会显示一个废弃警告:

(方法) Connection.confirmTransaction(strategy: string, commitment?: Commitment): Promise<RpcResponseAndContext> (+1重载) @deprecated — 现在请使用TransactionConfirmationConfig调用confirmTransaction

'connection.confirmTransaction'的签名'(strategy: string, commitment?: Commitment): Promise<RpcResponseAndContext>'已过时

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

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


嘿,如果你还没有这样做,请承诺加入Solana Stackexchange。area51.stackexchange.com/proposals/126615/solana 你可以大大帮助我们达到目标! - Jacob Creech
不幸的是,看起来 文档仍然使用已弃用的方法 - mikemaccana
1个回答

34

新的方法签名看起来像这样

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

所以,现在它期望的是BlockheightBasedTransactionConfirmationStrategy而不是之前的TransactionSignature

其中

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

3
嘿,如果你还没有这样做,请加入Solana Stackexchange。area51.stackexchange.com/proposals/126615/solana 你可以大大帮助我们实现目标! - Jacob Creech

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接