-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
290 lines (259 loc) · 7.3 KB
/
index.js
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
// Necessary imports
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const moment = require('moment');
// Express Initialization
const app = express();
const port = 3000;
// Static declarations
const dbURL = 'mongodb://localhost:27017/';
const dbNAME = 'libra-local';
// TODO: Simplify this process so that latest + all are in one query simply dependent on passed request parameters.
// Latest transactions
app.get('/latest', (req, res) => {
// Mongo retrieval and output
MongoClient.connect(dbURL, { useNewUrlParser : true }, function(error, mng) {
if (error) {
throw error;
} else {
mng.db(dbNAME).collection("transactions").find({}).sort({
_id: -1 // Reverse order retrieval
}).limit(15).toArray(function (error, result) {
if (error) {
throw error;
} else {
cleanedResult = JSON.stringify(result);
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader('Content-Type', 'application/json');
res.end(cleanedResult);
}
mng.close();
});
}
});
});
// All transactions TODO: Setup content encoding with gzip to optimize on payload size.
app.get('/all', (req, res) => {
// Mongo retrieval and output
MongoClient.connect(dbURL, { useNewUrlParser : true }, function(error, mng) {
if (error) {
throw error;
} else {
mng.db(dbNAME).collection("transactions").find({}).sort({
_id: -1 // Reverse order retrieval
}).limit(0).toArray(function (error, result) {
if (error) {
throw error;
} else {
cleanedResult = JSON.stringify(result);
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader('Content-Type', 'application/json');
res.end(cleanedResult);
}
mng.close();
});
}
});
});
app.get('/statistics/:query', (req, res) => {
let queryValue = req.params.query.toString();
let currentTime = moment().unix();
let time24prev = moment().unix() - 86400;
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader('Content-Type', 'application/json');
MongoClient.connect(dbURL, { useNewUrlParser : true }, function (error, mng) {
if (error) {
throw error;
} else {
mng.db(dbNAME).collection("addresses").find({
}, function (error, result) {
if (error) {
throw error;
} else {
mng.close();
console.log(currentTime);
result = JSON.stringify(result);
res.end(result);
}
});
}
});
/*if (queryValue === "uniqueAddresses") {
res.end('woo!');
} else {
res.end('Your query has an error. Please try again.');
}*/
});
app.post('/query/', (req, res) => {
let queryValue = '';
req.on('data', chunk => {
queryValue += chunk.toString();
});
req.on('end', () => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader('Content-Type', 'application/json');
if (queryValue.length == 64) {
MongoClient.connect(dbURL, { useNewUrlParser: true}, function (error, mng) {
if (error) {
throw error;
} else {
mng.db(dbNAME).collection("addresses").findOne({
_id: queryValue
}, function (error, result) {
if (error) {
throw error;
} else {
let balance = 0;
let txs = [];
let itemsProcessed = 0;
if (typeof (result.received) != 'undefined') {
result.received.forEach(tx => {
mng.db(dbNAME).collection("transactions").findOne({
_id: Number(tx)
}, function (error, resultTwo) {
if (error) {
throw error;
} else {
balance += resultTwo.value;
txs.push(resultTwo);
itemsProcessed++;
if (itemsProcessed == result.received.length) {
processRequest();
}
}
});
});
} else {
processRequest();
}
/* TODO: Rewrite copied function to optimize */
function processRequest() {
let itemsProcessed = 0;
if (typeof (result.sent) != 'undefined') {
result.sent.forEach(tx => {
mng.db(dbNAME).collection("transactions").findOne({
_id: Number(tx)
}, function (err, result2) {
if (err) throw err;
balance -= result2.value;
txs.push(result2);
itemsProcessed++;
if (itemsProcessed == result.sent.length) {
prepareData();
}
});
});
} else {
prepareData();
}
}
function prepareData() {
txs.sort(function (a, b) {
var aNum = parseInt(a._id);
var bNum = parseInt(b._id);
return bNum - aNum;
});
returnData();
}
function returnData() {
mng.close();
res.setHeader("Access-Control-Allow-Origin", "*");
res.end(JSON.stringify({
balance: balance,
txs: txs
}));
}
}
});
}
});
} else if (queryValue.length < 32) {
MongoClient.connect(dbURL, { useNewUrlParser : true }, function (error, mng) {
if (error) {
throw error;
} else {
mng.db(dbNAME).collection("transactions").findOne({
_id: Number(queryValue)
}, function (error, result) {
if (error) {
throw error;
} else {
mng.close();
result = JSON.stringify(result);
res.end(result);
}
});
}
});
} else {
res.send('Your query has an error. Please try again.');
}
});
});
app.get('/statistics', (req, res) => {
// Mongo retrival initiation
MongoClient.connect(dbURL, { useNewUrlParser : true }, function(error, mng) {
if (error) {
throw error;
} else {
const requestTime = moment().unix(); // Time of query
const requestTime24 = moment().unix() - 86400; // Time 24hrs before query
const requestTime48 = moment().unix() - 172800; // Time 24hrs before query
// Sent Transactions (24H)
let currentTransactions = 0;
let historicTransactions = 0;
let historic48Transactions = 0;
let ratio = 0;
mng.db(dbNAME).collection("transactions").find({
}).toArray(function (error, result) {
if (error) {
throw error;
} else {
mng.close();
// Get currentTransactions
currentTransactions = result.length;
console.log(currentTransactions);
// Get currentTransactions - 24h
for (let i = 0; i < result.length; i++) {
if (result[i].time < requestTime24) {
historicTransactions++;
}
}
// Get currentTransactions - 48h
for (let i = 0; i < result.length; i++) {
if (result[i].time < requestTime48) {
historic48Transactions++;
}
}
// Get ratio
ratio = (currentTransactions - historicTransactions) / (historicTransactions - historic48Transactions);
console.log(currentTransactions, historicTransactions, historic48Transactions, ratio);
res.send('test');
}
});
/*mng.db(dbNAME).collection("transactions").find({
"timestamp": {
$lt: new Date(),
$gte: new Date(new Date().setDate(new Date().getDate()-1))
}
}).toArray(function (error, result) {
if (error) {
throw error;
} else {
count24 = result.length;
console.log(count24);
}
mng.close();
});*/
// Latest version
// Libra Volume (24H)
// Average TX Fee (24H)
// P2P / Mint Transactions
// Average TPS
// Unique Addresses
// Total Transactions
// Total Libra Supply
}
});
});
app.listen(port);