tardis-dev 18.9.0 → 18.10.1

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.
Files changed (73) hide show
  1. package/dist/consts.d.ts +2 -1
  2. package/dist/consts.d.ts.map +1 -1
  3. package/dist/consts.js +14 -0
  4. package/dist/consts.js.map +1 -1
  5. package/dist/mappers/ascendex.d.ts +4 -4
  6. package/dist/mappers/aster.d.ts +8 -8
  7. package/dist/mappers/backpack.d.ts +53 -0
  8. package/dist/mappers/backpack.d.ts.map +1 -0
  9. package/dist/mappers/backpack.js +223 -0
  10. package/dist/mappers/backpack.js.map +1 -0
  11. package/dist/mappers/binance.d.ts +18 -18
  12. package/dist/mappers/binanceeuropeanoptions.d.ts +1 -1
  13. package/dist/mappers/bitfinex.d.ts +8 -8
  14. package/dist/mappers/bitflyer.d.ts +1 -1
  15. package/dist/mappers/bitget.d.ts +1 -1
  16. package/dist/mappers/bithumb.d.ts +2 -2
  17. package/dist/mappers/bitmex.d.ts +2 -2
  18. package/dist/mappers/bitnomial.d.ts +1 -1
  19. package/dist/mappers/bitstamp.d.ts +2 -2
  20. package/dist/mappers/bitvavo.d.ts +3 -3
  21. package/dist/mappers/blockchaincom.d.ts +2 -2
  22. package/dist/mappers/bullish.d.ts +5 -5
  23. package/dist/mappers/bybit.d.ts +10 -10
  24. package/dist/mappers/bybitspot.d.ts +1 -1
  25. package/dist/mappers/coinbase.d.ts +1 -1
  26. package/dist/mappers/coinbasederivatives.d.ts +4 -4
  27. package/dist/mappers/coinbaseinternational.d.ts +2 -2
  28. package/dist/mappers/coinflex.d.ts +1 -1
  29. package/dist/mappers/cryptocom.d.ts +5 -5
  30. package/dist/mappers/cryptofacilities.d.ts +2 -2
  31. package/dist/mappers/delta.d.ts +1 -1
  32. package/dist/mappers/delta_india.d.ts +5 -5
  33. package/dist/mappers/deribit.d.ts +3 -3
  34. package/dist/mappers/dydx.d.ts +3 -3
  35. package/dist/mappers/dydxv4.d.ts +4 -4
  36. package/dist/mappers/ftx.d.ts +8 -8
  37. package/dist/mappers/gateio.d.ts +1 -1
  38. package/dist/mappers/gateiofutures.d.ts +4 -4
  39. package/dist/mappers/gemini.d.ts +1 -1
  40. package/dist/mappers/huobi.d.ts +26 -26
  41. package/dist/mappers/hyperliquid.d.ts +3 -3
  42. package/dist/mappers/index.d.ts +241 -199
  43. package/dist/mappers/index.d.ts.map +1 -1
  44. package/dist/mappers/index.js +2 -1
  45. package/dist/mappers/index.js.map +1 -1
  46. package/dist/mappers/kucoin.d.ts +3 -3
  47. package/dist/mappers/kucoinfutures.d.ts +4 -4
  48. package/dist/mappers/lighter.d.ts +10 -10
  49. package/dist/mappers/mapper.d.ts +7 -0
  50. package/dist/mappers/mapper.d.ts.map +1 -1
  51. package/dist/mappers/mapper.js.map +1 -1
  52. package/dist/mappers/mexc.d.ts +3 -3
  53. package/dist/mappers/mexcfutures.d.ts +3 -3
  54. package/dist/mappers/okex.d.ts +15 -15
  55. package/dist/mappers/phemex.d.ts +1 -1
  56. package/dist/mappers/polymarket.d.ts +3 -3
  57. package/dist/mappers/serum.d.ts +9 -9
  58. package/dist/mappers/upbit.d.ts +2 -2
  59. package/dist/mappers/whitebit.d.ts +4 -4
  60. package/dist/realtimefeeds/backpack.d.ts +19 -0
  61. package/dist/realtimefeeds/backpack.d.ts.map +1 -0
  62. package/dist/realtimefeeds/backpack.js +65 -0
  63. package/dist/realtimefeeds/backpack.js.map +1 -0
  64. package/dist/realtimefeeds/index.d.ts.map +1 -1
  65. package/dist/realtimefeeds/index.js +2 -0
  66. package/dist/realtimefeeds/index.js.map +1 -1
  67. package/package.json +1 -1
  68. package/src/consts.ts +15 -0
  69. package/src/mappers/backpack.ts +325 -0
  70. package/src/mappers/index.ts +2 -0
  71. package/src/mappers/mapper.ts +7 -0
  72. package/src/realtimefeeds/backpack.ts +78 -0
  73. package/src/realtimefeeds/index.ts +2 -0
