-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathbitget.js
More file actions
171 lines (143 loc) · 3.71 KB
/
bitget.js
File metadata and controls
171 lines (143 loc) · 3.71 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
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
const Exchange = require('../exchange')
const { sleep } = require('../helper')
const WebSocket = require('websocket').w3cwebsocket
class Bitget extends Exchange {
constructor() {
super()
this.id = 'BITGET'
this.maxConnectionsPerApi = 50
this.endpoints = {
PRODUCTS: [
'https://api.bitget.com/api/spot/v1/public/products',
'https://api.bitget.com/api/mix/v1/market/contracts?productType=umcbl',
'https://api.bitget.com/api/mix/v1/market/contracts?productType=dmcbl',
'https://api.bitget.com/api/mix/v1/market/contracts?productType=cmcbl'
],
}
this.url = (pair) => {
if (this.types[pair] === 'spot') {
return 'wss://ws.bitget.com/spot/v1/stream'
}
return 'wss://ws.bitget.com/mix/v1/stream'
};
}
formatProducts(responses) {
const products = []
const types = {}
/*
umcbl USDT perpetual contract
dmcbl Universal margin perpetual contract
cmcbl USDC perpetual contract
*/
for (const response of responses) {
const type = ['spot', 'umcbl', 'dmcbl', 'cmcbl'][
responses.indexOf(response)
]
for (const product of response.data) {
let symbol
if (type === 'spot') {
symbol = product.symbolName
} else {
symbol = product.symbol
}
products.push(symbol)
types[symbol] = type
}
}
return {
products,
types
}
}
/**
* Sub
* @param {WebSocket} api
* @param {string} pair
*/
async subscribe(api, pair) {
if (!(await super.subscribe.apply(this, arguments))) {
return
}
api.send(
JSON.stringify({
op: 'subscribe',
args: [
{
instType: this.types[pair] === 'spot' ? 'sp' : 'mc',
channel: 'trade',
instId: pair.replace(/_.*/, '')
}
]
})
)
// this websocket api have a limit of about 10 messages per second.
await sleep(150 * this.apis.length)
}
/**
* Sub
* @param {WebSocket} api
* @param {string} pair
*/
async unsubscribe(api, pair) {
if (!(await super.unsubscribe.apply(this, arguments))) {
return
}
api.send(
JSON.stringify({
op: 'unsubscribe',
args: [
{
instType: this.types[pair] === 'spot' ? 'sp' : 'mc',
channel: 'trade',
instId: pair.replace(/_.*/, '')
}
]
})
)
// this websocket api have a limit of about 10 messages per second.
await sleep(150 * this.apis.length)
}
formatTrade(trade, pair) {
return {
exchange: this.id,
pair: pair,
timestamp: +trade[0],
price: +trade[1],
size: +trade[2],
side: trade[3]
}
}
onMessage(event, api) {
if (event.data === 'pong') {
return
}
const json = JSON.parse(event.data)
if (json.action !== 'update') {
return
}
if (json.data.length) {
if (json.arg.instType === 'mc') {
if (/USDT$/.test(json.arg.instId)) {
json.arg.instId += '_UMCBL' // USDT perpetual contract
} else if (/USD$/.test(json.arg.instId)) {
json.arg.instId += '_DMCBL' // Universal margin perpetual contract
} else if (/PERP$/.test(json.arg.instId)) {
json.arg.instId += '_CMCBL' // USDC perpetual contract
}
} else if (json.arg.instType === 'sp') {
json.arg.instId += '_SPBL'
}
return this.emitTrades(
api.id,
json.data.map(trade => this.formatTrade(trade, json.arg.instId))
)
}
}
onApiCreated(api) {
this.startKeepAlive(api, 'ping', 30000)
}
onApiRemoved(api) {
this.stopKeepAlive(api)
}
}
module.exports = Bitget