-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
41 lines (36 loc) · 920 Bytes
/
app.js
File metadata and controls
41 lines (36 loc) · 920 Bytes
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
const Koa = require('koa');
const helmet = require('koa-helmet');
const compress = require('koa-compress');
const _ = require('lodash');
const uniswap = require('./libs/uniswap');
const Cache = require('node-cache');
const cache = new Cache({ stdTTL: 4320 });
const app = new Koa();
app
.use(helmet())
.use(compress())
.use(async (ctx) => {
const uniswapv2 = (skip = 0) => {
return uniswap(skip).then((pairs) => {
if (pairs.length) {
return Promise.all([pairs, uniswap(skip + 1)]);
}
return pairs;
})
};
const data = cache.get('data') || await uniswapv2();
// cache data
if (!cache.ttl('data')) {
cache.set('data', data);
}
// sort response
const response = _
.chain(data)
.flatten()
.orderBy('returnOnInvestment', 'desc')
.value();
ctx.body = {
data: response,
};
})
.listen(3000);