@@ -0,0 +1,325 @@
1
+ import { asNumberOrUndefined, CircularBuffer, fromMicroSecondsToDate, isNullOrEmptyArray } from '../handy.ts'
2
+ import { BookChange, BookTicker, DerivativeTicker, Liquidation, Trade } from '../types.ts'
3
+ import { Mapper, PendingTickerInfoHelper } from './mapper.ts'
4
+ import { exchangeMappers, isRealTime } from './registry.ts'
5
+
6
+ export const backpackMappers = exchangeMappers({
7
+ backpack: {
8
+ trades: () => new BackpackTradesMapper(),
9
+ bookChanges: (localTimestamp) => new BackpackBookChangeMapper(isRealTime(localTimestamp)),
10
+ bookTickers: () => new BackpackBookTickerMapper(),
11
+ derivativeTickers: () => new BackpackDerivativeTickerMapper(),
12
+ liquidations: () => new BackpackLiquidationsMapper()
13
+ }
14
+ })
15
+
16
+ class BackpackTradesMapper implements Mapper<'backpack', Trade> {
17
+ canHandle(message: BackpackMessage<unknown>) {
18
+ return message.stream?.startsWith('trade.') === true
19
+ }
20
+
21
+ getFilters(symbols?: string[]) {
22
+ return [{ channel: 'trade', symbols } as const]
23
+ }
24
+
25
+ *map({ data }: BackpackTradeMessage, localTimestamp: Date) {
26
+ yield {
27
+ type: 'trade',
28
+ symbol: data.s.toUpperCase(),
29
+ exchange: 'backpack',
30
+ id: String(data.t),
31
+ price: Number(data.p),
32
+ amount: Number(data.q),
33
+ side: data.m ? 'sell' : 'buy',
34
+ timestamp: fromMicroSecondsToDate(data.T),
35
+ localTimestamp
36
+ } as const
37
+ }
38
+ }
39
+
40
+ class BackpackBookChangeMapper implements Mapper<'backpack', BookChange> {
41
+ private readonly bookStates = new Map<string, BackpackBookState>()
42
+
43
+ constructor(private readonly validateSequences: boolean) {}
44
+
45
+ canHandle(message: BackpackMessage<unknown>) {
46
+ return message.stream?.startsWith('depth.') === true || message.stream?.startsWith('depthSnapshot.') === true
47
+ }
48
+
49
+ getFilters(symbols?: string[]) {
50
+ return [{ channel: 'depth', symbols } as const, { channel: 'depthSnapshot', symbols } as const]
51
+ }
52
+
53
+ *map({ stream, data }: BackpackDepthMessage | BackpackSnapshotMessage, localTimestamp: Date): IterableIterator<BookChange> {
54
+ const symbol = stream.slice(stream.indexOf('.') + 1)
55
+
56
+ let bookState = this.bookStates.get(symbol)
57
+ if (bookState === undefined) {
58
+ bookState = { buffered: new CircularBuffer(2000) }
59
+ this.bookStates.set(symbol, bookState)
60
+ }
61
+
62
+ if ('lastUpdateId' in data) {
63
+ if (bookState.sequence !== undefined) {
64
+ return
65
+ }
66
+
67
+ // Bound memory while waiting, but never silently lose updates needed after this snapshot.
68
+ if (bookState.discardedThrough !== undefined && Number(data.lastUpdateId) < bookState.discardedThrough) {
69
+ throw new Error(`Backpack snapshot for ${symbol} does not cover discarded buffered updates`)
70
+ }
71
+ bookState.sequence = Number(data.lastUpdateId)
72
+ // REST snapshot prices drop trailing zeros ("74.8") while depth prices keep them ("74.80"), so match levels by number.
73
+ const bidsByPrice = new Map(data.bids.map((level) => [Number(level[0]), level]))
74
+ const asksByPrice = new Map(data.asks.map((level) => [Number(level[0]), level]))
75
+
76
+ // Updates received before the snapshot are applied to it, so output starts with one current book
77
+ // and never goes back in local time.
78
+ for (const update of bookState.buffered.items()) {
79
+ if (this.advanceSequence(update, bookState)) {
80
+ this.applyLevelUpdates(bidsByPrice, update.b)
81
+ this.applyLevelUpdates(asksByPrice, update.a)
82
+ }
83
+ }
84
+
85
+ bookState.buffered.clear()
86
+
87
+ yield {
88
+ type: 'book_change',
89
+ exchange: 'backpack',
90
+ symbol: symbol.toUpperCase(),
91
+ isSnapshot: true,
92
+ bids: [...bidsByPrice.values()].map(this.mapLevel),
93
+ asks: [...asksByPrice.values()].map(this.mapLevel),
94
+ timestamp: fromMicroSecondsToDate(data.timestamp),
95
+ localTimestamp
96
+ }
97
+ } else if (bookState.sequence === undefined) {
98
+ const discarded = bookState.buffered.append(data)
99
+ if (discarded !== undefined) bookState.discardedThrough = discarded.u
100
+ } else if (this.advanceSequence(data, bookState)) {
101
+ yield this.mapUpdate(data, localTimestamp)
102
+ }
103
+ }
104
+
105
+ private advanceSequence(data: BackpackDepthMessage['data'], bookState: BackpackBookState) {
106
+ if (data.u <= bookState.sequence!) {
107
+ return false
108
+ }
109
+
110
+ if (this.validateSequences && (data.U <= bookState.sequence! + 1 && data.u >= bookState.sequence! + 1) == false) {
111
+ throw new Error(`Backpack depth sequence gap for ${data.s}: expected ${bookState.sequence! + 1}, received ${data.U}-${data.u}`)
112
+ }
113
+ bookState.sequence = data.u
114
+ return true
115
+ }
116
+
117
+ private applyLevelUpdates(levelsByPrice: Map<number, [string, string]>, updates: BackpackLevels) {
118
+ for (const level of updates) {
119
+ const price = Number(level[0])
120
+ if (Number(level[1]) === 0) {
121
+ levelsByPrice.delete(price)
122
+ } else {
123
+ levelsByPrice.set(price, level)
124
+ }
125
+ }
126
+ }
127
+
128
+ private mapUpdate(data: BackpackDepthMessage['data'], localTimestamp: Date): BookChange {
129
+ return {
130
+ type: 'book_change',
131
+ exchange: 'backpack',
132
+ symbol: data.s.toUpperCase(),
133
+ isSnapshot: false,
134
+ bids: data.b.map(this.mapLevel),
135
+ asks: data.a.map(this.mapLevel),
136
+ timestamp: fromMicroSecondsToDate(data.E),
137
+ localTimestamp
138
+ }
139
+ }
140
+ private mapLevel([price, amount]: [string, string]) {
141
+ return {
142
+ price: Number(price),
143
+ amount: Number(amount)
144
+ }
145
+ }
146
+ }
147
+
148
+ class BackpackBookTickerMapper implements Mapper<'backpack', BookTicker> {
149
+ canHandle(message: BackpackMessage<unknown>) {
150
+ return message.stream?.startsWith('bookTicker.') === true
151
+ }
152
+ getFilters(symbols?: string[]) {
153
+ return [{ channel: 'bookTicker', symbols } as const]
154
+ }
155
+ *map({ data }: BackpackBookTickerMessage, localTimestamp: Date) {
156
+ yield {
157
+ type: 'book_ticker',
158
+ exchange: 'backpack',
159
+ symbol: data.s.toUpperCase(),
160
+ askPrice: asNumberOrUndefined(data.a),
161
+ askAmount: asNumberOrUndefined(data.A),
162
+ bidPrice: asNumberOrUndefined(data.b),
163
+ bidAmount: asNumberOrUndefined(data.B),
164
+ timestamp: fromMicroSecondsToDate(data.E),
165
+ localTimestamp
166
+ } as const
167
+ }
168
+ }
169
+
170
+ class BackpackDerivativeTickerMapper implements Mapper<'backpack', DerivativeTicker> {
171
+ private readonly pending = new PendingTickerInfoHelper()
172
+
173
+ canHandle(message: BackpackMessage<unknown>) {
174
+ return /^(markPrice|openInterest|ticker)\..+_PERP$/.test(message.stream)
175
+ }
176
+
177
+ getFilters(symbols?: string[]) {
178
+ if (!isNullOrEmptyArray(symbols)) {
179
+ symbols = symbols!.filter((symbol) => symbol.endsWith('_PERP'))
180
+ if (symbols.length === 0) {
181
+ return []
182
+ }
183
+ }
184
+ return [
185
+ { channel: 'markPrice', symbols } as const,
186
+ { channel: 'openInterest', symbols } as const,
187
+ { channel: 'ticker', symbols } as const
188
+ ]
189
+ }
190
+
191
+ *map(
192
+ { stream, data }: BackpackTickerMessage | BackpackMarkPriceMessage | BackpackOpenInterestMessage,
193
+ localTimestamp: Date
194
+ ): IterableIterator<DerivativeTicker> {
195
+ const ticker = this.pending.getPendingTickerInfo(data.s.toUpperCase(), 'backpack')
196
+
197
+ // Ticker and open interest update cached state. markPrice owns output and its
198
+ // microsecond event timestamp, avoiding regressions between independent channels.
199
+ if (stream.startsWith('openInterest.')) {
200
+ ticker.updateOpenInterest(asNumberOrUndefined((data as BackpackOpenInterestMessage['data']).o))
201
+ return
202
+ }
203
+
204
+ if (stream.startsWith('ticker.')) {
205
+ ticker.updateLastPrice(asNumberOrUndefined((data as BackpackTickerMessage['data']).c))
206
+ return
207
+ }
208
+
209
+ const mark = data as BackpackMarkPriceMessage['data']
210
+ ticker.updateMarkPrice(asNumberOrUndefined(mark.p))
211
+ ticker.updateIndexPrice(asNumberOrUndefined(mark.i))
212
+ ticker.updateFundingRate(asNumberOrUndefined(mark.f))
213
+ ticker.updateFundingTimestamp(new Date(mark.n))
214
+ ticker.updateTimestamp(fromMicroSecondsToDate(mark.E))
215
+
216
+ if (ticker.hasChanged()) {
217
+ yield ticker.getSnapshot(localTimestamp)
218
+ }
219
+ }
220
+ }
221
+
222
+ class BackpackLiquidationsMapper implements Mapper<'backpack', Liquidation> {
223
+ canHandle(message: BackpackMessage<unknown>) {
224
+ return message.stream?.startsWith('liquidation.') === true
225
+ }
226
+
227
+ getFilters(symbols?: string[]) {
228
+ if (!isNullOrEmptyArray(symbols)) {
229
+ symbols = symbols!.filter((symbol) => symbol.endsWith('_PERP'))
230
+ if (symbols.length === 0) {
231
+ return []
232
+ }
233
+ }
234
+ return [{ channel: 'liquidation', symbols } as const]
235
+ }
236
+
237
+ *map({ data }: BackpackLiquidationMessage, localTimestamp: Date) {
238
+ yield {
239
+ type: 'liquidation',
240
+ exchange: 'backpack',
241
+ symbol: data.s.toUpperCase(),
242
+ id: undefined,
243
+ price: Number(data.p),
244
+ amount: Number(data.q),
245
+ side: data.S === 'Bid' ? 'buy' : 'sell',
246
+ timestamp: fromMicroSecondsToDate(data.E),
247
+ localTimestamp
248
+ } as const
249
+ }
250
+ }
251
+
252
+ type BackpackBookState = {
253
+ sequence?: number
254
+ discardedThrough?: number
255
+ buffered: CircularBuffer<BackpackDepthMessage['data']>
256
+ }
257
+
258
+ type BackpackMessage<T> = {
259
+ stream: string
260
+ data: T
261
+ }
262
+
263
+ type BackpackTradeMessage = BackpackMessage<{
264
+ s: string
265
+ t: number
266
+ p: string
267
+ q: string
268
+ m: boolean
269
+ T: number
270
+ }>
271
+
272
+ type BackpackDepthMessage = BackpackMessage<{
273
+ s: string
274
+ U: number
275
+ u: number
276
+ a: BackpackLevels
277
+ b: BackpackLevels
278
+ E: number
279
+ }>
280
+
281
+ export type BackpackSnapshotMessage = BackpackMessage<{
282
+ lastUpdateId: string
283
+ timestamp: number
284
+ asks: BackpackLevels
285
+ bids: BackpackLevels
286
+ }>
287
+
288
+ type BackpackLevels = [string, string][]
289
+
290
+ type BackpackBookTickerMessage = BackpackMessage<{
291
+ s: string
292
+ a: string | null
293
+ A: string | null
294
+ b: string | null
295
+ B: string | null
296
+ E: number
297
+ }>
298
+
299
+ type BackpackTickerMessage = BackpackMessage<{
300
+ s: string
301
+ E: number
302
+ c: string
303
+ }>
304
+
305
+ type BackpackMarkPriceMessage = BackpackMessage<{
306
+ s: string
307
+ E: number
308
+ p: string
309
+ f: string
310
+ i: string
311
+ n: number
312
+ }>
313
+
314
+ type BackpackOpenInterestMessage = BackpackMessage<{
315
+ s: string
316
+ o: string
317
+ }>
318
+
319
+ type BackpackLiquidationMessage = BackpackMessage<{
320
+ s: string
321
+ S: 'Bid' | 'Ask'
322
+ p: string
323
+ q: string
324
+ E: number
325
+ }>
@@ -1,6 +1,7 @@
1
1
  import { BookChange, BookTicker, DerivativeTicker, Exchange, Liquidation, NormalizedData, OptionSummary, Trade } from '../types.ts'
