forked from Tucsky/aggr-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
397 lines (322 loc) · 10.3 KB
/
config.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
394
395
396
397
const fs = require('fs')
const path = require('path')
const decamelize = require('decamelize')
require('dotenv').config({
override: true
})
console.log(`[init] reading config.json...`)
/* Default configuration (its not ok to change here!, use config.json.)
*/
const defaultConfig = {
// default pairs we track
pairs: [
'BITFINEX:BTCUSD',
'BINANCE:btcusdt',
'OKEX:BTC-USDT',
'KRAKEN:XBT/USD',
'COINBASE:BTC-USD',
'POLONIEX:BTC_USDT',
'HUOBI:btcusdt',
'BITSTAMP:btcusd',
'BITMEX:XBTUSD',
'BITFINEX:BTCF0:USTF0',
'OKEX:BTC-USD-SWAP',
'OKEX:BTC-USDT-SWAP',
'BINANCE_FUTURES:btcusdt',
'BINANCE_FUTURES:btcusd_perp',
'HUOBI:BTC-USD',
'KRAKEN:PI_XBTUSD',
'DERIBIT:BTC-PERPETUAL',
'BYBIT:BTCUSD',
'BYBIT:BTCUSDT',
'BYBIT:ETHUSD',
'BYBIT:ETHUSDT',
'BITSTAMP:ethusd',
'BITMEX:ETHUSD',
'KRAKEN:PI_ETHUSD',
'BITFINEX:ETHUSD',
'COINBASE:ETH-USD',
'OKEX:ETH-USDT',
'OKEX:ETH-USDT-SWAP',
'OKEX:ETH-USD-SWAP',
'BINANCE_FUTURES:ethusdt',
'DERIBIT:ETH-PERPETUAL',
'KRAKEN:ETH/USD',
'HUOBI:ethusdt',
'HUOBI:ETH-USD',
'BINANCE_FUTURES:ethusd_perp'
],
// non standard products
extraProducts: [],
// will connect to exchanges and subscribe to pairs on startup
collect: true,
// default server port
port: 3000,
// restrict origin (now using regex)
origin: '.*',
// max n of bars a user can get in 1 call
maxFetchLength: 100000,
// persist connections between reload, and use that information to attempt filling the holes on startup
persistConnections: true,
// won't persist connection older than this (default 4h)
staleConnectionThreshold: 1000 * 60 * 60 * 4,
// reconnection threshold (default 1h without incoming trade, reconnect the whole api)
// config.reconnectionThreshold now accepts simple formula which can use the `connection` variable
// ex: `7200000 / Math.log(Math.exp(1) + connection.avg)`
// for 0 hit/min = 2h
// for 0.1 hit/min = 1h, 55m, 48s
// for 1 hit/min = 1h, 31m, 22s
// for 10 hit/min = 47m, 11s
// for 100 hit/min = 25m, 54s
// for 1000 hit/min = 17m, 21s
reconnectionThreshold: 3600000,
// delay between each recovery requests of a same pair (default 250ms)
recoveryRequestDelay: 250,
// bypass origin restriction for given ips (comma separated)
whitelist: [],
// enable api (historical/{from in ms}/{to in ms}/{timesfame in ms}/{markets separated by +})
api: true,
// storage solution, either
// false | null (no storage)
// "files" (periodical text file),
// "influx" (timeserie database),
// NB: use array or comma separated storage names for multiple storage solution
// default = "files" just store in text files, no further installation required.
storage: ['files'],
// store interval (in ms)
backupInterval: 10000,
// influx db server to use when storage is set to "influx"
influxHost: 'localhost',
influxPort: 8086,
// influx database
influxDatabase: 'significant_trades',
// base name measurement used to store the bars
// if influxMeasurement is "trades" and influxTimeframe is "10000", influx will save to trades_10s
influxMeasurement: 'trades',
// timeframe in ms (default 10s === 10000ms)
// this is lowest timeframe that influx will use to group the trades
influxTimeframe: 10000,
// downsampling
influxResampleTo: [
1000 * 30,
1000 * 60,
1000 * 60 * 3,
1000 * 60 * 5,
1000 * 60 * 15,
1000 * 60 * 30,
1000 * 60 * 60,
1000 * 60 * 60 * 2,
1000 * 60 * 60 * 4,
1000 * 60 * 60 * 6,
1000 * 60 * 60 * 24
],
// trigger resample every minute
influxResampleInterval: 60000,
// number of bars to retain within influx db per timeframe
influxRetentionPerTimeframe: 5000,
// prefix aggr retention policies with this (unused rp using that prefix get automaticaly removed)
influxRetentionPrefix: 'aggr_',
// create new text file every N ms when storage is set to "file" (default 1h)
filesInterval: 3600000,
// default place to store the trades data files
filesLocation: './data',
// automatic compression of file once done working with it
filesGzipAfterUse: true,
// close file stream after it's expired, allow for a delay before closing the file stream
// which can be useful when exchanges trades are lagging (default 5m)
filesCloseAfter: 300000,
// choose whether or not enable rate limiting on the provided api
enableRateLimit: false,
// rate limit time window (default 15m)
rateLimitTimeWindow: 1000 * 60 * 15,
// rate limit max request per rateLimitTimeWindow (default 30)
rateLimitMax: 30,
// enable cluster mode where you have node dedicated to collecting and 1 master node dedicated to serving data
// one client request the data, the main cluster node will ask collectors for realtime data that is not yet in saved in the DB
// reducing writes rate on influx yet allowing realtime data fetch for the client
// config.api: true === cluster node
// config.collect: true === collector node
influxCollectors: false,
// collector to cluster reconnection delay (default 10s)
influxCollectorsReconnectionDelay: 1000 * 10,
// unix socket used to communicate
influxCollectorsClusterSocketPath: '/tmp/aggr.sock',
// you must set an ID to each cluster in order to use push notifications with collectors (unique id to associate with notification endpoints persistences)
id: null,
publicVapidKey: null,
privateVapidKey: null,
alertExpiresAfter: 1000 * 60 * 60 * 24 * 7,
alertEndpointExpiresAfter: 1000 * 60 * 60 * 24 * 30,
indexExchangeBlacklist: [],
indexQuoteWhitelist: ['USD', 'USDT', 'FDUSD', 'USDC'],
// verbose
debug: false
}
/* Merge default
*/
const commandSettings = {}
if (process.argv.length > 2) {
let exchanges = []
process.argv.slice(2).forEach(arg => {
const keyvalue = arg.split('=')
if (keyvalue.length > 1) {
commandSettings[keyvalue[0]] = isNaN(+keyvalue[1])
? keyvalue[1]
: +keyvalue[1]
} else if (/^\w+$/.test(keyvalue[0])) {
exchanges.push(keyvalue[0])
}
})
if (exchanges.length) {
commandSettings.exchanges = exchanges
}
}
/* Load custom server configuration
*/
let userSettings = {}
let configPath =
commandSettings.config ||
commandSettings.configFile ||
commandSettings.configPath ||
'config.json'
try {
console.log('[init] using config file ' + configPath)
configPath = path.resolve(__dirname, '../' + configPath)
const configExamplePath = path.resolve(__dirname, '../config.json.example')
if (
!fs.existsSync(configPath) &&
fs.existsSync(configExamplePath)
) {
fs.copyFileSync(configExamplePath, configPath)
}
commandSettings.configPath = configPath
userSettings = require(configPath) || {}
} catch (error) {
throw new Error(
`Unable to parse ${
configPath !== '../config.json' ? 'specified ' : ''
}configuration file\n\n${error.message}`
)
}
/* Merge cmd & file configuration
*/
const config = Object.assign(defaultConfig, userSettings, commandSettings)
if (process.env.RUNNING_IN_DOCKER) {
/* Override config with ENV variables using decamelize + uppercase
(e.g. influxPreheatRange -> INFLUX_PREHEAT_RANGE)
*/
Object.keys(config).forEach(k => {
config_to_env_key = decamelize(k, '_').toUpperCase()
config_env_value = process.env[config_to_env_key]
if (config_env_value) {
config[k] = config_env_value
console.log(
`overriding '${k}' to '${config_env_value}' via env '${config_to_env_key}'`
)
}
})
}
/**
* Set Alert service Vapid keys from ENV to config file
*/
if (process.env.PUBLIC_VAPID_KEY && process.env.PRIVATE_VAPID_KEY) {
if (!config.publicVapidKey) {
config.publicVapidKey = process.env.PUBLIC_VAPID_KEY
}
if (!config.privateVapidKey) {
config.privateVapidKey = process.env.PRIVATE_VAPID_KEY
}
}
/* Validate storage
*/
if (config.storage) {
if (!Array.isArray(config.storage)) {
if (config.storage.indexOf(',') !== -1) {
config.storage = config.storage.split(',').map(a => a.trim())
} else {
config.storage = [config.storage.trim()]
}
}
for (let storage of config.storage) {
const storagePath = path.resolve(__dirname, 'storage/' + storage + '.js')
if (!fs.existsSync(storagePath)) {
throw new Error(`Unknown storage solution "${storagePath}"`)
}
}
} else {
config.storage = null
}
/* Others validations
*/
if (config.whitelist && config.whitelist === 'string') {
config.whitelist = config.whitelist.split(',').map(a => a.trim())
} else if (!config.whitelist) {
config.whitelist = []
}
if (
typeof config.indexExchangeBlacklist === 'string' &&
config.indexExchangeBlacklist.trim().length
) {
config.indexExchangeBlacklist = config.indexExchangeBlacklist
.split(',')
.map(a => a.trim())
} else if (!Array.isArray(config.indexExchangeBlacklist)) {
config.indexExchangeBlacklist = []
}
if (
typeof config.indexQuoteWhitelist === 'string' &&
config.indexQuoteWhitelist.trim().length
) {
config.indexQuoteWhitelist = config.indexQuoteWhitelist
.split(',')
.map(a => a.trim())
} else if (!Array.isArray(config.indexQuoteWhitelist)) {
config.indexQuoteWhitelist = []
}
if (config.pair) {
config.pairs = Array.isArray(config.pair)
? config.pair
: config.pair.split(',')
delete config.pair
}
if (!Array.isArray(config.pairs)) {
if (config.pairs) {
config.pairs = config.pairs
.split(',')
.map(a => a.trim())
.filter(a => a.length)
} else {
config.pairs = []
}
}
if (!config.pairs.length) {
if (config.collect) {
console.warn('[warning!] no pairs selected for collection')
}
config.pairs = []
}
if (config.exchanges && typeof config.exchanges === 'string') {
config.exchanges = config.exchanges
.split(',')
.map(a => a.trim())
.filter(a => a.length)
}
if (!config.storage && config.collect) {
console.warn(
`[warning!] server will not persist any of the data it is receiving`
)
}
if (!config.collect && !config.api) {
console.warn(`[warning!] server has no purpose`)
}
if (!config.storage && !config.collect && config.api) {
console.warn(
`[warning!] api is enabled but neither storage or collect is enabled (may be useless)`
)
}
if (!config.debug) {
console.debug = function () {}
} else {
console.debug = console.log
}
module.exports = config