Solana: Solana web3.js getParsedTransaction returning null
Here is an article about the issue encountered while parsing transactions on the Solana blockchain using web3.js:
Title: Solana Web3.js: ParsedTransaction returns Null despite logs being available
Introduction
For a developer building applications on the Solana blockchain, parsing transaction details is essential to understanding program conditions and triggering user logic. However, in this article, we will look at an issue that occurs when trying to access transactions parsed via web3.js in a specific account.
Issue: ParsedTransaction returns Null
We have written a script that listens to Solana logs for a specific public key. The code is designed to parse transaction details whenever a program condition is executed:
const solana = require("solana-web3");
const connection = new solana.Connection();
connection.onLogs(publicKey, (result) => {
const parsedTransaction = result.parsedTransactions;
console.log(parsedTransaction);
});
After configuring the script and connecting to the Solana network, we expect to see transaction details in the parsedTransactions
array. However, when we log the output of this code, we see that it returns an empty object:
{
"error": null,
"information": null,
"status": null,
"result": {
"transactions": []
}
}
What went wrong?
In Solana, when retrieving transactions using connection.getEntry
, the result is a Web3Transaction
object. However, in this case we do not explicitly retrieve these transactions. Instead, we rely on the parsedTransactions
array provided by web3.js.
The problem arises because the getEntry
method does not guarantee that all transaction records will be returned when using result.parsedTransactions
. In fact, if the program condition is met and there are no matching transactions, result.parsedTransactions
will return an empty array ([]
). The code we wrote assumes that this array is never empty.
Problem Solution
To solve this problem, you can use a different approach to parsing transaction details. One way to do this is to extract all transactions and then filter out the ones that do not match your condition:
const solana = require("solana-web3");
const connection = new solana.Connection();
connection.onLogs(publicKey, (result) => {
const parsedTransactions = result.parsedTransactions;
if (!parsedTransactions.length) returns; // Ignore empty arrays
const matchingTransactions = parsedTransactions.filter((transaction) => {
// Implement your program condition here
if (/ your condition /) {
return true;
}
return false;
});
console.log(matching transactions);
});
Alternatively, you can use the getEntries
method to retrieve all transaction records and then map the record IDs to the corresponding Web3Transaction objects:
const solana = require("solana-web3");
const connection = new solana.Connection();
connection.onLogs(publicKey, (result) => {
constant transaction = result.transactionsanalyzed;
const entryToTransactionMap = {};
for (const transaction transaction) {
const entryId = transaction.entryId.toString();
if (!entryToTransactionMap[entryId]) {
entryToTransactionMap[entryId][transaction.id.toString()] = transaction;
}
console.log(entryToTransactionMap);
});
Using one of these approaches, it will be possible to analyze the details of transactions even if the program condition is not satisfied.