-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
290 lines (247 loc) · 10.7 KB
/
client.js
File metadata and controls
290 lines (247 loc) · 10.7 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
const { Client } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
const { getIntent } = require('./intent.js');
const GraphService = require('./services/graphService.js');
const SubstreamsService = require('./services/substreamsService.js');
const client = new Client();
const graphService = new GraphService();
const substreamsService = new SubstreamsService();
client.on('qr', (qr) => {
qrcode.generate(qr, { small: true });
});
client.on('ready', () => {
console.log('Client is ready!');
});
client.on('message', async (msg) => {
try {
const intentResponse = await getIntent(msg.body);
console.log('Intent response:', intentResponse);
// Clean the response by removing markdown code blocks if present
let cleanResponse = intentResponse.trim();
if (cleanResponse.startsWith('```json')) {
cleanResponse = cleanResponse.replace(/^```json\s*/, '').replace(/\s*```$/, '');
} else if (cleanResponse.startsWith('```')) {
cleanResponse = cleanResponse.replace(/^```\s*/, '').replace(/\s*```$/, '');
}
// Parse the JSON response and handle accordingly
const intent = JSON.parse(cleanResponse);
let responseMessage = '';
// Handle different intents
switch (intent.intent) {
case 'create-wallet':
case 'inr-transac':
case 'swap-and-send-on-chain':
case 'swap-and-send-cross-chain':
case 'other-misc':
case 'other-trash':
responseMessage = intent.message;
break;
case 'eth-tranfer':
case 'erc-20-transfer':
responseMessage = `Processing ${intent.intent}...\nTo: ${intent.to}\nAmount: ${intent.amount}`;
break;
case 'transaction-history':
try {
const network = intent.network || 'ethereum';
const limit = intent.limit || 50;
if (intent.targetAddress) {
// Get transactions between two addresses
const result = await graphService.getTransactionHistory(
intent.userAddress,
intent.targetAddress,
network,
limit
);
responseMessage = formatTransactionHistoryResponse(result);
} else {
// Get user's complete transaction history
const result = await graphService.getUserTransactionHistory(
intent.userAddress,
network,
limit
);
responseMessage = formatTransactionHistoryResponse(result);
}
} catch (error) {
responseMessage = `Error fetching transaction history: ${error.message}`;
}
break;
case 'token-analytics':
try {
const network = intent.network || 'ethereum';
const timeframe = intent.timeframe || '7d';
const result = await graphService.getTokenAnalytics(
intent.tokenAddress,
network,
timeframe
);
responseMessage = formatTokenAnalyticsResponse(result);
} catch (error) {
responseMessage = `Error fetching token analytics: ${error.message}`;
}
break;
case 'network-stats':
try {
const network = intent.network || 'ethereum';
const result = await graphService.getNetworkStatistics(network);
responseMessage = formatNetworkStatsResponse(result);
} catch (error) {
responseMessage = `Error fetching network statistics: ${error.message}`;
}
break;
case 'realtime-transactions':
try {
const network = intent.network || 'ethereum';
const limit = intent.limit || 20;
const result = await substreamsService.getRealtimeTransactions(
intent.userAddress,
network,
limit
);
responseMessage = formatRealtimeTransactionsResponse(result);
} catch (error) {
responseMessage = `Error fetching real-time transactions: ${error.message}`;
}
break;
case 'defi-analytics':
try {
const network = intent.network || 'ethereum';
const result = await substreamsService.getRealtimeDeFiAnalytics(
intent.protocolAddress,
network
);
responseMessage = formatDeFiAnalyticsResponse(result);
} catch (error) {
responseMessage = `Error fetching DeFi analytics: ${error.message}`;
}
break;
default:
responseMessage = intent.message || 'Unknown intent. Please try again.';
}
if (responseMessage) {
console.log('📱 Complete WhatsApp Response:');
console.log('=' .repeat(50));
console.log(responseMessage);
console.log('=' .repeat(50));
// msg.reply(responseMessage); // Commented out for testing
}
} catch (error) {
console.error('Error processing message:', error);
// msg.reply('Sorry, I encountered an error processing your request. Please try again.');
}
});
// Response formatting functions
function formatTransactionHistoryResponse(result) {
if (!result.transactions || result.transactions.length === 0) {
let response = result.message;
if (result.note) {
response += `\n\n⚠️ ${result.note}`;
}
return response;
}
let response = `${result.message}\n\n`;
response += `Total Value: $${result.totalValueUSD}\n\n`;
result.transactions.slice(0, 10).forEach((tx, index) => {
response += `${index + 1}. ${tx.type} ${tx.token0.symbol}/${tx.token1.symbol}\n`;
response += ` Date: ${tx.date} ${tx.time}\n`;
response += ` Value: $${tx.usdValue}\n`;
response += ` TX: ${tx.txHash.substring(0, 10)}...\n\n`;
});
if (result.transactions.length > 10) {
response += `... and ${result.transactions.length - 10} more transactions`;
}
if (result.note) {
response += `\n\n⚠️ ${result.note}`;
}
return response;
}
function formatTokenAnalyticsResponse(result) {
if (!result.analytics) {
return result.message;
}
const analytics = result.analytics;
let response = `${result.message}\n\n`;
response += `📊 Token Analytics:\n`;
response += `Symbol: ${analytics.symbol}\n`;
response += `Name: ${analytics.name}\n`;
response += `Total Supply: ${analytics.totalSupply}\n`;
response += `Volume (24h): $${analytics.volume24h}\n`;
response += `Volume (7d): $${analytics.volume7d}\n`;
response += `Transactions: ${analytics.txCount}\n`;
response += `TVL: $${analytics.totalValueLockedUSD}\n`;
response += `Price (ETH): ${analytics.priceInETH} ETH\n`;
// Add additional fields if available from alternative APIs
if (analytics.priceUSD) {
response += `Price (USD): $${analytics.priceUSD}\n`;
}
if (analytics.marketCap) {
response += `Market Cap: $${analytics.marketCap}\n`;
}
if (analytics.priceChange24h) {
response += `24h Change: ${analytics.priceChange24h}\n`;
}
if (analytics.note) {
response += `\n⚠️ ${analytics.note}\n`;
}
return response;
}
function formatNetworkStatsResponse(result) {
if (!result.statistics) {
return result.message;
}
const stats = result.statistics;
let response = `${result.message}\n\n`;
response += `🌐 Network Statistics:\n`;
response += `Total Volume: $${stats.totalVolumeUSD}\n`;
response += `Total Value Locked: $${stats.totalValueLockedUSD}\n`;
response += `Total Transactions: ${stats.totalTransactions}\n`;
response += `Total Pairs: ${stats.totalPairs}\n\n`;
if (stats.topTokens && stats.topTokens.length > 0) {
response += `🏆 Top Tokens:\n`;
stats.topTokens.slice(0, 5).forEach((token, index) => {
response += `${index + 1}. ${token.symbol} - Vol: $${token.volumeUSD}\n`;
});
}
if (stats.note) {
response += `\n⚠️ ${stats.note}`;
}
return response;
}
function formatRealtimeTransactionsResponse(result) {
if (!result.transactions || result.transactions.length === 0) {
return result.message;
}
let response = `${result.message}\n\n`;
response += `Total Value: ${result.totalValueETH} ETH\n\n`;
result.transactions.slice(0, 10).forEach((tx, index) => {
response += `${index + 1}. ${tx.from.substring(0, 6)}... → ${tx.to.substring(0, 6)}...\n`;
response += ` Value: ${tx.value} ETH\n`;
response += ` Date: ${tx.date} ${tx.time}\n`;
response += ` Status: ${tx.status}\n`;
response += ` TX: ${tx.hash.substring(0, 10)}...\n\n`;
});
if (result.transactions.length > 10) {
response += `... and ${result.transactions.length - 10} more transactions`;
}
return response;
}
function formatDeFiAnalyticsResponse(result) {
if (!result.analytics) {
return result.message;
}
const analytics = result.analytics;
let response = `${result.message}\n\n`;
response += `🏦 DeFi Protocol Analytics:\n`;
response += `Name: ${analytics.name}\n`;
response += `Type: ${analytics.type}\n`;
response += `TVL: $${analytics.totalValueLocked}\n`;
response += `Volume (24h): $${analytics.volume24h}\n`;
response += `Volume (7d): $${analytics.volume7d}\n`;
response += `Users (24h): ${analytics.users24h}\n`;
response += `Users (7d): ${analytics.users7d}\n`;
response += `Transactions (24h): ${analytics.transactions24h}\n`;
response += `Fees (24h): $${analytics.fees24h}\n`;
response += `Last Updated: ${analytics.lastUpdated}\n`;
return response;
}
client.initialize();