-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient-example.js
More file actions
89 lines (76 loc) · 3.1 KB
/
client-example.js
File metadata and controls
89 lines (76 loc) · 3.1 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
const EventSource = require('eventsource');
const { Graph } = require('redis');
const { aggregateTransactions } = require('./services/common/aggregationService');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const address = 'TJGd9GErpVFSuyAhsi5MJ8bGXU6HzaMrWG';
const eventSource = new EventSource(`http://localhost:8000/api/trx/stream/transactions/${address}`);
// Initialize CSV writer
const csvWriter = createCsvWriter({
path: 'transactions_log.csv',
header: [
{ id: 'layer', title: 'LAYER' },
{ id: 'from_address', title: 'FROM_ADDRESS' },
{ id: 'to_address', title: 'TO_ADDRESS' },
{ id: 'value', title: 'VALUE' },
{ id: 'totalProcessed', title: 'TOTAL_PROCESSED' },
{ id: 'GraphData', title: 'GRAPH_DATA' },
{ id: 'aggregatedTransactions', title: 'AGGREGATED_TRANSACTIONS' },
]
});
eventSource.onmessage = function(event) {
const data = JSON.parse(event.data);
if (data.type === 'transactions') {
console.log(`Processing transactions for layer ${data.layerNumber}`);
console.log(JSON.stringify(data))
data.aggregateTransactions.forEach((transaction, index) => {
const logEntry = {
layer: data.layerNumber,
from_address: transaction.from_address,
to_address: transaction.to_address,
value: transaction.value / 100000000,
txid: transaction.txid,
totalProcessed: data.totalProcessed,
GraphData:JSON.stringify(data.graphData),
aggregatedTransactions:JSON.stringify(data.aggregatedTransactions)
};
console.log(data.graphData)
if (index === 0) {
console.log('First transaction in batch:', JSON.stringify(transaction, null, 2));
}
// Write the log entry to the CSV file
csvWriter.writeRecords([logEntry])
.then(() => console.log('Transaction log entry written to CSV file.'))
.catch(err => console.error('Error writing to CSV:', err));
});
} else if (data.type === 'info' || data.type === 'error') {
console.log(`${data.type.toUpperCase()}: ${data.message}`);
}
};
eventSource.onerror = function(error) {
console.error('EventSource failed:', error);
if (error.status) {
console.error('HTTP status code:', error.status);
}
if (error.message) {
console.error('Error message:', error.message);
}
eventSource.close();
};
eventSource.onopen = function() {
console.log("Connection to server opened.");
};
// Add a listener for the 'end' event type which should be sent by the server
eventSource.addEventListener('close', function() {
console.log('End of data stream.');
eventSource.close(); // Close the connection after receiving 'end' event
});
// const apiKey = '9788928a-bbdb-44b7-96d7-0ba1198200d1';
// const endpoint = 'https://apilist.tronscanapi.com/api/transfer/trx?address=TSTVYwFDp7SBfZk7Hrz3tucwQVASyJdwC7&start=0&limit=20&direction=0&reverse=true&fee=true&db_version=1&start_timestamp=&end_timestamp=';
// fetch(endpoint, {
// headers: {
// 'TRON-PRO-API-KEY': apiKey
// }
// })
// .then(response => response.json())
// .then(data => console.log(data))
// .catch(error => console.error(error));