-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstats.ts
More file actions
193 lines (159 loc) · 7.36 KB
/
stats.ts
File metadata and controls
193 lines (159 loc) · 7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* Stats-related examples for the Solana Tracker API
*/
import { Client, processEventsAsync } from '@solana-tracker/data-api'
import { handleError } from './utils';
// Initialize the API client with your API key
const client = new Client({
apiKey: 'YOUR_API_KEY',
});
/**
* Example 1: Get detailed token stats across various time intervals
*/
export async function getTokenDetailedStats(tokenAddress: string) {
try {
const stats = await client.getTokenStats(tokenAddress);
console.log(`\n=== Detailed Stats for ${tokenAddress} ===`);
// Display stats for different timeframes
const timeframes = ['1m', '5m', '15m', '1h', '4h', '24h'];
timeframes.forEach(timeframe => {
if (stats[timeframe]) {
const tfStats = stats[timeframe];
console.log(`\n${timeframe.toUpperCase()} Stats:`);
console.log(`Price: $${tfStats.price.toFixed(6)}`);
console.log(`Price Change: ${tfStats.priceChangePercentage.toFixed(2)}%`);
console.log(`Transactions: ${tfStats.transactions} (${tfStats.buys} buys, ${tfStats.sells} sells)`);
console.log(`Unique Wallets: ${tfStats.wallets}`);
console.log(`Volume: Buy $${tfStats.volume.buys.toFixed(2)}, Sell $${tfStats.volume.sells.toFixed(2)}, Total $${tfStats.volume.total.toFixed(2)}`);
console.log(`Unique Buyers: ${tfStats.buyers}`);
console.log(`Unique Sellers: ${tfStats.sellers}`);
}
});
// Extract most recent timeframe for detailed analysis
const mostRecentTimeframe = stats['24h'] || stats['4h'] || stats['1h'] || stats['15m'] || stats['5m'] || stats['1m'];
if (mostRecentTimeframe) {
console.log('\nDetailed Analysis:');
console.log(`Buy/Sell Ratio: ${(mostRecentTimeframe.buys / (mostRecentTimeframe.sells || 1)).toFixed(2)}`);
console.log(`Average Buy Size: $${(mostRecentTimeframe.volume.buys / (mostRecentTimeframe.buys || 1)).toFixed(2)}`);
console.log(`Average Sell Size: $${(mostRecentTimeframe.volume.sells / (mostRecentTimeframe.sells || 1)).toFixed(2)}`);
const buyPercentage = (mostRecentTimeframe.buys / mostRecentTimeframe.transactions) * 100;
const sellPercentage = (mostRecentTimeframe.sells / mostRecentTimeframe.transactions) * 100;
console.log(`Buy Percentage: ${buyPercentage.toFixed(2)}%`);
console.log(`Sell Percentage: ${sellPercentage.toFixed(2)}%`);
const uniqueTraderPercentage = (mostRecentTimeframe.wallets / mostRecentTimeframe.transactions) * 100;
console.log(`Unique Trader Percentage: ${uniqueTraderPercentage.toFixed(2)}%`);
}
return stats;
} catch (error) {
handleError(error);
return null;
}
}
/**
* Example 2: Get detailed pool-specific stats
*/
export async function getPoolDetailedStats(tokenAddress: string, poolAddress: string) {
try {
const stats = await client.getPoolStats(tokenAddress, poolAddress);
console.log(`\n=== Detailed Pool Stats for ${tokenAddress} (Pool: ${poolAddress}) ===`);
// Display stats for different timeframes
const timeframes = ['1m', '5m', '15m', '1h', '4h', '24h'];
timeframes.forEach(timeframe => {
if (stats[timeframe]) {
const tfStats = stats[timeframe];
console.log(`\n${timeframe.toUpperCase()} Stats:`);
console.log(`Price: $${tfStats.price.toFixed(6)}`);
console.log(`Price Change: ${tfStats.priceChangePercentage.toFixed(2)}%`);
console.log(`Transactions: ${tfStats.transactions} (${tfStats.buys} buys, ${tfStats.sells} sells)`);
console.log(`Volume: $${tfStats.volume.total.toFixed(2)}`);
}
});
return stats;
} catch (error) {
handleError(error);
return null;
}
}
/**
* Example 3: Compare stats across different time periods
*/
export async function compareTokenStats(tokenAddress: string) {
try {
const stats = await client.getTokenStats(tokenAddress);
console.log(`\n=== Comparative Analysis for ${tokenAddress} ===`);
// Extract stats from different timeframes
const timeframes = ['1h', '4h', '24h'];
const availableStats = timeframes.filter(tf => stats[tf]).map(tf => ({
timeframe: tf,
stats: stats[tf]
}));
if (availableStats.length < 2) {
console.log('Not enough timeframes available for comparison');
return stats;
}
console.log('\nPrice Movement:');
availableStats.forEach(({ timeframe, stats: tfStats }) => {
console.log(`${timeframe.toUpperCase()}: ${tfStats.priceChangePercentage > 0 ? '+' : ''}${tfStats.priceChangePercentage.toFixed(2)}%`);
});
console.log('\nVolume Comparison:');
availableStats.forEach(({ timeframe, stats: tfStats }) => {
console.log(`${timeframe.toUpperCase()}: $${tfStats.volume.total.toFixed(2)}`);
});
console.log('\nTransaction Count:');
availableStats.forEach(({ timeframe, stats: tfStats }) => {
console.log(`${timeframe.toUpperCase()}: ${tfStats.transactions} (${tfStats.buys} buys, ${tfStats.sells} sells)`);
});
console.log('\nUnique Wallet Count:');
availableStats.forEach(({ timeframe, stats: tfStats }) => {
console.log(`${timeframe.toUpperCase()}: ${tfStats.wallets}`);
});
// Calculate trends
if (availableStats.length >= 2) {
const shortTerm = availableStats[0].stats;
const longTerm = availableStats[availableStats.length - 1].stats;
console.log('\nTrend Analysis:');
// Volume trend
const volumeTrend = (shortTerm.volume.total / (longTerm.volume.total / getTimeframeHours(availableStats[availableStats.length - 1].timeframe) * getTimeframeHours(availableStats[0].timeframe)) - 1) * 100;
console.log(`Volume Trend: ${volumeTrend > 0 ? '+' : ''}${volumeTrend.toFixed(2)}% (relative to timeframe)`);
// Transaction trend
const txTrend = (shortTerm.transactions / (longTerm.transactions / getTimeframeHours(availableStats[availableStats.length - 1].timeframe) * getTimeframeHours(availableStats[0].timeframe)) - 1) * 100;
console.log(`Transaction Trend: ${txTrend > 0 ? '+' : ''}${txTrend.toFixed(2)}% (relative to timeframe)`);
// Buy/Sell ratio trend
const shortTermRatio = shortTerm.buys / (shortTerm.sells || 1);
const longTermRatio = longTerm.buys / (longTerm.sells || 1);
const ratioDiff = ((shortTermRatio / longTermRatio) - 1) * 100;
console.log(`Buy/Sell Ratio Trend: ${ratioDiff > 0 ? '+' : ''}${ratioDiff.toFixed(2)}%`);
}
return stats;
} catch (error) {
handleError(error);
return null;
}
}
/**
* Example 3: Get token events for live updates.
*/
export async function getTokenEvents() {
const events = await client.getEvents('6p6xgHyF7AeE6TZkSmFsko444wqoP15icUSqi2jfGiPN');
console.log(`\n=== Token Events ===`);
console.log(events)
// In real life applications you store events and add new trades from Datastream to it. Then run processEventsAsync to get live statistics
console.log(`\nProcessing events...`);
const stats = await processEventsAsync(events);
console.log(stats['24h'])
}
/**
* Helper function to convert timeframe to hours
*/
function getTimeframeHours(timeframe: string): number {
switch (timeframe) {
case '1m': return 1/60;
case '5m': return 5/60;
case '15m': return 15/60;
case '30m': return 30/60;
case '1h': return 1;
case '4h': return 4;
case '24h': return 24;
default: return 1;
}
}