tardis-dev 18.8.2 → 18.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/consts.d.ts +3 -1
- package/dist/consts.d.ts.map +1 -1
- package/dist/consts.js +19 -2
- package/dist/consts.js.map +1 -1
- package/dist/mappers/ascendex.d.ts +4 -4
- package/dist/mappers/aster.d.ts +8 -8
- package/dist/mappers/backpack.d.ts +53 -0
- package/dist/mappers/backpack.d.ts.map +1 -0
- package/dist/mappers/backpack.js +201 -0
- package/dist/mappers/backpack.js.map +1 -0
- package/dist/mappers/binance.d.ts +18 -18
- package/dist/mappers/binanceeuropeanoptions.d.ts +1 -1
- package/dist/mappers/bitfinex.d.ts +8 -8
- package/dist/mappers/bitflyer.d.ts +1 -1
- package/dist/mappers/bitget.d.ts +1 -1
- package/dist/mappers/bithumb.d.ts +2 -2
- package/dist/mappers/bitmex.d.ts +2 -2
- package/dist/mappers/bitnomial.d.ts +1 -1
- package/dist/mappers/bitstamp.d.ts +2 -2
- package/dist/mappers/bitvavo.d.ts +3 -3
- package/dist/mappers/blockchaincom.d.ts +2 -2
- package/dist/mappers/bullish.d.ts +5 -5
- package/dist/mappers/bybit.d.ts +10 -10
- package/dist/mappers/bybitspot.d.ts +1 -1
- package/dist/mappers/coinbase.d.ts +1 -1
- package/dist/mappers/coinbasederivatives.d.ts +4 -4
- package/dist/mappers/coinbaseinternational.d.ts +2 -2
- package/dist/mappers/coinflex.d.ts +1 -1
- package/dist/mappers/cryptocom.d.ts +5 -5
- package/dist/mappers/cryptofacilities.d.ts +2 -2
- package/dist/mappers/delta.d.ts +1 -1
- package/dist/mappers/delta_india.d.ts +5 -5
- package/dist/mappers/deribit.d.ts +3 -3
- package/dist/mappers/dydx.d.ts +3 -3
- package/dist/mappers/dydxv4.d.ts +4 -4
- package/dist/mappers/ftx.d.ts +8 -8
- package/dist/mappers/gateio.d.ts +1 -1
- package/dist/mappers/gateiofutures.d.ts +4 -4
- package/dist/mappers/gemini.d.ts +1 -1
- package/dist/mappers/huobi.d.ts +26 -26
- package/dist/mappers/hyperliquid.d.ts +3 -3
- package/dist/mappers/index.d.ts +249 -195
- package/dist/mappers/index.d.ts.map +1 -1
- package/dist/mappers/index.js +3 -1
- package/dist/mappers/index.js.map +1 -1
- package/dist/mappers/kucoin.d.ts +3 -3
- package/dist/mappers/kucoinfutures.d.ts +4 -4
- package/dist/mappers/lighter.d.ts +10 -10
- package/dist/mappers/mapper.d.ts +7 -0
- package/dist/mappers/mapper.d.ts.map +1 -1
- package/dist/mappers/mapper.js.map +1 -1
- package/dist/mappers/mexc.d.ts +3 -3
- package/dist/mappers/mexcfutures.d.ts +3 -3
- package/dist/mappers/okex.d.ts +15 -15
- package/dist/mappers/phemex.d.ts +1 -1
- package/dist/mappers/polymarket.d.ts +3 -3
- package/dist/mappers/serum.d.ts +9 -9
- package/dist/mappers/upbit.d.ts +2 -2
- package/dist/mappers/whitebit.d.ts +10 -0
- package/dist/mappers/whitebit.d.ts.map +1 -0
- package/dist/mappers/whitebit.js +146 -0
- package/dist/mappers/whitebit.js.map +1 -0
- package/dist/realtimefeeds/backpack.d.ts +19 -0
- package/dist/realtimefeeds/backpack.d.ts.map +1 -0
- package/dist/realtimefeeds/backpack.js +65 -0
- package/dist/realtimefeeds/backpack.js.map +1 -0
- package/dist/realtimefeeds/index.d.ts.map +1 -1
- package/dist/realtimefeeds/index.js +5 -1
- package/dist/realtimefeeds/index.js.map +1 -1
- package/dist/realtimefeeds/whitebit.d.ts +37 -0
- package/dist/realtimefeeds/whitebit.d.ts.map +1 -0
- package/dist/realtimefeeds/whitebit.js +115 -0
- package/dist/realtimefeeds/whitebit.js.map +1 -0
- package/package.json +1 -1
- package/src/consts.ts +21 -2
- package/src/mappers/backpack.ts +303 -0
- package/src/mappers/index.ts +5 -1
- package/src/mappers/mapper.ts +7 -0
- package/src/mappers/whitebit.ts +266 -0
- package/src/realtimefeeds/backpack.ts +78 -0
- package/src/realtimefeeds/index.ts +5 -1
- package/src/realtimefeeds/whitebit.ts +147 -0
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { asNonZeroNumberOrUndefined, asNumberOrUndefined, fromMicroSecondsToDate, isNullOrEmptyArray, upperCaseSymbols } from '../handy.ts'
|
|
2
|
+
import { BookChange, BookTicker, DerivativeTicker, Trade } from '../types.ts'
|
|
3
|
+
import { Mapper, PendingTickerInfoHelper } from './mapper.ts'
|
|
4
|
+
import { exchangeMappers } from './registry.ts'
|
|
5
|
+
|
|
6
|
+
export const whitebitMappers = exchangeMappers({
|
|
7
|
+
whitebit: {
|
|
8
|
+
trades: () => new WhiteBitTradesMapper(),
|
|
9
|
+
bookChanges: () => new WhiteBitBookChangeMapper(),
|
|
10
|
+
bookTickers: () => new WhiteBitBookTickerMapper(),
|
|
11
|
+
derivativeTickers: () => new WhiteBitDerivativeTickerMapper()
|
|
12
|
+
}
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
class WhiteBitTradesMapper implements Mapper<'whitebit', Trade> {
|
|
16
|
+
canHandle(message: WhiteBitMessage) {
|
|
17
|
+
return message.method === 'trades_update'
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
getFilters(symbols?: string[]) {
|
|
21
|
+
return [{ channel: 'trades_update', symbols: upperCaseSymbols(symbols) } as const]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
*map(message: WhiteBitTrades, localTimestamp: Date): IterableIterator<Trade> {
|
|
25
|
+
const [symbol, trades] = message.params
|
|
26
|
+
// Subscription backfills are newest first; live executions are incremental.
|
|
27
|
+
// A singleton has no snapshot marker, so retain it rather than discard a possible live trade at replay startup.
|
|
28
|
+
if (trades.length > 1 && trades[0].id > trades[trades.length - 1].id) {
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
for (const trade of trades) {
|
|
33
|
+
yield {
|
|
34
|
+
type: 'trade',
|
|
35
|
+
exchange: 'whitebit',
|
|
36
|
+
symbol: symbol.toUpperCase(),
|
|
37
|
+
id: String(trade.id),
|
|
38
|
+
price: Number(trade.price),
|
|
39
|
+
amount: Number(trade.amount),
|
|
40
|
+
side: trade.type,
|
|
41
|
+
timestamp: fromMicroSecondsToDate(Math.round(trade.time * 1e6)),
|
|
42
|
+
localTimestamp
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
class WhiteBitBookChangeMapper implements Mapper<'whitebit', BookChange> {
|
|
49
|
+
canHandle(message: WhiteBitMessage) {
|
|
50
|
+
return message.method === 'depth_update'
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
getFilters(symbols?: string[]) {
|
|
54
|
+
return [{ channel: 'depth_update', symbols: upperCaseSymbols(symbols) } as const]
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
*map(message: WhiteBitDepth, localTimestamp: Date): IterableIterator<BookChange> {
|
|
58
|
+
const [isSnapshot, book, symbol] = message.params
|
|
59
|
+
|
|
60
|
+
yield {
|
|
61
|
+
type: 'book_change',
|
|
62
|
+
exchange: 'whitebit',
|
|
63
|
+
symbol: symbol.toUpperCase(),
|
|
64
|
+
isSnapshot,
|
|
65
|
+
bids: (book.bids ?? []).map(this._mapBookLevel),
|
|
66
|
+
asks: (book.asks ?? []).map(this._mapBookLevel),
|
|
67
|
+
timestamp: fromMicroSecondsToDate(Math.round(book.timestamp * 1e6)),
|
|
68
|
+
localTimestamp
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private _mapBookLevel([price, amount]: [string, string]) {
|
|
73
|
+
return {
|
|
74
|
+
price: Number(price),
|
|
75
|
+
amount: Number(amount)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
class WhiteBitBookTickerMapper implements Mapper<'whitebit', BookTicker> {
|
|
81
|
+
canHandle(message: WhiteBitMessage) {
|
|
82
|
+
return message.method === 'bookTicker_update'
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
getFilters(symbols?: string[]) {
|
|
86
|
+
return [{ channel: 'bookTicker_update', symbols: upperCaseSymbols(symbols) } as const]
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
*map(message: WhiteBitBookTicker, localTimestamp: Date): IterableIterator<BookTicker> {
|
|
90
|
+
for (const [transactionTime, , symbol, , bidPrice, bidAmount, askPrice, askAmount] of message.params) {
|
|
91
|
+
yield {
|
|
92
|
+
type: 'book_ticker',
|
|
93
|
+
exchange: 'whitebit',
|
|
94
|
+
symbol: symbol.toUpperCase(),
|
|
95
|
+
bidPrice: asNonZeroNumberOrUndefined(bidPrice),
|
|
96
|
+
bidAmount: asNonZeroNumberOrUndefined(bidAmount),
|
|
97
|
+
askPrice: asNonZeroNumberOrUndefined(askPrice),
|
|
98
|
+
askAmount: asNonZeroNumberOrUndefined(askAmount),
|
|
99
|
+
timestamp: fromMicroSecondsToDate(Math.round(transactionTime * 1e6)),
|
|
100
|
+
localTimestamp
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
class WhiteBitDerivativeTickerMapper implements Mapper<'whitebit', DerivativeTicker> {
|
|
107
|
+
private readonly _pendingTickers = new PendingTickerInfoHelper()
|
|
108
|
+
private readonly _symbolsWithTrades = new Set<string>()
|
|
109
|
+
|
|
110
|
+
canHandle(message: WhiteBitMessage) {
|
|
111
|
+
return (
|
|
112
|
+
message.method === 'premiumIndex_update' ||
|
|
113
|
+
message.method === 'futures_update' ||
|
|
114
|
+
(message.method === 'trades_update' && (message as WhiteBitTrades).params[0].endsWith('_PERP'))
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
getFilters(symbols?: string[]) {
|
|
119
|
+
symbols = upperCaseSymbols(symbols)
|
|
120
|
+
const perpetuals = symbols?.filter((symbol) => symbol.endsWith('_PERP')) ?? []
|
|
121
|
+
|
|
122
|
+
return [
|
|
123
|
+
{ channel: 'trades_update', symbols } as const,
|
|
124
|
+
...(isNullOrEmptyArray(symbols) || perpetuals.length > 0
|
|
125
|
+
? [{ channel: 'premiumIndex_update', symbols: perpetuals } as const, { channel: 'futures_update', symbols: perpetuals } as const]
|
|
126
|
+
: [])
|
|
127
|
+
]
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
*map({ method, params }: WhiteBitPremiumIndex | WhiteBitFutures | WhiteBitTrades, localTimestamp: Date) {
|
|
131
|
+
const symbol = params[0].toUpperCase()
|
|
132
|
+
const ticker = this._pendingTickers.getPendingTickerInfo(symbol, 'whitebit')
|
|
133
|
+
|
|
134
|
+
if (method === 'trades_update') {
|
|
135
|
+
// Backfills can initialize the latest price, but never emit historical derivative ticker events.
|
|
136
|
+
let latestTrade = params[1][0]
|
|
137
|
+
for (const trade of params[1]) {
|
|
138
|
+
if (trade.id > latestTrade.id) {
|
|
139
|
+
latestTrade = trade
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (latestTrade === undefined) {
|
|
143
|
+
return
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
this._symbolsWithTrades.add(symbol)
|
|
147
|
+
ticker.updateLastPrice(asNumberOrUndefined(latestTrade.price))
|
|
148
|
+
return
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (method === 'futures_update') {
|
|
152
|
+
// REST has no trade timestamp, so use its price only until a WebSocket trade has been accepted.
|
|
153
|
+
ticker.updateOpenInterest(asNumberOrUndefined(params[1].open_interest))
|
|
154
|
+
if (!this._symbolsWithTrades.has(symbol)) {
|
|
155
|
+
ticker.updateLastPrice(asNumberOrUndefined(params[1].last_price))
|
|
156
|
+
}
|
|
157
|
+
return
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const [, markPrice, indexPrice, fundingRate, fundingTime, serverTime] = params
|
|
161
|
+
ticker.updateMarkPrice(asNumberOrUndefined(markPrice))
|
|
162
|
+
ticker.updateIndexPrice(asNumberOrUndefined(indexPrice))
|
|
163
|
+
ticker.updateFundingRate(asNumberOrUndefined(fundingRate))
|
|
164
|
+
ticker.updateFundingTimestamp(new Date(fundingTime))
|
|
165
|
+
ticker.updateTimestamp(fromMicroSecondsToDate(Math.round(serverTime * 1e6)))
|
|
166
|
+
if (ticker.hasChanged()) {
|
|
167
|
+
yield ticker.getSnapshot(localTimestamp)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
type WhiteBitMessage<TMethod extends string = string, TParams = unknown> = {
|
|
173
|
+
method: TMethod
|
|
174
|
+
params: TParams
|
|
175
|
+
id: null
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** @see https://docs.whitebit.com/websocket/market-streams/trades */
|
|
179
|
+
type WhiteBitTrades = WhiteBitMessage<
|
|
180
|
+
'trades_update',
|
|
181
|
+
[
|
|
182
|
+
symbol: string,
|
|
183
|
+
trades: {
|
|
184
|
+
id: number
|
|
185
|
+
time: number
|
|
186
|
+
price: string
|
|
187
|
+
amount: string
|
|
188
|
+
type: 'buy' | 'sell'
|
|
189
|
+
rpi?: boolean
|
|
190
|
+
}[]
|
|
191
|
+
]
|
|
192
|
+
>
|
|
193
|
+
|
|
194
|
+
/** @see https://docs.whitebit.com/websocket/market-streams/depth */
|
|
195
|
+
type WhiteBitDepth = WhiteBitMessage<
|
|
196
|
+
'depth_update',
|
|
197
|
+
[
|
|
198
|
+
isSnapshot: boolean,
|
|
199
|
+
book: {
|
|
200
|
+
timestamp: number
|
|
201
|
+
event_time: number
|
|
202
|
+
update_id: number
|
|
203
|
+
past_update_id?: number
|
|
204
|
+
bids?: [price: string, amount: string][]
|
|
205
|
+
asks?: [price: string, amount: string][]
|
|
206
|
+
},
|
|
207
|
+
symbol: string
|
|
208
|
+
]
|
|
209
|
+
>
|
|
210
|
+
|
|
211
|
+
/** @see https://docs.whitebit.com/websocket/market-streams/book-ticker */
|
|
212
|
+
type WhiteBitBookTicker = WhiteBitMessage<
|
|
213
|
+
'bookTicker_update',
|
|
214
|
+
[
|
|
215
|
+
transactionTime: number,
|
|
216
|
+
messageTime: number,
|
|
217
|
+
symbol: string,
|
|
218
|
+
updateId: number,
|
|
219
|
+
bidPrice: string,
|
|
220
|
+
bidAmount: string,
|
|
221
|
+
askPrice: string,
|
|
222
|
+
askAmount: string
|
|
223
|
+
][]
|
|
224
|
+
>
|
|
225
|
+
|
|
226
|
+
/** @see https://docs.whitebit.com/websocket/market-streams/premium-index */
|
|
227
|
+
type WhiteBitPremiumIndex = WhiteBitMessage<
|
|
228
|
+
'premiumIndex_update',
|
|
229
|
+
[symbol: string, markPrice: string, indexPrice: string, fundingRate: string, fundingTime: number, serverTime: number]
|
|
230
|
+
>
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Recorder-generated envelope containing the original REST market record.
|
|
234
|
+
* @see https://docs.whitebit.com/api-reference/market-data/available-futures-markets-list
|
|
235
|
+
*/
|
|
236
|
+
type WhiteBitFutures = WhiteBitMessage<
|
|
237
|
+
'futures_update',
|
|
238
|
+
[
|
|
239
|
+
symbol: string,
|
|
240
|
+
market: {
|
|
241
|
+
ticker_id: string
|
|
242
|
+
stock_currency: string
|
|
243
|
+
money_currency: string
|
|
244
|
+
last_price: string
|
|
245
|
+
stock_volume: string
|
|
246
|
+
money_volume: string
|
|
247
|
+
bid: string
|
|
248
|
+
ask: string
|
|
249
|
+
high: string
|
|
250
|
+
low: string
|
|
251
|
+
product_type: 'Perpetual'
|
|
252
|
+
open_interest: string
|
|
253
|
+
mark_price: string
|
|
254
|
+
index_price: string
|
|
255
|
+
index_name: string
|
|
256
|
+
index_currency: string
|
|
257
|
+
funding_rate: string
|
|
258
|
+
funding_cap: string
|
|
259
|
+
funding_floor: string
|
|
260
|
+
next_funding_rate_timestamp: string
|
|
261
|
+
brackets: Record<string, number>
|
|
262
|
+
max_leverage: number
|
|
263
|
+
funding_interval_minutes: number
|
|
264
|
+
}
|
|
265
|
+
]
|
|
266
|
+
> & { generated: true }
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { EXCHANGE_CHANNELS_INFO } from '../consts.ts'
|
|
2
|
+
import { batch, getJSON } from '../handy.ts'
|
|
3
|
+
import type { BackpackSnapshotMessage } from '../mappers/backpack.ts'
|
|
4
|
+
import { Filter } from '../types.ts'
|
|
5
|
+
import { MultiConnectionRealTimeFeedBase, RealTimeFeedBase } from './realtimefeed.ts'
|
|
6
|
+
|
|
7
|
+
export class BackpackRealTimeFeed extends MultiConnectionRealTimeFeedBase {
|
|
8
|
+
protected *_getRealTimeFeeds(exchange: string, filters: Filter<string>[], timeoutIntervalMS?: number, onError?: (error: Error) => void) {
|
|
9
|
+
for (const filter of filters) {
|
|
10
|
+
if (!filter.symbols?.length) {
|
|
11
|
+
throw new Error('Backpack requires explicitly specified symbols for live subscriptions')
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const symbols = [...new Set(filters.flatMap((filter) => filter.symbols!))]
|
|
16
|
+
for (const instruments of batch(symbols, 20)) {
|
|
17
|
+
const selected = new Set(instruments)
|
|
18
|
+
const connectionFilters = filters
|
|
19
|
+
.map((filter) => ({
|
|
20
|
+
channel: filter.channel,
|
|
21
|
+
symbols: filter.symbols!.filter((symbol) => selected.has(symbol))
|
|
22
|
+
}))
|
|
23
|
+
.filter((filter) => filter.symbols.length > 0)
|
|
24
|
+
|
|
25
|
+
yield new BackpackSingleConnectionRealTimeFeed(exchange, connectionFilters, timeoutIntervalMS, onError)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class BackpackSingleConnectionRealTimeFeed extends RealTimeFeedBase {
|
|
31
|
+
protected readonly wssURL = 'wss://ws.backpack.exchange'
|
|
32
|
+
protected readonly httpURL: string = 'https://api.backpack.exchange'
|
|
33
|
+
private readonly channels = new Set<string>(EXCHANGE_CHANNELS_INFO.backpack)
|
|
34
|
+
|
|
35
|
+
protected mapToSubscribeMessages(filters: Filter<string>[]) {
|
|
36
|
+
const depthSymbols = new Set(filters.filter((filter) => filter.channel === 'depth').flatMap((filter) => filter.symbols ?? []))
|
|
37
|
+
for (const filter of filters) {
|
|
38
|
+
if (!this.channels.has(filter.channel)) {
|
|
39
|
+
throw new Error(`Unsupported Backpack channel: ${filter.channel}`)
|
|
40
|
+
}
|
|
41
|
+
if (!filter.symbols?.length) {
|
|
42
|
+
throw new Error('Backpack requires explicitly specified symbols for live subscriptions')
|
|
43
|
+
}
|
|
44
|
+
if (filter.channel === 'depthSnapshot' && filter.symbols.some((symbol) => !depthSymbols.has(symbol))) {
|
|
45
|
+
throw new Error('Backpack depthSnapshot requires a depth subscription for each snapshot symbol')
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const params = [
|
|
50
|
+
...new Set(
|
|
51
|
+
filters
|
|
52
|
+
.filter((filter) => filter.channel !== 'depthSnapshot')
|
|
53
|
+
.flatMap((filter) => filter.symbols!.map((symbol) => `${filter.channel}.${symbol}`))
|
|
54
|
+
)
|
|
55
|
+
]
|
|
56
|
+
return [{ method: 'SUBSCRIBE', params }]
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
protected messageIsError(message: { error?: unknown }) {
|
|
60
|
+
return message.error !== undefined
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
protected async provideManualSnapshots(filters: Filter<string>[], shouldCancel: () => boolean) {
|
|
64
|
+
const symbols = [...new Set(filters.filter((filter) => filter.channel === 'depthSnapshot').flatMap((filter) => filter.symbols!))]
|
|
65
|
+
for (const instruments of batch(symbols, 4)) {
|
|
66
|
+
if (shouldCancel()) return
|
|
67
|
+
await Promise.all(
|
|
68
|
+
instruments.map(async (symbol) => {
|
|
69
|
+
const { data } = await getJSON<BackpackSnapshotMessage['data']>(
|
|
70
|
+
`${this.httpURL}/api/v1/depth?symbol=${encodeURIComponent(symbol)}`
|
|
71
|
+
)
|
|
72
|
+
if (shouldCancel()) return
|
|
73
|
+
this.manualSnapshotsBuffer.push({ stream: `depthSnapshot.${symbol}`, generated: true, data })
|
|
74
|
+
})
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Exchange, Filter } from '../types.ts'
|
|
2
2
|
import { AsterFuturesRealTimeFeed, AsterRealTimeFeed } from './aster.ts'
|
|
3
|
+
import { BackpackRealTimeFeed } from './backpack.ts'
|
|
3
4
|
import {
|
|
4
5
|
BinanceFuturesRealTimeFeed,
|
|
5
6
|
BinanceJerseyRealTimeFeed,
|
|
@@ -59,6 +60,7 @@ import { BullishRealTimeFeed } from './bullish.ts'
|
|
|
59
60
|
import { MexcFuturesRealTimeFeed } from './mexcfutures.ts'
|
|
60
61
|
import { MexcRealTimeFeed } from './mexc.ts'
|
|
61
62
|
import { PolymarketRealTimeFeed } from './polymarket.ts'
|
|
63
|
+
import { WhiteBitRealTimeFeed } from './whitebit.ts'
|
|
62
64
|
|
|
63
65
|
export * from './realtimefeed.ts'
|
|
64
66
|
|
|
@@ -66,6 +68,7 @@ const realTimeFeedsMap: {
|
|
|
66
68
|
[key in Exchange]?: RealTimeFeed
|
|
67
69
|
} = {
|
|
68
70
|
aster: AsterRealTimeFeed,
|
|
71
|
+
backpack: BackpackRealTimeFeed,
|
|
69
72
|
'aster-futures': AsterFuturesRealTimeFeed,
|
|
70
73
|
binance: BinanceRealTimeFeed,
|
|
71
74
|
'binance-jersey': BinanceJerseyRealTimeFeed,
|
|
@@ -130,7 +133,8 @@ const realTimeFeedsMap: {
|
|
|
130
133
|
'lighter-rh': LighterRhRealTimeFeed,
|
|
131
134
|
bullish: BullishRealTimeFeed,
|
|
132
135
|
mexc: MexcRealTimeFeed,
|
|
133
|
-
polymarket: PolymarketRealTimeFeed
|
|
136
|
+
polymarket: PolymarketRealTimeFeed,
|
|
137
|
+
whitebit: WhiteBitRealTimeFeed
|
|
134
138
|
}
|
|
135
139
|
|
|
136
140
|
export function getRealTimeFeedFactory(exchange: Exchange): RealTimeFeed {
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { Writable } from 'stream'
|
|
2
|
+
import { setTimeout as sleep } from 'node:timers/promises'
|
|
3
|
+
import { batch, getJSON } from '../handy.ts'
|
|
4
|
+
import { Filter } from '../types.ts'
|
|
5
|
+
import { MultiConnectionRealTimeFeedBase, PoolingClientBase, RealTimeFeedBase } from './realtimefeed.ts'
|
|
6
|
+
|
|
7
|
+
export class WhiteBitRealTimeFeed extends MultiConnectionRealTimeFeedBase {
|
|
8
|
+
protected readonly httpURL: string = 'https://whitebit.com/api/v4/public'
|
|
9
|
+
|
|
10
|
+
protected *_getRealTimeFeeds(exchange: string, filters: Filter<string>[], timeoutIntervalMS?: number, onError?: (error: Error) => void) {
|
|
11
|
+
filters = filters.map((filter) => ({
|
|
12
|
+
...filter,
|
|
13
|
+
symbols: filter.symbols?.map((symbol) => symbol.toUpperCase())
|
|
14
|
+
}))
|
|
15
|
+
const webSocketFilters = filters.filter((filter) => filter.channel !== 'futures_update')
|
|
16
|
+
for (const filter of webSocketFilters) {
|
|
17
|
+
if (filter.symbols === undefined || filter.symbols.length === 0) {
|
|
18
|
+
throw new Error('WhiteBitRealTimeFeed requires explicitly specified symbols when subscribing to live feed')
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const webSocketSymbols = [...new Set(webSocketFilters.flatMap((filter) => filter.symbols!))]
|
|
22
|
+
const spotSymbols = webSocketSymbols.filter((symbol) => !symbol.endsWith('_PERP'))
|
|
23
|
+
const perpetualSymbols = webSocketSymbols.filter((symbol) => symbol.endsWith('_PERP'))
|
|
24
|
+
let connectionIndex = 0
|
|
25
|
+
|
|
26
|
+
for (const marketSymbols of [spotSymbols, perpetualSymbols]) {
|
|
27
|
+
for (const group of batch(marketSymbols, 50)) {
|
|
28
|
+
yield new WhiteBitWebSocketRealTimeFeed(
|
|
29
|
+
exchange,
|
|
30
|
+
webSocketFilters
|
|
31
|
+
.map((filter) => ({ ...filter, symbols: filter.symbols!.filter((symbol) => group.includes(symbol)) }))
|
|
32
|
+
.filter((filter) => filter.symbols.length > 0),
|
|
33
|
+
timeoutIntervalMS,
|
|
34
|
+
onError,
|
|
35
|
+
connectionIndex++ * 50
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const futuresFilters = filters.filter((filter) => filter.channel === 'futures_update')
|
|
41
|
+
if (futuresFilters.length > 0) {
|
|
42
|
+
const allFutures = futuresFilters.some((filter) => !filter.symbols?.length)
|
|
43
|
+
yield new WhiteBitFuturesClient(
|
|
44
|
+
exchange,
|
|
45
|
+
allFutures ? [] : futuresFilters.flatMap((filter) => filter.symbols!),
|
|
46
|
+
this.httpURL,
|
|
47
|
+
onError
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export class WhiteBitWebSocketRealTimeFeed extends RealTimeFeedBase {
|
|
54
|
+
protected readonly wssURL = 'wss://wss.whitebit.com/ws'
|
|
55
|
+
private readonly _lastUpdateIds = new Map<string, number>()
|
|
56
|
+
|
|
57
|
+
constructor(
|
|
58
|
+
exchange: string,
|
|
59
|
+
filters: Filter<string>[],
|
|
60
|
+
timeoutIntervalMS: number | undefined,
|
|
61
|
+
onError?: (error: Error) => void,
|
|
62
|
+
private readonly _startDelayMS = 0
|
|
63
|
+
) {
|
|
64
|
+
super(exchange, filters, timeoutIntervalMS, onError)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
protected async getWebSocketUrl() {
|
|
68
|
+
await sleep(this._startDelayMS)
|
|
69
|
+
return super.getWebSocketUrl()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
protected mapToSubscribeMessages(filters: Filter<string>[]) {
|
|
73
|
+
this._lastUpdateIds.clear()
|
|
74
|
+
let id = 0
|
|
75
|
+
return filters.flatMap(({ channel, symbols }) => {
|
|
76
|
+
const method = channel.replace('_update', '_subscribe')
|
|
77
|
+
|
|
78
|
+
if (channel === 'depth_update') {
|
|
79
|
+
return symbols!.map((symbol) => ({
|
|
80
|
+
id: ++id,
|
|
81
|
+
method,
|
|
82
|
+
params: [symbol, 100, '0', true]
|
|
83
|
+
}))
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (channel === 'bookTicker_update') {
|
|
87
|
+
return symbols!.map((symbol) => ({
|
|
88
|
+
id: ++id,
|
|
89
|
+
method,
|
|
90
|
+
params: [symbol]
|
|
91
|
+
}))
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return [
|
|
95
|
+
{
|
|
96
|
+
id: ++id,
|
|
97
|
+
method,
|
|
98
|
+
params: symbols!
|
|
99
|
+
}
|
|
100
|
+
]
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
protected messageIsError(message: { error?: unknown }) {
|
|
105
|
+
return message.error !== undefined && message.error !== null
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
protected onMessage(message: { method?: string; params?: [boolean, { update_id: number; past_update_id?: number }, string] }) {
|
|
109
|
+
if (message.method !== 'depth_update') {
|
|
110
|
+
return
|
|
111
|
+
}
|
|
112
|
+
const [isSnapshot, book, symbol] = message.params!
|
|
113
|
+
// Snapshot resets may jump over changes outside the subscribed depth; only deltas must link to the previous update.
|
|
114
|
+
if (!isSnapshot && book.past_update_id !== this._lastUpdateIds.get(symbol)) {
|
|
115
|
+
throw new Error(`WhiteBIT depth sequence gap for ${symbol}`)
|
|
116
|
+
}
|
|
117
|
+
this._lastUpdateIds.set(symbol, book.update_id)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
class WhiteBitFuturesClient extends PoolingClientBase {
|
|
122
|
+
constructor(
|
|
123
|
+
exchange: string,
|
|
124
|
+
private readonly _symbols: string[],
|
|
125
|
+
private readonly _httpURL: string,
|
|
126
|
+
onError?: (error: Error) => void
|
|
127
|
+
) {
|
|
128
|
+
super(exchange, 6, onError)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
protected async poolDataToStream(outputStream: Writable) {
|
|
132
|
+
const { data: response } = await getJSON<{ success: boolean; result: { ticker_id: string }[] }>(`${this._httpURL}/futures`, {
|
|
133
|
+
timeout: 5000
|
|
134
|
+
})
|
|
135
|
+
if (!response.success) {
|
|
136
|
+
throw new Error('WhiteBIT futures request was unsuccessful')
|
|
137
|
+
}
|
|
138
|
+
for (const market of response.result) {
|
|
139
|
+
if (this._symbols.length > 0 && !this._symbols.includes(market.ticker_id)) {
|
|
140
|
+
continue
|
|
141
|
+
}
|
|
142
|
+
if (outputStream.writable) {
|
|
143
|
+
outputStream.write({ method: 'futures_update', params: [market.ticker_id, market], id: null, generated: true })
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|