This repository was archived by the owner on Sep 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.js
127 lines (111 loc) · 3.53 KB
/
watch.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
"use strict";
require('dotenv').config();
const GdaxModule = require('gdax');
const TelegramBot = require('node-telegram-bot-api');
const PASSPHRASE = process.env.TRADING_BOT_PASSPHRASE;
const KEY = process.env.TRADING_BOT_KEY;
const SECRET = process.env.TRADING_BOT_SECRET;
const GDAX_URI = 'https://api.gdax.com';
const TELEGRAMTOKEN = process.env.TELEGRAMTOKEN;
const TELEGRAMCHATID = process.env.TELEGRAMCHATID;
const bot = new TelegramBot(TELEGRAMTOKEN, {polling: true});
let authenticatedClient = null;
let publicClient = null;
let gdax_ws = null;
const getProductTickerCallback = (error, response, data) => {
if (error)
return console.log(error);
if (data != null) {
currentPrice = parseFloat(data.bid);
} else {
console.log('No price avail');
}
}
const init_ws_stream = () => {
gdax_ws = new GdaxModule.WebsocketClient(['BCH-EUR','BTC-EUR','ETH-EUR', 'LTC-EUR'], 'wss://ws-feed.gdax.com', {
key: KEY,
secret: SECRET,
passphrase: PASSPHRASE,
}, {
heartbeat: true,
channels: ['user', 'heartbeat']
})
gdax_ws.on('message', (data) => {
switch (data.type) {
case "heartbeat":
case "subscriptions":
return
default:
process_ws_message(data)
break
}
})
gdax_ws.on('error', (error) => {
console.log(error)
})
gdax_ws.on('close', (data) => {
ws_reconnect(gdax_ws, data)
})
}
const ws_reconnect = (ws, data) => {
console.log(`GDAX websocket disconnected with data: ${data}`)
// try to re-connect the first time...
ws.connect()
let count = 1
// attempt to re-connect every 30 seconds.
// TODO: maybe use an exponential backoff instead
const interval = setInterval(() => {
if (!ws.socket) {
console.log(`Reconnecting to GDAX (attempt ${count++})`)
//count++
ws.connect()
} else {
console.log('GDAX reconnected')
clearInterval(interval)
}
}, 10000)
}
const sendMessage = (error, response, data) => {
if (error) {
console.log(error)
return
}
console.log(data)
bot.sendMessage(TELEGRAMCHATID, `${data.done_at} :: _${data.side} ${data.product_id}_ *Filled* for ${data.filled_size} type ${data.type}.`,{'parse_mode': 'Markdown'});
}
/**
*
*/
const process_ws_message = (data) => {
switch (data.type) {
case 'done': {
switch (data.reason) {
case 'canceled':
bot.sendMessage(TELEGRAMCHATID, `${data.time} :: _${data.side} ${data.product_id}_ *Canceled* for ${data.remaining_size}.`,{'parse_mode': 'Markdown'});
break
case 'filled':
authenticatedClient.getOrder(data.order_id, sendMessage)
break
}
} break
case 'match': {
let order_id = null
switch (data.side) {
case 'buy':
order_id = data.maker_order_id
break
case 'sell':
order_id = data.taker_order_id
break
}
if (!order_id) return
} break
}
}
publicClient = new GdaxModule.PublicClient(GDAX_URI);
authenticatedClient = new GdaxModule.AuthenticatedClient(KEY, SECRET, PASSPHRASE, GDAX_URI);
function main() {
bot.sendMessage(TELEGRAMCHATID,'*Klaar om watchen*',{'parse_mode': 'Markdown'});
init_ws_stream();
}
main();