-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
161 lines (146 loc) · 4.07 KB
/
main.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
const binanceWebSocket = "wss://stream.binance.com/stream"
const binanceFuturesWebSocket = "wss://fstream.binance.com/stream"
const BinanceFuturesMessage = '{"method":"SUBSCRIBE","params":["!miniTicker@arr"],"id":1}'
const BinanceMessage = '{"method":"SUBSCRIBE","params":["!miniTicker@arr@3000ms","!ticker_1h@arr@3000ms","!ticker_4h@arr@3000ms"],"id":1}'
const ticker = {
1:"!ticker_1h@arr@3000ms",
4:"!ticker_4h@arr@3000ms",
24:"!miniTicker@arr@3000ms"
}
/**
* A program that retrieves data from a websocket endpoint in realtime,
* as it appears on the official Binance website Markets: https://www.binance.com/en/markets
*/
class Binance {
/**
* Endpoint retrieved from the interface
* @param endpoint
* @param print
* @param callback
* @param message
* @param _disconnectCallback
*/
constructor(endpoint, print= 1, callback = this.printData, message = BinanceMessage, _disconnectCallback = this.reconnect) {
this.callback = callback
this.print = print
this.endpoint = endpoint
this.ticker = ticker[1]
this.sendMessage = message
this.ws = new WebSocket(endpoint);
this.metadata = undefined
this.requestNumber = 0
this.block = 0
this.info = undefined
this.blocks = undefined
this._disconnectCallback = _disconnectCallback
this.txns = undefined
this.connected = false
this.connections = []
this.requests = 0
}
/**
* Get last block number mint
* @returns {*}
*/
get getLastBlock ()
{
return this.info.lastblock
}
/**
* 10 randomly selected transactions of the current response from the websocket
* @returns {*}
*/
get getTransactions ()
{
return this.txns
}
/**
* MarketCap getter
* @returns {*}
*/
get getMarketCap()
{
return this.info.marketcap
}
/**
* Block getter
* @returns {*}
*/
get getBlocks()
{
return this.blocks
}
/**
* Prints internal data of each block
*/
printData()
{
}
reconnect(){
this.ws = new WebSocket(this.endpoint)
this.run()
}
/**
* Subscribes to the websocket
* @param event
*/
subscribe (event)
{
this.ws.onopen = (response=> {
this.connected = true
this.ws.send(event)
})
this.ws.onclose = (response=> {
this.connected = false
if(this.connections.length === 0) {
this.connections.push(setInterval(() => {
console.log("reconnection")
this.connected ? clearInterval(this.connections.pop()) : this._disconnectCallback()
}, 3000))
}
})
this.ws.onmessage = (response => {
!this.requestNumber ? this.saveConnection(response.data) : this.retrieveData(response.data)
this.requestNumber += 1
})
}
/**
* First message sent by the endpoint when the first connection in established
* @param response
*/
saveConnection(response)
{
console.log(JSON.parse(response).event)
}
/**
* Receives the relevant data of the endpoint and retrieves the data by checking redundant blocks
* @param response
*/
retrieveData(response)
{
if(this.ticker === JSON.parse(response).stream) {
this.requests++
console.log(`request n.${this.requests}`)
this.metadata = JSON.parse(response).data.filter(obj => obj.s.includes("USDT"))
this.callback(this.metadata)
}
}
/**
* Runs the main program by subscribing to the websocket
* and sends the message for a request of the relevant metadata
*/
run ()
{
this.subscribe(this.sendMessage)
}
}
/**
* Creates a new instance of the class and calls the run method to start running the program
*/
function main(){
new Binance(binanceWebSocket, 1).run()
}
/**
* Runs the main function of the program
*/
// main()