tardis-dev 18.5.1 → 18.6.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.
- package/dist/consts.d.ts +1 -1
- package/dist/consts.js +1 -1
- package/dist/consts.js.map +1 -1
- package/dist/downloaddatasets.d.ts.map +1 -1
- package/dist/downloaddatasets.js +10 -4
- package/dist/downloaddatasets.js.map +1 -1
- package/dist/handy.d.ts +2 -1
- package/dist/handy.d.ts.map +1 -1
- package/dist/handy.js +16 -7
- package/dist/handy.js.map +1 -1
- package/dist/mappers/bitflyer.js +3 -3
- package/dist/mappers/bitflyer.js.map +1 -1
- package/dist/mappers/bitget.js +9 -9
- package/dist/mappers/bitget.js.map +1 -1
- package/dist/mappers/dydxv4.js +4 -0
- package/dist/mappers/dydxv4.js.map +1 -1
- package/dist/mappers/lighter.d.ts.map +1 -1
- package/dist/mappers/lighter.js +4 -3
- package/dist/mappers/lighter.js.map +1 -1
- package/dist/mappers/phemex.d.ts.map +1 -1
- package/dist/mappers/phemex.js +47 -10
- package/dist/mappers/phemex.js.map +1 -1
- package/dist/realtimefeeds/bithumb.d.ts.map +1 -1
- package/dist/realtimefeeds/bithumb.js +4 -0
- package/dist/realtimefeeds/bithumb.js.map +1 -1
- package/dist/realtimefeeds/coinbasederivatives.d.ts.map +1 -1
- package/dist/realtimefeeds/coinbasederivatives.js +3 -11
- package/dist/realtimefeeds/coinbasederivatives.js.map +1 -1
- package/dist/realtimefeeds/index.d.ts.map +1 -1
- package/dist/realtimefeeds/index.js +1 -2
- package/dist/realtimefeeds/index.js.map +1 -1
- package/dist/realtimefeeds/okex.d.ts +0 -3
- package/dist/realtimefeeds/okex.d.ts.map +1 -1
- package/dist/realtimefeeds/okex.js +0 -5
- package/dist/realtimefeeds/okex.js.map +1 -1
- package/dist/realtimefeeds/realtimefeed.d.ts +6 -2
- package/dist/realtimefeeds/realtimefeed.d.ts.map +1 -1
- package/dist/realtimefeeds/realtimefeed.js +73 -26
- package/dist/realtimefeeds/realtimefeed.js.map +1 -1
- package/dist/realtimefeeds/upbit.d.ts.map +1 -1
- package/dist/realtimefeeds/upbit.js +2 -1
- package/dist/realtimefeeds/upbit.js.map +1 -1
- package/package.json +1 -1
- package/src/consts.ts +1 -1
- package/src/downloaddatasets.ts +13 -4
- package/src/handy.ts +17 -7
- package/src/mappers/bitflyer.ts +3 -3
- package/src/mappers/bitget.ts +9 -9
- package/src/mappers/dydxv4.ts +7 -1
- package/src/mappers/lighter.ts +4 -3
- package/src/mappers/phemex.ts +52 -10
- package/src/realtimefeeds/bithumb.ts +5 -0
- package/src/realtimefeeds/coinbasederivatives.ts +3 -11
- package/src/realtimefeeds/index.ts +1 -2
- package/src/realtimefeeds/okex.ts +0 -6
- package/src/realtimefeeds/realtimefeed.ts +89 -28
- package/src/realtimefeeds/upbit.ts +2 -1
package/src/mappers/phemex.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { asNonZeroNumberOrUndefined } from '../handy.ts'
|
|
1
2
|
import { Mapper, PendingTickerInfoHelper } from './mapper.ts'
|
|
2
3
|
import { Trade, BookChange, DerivativeTicker } from '../types.ts'
|
|
3
4
|
import { exchangeMappers } from './registry.ts'
|
|
@@ -20,17 +21,52 @@ const fromNanoSecondsToDate = (nanos: number) => {
|
|
|
20
21
|
return timestamp
|
|
21
22
|
}
|
|
22
23
|
|
|
24
|
+
// Phemex spot messages scale amounts by the base currency valueScale and prices by the quote currency valueScale from
|
|
25
|
+
// /public/products. Spot pairs not listed here have a base currency valueScale of 8; TRY and BRZ are the only quote
|
|
26
|
+
// currencies with a valueScale other than 8 (4).
|
|
27
|
+
// prettier-ignore
|
|
28
|
+
const SPOT_QTY_SCALE_GROUPS: [number, string[]][] = [
|
|
29
|
+
[1e2, ['s1000VINUUSDT', 'sLUNCUSDT', 'sPEPEUSDT', 'sSHIBUSDC', 'sSHIBUSDT', 'sSOSUSDT']],
|
|
30
|
+
[1e3, ['s1000000PITUSDT', 'sGUNUSDT']],
|
|
31
|
+
[1e4, [
|
|
32
|
+
's1000000BABYDOGEUSDT', 's10000LADYSUSDT', 's1000BONKUSDT', 's1000BRISEUSDT', 's1000CHEEMSUSDT', 's1000FLOKIUSDT', 's1000MADUSDT',
|
|
33
|
+
's1000MOGUSDT', 's1000SATSUSDT', 's1000TROLLUSDT', 's1000VOLTUSDT', 's1000WHYUSDT', 's1EARTHUSDT', 'sACHUSDT', 'sACSUSDT', 'sAMPUSDT',
|
|
34
|
+
'sANDYUSDT', 'sANKRUSDT', 'sARPAUSDT', 'sASTRUSDT', 'sASTUSDT', 'sBENDUSDT', 'sBLOKUSDT', 'sBRZUSDC', 'sBRZUSDT', 'sCATSUSDT',
|
|
35
|
+
'sCATUSDT', 'sCELRUSDT', 'sCELUSDT', 'sCRTSUSDT', 'sDCUSDT', 'sDENTUSDT', 'sDFUSDT', 'sDNTUSDT', 'sEPXUSDT', 'sFORUSDT', 'sGMMUSDT',
|
|
36
|
+
'sGTOUSDT', 'sHOTUSDT', 'sIDEXUSDT', 'sINGUSDT', 'sINSUSDC', 'sINSUSDT', 'sJASMYUSDT', 'sKEYUSDT', 'sLEVERUSDT', 'sLMRUSDT', 'sMDTUSDT',
|
|
37
|
+
'sMEMEUSDT', 'sMFTUSDT', 'sMXCUSDT', 'sNESTUSDT', 'sOMUSDT', 'sONSTONUSDT', 'sOOKIUSDT', 'sORBSUSDT', 'sPENGUUSDT', 'sPEOPLEUSDT',
|
|
38
|
+
'sPERLUSDT', 'sPONDUSDT', 'sPUFFYUSDT', 'sQIUSDT', 'sRACAUSDT', 'sRAMPUSDT', 'sRANKERUSDT', 'sRATSUSDT', 'sREAPUSDT', 'sREEFUSDT',
|
|
39
|
+
'sRSRUSDT', 'sSHILLUSDT', 'sSKLUSDT', 'sSLPUSDT', 'sSNTUSDT', 'sSPELLUSDT', 'sSTMXUSDT', 'sSTPTUSDT', 'sTELUSDT', 'sTLMUSDT',
|
|
40
|
+
'sTOSHIUSDT', 'sTRVLUSDT', 'sTURBOUSDT', 'sTUSDT', 'sTVKUSDT', 'sUMEEUSDT', 'sVPADUSDT', 'sVRAUSDT', 'sVRGWUSDT', 'sVRUSDT', 'sWENUSDT',
|
|
41
|
+
'sWOLFUSDT', 'sWWYUSDT', 'sXENUSDT', 'sXUSDT', 'sXYOUSDT', 'sZENIUSDT'
|
|
42
|
+
]],
|
|
43
|
+
[1e6, [
|
|
44
|
+
's1000EBPTCUSDT', 'sBNKRUSDT', 'sCTXUSDT', 'sDEGENUSDT', 'sGOATUSDT', 'sJUPUSDT', 'sKMNOUSDT', 'sMAGAUSDT', 'sMEWUSDT', 'sPOLYXUSDT',
|
|
45
|
+
'sPYTHUSDT', 'sSEIUSDT', 'sWERUSDT', 'sWINUSDT', 'sZEROUSDT'
|
|
46
|
+
]]
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
const SPOT_QTY_SCALES = new Map(SPOT_QTY_SCALE_GROUPS.flatMap(([scale, symbols]) => symbols.map((symbol) => [symbol, scale] as const)))
|
|
50
|
+
|
|
51
|
+
// Phemex reused sREDUSDT: recorded RED TOKEN data (valueScale 4, now REDOLD) ends on 2023-07-13 and RedStone data
|
|
52
|
+
// (valueScale 8) starts on 2025-03-06 at 13:08 UTC, before its 13:30 listTime.
|
|
53
|
+
const RED_STONE_LISTING_DAY = new Date('2025-03-06T00:00:00.000Z')
|
|
54
|
+
|
|
23
55
|
function getPriceScale(symbol: string) {
|
|
24
56
|
if (symbol.startsWith('s')) {
|
|
25
|
-
return 1e8
|
|
57
|
+
return symbol.endsWith('TRY') || symbol.endsWith('BRZ') ? 1e4 : 1e8
|
|
26
58
|
}
|
|
27
59
|
|
|
28
60
|
return 1e4
|
|
29
61
|
}
|
|
30
62
|
|
|
31
|
-
function getQtyScale(symbol: string) {
|
|
63
|
+
function getQtyScale(symbol: string, localTimestamp: Date) {
|
|
64
|
+
if (symbol === 'sREDUSDT' && localTimestamp < RED_STONE_LISTING_DAY) {
|
|
65
|
+
return 1e4
|
|
66
|
+
}
|
|
67
|
+
|
|
32
68
|
if (symbol.startsWith('s')) {
|
|
33
|
-
return 1e8
|
|
69
|
+
return SPOT_QTY_SCALES.get(symbol) ?? 1e8
|
|
34
70
|
}
|
|
35
71
|
|
|
36
72
|
return 1
|
|
@@ -279,7 +315,7 @@ const phemexTradesMapper: Mapper<'phemex', Trade> = {
|
|
|
279
315
|
exchange: 'phemex',
|
|
280
316
|
id: undefined,
|
|
281
317
|
price: priceEp / getPriceScale(symbol),
|
|
282
|
-
amount: qty / getQtyScale(symbol),
|
|
318
|
+
amount: qty / getQtyScale(symbol, localTimestamp),
|
|
283
319
|
side: side === 'Buy' ? 'buy' : 'sell',
|
|
284
320
|
timestamp: fromNanoSecondsToDate(timestamp),
|
|
285
321
|
localTimestamp: localTimestamp
|
|
@@ -304,11 +340,11 @@ const phemexTradesMapper: Mapper<'phemex', Trade> = {
|
|
|
304
340
|
}
|
|
305
341
|
|
|
306
342
|
const mapBookLevelForSymbol =
|
|
307
|
-
(symbol: string) =>
|
|
343
|
+
(symbol: string, localTimestamp: Date) =>
|
|
308
344
|
([priceEp, qty]: PhemexBookLevel) => {
|
|
309
345
|
return {
|
|
310
346
|
price: priceEp / getPriceScale(symbol),
|
|
311
|
-
amount: qty / getQtyScale(symbol)
|
|
347
|
+
amount: qty / getQtyScale(symbol, localTimestamp)
|
|
312
348
|
}
|
|
313
349
|
}
|
|
314
350
|
|
|
@@ -362,7 +398,7 @@ const phemexBookChangeMapper: Mapper<'phemex', BookChange> = {
|
|
|
362
398
|
}
|
|
363
399
|
|
|
364
400
|
if ('book' in message) {
|
|
365
|
-
const mapBookLevel = mapBookLevelForSymbol(symbol)
|
|
401
|
+
const mapBookLevel = mapBookLevelForSymbol(symbol, localTimestamp)
|
|
366
402
|
yield {
|
|
367
403
|
type: 'book_change',
|
|
368
404
|
symbol: symbol.toUpperCase(),
|
|
@@ -457,14 +493,20 @@ class PhemexDerivativeTickerMapper implements Mapper<'phemex', DerivativeTicker>
|
|
|
457
493
|
fundingRateRr,
|
|
458
494
|
predFundingRateRr
|
|
459
495
|
] of message.data) {
|
|
496
|
+
// The pack keeps delisted contracts with zero prices.
|
|
497
|
+
if (Number(lastRp) === 0 && Number(indexRp) === 0 && Number(markRp) === 0) {
|
|
498
|
+
continue
|
|
499
|
+
}
|
|
500
|
+
|
|
460
501
|
const pendingTickerInfo = this.pendingTickerInfoHelper.getPendingTickerInfo(symbol, 'phemex')
|
|
461
502
|
|
|
462
503
|
pendingTickerInfo.updateFundingRate(Number(fundingRateRr))
|
|
463
504
|
pendingTickerInfo.updatePredictedFundingRate(Number(predFundingRateRr))
|
|
464
|
-
pendingTickerInfo.updateIndexPrice(
|
|
465
|
-
pendingTickerInfo.updateMarkPrice(
|
|
505
|
+
pendingTickerInfo.updateIndexPrice(asNonZeroNumberOrUndefined(indexRp))
|
|
506
|
+
pendingTickerInfo.updateMarkPrice(asNonZeroNumberOrUndefined(markRp))
|
|
466
507
|
pendingTickerInfo.updateOpenInterest(Number(openInterestRv))
|
|
467
|
-
|
|
508
|
+
// Contracts without trades in the last 24 hours report lastRp as 0.
|
|
509
|
+
pendingTickerInfo.updateLastPrice(asNonZeroNumberOrUndefined(lastRp))
|
|
468
510
|
pendingTickerInfo.updateTimestamp(fromNanoSecondsToDate(message.timestamp))
|
|
469
511
|
|
|
470
512
|
if (pendingTickerInfo.hasChanged()) {
|
|
@@ -21,6 +21,11 @@ export class BithumbRealTimeFeed extends RealTimeFeedBase {
|
|
|
21
21
|
codes: filter.symbols
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
// trade SNAPSHOT is the market's last trade, often hours old; Bithumb honours only the camelCase flag
|
|
25
|
+
if (filter.channel === 'trade') {
|
|
26
|
+
return { ...subscription, isOnlyRealtime: true }
|
|
27
|
+
}
|
|
28
|
+
|
|
24
29
|
return filter.channel === 'orderbook' ? { ...subscription, level: 1 } : subscription
|
|
25
30
|
})
|
|
26
31
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Writable } from 'node:stream'
|
|
2
|
-
import {
|
|
2
|
+
import { getJSON, optimizeFilters } from '../handy.ts'
|
|
3
3
|
import { Filter } from '../types.ts'
|
|
4
4
|
import { MultiConnectionRealTimeFeedBase, PoolingClientBase, RealTimeFeedBase } from './realtimefeed.ts'
|
|
5
5
|
|
|
@@ -12,16 +12,8 @@ export class CoinbaseDerivativesRealTimeFeed extends MultiConnectionRealTimeFeed
|
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
for (const group of groups) {
|
|
18
|
-
const groupedFilters = wsFilters
|
|
19
|
-
.map((f) => ({
|
|
20
|
-
...f,
|
|
21
|
-
symbols: f.symbols?.filter((s) => group.includes(s))
|
|
22
|
-
}))
|
|
23
|
-
.filter((f) => f.symbols?.length || ['heartbeats', 'subscriptions'].includes(f.channel))
|
|
24
|
-
yield new CoinbaseDerivativesSingleConnectionRealTimeFeed(exchange, groupedFilters, timeoutIntervalMS, onError)
|
|
15
|
+
if (wsFilters.length > 0) {
|
|
16
|
+
yield new CoinbaseDerivativesSingleConnectionRealTimeFeed(exchange, wsFilters, timeoutIntervalMS, onError)
|
|
25
17
|
}
|
|
26
18
|
|
|
27
19
|
const productFilters = filters.filter((f) => f.channel === 'products')
|
|
@@ -29,7 +29,7 @@ import {
|
|
|
29
29
|
HuobiDMOptionsRealTimeFeed
|
|
30
30
|
} from './huobi.ts'
|
|
31
31
|
import { KrakenRealTimeFeed } from './kraken.ts'
|
|
32
|
-
import {
|
|
32
|
+
import { OkexOptionsRealTimeFeed, OkexRealTimeFeed } from './okex.ts'
|
|
33
33
|
import { RealTimeFeed } from './realtimefeed.ts'
|
|
34
34
|
import { PhemexRealTimeFeed } from './phemex.ts'
|
|
35
35
|
import { DeltaRealTimeFeed } from './delta.ts'
|
|
@@ -95,7 +95,6 @@ const realTimeFeedsMap: {
|
|
|
95
95
|
'huobi-dm-linear-swap': HuobiDMLinearSwapRealTimeFeed,
|
|
96
96
|
huobi: HuobiRealTimeFeed,
|
|
97
97
|
bybit: BybitRealTimeDataFeed,
|
|
98
|
-
okcoin: OKCoinRealTimeFeed,
|
|
99
98
|
hitbtc: HitBtcRealTimeFeed,
|
|
100
99
|
phemex: PhemexRealTimeFeed,
|
|
101
100
|
delta: DeltaRealTimeFeed,
|
|
@@ -102,12 +102,6 @@ class OkexSingleRealTimeFeed extends RealTimeFeedBase {
|
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
export class OKCoinRealTimeFeed extends OkexSingleRealTimeFeed {
|
|
106
|
-
constructor(exchange: string, filters: Filter<string>[], timeoutIntervalMS: number | undefined, onError?: (error: Error) => void) {
|
|
107
|
-
super('wss://real.okcoin.com:8443/ws/v5/public', exchange, filters, timeoutIntervalMS, onError)
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
105
|
export class OkexOptionsRealTimeFeed extends MultiConnectionRealTimeFeedBase {
|
|
112
106
|
protected *_getRealTimeFeeds(exchange: string, filters: Filter<string>[], timeoutIntervalMS?: number, onError?: (error: Error) => void) {
|
|
113
107
|
const nonBusinessFilters = filters.filter((f) => f.channel !== 'trades-all')
|
|
@@ -5,7 +5,7 @@ import { setTimeout as sleep } from 'node:timers/promises'
|
|
|
5
5
|
import { createDebug, type DebugLogger } from '../debug.ts'
|
|
6
6
|
import { getProxyAgent, HttpClientError, ONE_SEC_IN_MS, optimizeFilters } from '../handy.ts'
|
|
7
7
|
import { getMessageParser } from '../parsemessage.ts'
|
|
8
|
-
import { createManagedRealTimeIterator, mergeRealTime, type ManagedRealTimeIterator } from '../realtimeiterator.ts'
|
|
8
|
+
import { closeIterator, createManagedRealTimeIterator, mergeRealTime, type ManagedRealTimeIterator } from '../realtimeiterator.ts'
|
|
9
9
|
import { Exchange, Filter } from '../types.ts'
|
|
10
10
|
|
|
11
11
|
export type RealTimeFeed = {
|
|
@@ -19,7 +19,7 @@ export type RealTimeFeed = {
|
|
|
19
19
|
|
|
20
20
|
let connectionCounter = 1
|
|
21
21
|
|
|
22
|
-
export type RealTimeFeedIterable =
|
|
22
|
+
export type RealTimeFeedIterable = { [Symbol.asyncIterator](): ManagedRealTimeIterator<any> }
|
|
23
23
|
|
|
24
24
|
type RealTimeFeedConnection = {
|
|
25
25
|
id: number
|
|
@@ -95,6 +95,7 @@ export abstract class RealTimeFeedBase implements RealTimeFeedIterable {
|
|
|
95
95
|
let staleConnectionTimerId: NodeJS.Timeout | undefined
|
|
96
96
|
let pingTimerId: NodeJS.Timeout | undefined
|
|
97
97
|
let connectionError: Error | undefined
|
|
98
|
+
let delivered = false
|
|
98
99
|
|
|
99
100
|
try {
|
|
100
101
|
this.manualSnapshotsBuffer.length = 0
|
|
@@ -173,6 +174,7 @@ export abstract class RealTimeFeedBase implements RealTimeFeedIterable {
|
|
|
173
174
|
this.onMessage(messageDeserialized)
|
|
174
175
|
|
|
175
176
|
yield messageDeserialized
|
|
177
|
+
delivered = true
|
|
176
178
|
|
|
177
179
|
if (retries > 0) {
|
|
178
180
|
// reset retries counter as we've received correct message from the connection
|
|
@@ -216,33 +218,22 @@ export abstract class RealTimeFeedBase implements RealTimeFeedIterable {
|
|
|
216
218
|
if (connectionError !== undefined) {
|
|
217
219
|
this._onError?.(connectionError)
|
|
218
220
|
retries++
|
|
219
|
-
|
|
220
|
-
const MAX_DELAY = 32 * 1000
|
|
221
|
-
const httpError = connectionError instanceof HttpClientError ? connectionError : undefined
|
|
222
|
-
const isRateLimited = httpError?.status === 418 || httpError?.status === 429 || connectionError.message.includes('429')
|
|
223
|
-
|
|
224
|
-
if (isRateLimited) {
|
|
225
|
-
const fallbackDelay = httpError?.status === 418 ? 2 * 60 * 1000 : (MAX_DELAY / 2) * retries
|
|
226
|
-
retryDelay = httpError?.retryAfterMS ?? fallbackDelay
|
|
227
|
-
} else {
|
|
228
|
-
retryDelay = Math.min(Math.pow(2, retries - 1) * 1000, MAX_DELAY)
|
|
229
|
-
}
|
|
221
|
+
retryDelay = getRetryDelay(connectionError, retries)
|
|
230
222
|
|
|
231
223
|
this.debug(
|
|
232
|
-
'(connection id: %d) %s real-time feed connection error, retries count: %d, next retry delay: %dms,
|
|
224
|
+
'(connection id: %d) %s real-time feed connection error, retries count: %d, next retry delay: %dms, error message: %o',
|
|
233
225
|
connectionId,
|
|
234
226
|
this._exchange,
|
|
235
227
|
retries,
|
|
236
228
|
retryDelay,
|
|
237
|
-
isRateLimited,
|
|
238
229
|
connectionError
|
|
239
230
|
)
|
|
240
231
|
}
|
|
241
232
|
|
|
242
|
-
yield { __disconnect__: true }
|
|
233
|
+
yield { __disconnect__: true, error: connectionError, delivered }
|
|
243
234
|
|
|
244
235
|
if (retryDelay !== undefined) {
|
|
245
|
-
await
|
|
236
|
+
await wait(retryDelay, this._closeController.signal)
|
|
246
237
|
}
|
|
247
238
|
}
|
|
248
239
|
}
|
|
@@ -343,7 +334,7 @@ export abstract class RealTimeFeedBase implements RealTimeFeedIterable {
|
|
|
343
334
|
|
|
344
335
|
this._send(connection, message)
|
|
345
336
|
if (this.throttleSubscribeMS > 0) {
|
|
346
|
-
await
|
|
337
|
+
await wait(this.throttleSubscribeMS, connection.controller.signal)
|
|
347
338
|
}
|
|
348
339
|
}
|
|
349
340
|
|
|
@@ -352,7 +343,7 @@ export abstract class RealTimeFeedBase implements RealTimeFeedIterable {
|
|
|
352
343
|
const messagesBeforeSnapshots = subscribeMessages.length === 0 ? 0 : subscribeMessages.length + 1
|
|
353
344
|
// Wait for subscription acknowledgements and the first data message before fetching snapshots.
|
|
354
345
|
while (shouldCancel() === false && connection.receivedMessagesCount < messagesBeforeSnapshots) {
|
|
355
|
-
await
|
|
346
|
+
await wait(100, connection.controller.signal)
|
|
356
347
|
}
|
|
357
348
|
|
|
358
349
|
if (shouldCancel()) {
|
|
@@ -411,18 +402,31 @@ export abstract class RealTimeFeedBase implements RealTimeFeedIterable {
|
|
|
411
402
|
this.manualSnapshotsBuffer.length = 0
|
|
412
403
|
}
|
|
413
404
|
}
|
|
405
|
+
}
|
|
414
406
|
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
}
|
|
407
|
+
async function wait(delayMS: number, signal: AbortSignal) {
|
|
408
|
+
try {
|
|
409
|
+
await sleep(delayMS, undefined, { signal })
|
|
410
|
+
} catch (error) {
|
|
411
|
+
if (signal.aborted === false) {
|
|
412
|
+
throw error
|
|
422
413
|
}
|
|
423
414
|
}
|
|
424
415
|
}
|
|
425
416
|
|
|
417
|
+
function getRetryDelay(error: Error, retries: number) {
|
|
418
|
+
const MAX_DELAY = 32 * 1000
|
|
419
|
+
const httpError = error instanceof HttpClientError ? error : undefined
|
|
420
|
+
const isRateLimited = httpError?.status === 418 || httpError?.status === 429 || error.message.includes('429')
|
|
421
|
+
|
|
422
|
+
if (isRateLimited) {
|
|
423
|
+
const fallbackDelay = httpError?.status === 418 ? 2 * 60 * 1000 : (MAX_DELAY / 2) * retries
|
|
424
|
+
return httpError?.retryAfterMS ?? fallbackDelay
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
return Math.min(Math.pow(2, retries - 1) * 1000, MAX_DELAY)
|
|
428
|
+
}
|
|
429
|
+
|
|
426
430
|
function resolveOrAbort<T>(promise: Promise<T>, signal: AbortSignal): Promise<T | typeof ABORTED> {
|
|
427
431
|
if (signal.aborted) {
|
|
428
432
|
return Promise.resolve(ABORTED)
|
|
@@ -449,7 +453,12 @@ function resolveOrAbort<T>(promise: Promise<T>, signal: AbortSignal): Promise<T
|
|
|
449
453
|
})
|
|
450
454
|
}
|
|
451
455
|
|
|
456
|
+
// A disconnect resets all normalized state, so when any connection drops, the feed closes all its connections and pollers,
|
|
457
|
+
// yields one disconnect and opens them again after the retry delay of the failed connection.
|
|
452
458
|
export abstract class MultiConnectionRealTimeFeedBase implements RealTimeFeedIterable {
|
|
459
|
+
private readonly _closeController = new AbortController()
|
|
460
|
+
private _connections?: ManagedRealTimeIterator<any>
|
|
461
|
+
|
|
453
462
|
constructor(
|
|
454
463
|
private readonly _exchange: string,
|
|
455
464
|
private readonly _filters: Filter<string>[],
|
|
@@ -458,8 +467,44 @@ export abstract class MultiConnectionRealTimeFeedBase implements RealTimeFeedIte
|
|
|
458
467
|
) {}
|
|
459
468
|
|
|
460
469
|
[Symbol.asyncIterator](): ManagedRealTimeIterator<any> {
|
|
461
|
-
|
|
462
|
-
|
|
470
|
+
return createManagedRealTimeIterator(this._stream(), () => {
|
|
471
|
+
this._closeController.abort()
|
|
472
|
+
return this._connections?.return()
|
|
473
|
+
})
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
private async *_stream() {
|
|
477
|
+
let retries = 0
|
|
478
|
+
|
|
479
|
+
while (this._closeController.signal.aborted === false) {
|
|
480
|
+
this._connections = mergeRealTime(
|
|
481
|
+
Array.from(this._getRealTimeFeeds(this._exchange, this._filters, this._timeoutIntervalMS, this._onError), untilDisconnect)
|
|
482
|
+
)
|
|
483
|
+
let disconnect: { error?: Error; delivered?: boolean } | undefined
|
|
484
|
+
|
|
485
|
+
for await (const message of this._connections) {
|
|
486
|
+
if (message.__disconnect__ === true) {
|
|
487
|
+
disconnect = message
|
|
488
|
+
break
|
|
489
|
+
}
|
|
490
|
+
yield message
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
if (disconnect === undefined) {
|
|
494
|
+
return
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
yield { __disconnect__: true }
|
|
498
|
+
|
|
499
|
+
if (disconnect.delivered === true) {
|
|
500
|
+
retries = 0
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
if (disconnect.error !== undefined) {
|
|
504
|
+
retries++
|
|
505
|
+
await wait(getRetryDelay(disconnect.error, retries), this._closeController.signal)
|
|
506
|
+
}
|
|
507
|
+
}
|
|
463
508
|
}
|
|
464
509
|
|
|
465
510
|
protected abstract _getRealTimeFeeds(
|
|
@@ -470,6 +515,22 @@ export abstract class MultiConnectionRealTimeFeedBase implements RealTimeFeedIte
|
|
|
470
515
|
): IterableIterator<RealTimeFeedIterable>
|
|
471
516
|
}
|
|
472
517
|
|
|
518
|
+
// A connection stops at its disconnect, so it cannot reconnect on its own before the feed restarts all connections.
|
|
519
|
+
function untilDisconnect(feed: RealTimeFeedIterable) {
|
|
520
|
+
const messages = feed[Symbol.asyncIterator]()
|
|
521
|
+
|
|
522
|
+
async function* stream() {
|
|
523
|
+
for await (const message of messages) {
|
|
524
|
+
yield message
|
|
525
|
+
if (message.__disconnect__ === true) {
|
|
526
|
+
return
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
return createManagedRealTimeIterator(stream(), () => closeIterator(messages))
|
|
532
|
+
}
|
|
533
|
+
|
|
473
534
|
export abstract class PoolingClientBase implements RealTimeFeedIterable {
|
|
474
535
|
protected readonly debug: DebugLogger
|
|
475
536
|
private _tid: NodeJS.Timeout | undefined = undefined
|
|
@@ -10,10 +10,11 @@ export class UpbitRealTimeFeed extends RealTimeFeedBase {
|
|
|
10
10
|
throw new Error('UpbitRealTimeFeed requires explicitly specified symbols when subscribing to live feed')
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
// trade SNAPSHOT is the market's last trade, often hours old; orderbook and ticker SNAPSHOT give the current state
|
|
13
14
|
return {
|
|
14
15
|
type: filter.channel,
|
|
15
16
|
codes: filter.symbols,
|
|
16
|
-
isOnlyRealtime: true
|
|
17
|
+
...(filter.channel === 'trade' ? { isOnlyRealtime: true } : {})
|
|
17
18
|
}
|
|
18
19
|
})
|
|
19
20
|
|