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
CodePudding user response:
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;
};