forked from bitfinexcom/bfx-stuff-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbi.js
More file actions
71 lines (59 loc) · 1.51 KB
/
bi.js
File metadata and controls
71 lines (59 loc) · 1.51 KB
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
const WebSocket = require('ws')
const crypto = require('crypto')
const fs = require('fs')
let conf
try {
conf = JSON.parse(fs.readFileSync(`${__dirname}/config.json`, 'utf8'))
} catch (e) {
console.error('config.json: file not found')
process.exit(-1)
}
if (!process.argv[2]) {
console.error('example not specified')
process.exit(-1)
}
let script
try {
script = require(`${__dirname}/examples/${process.argv[2]}.js`)
} catch (e) {
console.error('script: not found')
process.exit(-1)
}
if (!script.run) {
console.error('script: run function not found')
process.exit(-1)
}
const w1_apiKey = conf.apiKey
const w1_apiSecret = conf.apiSecret
const wss = new WebSocket('wss://api.bitfinex.com/ws/2')
const authNonce = (new Date()).getTime() * 1000
const payload = 'AUTH' + authNonce
const signature = crypto.createHmac('sha384', w1_apiSecret).update(payload).digest('hex')
wss.on('message', function (data) {
data = JSON.parse(data)
if (data.event) {
if (data.event === 'auth' && data.status !== 'OK') {
console.error('AUTHENTICATION FAILED')
process.exit(-1)
}
}
})
wss.on('open', function () {
wss.send(JSON.stringify({
event: 'auth',
apiKey: w1_apiKey,
authSig: signature,
authPayload: payload,
authNonce: +authNonce
}))
setInterval(() => {
wss.send(JSON.stringify({ event: 'ping' }))
}, 15000)
setTimeout(() => {
script.run(wss, conf, process.argv[3])
}, 2000)
})
wss.on('close', function () {
console.error('SOCKET CLOSED')
process.exit(-1)
})