@imbingox/acex 0.4.0-beta.5 → 0.4.0-beta.6
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/docs/api.md +21 -0
- package/package.json +1 -1
- package/src/managers/market-manager.ts +147 -31
- package/src/types/market.ts +11 -0
package/docs/api.md
CHANGED
|
@@ -345,6 +345,7 @@ interface MarketManager {
|
|
|
345
345
|
readonly events: MarketEventStreams;
|
|
346
346
|
|
|
347
347
|
loadMarkets(): Promise<void>;
|
|
348
|
+
reloadMarkets(venue?: Venue): Promise<MarketCatalogReloadSummary[]>;
|
|
348
349
|
listMarkets(venue?: Venue): MarketDefinition[];
|
|
349
350
|
getMarket(venue: Venue, symbol: string): MarketDefinition | undefined;
|
|
350
351
|
getMarkets(symbol: string): MarketDefinition[];
|
|
@@ -368,6 +369,7 @@ interface MarketManager {
|
|
|
368
369
|
|
|
369
370
|
```ts
|
|
370
371
|
await client.market.loadMarkets();
|
|
372
|
+
const reloadSummaries = await client.market.reloadMarkets("binance");
|
|
371
373
|
|
|
372
374
|
const all = client.market.listMarkets();
|
|
373
375
|
const binanceOnly = client.market.listMarkets("binance");
|
|
@@ -378,6 +380,25 @@ const allBtcPerp = client.market.getMarkets("BTC/USDT:USDT");
|
|
|
378
380
|
|
|
379
381
|
`getMarkets(symbol)` 严格按完整统一 symbol 匹配。
|
|
380
382
|
|
|
383
|
+
`loadMarkets()` 会懒加载并缓存当前已注册 venue 的市场目录;已加载过的 venue 不会重复拉取。`reloadMarkets(venue?)` 用于主动刷新市场目录:传入 `venue` 时只刷新该 venue,省略时刷新所有已注册 market adapter。它和 `loadMarkets()` 一样不要求 client 已 `start()`,因此可在 `start()` 前或 `stop()` 后调用。
|
|
384
|
+
|
|
385
|
+
`reloadMarkets()` 返回每个 venue 的刷新摘要:
|
|
386
|
+
|
|
387
|
+
```ts
|
|
388
|
+
type MarketCatalogReloadSummary = {
|
|
389
|
+
venue: Venue;
|
|
390
|
+
added: string[];
|
|
391
|
+
removed: string[];
|
|
392
|
+
total: number;
|
|
393
|
+
ok: boolean;
|
|
394
|
+
error?: AcexError;
|
|
395
|
+
};
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
`added` / `removed` 是本次刷新相对旧目录变化的 symbol 列表,`total` 是刷新后该 venue 的目录数量。catalog 拉取失败时,对应 summary 为 `ok: false`,`error.code = "MARKET_CATALOG_LOAD_FAILED"`,旧目录会保留,方法不会因为该 venue 的 catalog 失败而 reject;未注册 runtime adapter 的合法 venue(例如当前 market adapter 未接入的 `bybit`)仍会抛 `VENUE_NOT_SUPPORTED`。
|
|
399
|
+
|
|
400
|
+
如果刷新会新增 symbol,调用方应先 `await client.market.reloadMarkets(venue)`,再按 summary 订阅新增 symbol。已加载 venue 上的后台 reload 不会阻塞并发 `subscribe*()`;reload 完成前订阅新增 symbol 仍可能按旧目录返回 `MARKET_NOT_FOUND`。
|
|
401
|
+
|
|
381
402
|
`MarketDefinition` 见 [§9](#9-数据类型参考)。价格/数量相关字段(`priceStep`、`amountStep`、`contractSize`、`minAmount`、`minNotional`)都是 canonical decimal string;需要运算时用 `new BigNumber(field)` 自行解析。
|
|
382
403
|
|
|
383
404
|
归一化下单价格和数量:
|
package/package.json
CHANGED
|
@@ -23,6 +23,7 @@ import type {
|
|
|
23
23
|
FundingRateUpdatedEvent,
|
|
24
24
|
L1Book,
|
|
25
25
|
L1BookUpdatedEvent,
|
|
26
|
+
MarketCatalogReloadSummary,
|
|
26
27
|
MarketDataStatus,
|
|
27
28
|
MarketDataStreamStatus,
|
|
28
29
|
MarketDefinition,
|
|
@@ -63,6 +64,13 @@ interface MarketRecord {
|
|
|
63
64
|
fundingRateStream?: StreamHandle;
|
|
64
65
|
}
|
|
65
66
|
|
|
67
|
+
interface CatalogFetchResult {
|
|
68
|
+
venue: Venue;
|
|
69
|
+
added: string[];
|
|
70
|
+
removed: string[];
|
|
71
|
+
total: number;
|
|
72
|
+
}
|
|
73
|
+
|
|
66
74
|
const DEFAULT_INITIAL_L1_TIMEOUT_MS = 15_000;
|
|
67
75
|
const DEFAULT_L1_STALE_AFTER_MS = 15_000;
|
|
68
76
|
const DEFAULT_L1_RECONNECT_DELAY_MS = 1_000;
|
|
@@ -114,7 +122,10 @@ export class MarketManagerImpl
|
|
|
114
122
|
private readonly definitions = new Map<string, MarketDefinition>();
|
|
115
123
|
private readonly records = new Map<string, MarketRecord>();
|
|
116
124
|
private readonly loadedCatalogVenues = new Set<Venue>();
|
|
117
|
-
private readonly catalogPromises = new Map<
|
|
125
|
+
private readonly catalogPromises = new Map<
|
|
126
|
+
Venue,
|
|
127
|
+
Promise<CatalogFetchResult>
|
|
128
|
+
>();
|
|
118
129
|
private readonly initialL1TimeoutMs: number;
|
|
119
130
|
private readonly l1StaleAfterMs: number;
|
|
120
131
|
private readonly l1ReconnectDelayMs: number;
|
|
@@ -166,6 +177,30 @@ export class MarketManagerImpl
|
|
|
166
177
|
);
|
|
167
178
|
}
|
|
168
179
|
|
|
180
|
+
async reloadMarkets(venue?: Venue): Promise<MarketCatalogReloadSummary[]> {
|
|
181
|
+
if (venue !== undefined) {
|
|
182
|
+
this.assertSupportedVenue(venue);
|
|
183
|
+
return [await this.reloadVenue(venue)];
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const venues = [...this.adapters.keys()];
|
|
187
|
+
const settled = await Promise.allSettled(
|
|
188
|
+
venues.map((registeredVenue) => this.reloadVenue(registeredVenue)),
|
|
189
|
+
);
|
|
190
|
+
const summaries: MarketCatalogReloadSummary[] = [];
|
|
191
|
+
|
|
192
|
+
for (const result of settled) {
|
|
193
|
+
if (result.status === "fulfilled") {
|
|
194
|
+
summaries.push(result.value);
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
throw result.reason;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return summaries;
|
|
202
|
+
}
|
|
203
|
+
|
|
169
204
|
async subscribeL1Book(input: SubscribeL1BookInput): Promise<void> {
|
|
170
205
|
this.context.assertStarted();
|
|
171
206
|
const market = await this.resolveMarketDefinition(input);
|
|
@@ -438,51 +473,132 @@ export class MarketManagerImpl
|
|
|
438
473
|
return;
|
|
439
474
|
}
|
|
440
475
|
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
catalogPromise = this.fetchAndStoreMarketCatalog(venue);
|
|
444
|
-
this.catalogPromises.set(venue, catalogPromise);
|
|
445
|
-
}
|
|
476
|
+
await this.fetchCatalogCoalesced(venue);
|
|
477
|
+
}
|
|
446
478
|
|
|
479
|
+
private async reloadVenue(venue: Venue): Promise<MarketCatalogReloadSummary> {
|
|
447
480
|
try {
|
|
448
|
-
await
|
|
481
|
+
const result = await this.fetchCatalogCoalesced(venue);
|
|
482
|
+
return { ...result, ok: true };
|
|
449
483
|
} catch (error) {
|
|
450
|
-
|
|
484
|
+
if (
|
|
485
|
+
error instanceof AcexError &&
|
|
486
|
+
error.code === "MARKET_CATALOG_LOAD_FAILED"
|
|
487
|
+
) {
|
|
488
|
+
return {
|
|
489
|
+
venue,
|
|
490
|
+
added: [],
|
|
491
|
+
removed: [],
|
|
492
|
+
total: this.countVenueMarkets(venue),
|
|
493
|
+
ok: false,
|
|
494
|
+
error,
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
|
|
451
498
|
throw error;
|
|
452
499
|
}
|
|
453
500
|
}
|
|
454
501
|
|
|
455
|
-
private async
|
|
502
|
+
private async fetchCatalogCoalesced(
|
|
503
|
+
venue: Venue,
|
|
504
|
+
): Promise<CatalogFetchResult> {
|
|
505
|
+
let catalogPromise = this.catalogPromises.get(venue);
|
|
506
|
+
if (!catalogPromise) {
|
|
507
|
+
catalogPromise = this.fetchAndStoreMarketCatalog(venue).finally(() => {
|
|
508
|
+
this.catalogPromises.delete(venue);
|
|
509
|
+
});
|
|
510
|
+
this.catalogPromises.set(venue, catalogPromise);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
return await catalogPromise;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
private async fetchAndStoreMarketCatalog(
|
|
517
|
+
venue: Venue,
|
|
518
|
+
): Promise<CatalogFetchResult> {
|
|
456
519
|
const adapter = this.getMarketAdapter(venue);
|
|
520
|
+
let markets: MarketDefinition[];
|
|
457
521
|
|
|
458
522
|
try {
|
|
459
|
-
|
|
523
|
+
markets = await adapter.loadMarkets();
|
|
524
|
+
} catch (error) {
|
|
525
|
+
throw this.createCatalogLoadError(venue, error);
|
|
526
|
+
}
|
|
460
527
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
528
|
+
const mismatchedMarket = markets.find((market) => market.venue !== venue);
|
|
529
|
+
if (mismatchedMarket) {
|
|
530
|
+
throw this.createCatalogLoadError(
|
|
531
|
+
venue,
|
|
532
|
+
new Error(
|
|
533
|
+
`Market catalog from ${venue} included ${mismatchedMarket.venue} market: ${mismatchedMarket.symbol}`,
|
|
534
|
+
),
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
const previousKeys = this.getVenueMarketKeys(venue);
|
|
466
539
|
|
|
467
|
-
|
|
468
|
-
|
|
540
|
+
for (const [key, market] of this.definitions) {
|
|
541
|
+
if (market.venue === venue) {
|
|
542
|
+
this.definitions.delete(key);
|
|
469
543
|
}
|
|
544
|
+
}
|
|
470
545
|
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
const wrapped = new AcexError(
|
|
474
|
-
"MARKET_CATALOG_LOAD_FAILED",
|
|
475
|
-
`Failed to load market catalog from ${venue}`,
|
|
476
|
-
);
|
|
477
|
-
this.context.publishRuntimeError(
|
|
478
|
-
"adapter",
|
|
479
|
-
error instanceof Error
|
|
480
|
-
? error
|
|
481
|
-
: new Error("Unknown catalog load failure"),
|
|
482
|
-
{ venue },
|
|
483
|
-
);
|
|
484
|
-
throw wrapped;
|
|
546
|
+
for (const market of markets) {
|
|
547
|
+
this.definitions.set(marketKey(market), market);
|
|
485
548
|
}
|
|
549
|
+
|
|
550
|
+
this.loadedCatalogVenues.add(venue);
|
|
551
|
+
|
|
552
|
+
const currentKeys = this.getVenueMarketKeys(venue);
|
|
553
|
+
return {
|
|
554
|
+
venue,
|
|
555
|
+
added: this.diffMarketSymbols(venue, currentKeys, previousKeys),
|
|
556
|
+
removed: this.diffMarketSymbols(venue, previousKeys, currentKeys),
|
|
557
|
+
total: currentKeys.size,
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
private getVenueMarketKeys(venue: Venue): Set<string> {
|
|
562
|
+
const keys = new Set<string>();
|
|
563
|
+
|
|
564
|
+
for (const [key, market] of this.definitions) {
|
|
565
|
+
if (market.venue === venue) {
|
|
566
|
+
keys.add(key);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
return keys;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
private countVenueMarkets(venue: Venue): number {
|
|
574
|
+
return this.getVenueMarketKeys(venue).size;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
private diffMarketSymbols(
|
|
578
|
+
venue: Venue,
|
|
579
|
+
left: Set<string>,
|
|
580
|
+
right: Set<string>,
|
|
581
|
+
): string[] {
|
|
582
|
+
const prefix = `${venue}:`;
|
|
583
|
+
return [...left]
|
|
584
|
+
.filter((key) => !right.has(key))
|
|
585
|
+
.map((key) => key.slice(prefix.length))
|
|
586
|
+
.sort((leftSymbol, rightSymbol) => leftSymbol.localeCompare(rightSymbol));
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
private createCatalogLoadError(venue: Venue, error: unknown): AcexError {
|
|
590
|
+
const wrapped = new AcexError(
|
|
591
|
+
"MARKET_CATALOG_LOAD_FAILED",
|
|
592
|
+
`Failed to load market catalog from ${venue}`,
|
|
593
|
+
);
|
|
594
|
+
this.context.publishRuntimeError(
|
|
595
|
+
"adapter",
|
|
596
|
+
error instanceof Error
|
|
597
|
+
? error
|
|
598
|
+
: new Error("Unknown catalog load failure"),
|
|
599
|
+
{ venue },
|
|
600
|
+
);
|
|
601
|
+
return wrapped;
|
|
486
602
|
}
|
|
487
603
|
|
|
488
604
|
private async resolveMarketDefinition(input: {
|
package/src/types/market.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type BigNumber from "bignumber.js";
|
|
2
|
+
import type { AcexError } from "../errors.ts";
|
|
2
3
|
import type { MarketFreshness, SubscriptionActivity, Venue } from "./shared.ts";
|
|
3
4
|
|
|
4
5
|
export type MarketType = "spot" | "swap" | "future";
|
|
@@ -26,6 +27,15 @@ export interface MarketDefinition {
|
|
|
26
27
|
raw: Record<string, unknown>;
|
|
27
28
|
}
|
|
28
29
|
|
|
30
|
+
export interface MarketCatalogReloadSummary {
|
|
31
|
+
venue: Venue;
|
|
32
|
+
added: string[];
|
|
33
|
+
removed: string[];
|
|
34
|
+
total: number;
|
|
35
|
+
ok: boolean;
|
|
36
|
+
error?: AcexError;
|
|
37
|
+
}
|
|
38
|
+
|
|
29
39
|
export interface MarketDataStatus {
|
|
30
40
|
venue: Venue;
|
|
31
41
|
symbol: string;
|
|
@@ -159,6 +169,7 @@ export interface MarketManager {
|
|
|
159
169
|
readonly events: MarketEventStreams;
|
|
160
170
|
|
|
161
171
|
loadMarkets(): Promise<void>;
|
|
172
|
+
reloadMarkets(venue?: Venue): Promise<MarketCatalogReloadSummary[]>;
|
|
162
173
|
subscribeL1Book(input: SubscribeL1BookInput): Promise<void>;
|
|
163
174
|
unsubscribeL1Book(input: SubscribeL1BookInput): Promise<void>;
|
|
164
175
|
subscribeFundingRate(input: SubscribeFundingRateInput): Promise<void>;
|