2
2
  import { ascendexMappers } from './ascendex.ts'
3
3
  import { asterMappers } from './aster.ts'
4
+ import { backpackMappers } from './backpack.ts'
4
5
  import { binanceMappers } from './binance.ts'
5
6
  import { binanceDexMappers } from './binancedex.ts'
6
7
  import { binanceEuropeanOptionsMappers } from './binanceeuropeanoptions.ts'
@@ -62,6 +63,7 @@ type Normalizer<M extends MapperCollection, U extends NormalizedData> = (<T exte
62
63
  const registeredMappers = mergeExchangeMappers(
63
64
  ascendexMappers,
64
65
  asterMappers,
66
+ backpackMappers,
65
67
  binanceMappers,
66
68
  binanceDexMappers,
67
69
  binanceEuropeanOptionsMappers,
@@ -5,6 +5,13 @@ export type Mapper<T extends Exchange, U extends NormalizedData> = {
5
5
 
6
6
  map(message: any, localTimestamp: Date): IterableIterator<U> | undefined
7
7
 
8
+ /**
9
+ * Returns the channel filters this mapper needs for the requested symbols.
10
+ *
11
+ * `undefined` and `[]` both mean all symbols, in the argument and in returned filters.
12
+ * Mappers that narrow symbols per channel must check `isNullOrEmptyArray(symbols)` before filtering,
13
+ * so that an empty request is not mistaken for "no matching symbols".
14
+ */
8
15
  getFilters: (symbols?: string[]) => FilterForExchange[T][]
9
16
  }
10
17
 
@@ -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,
@@ -67,6 +68,7 @@ const realTimeFeedsMap: {
67
68
  [key in Exchange]?: RealTimeFeed
68
69
  } = {
69
70
  aster: AsterRealTimeFeed,
71
+ backpack: BackpackRealTimeFeed,
70
72
  'aster-futures': AsterFuturesRealTimeFeed,
71
73
  binance: BinanceRealTimeFeed,
72
74
  'binance-jersey': BinanceJerseyRealTimeFeed,