-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.js
393 lines (366 loc) · 13.1 KB
/
webserver.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
const Koa = require('koa');
const KoaRouter = require('koa-router');
const cors = require('@koa/cors');
const bodyParser = require('koa-bodyparser');
const mount = require('koa-mount');
const conf = require('ocore/conf.js');
const db = require('ocore/db.js');
const { getAssetID, assetsMetadata } = require('./assets');
function enrichData(rows, asset) {
for (let r of rows) {
if (asset !== false || r.asset || r.asset === null) {
const a = r.asset !== undefined ? r.asset : asset;
if (a === null)
r.decimals = 9;
else if (typeof a === 'string')
r.decimals = assetsMetadata[a] ? assetsMetadata[a].decimals : null;
else
throw Error(`bad asset ${a}, ${asset}`);
}
if (r.asset || r.asset === null) {
if (r.asset === null)
r.symbol = 'RECH';
else if (assetsMetadata[r.asset])
r.symbol = assetsMetadata[r.asset].name;
}
}
return rows;
}
const app = new Koa();
app.use(cors());
app.use(bodyParser());
const apiRouter = new KoaRouter();
/* POST /address => stats for one AA address
req body: {
"address": "IFFGFP32MYAQZCBXNGCO3ARF3AM6VTNA",
"asset": null, // optional, null for bytes, if not preset - returns values for all assets individually
"timeframe": "hourly" // "hourly" or "daily"
"from": 448531, // either hour or day (depending on timeframe) number since unix epoch
"to": 457593 // same
}
curl --header "Content-Type: application/json" \
--request POST \
--data '{"address":"IFFGFP32MYAQZCBXNGCO3ARF3AM6VTNA","timeframe":"hourly","from": 448531,"to":457600}' \
http://localhost:8080/api/v1/address
*/
apiRouter.all('/address', async ctx => {
let req = ctx.request.body;
if (!req || Object.keys(req).length === 0)
req = ctx.query;
const asset = "asset" in req ? getAssetID(req.asset) : false;
const timeframe = req.timeframe === "daily" ? "daily" : "hourly";
let sql = `SELECT
${timeframe == "hourly" ? "hour" : "day"} AS period,
address,
asset,
amount_in,
amount_out,
usd_amount_in,
usd_amount_out,
triggers_count,
bounced_count,
num_users
FROM aa_stats_${timeframe}
WHERE address=?
AND period BETWEEN ? AND ?`;
if (asset !== false) {
sql += ` AND asset IS ?`;
}
sql += ` ORDER BY period ASC`;
const rows = await db.query(sql, [req.address, +req.from, +req.to, ...(asset !== false ? [asset] : [])]);
ctx.body = enrichData(rows, asset);
});
/* POST /address/tvl => TVL over time for one AA address
req body: {
"address": "IFFGFP32MYAQZCBXNGCO3ARF3AM6VTNA",
"asset": null, // optional, null for bytes, if not preset - returns values for all assets
"from": 448531, // hour number since unix epoch
"to": 457593 // same
}
curl --header "Content-Type: application/json" \
--request POST \
--data '{"address":"IFFGFP32MYAQZCBXNGCO3ARF3AM6VTNA","from": 448531,"to":457600}' \
http://localhost:8080/api/v1/address/tvl
*/
apiRouter.all('/address/tvl', async ctx => {
let req = ctx.request.body;
if (!req || Object.keys(req).length === 0)
req = ctx.query;
const asset = "asset" in req ? getAssetID(req.asset) : false;
let sql = `SELECT
hour AS period,
address,
asset,
balance,
usd_balance
FROM aa_balances_hourly
WHERE address=?
AND "hour" BETWEEN ? AND ?`;
if (asset !== false) {
sql += ` AND asset IS ?`;
}
sql += ` ORDER BY period ASC`;
const rows = await db.query(sql, [req.address, +req.from, +req.to, ...(asset !== false ? [asset] : [])]);
ctx.body = enrichData(rows, asset);
});
/* POST /total/tvl => total TVL over time
req body: {
"asset": null, // optional, null for bytes, if not preset - returns values for all assets
"from": 448531, // hour number since unix epoch
"to": 457593 // same
}
curl --header "Content-Type: application/json" \
--request POST \
--data '{"from": 448531,"to":457600}' \
http://localhost:8080/api/v1/total/tvl
*/
apiRouter.all('/total/tvl', async ctx => {
let req = ctx.request.body;
if (!req || Object.keys(req).length === 0)
req = ctx.query;
const asset = "asset" in req ? getAssetID(req.asset) : false;
let sql = `SELECT
hour AS period,
${asset !== false ? 'SUM(balance) AS balance,' : ''}
SUM(usd_balance) AS usd_balance
FROM aa_balances_hourly
WHERE "hour" BETWEEN ? AND ?
${asset !== false ? 'AND asset IS ?' : ''}
GROUP BY period`;
const rows = await db.query(sql, [+req.from, +req.to, ...(asset !== false ? [asset] : [])]);
ctx.body = enrichData(rows, asset);
});
/* POST /total/activity => total activity in terms of usd_amount_in / usd_amount_out / num of txs / over time
req body: {
"timeframe": "hourly", // or "daily"
"asset": null, // optional, null for bytes, if not preset - returns values for all assets
"from": 448531, // hour or day number since unix epoch
"to": 457593 // same
}
curl --header "Content-Type: application/json" \
--request POST \
--data '{"from": 448531,"to":457600}' \
http://localhost:8080/api/v1/total/activity
*/
apiRouter.all('/total/activity', async ctx => {
let req = ctx.request.body;
if (!req || Object.keys(req).length === 0)
req = ctx.query;
const timeframe = req.timeframe === "daily" ? "daily" : "hourly";
const period = timeframe === "daily" ? "day" : "hour";
const asset = "asset" in req ? getAssetID(req.asset) : false;
let sql = `SELECT
${timeframe == "hourly" ? "hour" : "day"} AS period,
${asset !== false ? 'SUM(amount_in) AS amount_in,' : ''}
${asset !== false ? 'SUM(amount_out) AS amount_out,' : ''}
SUM(usd_amount_in) AS usd_amount_in,
SUM(usd_amount_out) AS usd_amount_out,
SUM(triggers_count) AS triggers_count,
SUM(bounced_count) AS bounced_count,
SUM(num_users) AS num_users
FROM aa_stats_${timeframe}
WHERE ${period} BETWEEN ? AND ?
${asset !== false ? 'AND asset IS ?' : ''}
GROUP BY ${period}`;
const rows = await db.query(sql, [+req.from, +req.to, ...(asset !== false ? [asset] : [])]);
ctx.body = enrichData(rows, asset);
});
/* POST /top/aa/tvl => Top AAs by TVL
req body: {
"asset": null, // optional, null for bytes, if not preset - return top TVL in USD
"period": 448531 // hour number since unix epoch, if omitted - returns last hour
}
curl --header "Content-Type: application/json" \
--request POST \
--data '{}' \
http://localhost:8080/api/v1/top/aa/tvl
*/
apiRouter.all('/top/aa/tvl', async ctx => {
let req = ctx.request.body;
if (!req || Object.keys(req).length === 0)
req = ctx.query;
const asset = "asset" in req ? getAssetID(req.asset) : false;
const hour = "period" in req ? req.period : Math.floor(Date.now() / 1000 / 60 / 60)-1;
let sql = (asset !== false)
? `SELECT
hour AS period,
address,
balance,
usd_balance
FROM aa_balances_hourly
WHERE period=? AND asset IS ?
ORDER BY usd_balance DESC`
: `SELECT
hour AS period,
address,
SUM(usd_balance) AS usd_balance
FROM aa_balances_hourly
WHERE period=?
GROUP BY address
ORDER BY usd_balance DESC`;
const rows = await db.query(sql, [hour, ...(asset !== false ? [asset] : [])]);
ctx.body = enrichData(rows, asset);
});
apiRouter.all('/top/aa/combined/:type', async ctx => {
let type = ctx.params['type'];
if (!["usd_amount_in", "usd_amount_out", "triggers_count", "num_users", "usd_balance"].includes(type))
ctx.throw(404, 'type is incorrect');
let req = ctx.request.body;
if (!req || Object.keys(req).length === 0)
req = ctx.query;
// activity
const timeframe = req.timeframe === "daily" ? "daily" : "hourly";
const period = timeframe === "daily" ? "day" : "hour";
const period_length = timeframe === "daily" ? 1000 * 3600 * 24 : 1000 * 3600;
const from = typeof req.from === 'number' ? req.from : Math.floor(Date.now() / period_length) - 1;
const to = typeof req.to === 'number' ? req.to : Math.floor(Date.now() / period_length) - 1;
const activity_sql = `SELECT
address,
SUM(usd_amount_in) AS usd_amount_in,
SUM(usd_amount_out) AS usd_amount_out,
SUM(triggers_count) AS triggers_count,
SUM(bounced_count) AS bounced_count,
SUM(num_users) AS num_users
FROM aa_stats_${timeframe}
WHERE ${period} BETWEEN ? AND ? AND (usd_amount_in>0 OR usd_amount_out>0)
GROUP BY address`
const activity_rows = await db.query(activity_sql, [from, to]);
const activeAddresses = {};
for (let { address } of activity_rows)
activeAddresses[address] = true;
// tvl
const hour = "period" in req ? req.period : Math.floor(Date.now() / 1000 / 60 / 60)-1;
const tvl_sql = `SELECT
address,
SUM(usd_balance) AS usd_balance
FROM aa_balances_hourly
WHERE hour=? AND usd_balance>0
GROUP BY address`;
const tvl_rows = await db.query(tvl_sql, [hour]);
const tvlsByAddress = {};
for (let { address, usd_balance } of tvl_rows)
tvlsByAddress[address] = usd_balance;
// combined rows
const rows = activity_rows;
for (let row of rows)
row.usd_balance = tvlsByAddress[row.address] || 0;
for (let row of tvl_rows)
if (!activeAddresses[row.address])
rows.push({ ...row, usd_amount_in: 0, usd_amount_out: 0, triggers_count: 0, bounced_count: 0, num_users: 0 });
const secondary_type = type === 'usd_balance' ? 'usd_amount_in' : 'usd_balance';
rows.sort((r1, r2) => r2[type] || r1[type] ? r2[type] - r1[type] : r2[secondary_type] - r1[secondary_type]);
if (req.limit) {
const limit = req.limit | 0;
if (rows.length > limit)
rows.length = limit;
}
ctx.body = rows;
});
/* POST /top/aa/(amount_in|amount_out|triggers_count|num_users) => Top AAs by usd_amount_in / usd_amount_out / num of txs / num of users
req body: {
"timeframe": "hourly", // or "daily"
"limit": "50", // top-N, default = 50
"asset": null, // required, null for bytes, otherwise asset id
"from": 448531 // hour or day number since unix epoch, if omitted - returns last hour
"to": 457689
}
curl --header "Content-Type: application/json" \
--request POST \
--data '{"asset": null, "timeframe": "hourly"}' \
http://localhost:8080/api/v1/top/aa/amount_in
*/
apiRouter.all('/top/aa/:type', async ctx => {
let type = ctx.params['type'];
if (!["usd_amount_in", "usd_amount_out", "triggers_count", "num_users"].includes(type))
ctx.throw(404, 'type is incorrect');
let req = ctx.request.body;
if (!req || Object.keys(req).length === 0)
req = ctx.query;
const timeframe = req.timeframe === "daily" ? "daily" : "hourly";
const period = timeframe === "daily" ? "day" : "hour";
const period_length = timeframe === "daily" ? 1000 * 3600 * 24 : 1000 * 3600;
const from = typeof req.from === 'number' ? req.from : Math.floor(Date.now() / period_length) - 1;
const to = typeof req.to === 'number' ? req.to : Math.floor(Date.now() / period_length) - 1;
const limit = req.limit|0 || 50;
const asset = "asset" in req ? getAssetID(req.asset) : false;
let sql = `SELECT
address,
${asset !== false ? 'SUM(amount_in) AS amount_in,' : ''}
${asset !== false ? 'SUM(amount_out) AS amount_out,' : ''}
SUM(usd_amount_in) AS usd_amount_in,
SUM(usd_amount_out) AS usd_amount_out,
SUM(triggers_count) AS triggers_count,
SUM(bounced_count) AS bounced_count,
SUM(num_users) AS num_users
FROM aa_stats_${timeframe}
WHERE ${period} BETWEEN ? AND ?
${asset !== false ? 'AND asset IS ?' : ''}
GROUP BY address ORDER BY ${type} DESC LIMIT ${limit}`
const rows = await db.query(sql, [from, to, ...(asset !== false ? [asset] : [])]);
ctx.body = enrichData(rows, asset);
});
/* POST /top/asset/tvl => Top assets by TVL
req body: {
"limit": "50", // top-N, default = 50
"period": 457673 // hour number since unix epoch, if omitted - returns last hour
}
curl --header "Content-Type: application/json" \
--request POST \
--data '{}' \
http://localhost:8080/api/v1/top/asset/tvl
*/
apiRouter.all('/top/asset/tvl', async ctx => {
let req = ctx.request.body;
if (!req || Object.keys(req).length === 0)
req = ctx.query;
const hour = "period" in req ? +req.period : Math.floor(Date.now() / 1000 / 60 / 60)-1;
const limit = req.limit|0 || 50;
let sql = `SELECT
hour AS period,
asset,
SUM(balance) AS total_balance,
SUM(usd_balance) AS total_usd_balance
FROM aa_balances_hourly
WHERE period=?
GROUP BY asset
ORDER BY total_usd_balance DESC LIMIT ${limit}`;
const rows = await db.query(sql, [hour]);
ctx.body = enrichData(rows, true);
});
/* POST /top/asset/volume => Top assets by volume (amount_in)
req body: {
"limit": "50", // top-N, default = 50
"period": 457673 // hour number since unix epoch, if omitted - returns last hour
}
curl --header "Content-Type: application/json" \
--request POST \
--data '{}' \
http://localhost:8080/api/v1/top/asset/amount_in
*/
apiRouter.all('/top/asset/amount_in', async ctx => {
let type = ctx.params['type'];
let req = ctx.request.body;
if (!req || Object.keys(req).length === 0)
req = ctx.query;
const hour = "period" in req ? +req.period : Math.floor(Date.now() / 1000 / 60 / 60)-1;
const limit = req.limit|0 || 50;
let sql = `SELECT
hour AS period,
asset,
SUM(amount_in) AS total_amount_in,
SUM(usd_amount_in) AS total_usd_amount_in
FROM aa_stats_hourly
WHERE period=?
GROUP BY asset
ORDER BY total_usd_amount_in DESC LIMIT ${limit}`;
const rows = await db.query(sql, [hour]);
ctx.body = enrichData(rows, true);
});
apiRouter.all('/assets', async ctx => {
ctx.body = assetsMetadata;
});
app.use(mount('/api/v1', apiRouter.routes()));
async function start() {
app.listen(conf.webserverPort, () => console.log('Web server started on port ' + conf.webserverPort));
}
exports.start = start;