-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathfront.js
312 lines (249 loc) · 7.47 KB
/
front.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
'use strict'
import http from 'http'
import os from 'os'
import path from 'path'
import express from 'express'
import expressWs from 'express-ws'
import compression from 'compression'
import fetch from 'node-fetch'
import { promises as fs } from 'fs'
import { readFileSync } from 'fs'
import rateLimit from 'express-rate-limit'
import { MongoClient } from 'mongodb'
import { createClient } from 'redis'
/********************************
DexPairs.xyz
*********************************/
/* Dorian Bayart */
/* 2021 */
/********************************/
/*
* Frontend server
*
* Fetch JSON from backend
* Expose some public URLs
*/
const BACKEND_URL = 'http://127.0.0.1:3000'
const dir_home = os.homedir()
/*
*
* Mongo Configuration
*
*/
const MONGO_URL = 'mongodb://localhost:27017'
const MONGO_CLIENT = new MongoClient(MONGO_URL)
const DN_NAME = 'DexPairs'
let collections = {}
/* Redis */
const redis = createClient({
url: 'redis://localhost:6666'
})
let statistics = {
latests: [],
latests_nb: 250,
}
const limiter = rateLimit({
windowMs: 10*1000, // 10 seconds
max: process.env.NODE_ENV === 'production' ? 50 : 100
})
/* Const used for charts */
const INTERVAL_15M = '15m'
const INTERVAL_4H = '4h'
const INTERVAL_1D = '1d'
const INTERVAL_1W = '1w'
// Prepare Mongo collections
async function prepareCollections() {
// if(MONGO_CLIENT) {
// await MONGO_CLIENT.close()
// }
if(!MONGO_CLIENT) {
await MONGO_CLIENT.connect()
}
const db = MONGO_CLIENT.db(DN_NAME)
collections.coingecko = db.collection('coingecko')
//collections.bnbChainPancakeSwapSimple = db.collection('bnb-chain_pancakeswap_simple')
//collections.bnbChainPancakeSwapList = db.collection('bnb-chain_pancakeswap_list')
//collections.gnosisSimple = db.collection('gnosis_simple')
//collections.gnosisList = db.collection('gnosis_list')
console.log(collections)
setTimeout(prepareCollections, 60 * 60 * 1000)
//await redis.quit()
await redis.connect()
}
/* MAIN */
setTimeout(prepareCollections, 750)
/* server */
const port = process.env.PORT || 3001
const app = express()
app.use(compression())
app.use(limiter)
app.get('/', (req, res) => res.sendFile('/index.html', { root: '.' }))
app.get('/charts', (req, res) => res.sendFile('/charts.html', { root: '.' }))
app.get('/wallet', (req, res) => res.sendFile('/wallet.html', { root: '.' }))
app.get('/news', (req, res) => res.sendFile('/news.html', { root: '.' }))
app.get('/statistics', (req, res) => res.sendFile('/statistics.html', { root: '.' }))
app.get('/donate', (req, res) => res.sendFile('/donate.html', { root: '.' }))
app.get('/feed.atom', (req, res) => res.sendFile('/feed.atom', { root: '.' }))
app.get('/service-worker.js', (req, res) => res.sendFile('/service-worker.js', { root: '.' }))
app.use('/img', express.static('img'))
app.use('/news/', express.static('news/'))
app.use('/public', express.static('public'))
app.get('/beta/', (req, res) => res.sendFile('/beta/index.html', { root: '.' }))
app.get('/beta/charts', (req, res) => res.sendFile('/beta/charts.html', { root: '.' }))
app.get('/beta/wallet', (req, res) => res.sendFile('/beta/wallet.html', { root: '.' }))
app.get('/beta/news', (req, res) => res.sendFile('/beta/news.html', { root: '.' }))
app.get('/beta/feed.atom', (req, res) => res.sendFile('/beta/feed.atom', { root: '.' }))
app.use('/beta/public', express.static('beta/public'))
// Coingecko URL
app.get('/coingecko/:blockchain/:token', async (req, res) => {
const query = {
$and: [
{ platforms: { $regex : req.params.blockchain + '-' + req.params.token.toLowerCase() } },
{ market_cap: { $ne: false } }
]
}
res.json(await collections.coingecko.findOne(query))
})
// Uniswap URLs - Default
app.get('/:chain?/token/:token', async (req, res) => {
const data = await redis.get(req.params.chain+':data')
const parsed = JSON.parse(data)
if(
Object.keys(parsed).includes(req.params.token) ||
Object.keys(parsed).findIndex(address => parsed[address].s === req.params.token) !== -1
) {
res.sendFile('/index.html', { root: '.' })
} else {
// TODO Improve error => redirect to homepage
res.writeHead(400, {'Content-Type': 'text/html'})
res.end('This token does not exist !')
}
})
app.get('/:chain?/top', async (req, res) => {
const data = await redis.get(req.params.chain+':top')
res.json(JSON.parse(data))
})
app.get('/:chain?/simple', async (req, res) => {
const data = await redis.get(req.params.chain+':data')
res.json(JSON.parse(data))
})
app.get('/:chain?/charts/:token', async (req, res) => {
const data = await buildChartsData(req.params.chain, req.query.interval, req.params.token)
res.json(data)
})
app.get('/:chain?/charts/:token/:base', async (req, res) => {
const data = await buildChartsData(req.params.chain, req.query.interval, req.params.token, req.params.base)
res.json(data)
})
const server = app.listen(port, () => console.log(`Frontend start at ${port}`))
http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/html'})
res.end('Hello World')
})
const readAndFilterFile = async (filename, token, base) => {
const file = readFileSync(path.join(dir_home, filename), 'utf8')
const json = JSON.parse(file.toString())
let result = {}
if(token && base) {
result[token] = json[token]
result[base] = json[base]
} else {
result = json[token]
}
return result
}
const buildChartsData = async (chain, interval, token, base) => {
let chartTimeframe = 'c1'
switch (interval) {
case INTERVAL_15M:
chartTimeframe = 'c1'
break;
case INTERVAL_4H:
chartTimeframe = 'c2'
break;
case INTERVAL_1D:
chartTimeframe = 'c3'
break;
case INTERVAL_1W:
chartTimeframe = 'c4'
break;
default:
chartTimeframe = 'c1'
}
let chart_str = await redis.get(`${chain}:${chartTimeframe}:${token}`)
let chart = JSON.parse(chart_str)
let result = {}
if(chart) {
result[token] = chart
}
if(base) {
chart_str = await redis.get(`${chain}:${chartTimeframe}:${base}`)
chart = JSON.parse(chart_str)
if(chart) {
result[base] = chart
}
}
return result
}
/**
* WebSocket Server
*/
let wss = expressWs(app, server)
app.ws('/ws', async function(ws, req) {
ws.on('message', async function(data) {
const msg = JSON.parse(data)
console.log('msg', msg)
switch (msg.type) {
case 'connection':
console.log('client connected to WSS')
ws.send(JSON.stringify({
type: 'connection',
data: true
}))
break
case 'statistics':
broadcastStatistics(ws)
break
default:
console.log('other')
}
if(msg.url && !msg.url.includes('statistics')) {
statistics.latests.push(msg.url)
statistics.latests = statistics.latests.slice(-statistics.latests_nb)
broadcastStatistics(ws)
}
})
})
function broadcastStatistics(ws) {
console.log('broadcast')
wss.getWss().clients.forEach(client => {
//if (client != ws) {
client.send(JSON.stringify({
type: 'statistics',
data: statistics.latests//.filter(stats => stats.type === 'statistics').map(stats => stats.data)
}))
//}
})
}
// useful map - Simple data from DB to Json
function mapFromDbToJson(data) {
const result = {}
data.filter(token => token.p > 0).forEach((token) => {
result[token.id] = {
n: token.n,
s: token.s,
p: token.p,
t: token.t
}
})
return result
}
// useful Math.random timer
function getTimer() {
if(process.env.NODE_ENV === 'production') {
// between 15 and 45 seconds in Production Mode
return Math.round((30*Math.random() + 15)*1000)
}
// between 60 and 180 seconds in Dev Mode
return Math.round((120*Math.random() + 60)*1000)
}