This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
381 lines (361 loc) · 12.9 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
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
process.title = 'binance-profit-calculator'
const debug = !(process.env.NODE_ENV === 'production') || false
const testMode = false // don't make any remote requests
console.debug = (...args) => debug && console.log(...args)
/**
* Misc Libraries
**/
const Big = require('big.js')
const path = require('path')
const del = require('del')
/**
* File based JSON storage
**/
const store = require('data-store')('bpc', {
cwd: 'data'
})
store.create('configuration')
store.create('botStatus')
/**
* Config
**/
const prop = require('properties')
const propOptions = {
path: true,
namespaces: true
}
let config
const setupConfig = () => new Promise((resolve, reject) => {
prop.parse(path.join(__dirname, 'config.properties'), propOptions, (err, data) => {
if (!err) {
config = data
if (data.server.port) {
process.env.PORT = data.server.port
}
// for (let key in data) {
// console.log(key, data[key])
// }
resolve(data)
} else {
reject(err)
}
})
})
/**
* Trade tracking
* TODO: History can get very large to be sending out the entire thing
* on every trade activity. Find a way to minimize this bloat.
* (try to only send differences?)
**/
const tradeHistory = require('./lib/tradeHistory.js')
const addLineToHistory = (line) => {
// add to pairs _data (top of the stack, use Array.prototype.unshift() )
const myMarket = tradeHistory.DetectMarket(line.Market)
if (myMarket) {
// recalculate Amount, Fee, Bought, Sold, Difference, DifferenceWithoutBags
let databaseContainer = store.get('history')
if (!databaseContainer) {
databaseContainer = {}
}
databaseContainer = tradeHistory.DatabaseContainerNormalizer(databaseContainer)
databaseContainer = tradeHistory.LineProcessor(line, databaseContainer, true)
databaseContainer = tradeHistory.BagProcessor(databaseContainer)
databaseContainer = tradeHistory.ProfitProcessor(databaseContainer)
store.set('history', databaseContainer)
return databaseContainer
} else {
throw new Error('addLineToHistory', 'Undetectable market!', line.Market)
}
}
/**
* Binance
**/
const binanceLib = require('./lib/api/binance.js')
const binanceMLib = binanceLib.BinanceModule
let binanceModule
const setupBinanceModule = (config, ioRef) => new Promise((resolve, reject) => {
try {
binanceModule = new binanceMLib({
publicKey: config.api.public,
secretKey: config.api.secret,
reconnectOnDisconnected: true,
retriesMax: 5
})
binanceModule.on('error', (error) => {
console.debug('binanceModule.error', error)
if (error.code === binanceLib.BinanceError.AuthKeysIssue) {
store.botStatus.set('binanceMonitor', 'Off')
ioRef.emit('error', error)
// set global error flag
}
if (error.code === binanceLib.BinanceError.KeysNotPresent) {
store.botStatus.set('binanceMonitor', 'Off')
ioRef.emit('error', error)
// set global error flag
}
})
const userDataHandler = (data) => {
// TODO: Normalize the userData output so in case we add more exchanges,
// the package delivered to the front end is the same
if (data.eventType === 'executionReport') {
const accumulatedQuantity = Big(data.accumulatedQuantity)
const lastTradeQuantity = Big(data.lastTradeQuantity)
if ((data.executionType === 'TRADE' && data.orderStatus === 'FILLED') ||
(data.executionType === 'TRADE' && data.orderStatus === 'PARTIALLY_FILLED') ||
(data.executionType === 'EXPIRED' && data.orderStatus === 'EXPIRED' && accumulatedQuantity.gt(0))) {
const partiallyFilled = (data.executionType === 'TRADE' && data.orderStatus === 'PARTIALLY_FILLED') ? true : false
const lastTradePrice = Big(data.lastTradePrice)
const lastTradedTotal = lastTradeQuantity.times(lastTradePrice)
const accumulatedTotal = accumulatedQuantity.times(lastTradePrice)
const tradeTime = new Date(data.tradeTime)
const line = {
'Date(UTC)': tradeTime.toISOString().replace('T', ' ').replace(/\.\d+Z$/, ''),
'Market': data.symbol,
'Type': data.side,
'Price': lastTradePrice.toFixed(8).toString(),
'Amount': lastTradeQuantity.toFixed(8).toString(),
'Total': lastTradedTotal.toFixed(8).toString(),
'Fee': data.commission,
'Fee Coin': data.commissionAsset
}
console.log('userDataHandler', 'line created from trade activity', line)
const database = addLineToHistory(line)
if (database) {
ioRef.emit('history', socketIoPackageWrapper(database))
}
}
ioRef.emit('orderInfo', socketIoPackageWrapper(data))
}
}
binanceModule.on('userData', userDataHandler)
binanceModule.on('userDataDisconnected', (error) => {
console.log('binanceModule.userDataDisconnected', (error ? 'Forefully disconnected!' : 'Gracefully disconnected'))
})
binanceModule.on('balanceUpdate', (data) => {
ioRef.emit('balanceUpdate', socketIoPackageWrapper(data))
})
if (store.botStatus.get('binanceMonitor') === 'On') {
binanceModule.startUserDataMonitor()
}
resolve(binanceModule)
} catch (e) {
console.log(typeof e, e)
reject(e.message)
}
})
const turnOnBinanceMonitor = (cb = null) => {
if (binanceModule) {
console.debug('turnOnBinanceMonitor', 'Starting...')
binanceModule.startUserDataMonitor(cb)
store.botStatus.set('binanceMonitor', 'On')
// When first starting the bot, do a balanceUpdate
binanceModule.balanceUpdate()
// binanceModule.tickerPrice()
// .then((data) => {
// console.debug('turnOnBinanceMonitor', 'binanceMonitor.tickerPrice', 'Received', data.length)
// })
} else {
if (cb && typeof cb === 'function') {
cb(new binanceLib.BinanceError())
}
}
}
const turnOffBinanceMonitor = () => {
if (binanceModule) {
console.debug('turnOffBinanceMonitor', 'Stopping...')
binanceModule.stopUserDataMonitor()
}
store.botStatus.set('binanceMonitor', 'Off')
}
/**
* Process Configuration
**/
const processConfigurationChange = (key, value, ioRef, socketRef) => new Promise((resolve, reject) => {
console.debug('processConfigurationChange:', key, value)
switch (key) {
case 'binanceMonitor':
if (value === 'On') {
if (store.botStatus.get('binanceMonitor') === 'On') {
// monitor already on, don't do anything
// (maybe check on the running status)
console.debug('processConfigurationChange.binanceMonitor', 'Already On')
}
if (!store.botStatus.get('binanceMonitor') || store.botStatus.get('binanceMonitor') === 'Off') {
console.debug('processConfigurationChange.binanceMonitor', 'Turn On')
turnOnBinanceMonitor((error, ws) => {
if (error) {
reject(error)
} else {
resolve(ws)
}
})
} else {
// Already on
resolve()
}
} else if (value === 'Off') {
console.debug('processConfigurationChange.binanceMonitor', 'Turn Off')
// find a way to turn it off!
turnOffBinanceMonitor()
resolve()
}
break
default:
reject('Configuration key is invalid')
}
})
/**
* Web Server Setup
**/
const express = require('express')
const app = express()
const multer = require('multer')
const upload = multer({dest: path.join(__dirname, 'uploads')})
const http = require('http').Server(app)
const io = require('socket.io')(http)
const webpack = require('webpack')
const webpackDevMiddleware = require('webpack-dev-middleware')
const webpackHotMiddleware = require('webpack-hot-middleware')
const webpackConfigPromise = require('./build/webpack.dev.conf.js')
let userData = {
connected: 0,
handlers: {}
}
let coinMarketCapData = []
const socketIoPackageWrapper = (payload, status) => {
return {
timestamp: new Date(),
payload: payload,
status: (status ? status : {code: 200, msg: 'OK'})
}
}
const CoinmarketcapLib = require('node-coinmarketcap')
const coinmarketcap = new CoinmarketcapLib({
events: true, // Enable event system
refresh: 30, // Refresh time in seconds (Default: 60)
convert: "USD" // Convert price to different currencies. (Default USD)
})
const userConnectedHandler = (socket, ioRef) => {
if (userData.connected === 0) { // previous count, before this connection
// connect user services
coinmarketcap.getAll((data) => {
coinMarketCapData = data
ioRef.emit('coinmarketcap', socketIoPackageWrapper(coinMarketCapData))
})
userData.handlers.coinMarketCap = setInterval(() => {
coinmarketcap.getAll((data) => {
coinMarketCapData = data
ioRef.emit('coinmarketcap', socketIoPackageWrapper(coinMarketCapData))
})
// coinmarketcap.multi(coins => {
// for (let market in store.get('history')) {
// console.log(coins.get(market))
// }
// })
}, 30000) // every 30 seconds
}
userData.connected++
}
const userDisconnectedHandler = () => {
if (userData.connected === 1) { // previous count, before this connection
// disconnect all user related services
console.log('userDisconnectedHandler', 'Stopping user services')
clearTimeout(userData.handlers.coinMarketCap)
}
userData.connected--
}
const setupWebServer = (wpConfig) => new Promise((resolve, reject) => {
const webpackConfig = wpConfig,
compiler = webpack(wpConfig)
try {
app.post('/tradeHistory', upload.single('tradeHistory'), function (req, res, next) {
if (req.file.originalname.match(/\.(xls|xlsx|csv)$/i)) {
try {
// send this filename to the tradeHistory processor
const database = tradeHistory.ExcelProcessor(req.file.path)
store.del('history')
store.set('history', database)
console.debug('TradeHistory imported and saved')
io.emit('history', socketIoPackageWrapper(database))
res.sendStatus(200)
} catch (e) {
res.status(500).send('Error processing your history file: ' + e.message)
}
// delete uploaded file
del.sync([req.file.path])
} else {
res.status(500).send('Bad file type. Only XLS, XLSX, and CSV files are supported.')
}
})
app.use(express.static(config.server.docroot))
if (debug) {
// Set up Webpack Middleware
app.use(webpackDevMiddleware(compiler, {
noinfo: true,
publicPath: webpackConfig.output.publicPath
}))
// Set up Webpack Hot Module Reloading
app.use(webpackHotMiddleware(compiler, {
log: console.debug,
path: '/__webpack_hmr',
heartbeat: 10 * 1000
}))
}
// Set up socket.io streaming
io.on('connection', function(socket){
console.debug('Socket.io: A user connected')
userConnectedHandler(socket, io)
if (store.get('history')) {
socket.emit('history', socketIoPackageWrapper(store.get('history')))
}
if (coinMarketCapData) {
socket.emit('coinmarketcap', socketIoPackageWrapper(coinMarketCapData))
}
if (store.configuration) {
socket.emit('configuration', socketIoPackageWrapper(store.configuration.get()))
}
if (binanceModule && store.botStatus.get('binanceMonitor') === 'On') {
binanceModule.balanceUpdate()
}
// socket.emit('profits', {timestamp: new Date(), data: profitsWithUSD()})
socket.on('configuration', function(msg){
if (msg === 'Refresh') {
socket.emit('configuration', socketIoPackageWrapper(store.configuration.get()))
} else {
let key = msg.key || null
let value = msg.value || null
console.debug('Configuration:', key, value)
processConfigurationChange(key, value, io, socket).then(() => {
console.debug('Configuration', 'Returned successfully, notify everyone of config change')
store.configuration.set(key, value)
io.emit('configuration', socketIoPackageWrapper(store.configuration.get()))
}).catch((reason) => {
console.debug('Configuration', 'Returned error, notify user of error', reason)
socket.emit('configuration', socketIoPackageWrapper(null, {code: 500, msg: reason}))
})
}
})
socket.on('disconnect', function(){
console.debug('Socket.io: A user disconnected')
userDisconnectedHandler()
})
resolve()
})
// Listen on the port
http.listen(config.server.port, function(){
console.log('Express: listening on *:' + config.server.port)
})
} catch (e) {
reject(e)
}
})
setupConfig()
.then((config) => setupBinanceModule(config, io))
.then(() => webpackConfigPromise)
.then((wpConfig) => setupWebServer(wpConfig))
.then(() => console.log('App: Startup sequence finished'))
.catch(reason => {
console.error('App Error:', reason)
console.error('App: Setup sequence incomplete. Exiting now.')
process.exit(1)
})