0

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
Gene Lim
  • 1,068
  • 2
  • 14
  • 36

1 Answers1

1

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;
};
dr497
  • 474
  • 2
  • 3
  • Hey, thanks for the reply. I understand your suggestion in making sure that the transaction is valid But the reason for the forloop is because I am trying to get a list of transactions within the whole collections of NFTs, say 10000 NFTs in 1 collections. I am using getSignaturesForAddress to get the signatures of each NFTs and then getParsedTransactions(array of signatures), still no luck too. You could be right, RPC/network issue – Gene Lim Jul 02 '22 at 14:23
  • I've also updated my code to reflect the exact simulation I am doing. It is still returning null. Don't mind me asking, are you using QuickNode $9/month too? – Gene Lim Jul 02 '22 at 14:38
  • Thanks for providing more context. I am using an enterprise plan with Quicknode. Have you tried with another endpoint to check whether the issue might be on Quicknode? For instance `https://solana-api.projectserum.com`. Also you can make your code more efficient in terms of RPC requests by doing: `const txns = await connection.getTransactions(signatures2);` – dr497 Jul 02 '22 at 16:09
  • Hey thank you for the suggestion of getTransactions! This is much better than doing forloops. I've tried using projectserum and also genesysgo. Both having the same issue and i realized it is only affecting certain NFTs. I tried it again, it is working now. I think it could be the blockchain's issue in someway for that moment of time. – Gene Lim Jul 02 '22 at 23:25