Solana: Solana web3.js getParsedTransaction returns null

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:

const solana = require("solana-web3");

const connection = new solana.Connection();

connection.onLogs(publicKey, (result) => {

const parsedTransaction = result.parsedTransactions;

console.log(parsedTransaction);

});

After setting up the script and connecting to the Solana network, we expect to see the transaction details in the parsedTransactions array. However, when we log the output of this code, we notice that it is returning an empty object:

{

"error": null,

"info": null,

"status": null,

"result": {

"transactions": []

}

}

What went wrong?

In Solana, when you get transactions using connection.getEntry, the result is a Web3Transaction object. However, in this case, we are not getting these transactions explicitly. Instead, we rely on the parsedTransactions array provided by web3.js.

The problem arises because the getEntry method 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.parsedTransactions will return an empty array ([]). The code we wrote assumes that this array is always non-empty.

Problem Resolution

To work around 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:

const solana = require("solana-web3");

const connection = new solana.Connection();

connection.onLogs(publicKey, (result) => {

const parsedTransactions = result.parsedTransactions;

if (!parsedTransactions.length) return; // Ignore empty arrays

const matchingTransactions = parsedTransactions.filter((transaction) => {

// Implement your program's condition here

if (/ your condition /) {

return true;

}

return false;

});

console.log(matchingTransactions);

});

Alternatively, you can use the getEntries method to get all transaction entries and then create a mapping of the entry ids to their corresponding Web3Transaction objects:

const solana = require("solana-web3");

const connection = new solana.Connection();

connection.onLogs(publicKey, (result) => {

const transactions = result.parsedTransactions;

const entryToTransactionMap = {};

for (const transaction of transactions) {

const entryId = transaction.entryId.toString();

if (!entryToTransactionMap[entryId]) {

entryToTransactionMap[entryId] = {};

}

entryToTransactionMap[entryId][transaction.id.toString()] = transaction;

}

console.log(entryToTransactionMap);

});

If you use one of these approaches, you should be able to parse transaction details even when the program condition is not met.

Exit mobile version