Here is an article about the issue you are experiencing when parsing transactions on the Solana blockchain using web3.js:
Title:
Solana Web3.js: ParsedTransaction returns null even though logs are available
Introduction
As a developer building applications on the Solana blockchain, parsing transaction details is crucial to understanding program conditions and triggering custom logic. However, in this article, we will investigate an issue that arises when trying to access transactions parsed using web3.js on a given account.
The Issue: ParsedTransaction returns null
We have written a script that listens for Solana logs on a specific public key. The code is designed to parse transaction details whenever a program condition is met:
`javascript
const solana = require("solana-web3");
const connection = new solana.Connection();
connection.onLogs(publicKey, (result) => {
const parsedTransaction = result.parsedTransactions;
console.log(parsedTransaction);
});
parsedTransactions
After setting up the script and connecting to the Solana network, we expect to see the transaction details in the
array. However, when we log the output of this code, we notice that it is returning an empty object:
json
{
"error": null,
"info": null,
"status": null,
"result": {
"transactions": []
}
}
What went wrong?
In Solana, when you get transactions using connection.getEntry, the result is a
Web3Transactionobject. However, in this case we are not getting these transactions explicitly. Instead, we rely on the
parsedTransactionsarray provided by web3.js.
The problem arises because thegetEntrymethod does not guarantee that all transaction entries will be returned when using
result.parsedTransactions. In fact, if the program condition is met and there are no matching transactions,
result.parsedTransactionswill return an empty array (
[]). The code we wrote assumes that this array is always non-empty.
Solving the problem
To solve this problem, you can use a different approach to parse transaction details. One way to do this is to get all transactions and then filter out those that do not match your condition:
javascript
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's condition here
if (/ your condition /) {
returns true;
}
returns false;
});
console.log(matchingTransactions);
});
getEntries
Alternatively, you can use the
method to get all transaction entries and then create a mapping of the entry IDs to their corresponding Web3Transaction objects:
`javascriptconst solana = require("solana-web3");
const connection = new solana.Connection();
connection.onLogs(publicKey, (result) => {
const transactions = result.parsedTransactions;
const entryToTransactionMap = {};
for (const transaction transactions) {
const entryId = transaction.entryId.toString();
if (!entryToTransactionMap[entryId]) {
entryToTransactionMap[entryId] = {};
}
entryToTransactionMap[entryId][transaction.id.toString()] = transaction;
}
console.log(entryToTransactionMap);
});
By using one of these approaches, you should be able to parse transaction details even when the program condition is not met.