tardis-dev 18.9.0 → 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.
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 +201 -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 +303 -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,303 @@
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
+
73
+ yield {
74
+ type: 'book_change',
75
+ exchange: 'backpack',
76
+ symbol: symbol.toUpperCase(),
77
+ isSnapshot: true,
78
+ bids: data.bids.map(this.mapLevel),
79
+ asks: data.asks.map(this.mapLevel),
80
+ timestamp: fromMicroSecondsToDate(data.timestamp),
81
+ localTimestamp
82
+ }
83
+
84
+ for (const update of bookState.buffered.items()) {
85
+ yield* this.mapUpdate(update.data, update.localTimestamp, bookState)
86
+ }
87
+
88
+ bookState.buffered.clear()
89
+ } else if (bookState.sequence === undefined) {
90
+ const discarded = bookState.buffered.append({ data, localTimestamp })
91
+ if (discarded !== undefined) bookState.discardedThrough = discarded.data.u
92
+ } else {
93
+ yield* this.mapUpdate(data, localTimestamp, bookState)
94
+ }
95
+ }
96
+
97
+ private *mapUpdate(data: BackpackDepthMessage['data'], localTimestamp: Date, bookState: BackpackBookState): IterableIterator<BookChange> {
98
+ if (data.u <= bookState.sequence!) {
99
+ return
100
+ }
101
+
102
+ if (this.validateSequences && (data.U <= bookState.sequence! + 1 && data.u >= bookState.sequence! + 1) == false) {
103
+ throw new Error(`Backpack depth sequence gap for ${data.s}: expected ${bookState.sequence! + 1}, received ${data.U}-${data.u}`)
104
+ }
105
+ bookState.sequence = data.u
106
+
107
+ yield {
108
+ type: 'book_change',
109
+ exchange: 'backpack',
110
+ symbol: data.s.toUpperCase(),
111
+ isSnapshot: false,
112
+ bids: data.b.map(this.mapLevel),
113
+ asks: data.a.map(this.mapLevel),
114
+ timestamp: fromMicroSecondsToDate(data.E),
115
+ localTimestamp
116
+ }
117
+ }
118
+ private mapLevel([price, amount]: [string, string]) {
119
+ return {
120
+ price: Number(price),
121
+ amount: Number(amount)
122
+ }
123
+ }
124
+ }
125
+
126
+ class BackpackBookTickerMapper implements Mapper<'backpack', BookTicker> {
127
+ canHandle(message: BackpackMessage<unknown>) {
128
+ return message.stream?.startsWith('bookTicker.') === true
129
+ }
130
+ getFilters(symbols?: string[]) {
131
+ return [{ channel: 'bookTicker', symbols } as const]
132
+ }
133
+ *map({ data }: BackpackBookTickerMessage, localTimestamp: Date) {
134
+ yield {
135
+ type: 'book_ticker',
136
+ exchange: 'backpack',
137
+ symbol: data.s.toUpperCase(),
138
+ askPrice: asNumberOrUndefined(data.a),
139
+ askAmount: asNumberOrUndefined(data.A),
140
+ bidPrice: asNumberOrUndefined(data.b),
141
+ bidAmount: asNumberOrUndefined(data.B),
142
+ timestamp: fromMicroSecondsToDate(data.E),
143
+ localTimestamp
144
+ } as const
145
+ }
146
+ }
147
+
148
+ class BackpackDerivativeTickerMapper implements Mapper<'backpack', DerivativeTicker> {
149
+ private readonly pending = new PendingTickerInfoHelper()
150
+
151
+ canHandle(message: BackpackMessage<unknown>) {
152
+ return /^(markPrice|openInterest|ticker)\..+_PERP$/.test(message.stream)
153
+ }
154
+
155
+ getFilters(symbols?: string[]) {
156
+ if (!isNullOrEmptyArray(symbols)) {
157
+ symbols = symbols!.filter((symbol) => symbol.endsWith('_PERP'))
158
+ if (symbols.length === 0) {
159
+ return []
160
+ }
161
+ }
162
+ return [
163
+ { channel: 'markPrice', symbols } as const,
164
+ { channel: 'openInterest', symbols } as const,
165
+ { channel: 'ticker', symbols } as const
166
+ ]
167
+ }
168
+
169
+ *map(
170
+ { stream, data }: BackpackTickerMessage | BackpackMarkPriceMessage | BackpackOpenInterestMessage,
171
+ localTimestamp: Date
172
+ ): IterableIterator<DerivativeTicker> {
173
+ const ticker = this.pending.getPendingTickerInfo(data.s.toUpperCase(), 'backpack')
174
+
175
+ // Ticker and open interest update cached state. markPrice owns output and its
176
+ // microsecond event timestamp, avoiding regressions between independent channels.
177
+ if (stream.startsWith('openInterest.')) {
178
+ ticker.updateOpenInterest(asNumberOrUndefined((data as BackpackOpenInterestMessage['data']).o))
179
+ return
180
+ }
181
+
182
+ if (stream.startsWith('ticker.')) {
183
+ ticker.updateLastPrice(asNumberOrUndefined((data as BackpackTickerMessage['data']).c))
184
+ return
185
+ }
186
+
187
+ const mark = data as BackpackMarkPriceMessage['data']
188
+ ticker.updateMarkPrice(asNumberOrUndefined(mark.p))
189
+ ticker.updateIndexPrice(asNumberOrUndefined(mark.i))
190
+ ticker.updateFundingRate(asNumberOrUndefined(mark.f))
191
+ ticker.updateFundingTimestamp(new Date(mark.n))
192
+ ticker.updateTimestamp(fromMicroSecondsToDate(mark.E))
193
+
194
+ if (ticker.hasChanged()) {
195
+ yield ticker.getSnapshot(localTimestamp)
196
+ }
197
+ }
198
+ }
199
+
200
+ class BackpackLiquidationsMapper implements Mapper<'backpack', Liquidation> {
201
+ canHandle(message: BackpackMessage<unknown>) {
202
+ return message.stream?.startsWith('liquidation.') === true
203
+ }
204
+
205
+ getFilters(symbols?: string[]) {
206
+ if (!isNullOrEmptyArray(symbols)) {
207
+ symbols = symbols!.filter((symbol) => symbol.endsWith('_PERP'))
208
+ if (symbols.length === 0) {
209
+ return []
210
+ }
211
+ }
212
+ return [{ channel: 'liquidation', symbols } as const]
213
+ }
214
+
215
+ *map({ data }: BackpackLiquidationMessage, localTimestamp: Date) {
216
+ yield {
217
+ type: 'liquidation',
218
+ exchange: 'backpack',
219
+ symbol: data.s.toUpperCase(),
220
+ id: undefined,
221
+ price: Number(data.p),
222
+ amount: Number(data.q),
223
+ side: data.S === 'Bid' ? 'buy' : 'sell',
224
+ timestamp: fromMicroSecondsToDate(data.E),
225
+ localTimestamp
226
+ } as const
227
+ }
228
+ }
229
+
230
+ type BackpackBookState = {
231
+ sequence?: number
232
+ discardedThrough?: number
233
+ buffered: CircularBuffer<{ data: BackpackDepthMessage['data']; localTimestamp: Date }>
234
+ }
235
+
236
+ type BackpackMessage<T> = {
237
+ stream: string
238
+ data: T
239
+ }
240
+
241
+ type BackpackTradeMessage = BackpackMessage<{
242
+ s: string
243
+ t: number
244
+ p: string
245
+ q: string
246
+ m: boolean
247
+ T: number
248
+ }>
249
+
250
+ type BackpackDepthMessage = BackpackMessage<{
251
+ s: string
252
+ U: number
253
+ u: number
254
+ a: BackpackLevels
255
+ b: BackpackLevels
256
+ E: number
257
+ }>
258
+
259
+ export type BackpackSnapshotMessage = BackpackMessage<{
260
+ lastUpdateId: string
261
+ timestamp: number
262
+ asks: BackpackLevels
263
+ bids: BackpackLevels
264
+ }>
265
+
266
+ type BackpackLevels = [string, string][]
267
+
268
+ type BackpackBookTickerMessage = BackpackMessage<{
269
+ s: string
270
+ a: string | null
271
+ A: string | null
272
+ b: string | null
273
+ B: string | null
274
+ E: number
275
+ }>
276
+
277
+ type BackpackTickerMessage = BackpackMessage<{
278
+ s: string
279
+ E: number
280
+ c: string
281
+ }>
282
+
283
+ type BackpackMarkPriceMessage = BackpackMessage<{
284
+ s: string
285
+ E: number
286
+ p: string
287
+ f: string
288
+ i: string
289
+ n: number
290
+ }>
291
+
292
+ type BackpackOpenInterestMessage = BackpackMessage<{
293
+ s: string
294
+ o: string
295
+ }>
296
+
297
+ type BackpackLiquidationMessage = BackpackMessage<{
298
+ s: string
299
+ S: 'Bid' | 'Ask'
300
+ p: string
301
+ q: string
302
+ E: number
303
+ }>
@@ -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,