简体   繁体   English

Solana Web3.js getParsedTransaction 有时会返回 null

[英]Solana Web3.js getParsedTransaction sometimes return null

I am trying to get transaction values using signature id from any of the NFT's signature.我正在尝试使用来自任何 NFT 签名的签名 id 获取交易值。 For a test, I used the same signature and loop it 100 times to make sure that it is a valid signature with existing value对于测试,我使用了相同的签名并将其循环 100 次以确保它是具有现有值的有效签名

I did console.log on the index of the for loop.我在 for 循环的索引上做了 console.log。 For some reason, it is returning null sometimes within the loop.出于某种原因,它有时会在循环内返回 null。

I am using Quicknode's RPC.我正在使用 Quicknode 的 RPC。 ($9/month) (9 美元/月)

Am I missing something that caused this issue?我错过了导致这个问题的东西吗?

Code代码

async function test() {
    for (let i = 0; i < 100; i++) {
        signatures = await connection.getSignaturesForAddress(new PublicKey('BUPzNBDy3gVRRz1AqyzCDSnp15uAxh5j61dEj5GLLfx6'));
        let signatures2 = signatures.map(({ signature }) => signature)
        for (let a = 0; a < signatures2.length; a++) {
            let txns = await connection.getParsedTransaction(signatures2[a], 'finalized')
            if (!txns) {
                console.log('null')
            } else {
                console.log('ok')

            }
        }

    }
}

exports.get_collection_volume = (req, res) => {
    test()
}

Output Output

0
1
2
3
null
null
6
7
null
null
null
null
12
null
14
null
16

I cannot reproduce the issue (also using Quiknode), I suspect it's probably an RPC/network issue.我无法重现该问题(也使用 Quiknode),我怀疑这可能是 RPC/网络问题。

However, looping 100 times on getParsedTransaction is probably not the best way to verify the validity of a signature.但是,在getParsedTransaction上循环 100 次可能不是验证签名有效性的最佳方式。 Instead you can use getSignatureStatus and verify that the transaction has a confirmationStatus of finalized and that err is null .相反,您可以使用getSignatureStatus并验证交易的confirmationStatus状态为finalized并且errnull

For instance:例如:

const isValidSignature = async (connection: Connection, sig: string) => {
  const status = await connection.getSignatureStatus(sig, {
    searchTransactionHistory: true,
  });
  return (
    status.value?.err === null &&
    status.value?.confirmationStatus === "finalized"
  );
};

If you don't just want to verify that the signature is valid but also extract parsed transaction details you can do something like:如果您不仅要验证签名是否有效,还要提取已解析的交易详细信息,您可以执行以下操作:

const getParsedTx = async (connection: Connection, sig: string) => {
  const parsed = await connection.getParsedTransaction(sig, "finalized");
  if (!parsed || parsed?.meta?.err !== null) {
    throw new Error("Invalid signature");
  }
  return parsed;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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