solana web3.js getParsedTransaction有时返回null

发布于 2025-02-12 13:11:36 字数 914 浏览 0 评论 0原文

我正在尝试使用NFT签名中的任何一个签名ID获取事务值。对于测试,我使用了相同的签名并循环100次,以确保它是具有现有值的有效签名

。由于某种原因,它有时在循环中返回零。

我正在使用QuickNode的RPC。 (每月9美元)

我是否缺少引起此问题的东西?

代码

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

输出

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

I am trying to get transaction values using signature id from any of the NFT's signature. 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

I did console.log on the index of the for loop. For some reason, it is returning null sometimes within the loop.

I am using Quicknode's RPC. ($9/month)

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪是无色的血 2025-02-19 13:11:36

我无法复制该问题(也使用Quiknode),我怀疑这可能是RPC/网络问题。

但是,在getParsedTransaction上循环100次可能不是验证签名有效性的最佳方法。相反,您可以使用getignaturestatus并验证交易是否具有cresence status 最终确定的,并且errnull

例如:

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

如果您不仅要验证签名是否有效,还可以提取解析的交易详细信息,您可以做类似的事情:

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

I cannot reproduce the issue (also using Quiknode), I suspect it's probably an RPC/network issue.

However, looping 100 times on getParsedTransaction is probably not the best way to verify the validity of a signature. Instead you can use getSignatureStatus and verify that the transaction has a confirmationStatus of finalized and that err is null.

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;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文