05 Feb Solana: Monitor deposit transaction
Tracking Deposited Transactions on Solana: A Solution for Rechecking Last Hour Deposits
As a developer building applications on the Solana blockchain, you’re likely interested in tracking deposited transactions. In this article, we’ll explore a solution that allows you to track transferred assets (such as SOL or USDT) and recheck deposits made within the last hour.
What is a Transfer on Solana?
Before diving into our solution, let’s quickly review what a transfer means on Solana:
- A transfer involves sending assets from one address to another.
- In this context, we’re interested in tracking transferred SOL (Solana) or USDT (US Dollar Token) assets.
Using Subscribe Events for New Transactions
To track new transactions, you can use the solana-program-subscriptions
library, which provides a simple way to subscribe to event streams on the Solana blockchain. Here’s an example of how you might set up a subscription to receive new transfers:
import { Subscriptions } from 'solana-program/subscriptions';
const appInfo = {
pubkey: '',
name: '',
version: ''
};
// Create a new subscription instance
const subscription = await subscriptions.createSubscription(appInfo);
// Subscribe to the "newTransfer" event
subscription.on('newTransfer', (data) => {
console.log(Received transfer data: ${JSON.stringify(data)}
);
});
// Call the createTransfer
function to create a new transfer
const transferData = {
// Your transaction data here
};
await appInfo.createTransfer(transferData);
Rechecking Deposits within the Last Hour
To recheck deposits made within the last hour, you’ll need to use a different approach. One solution is to use the solana-program-subscriptions
library’s getEvents
function, which allows you to fetch a specific number of events from a subscription in a single request. Here’s an example:
import { Subscriptions } from 'solana-program/subscriptions';
const appInfo = {
pubkey: '',
name: '',
version: ''
};
// Create a new subscription instance
const subscription = await subscriptions.createSubscription(appInfo);
// Get all events for the last hour (1 hour)
const recentEvents = await subscription.getEvents({
limit: 1000, // fetch up to 1000 events
});
// Filter out any non-recent transfer data
const recentTransfers = recentEvents.filter((event) => event.type === 'newTransfer');
console.log(recentTransfers);
Example Use Cases
Here are some example use cases for this solution:
- Web3.js
: You can use the
solana-program-subscriptions
library in your web application’s JavaScript file to fetch recent transactions.
const Sub = require('solana-program/subscriptions');
// Create a new subscription instance
const subscription = await Sub.createSubscription({
pubkey: 'YOUR_pubkey',
name: 'RECENT_TRANSFEETS',
version: '1.0.0',
});
// Fetch recent events
const recentEvents = await subscription.getEvents();
console.log(recentEvents);
- Node.js
: You can use a Node.js module like
solana-program-subscriptions
to fetch recent transactions.
const { Subscriptions } = require('solana-program/subscriptions');
// Create a new subscription instance
const subscription = await Subscriptions.createSubscription({
pubkey: 'YOUR_pubkey',
name: 'RECENT_TRANSFEETS',
version: '1.0.0'
});
// Fetch recent events
const recentEvents = await subscription.getEvents();
console.log(recentEvents);
These examples demonstrate how you can use the solana-program-subscriptions
library to track deposited transactions and recheck deposits made within the last hour on Solana. By following these steps, you’ll be able to build a robust application that provides real-time tracking of assets transferred between addresses.
No Comments