This code snippet retrieves and displays the transaction logs from the Solana blockchain using a given transaction signature. It leverages the Solana Web3.js library to connect to the Solana mainnet-beta cluster and fetches the parsed transaction data.
- Node.js installed on your machine.
- NPM (Node Package Manager) installed.
- Internet connection to connect to the Solana mainnet-beta cluster.
-
Clone the repository or copy the code snippet:
git clone <repository-url> cd <repository-directory>
-
Install the required dependencies:
npm install @solana/web3.js
-
Replace the placeholder transaction signature with your own:
Edit the
transactionSignature
variable in the code snippet:const transactionSignature = 'CmMd4o7H5vaC5FeuzUT1AW1rAZHhQWB6sfedUz54XhbbqGRVV5thjppjVxNK5pDzddSSf56HQXHy1MyB72rPthm';
-
Run the script:
node index.js
The script performs the following steps:
-
Imports the Solana Web3.js library:
const solanaWeb3 = require('@solana/web3.js');
-
Connects to the Solana mainnet-beta cluster:
const connection = new solanaWeb3.Connection( solanaWeb3.clusterApiUrl('mainnet-beta'), 'confirmed', );
-
Fetches the confirmed transaction using the provided signature:
async function getTransactionLogs(signature) { try { const transaction = await connection.getParsedTransaction(signature, { maxSupportedTransactionVersion: 0 }); if (transaction) { console.log('Transaction:', transaction); const inners = transaction.meta.innerInstructions; console.log('Transaction inners:', inners); } else { console.log('Transaction not found'); } } catch (error) { console.error('Error fetching transaction:', error); } } getTransactionLogs(transactionSignature);
-
Logs the transaction details and inner instructions to the console:
If the transaction is found, the script logs the entire transaction object and the inner instructions to the console. Otherwise, it prints a message indicating that the transaction was not found.
The script outputs the transaction details and inner instructions in the following format:
Transaction: {
blockTime: 1720469977,
meta: { ... },
slot: 276446073,
transaction: { ... },
version: 0
}
Transaction inners: [
{ index: 4, instructions: [Array] },
{ index: 5, instructions: [Array] },
{ index: 6, instructions: [Array] },
{ index: 7, instructions: [Array] },
{ index: 8, instructions: [Array] },
{ index: 9, instructions: [Array] }
]
- Ensure you have a valid transaction signature to fetch the transaction logs.
- The
maxSupportedTransactionVersion
parameter is set to 0 to ensure compatibility with older transactions.