@usherlabs/cex-broker 0.2.40 → 0.2.42
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/commands/cli.js +928 -332
- package/dist/helpers/balance-update-archive-consumer.d.ts +16 -0
- package/dist/helpers/broker-execution-archive/index.d.ts +2 -2
- package/dist/helpers/broker-execution-archive/rows.d.ts +25 -0
- package/dist/helpers/broker-execution-archive/types.d.ts +5 -3
- package/dist/helpers/broker-execution-archive/writer.d.ts +31 -1
- package/dist/helpers/user-asset-archive-poller.d.ts +19 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +928 -332
- package/dist/index.js.map +11 -9
- package/package.json +1 -1
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { BrokerPoolEntry } from "./broker";
|
|
2
|
+
import { type BrokerExecutionArchiver } from "./broker-execution-archive";
|
|
3
|
+
import type { UserDataStreamSupervisor } from "./user-data-stream-supervisor";
|
|
4
|
+
type BalanceUpdateSubscriptionSource = Pick<UserDataStreamSupervisor, "subscribe">;
|
|
5
|
+
export declare class BalanceUpdateArchiveConsumer {
|
|
6
|
+
#private;
|
|
7
|
+
private readonly params;
|
|
8
|
+
constructor(params: {
|
|
9
|
+
brokers: Record<string, BrokerPoolEntry>;
|
|
10
|
+
archiver: BrokerExecutionArchiver;
|
|
11
|
+
userDataStreamSupervisor: BalanceUpdateSubscriptionSource;
|
|
12
|
+
});
|
|
13
|
+
start(): void;
|
|
14
|
+
stop(): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { archiveOrderExecutionInBackground, archiveSubscribeStreamInBackground, archiveTransferEventInBackground, archiveWithdrawalObservationsInBackground, captureMarketMetadataSnapshot, captureMarketMetadataSnapshotInBackground, } from "./capture";
|
|
2
2
|
export { hashMarketMetadata, redactErrorForArchive, redactSecretLiterals, redactStreamPayload, } from "./redact";
|
|
3
|
-
export { buildAccountBalanceSnapshotRow, buildCommonArchiveTags, buildFillEventArchiveRow, buildMarketMetadataSnapshotRow, buildOrderEventArchiveRow, buildSubscribeStreamArchiveRow, buildTransferEventArchiveRow, extractBinanceInternalTransferId, type FillArchiveFields, type NormalizedCcxtBalance, type NormalizedCcxtTransfer, normalizeCcxtBalanceForArchive, normalizeCcxtTradeForArchive, normalizeCcxtTransactionForArchive, normalizeTimestamp, type TransferArchiveFields, } from "./rows";
|
|
4
|
-
export { ACCOUNT_BALANCE_PRECISION_BASIS, ACCOUNT_BALANCE_SCOPE, ARCHIVE_SCHEMA_VERSION, BROKER_READ_SOURCE, BROKER_WRITE_SOURCE, type BrokerArchiveCommonTags, type BrokerArchiveRow, type BrokerArchiveSource, type BrokerArchiveTable, type OrderArchiveAction, type SubscribeArchiveType, type TransferEventKind, type TransferLifecycleAction, } from "./types";
|
|
3
|
+
export { buildAccountBalanceSnapshotRow, buildCommonArchiveTags, buildFillEventArchiveRow, buildMarketMetadataSnapshotRow, buildOrderEventArchiveRow, buildSubscribeStreamArchiveRow, buildTransferEventArchiveRow, buildUserAssetSnapshotRow, extractBinanceInternalTransferId, type FillArchiveFields, type NormalizedCcxtBalance, type NormalizedCcxtTransfer, type NormalizedUserAssets, normalizeBinanceUserAssetsForArchive, normalizeCcxtBalanceForArchive, normalizeCcxtTradeForArchive, normalizeCcxtTransactionForArchive, normalizeTimestamp, type TransferArchiveFields, } from "./rows";
|
|
4
|
+
export { ACCOUNT_BALANCE_PRECISION_BASIS, ACCOUNT_BALANCE_SCOPE, ARCHIVE_SCHEMA_VERSION, BROKER_READ_SOURCE, BROKER_WRITE_SOURCE, type BrokerArchiveCommonTags, type BrokerArchiveRow, type BrokerArchiveSource, type BrokerArchiveTable, type OrderArchiveAction, type SubscribeArchiveType, type TransferEventKind, type TransferLifecycleAction, USER_ASSET_BALANCE_SCOPE, USER_ASSET_PRECISION_BASIS, } from "./types";
|
|
5
5
|
export { DEFAULT_WITHDRAWAL_OBSERVATION_TRACKER_MAX_ENTRIES, WithdrawalObservationTracker, } from "./withdrawal-observation-tracker";
|
|
6
6
|
export { BrokerExecutionArchiveDurabilityError, BrokerExecutionArchiver, type BrokerExecutionArchiverOptions, createBrokerExecutionArchiverFromEnv, isArchiveOtelLogsEnabled, isBrokerExecutionArchiveTable, resolveArchiveForwarderUrlFromEnv, rethrowArchiveDurabilityError, } from "./writer";
|
|
@@ -18,6 +18,30 @@ export declare function buildAccountBalanceSnapshotRow(input: {
|
|
|
18
18
|
tags: BrokerArchiveCommonTags;
|
|
19
19
|
balance: NormalizedCcxtBalance;
|
|
20
20
|
}): BrokerArchiveRow;
|
|
21
|
+
export type NormalizedUserAssets = {
|
|
22
|
+
reportedAssets: string[];
|
|
23
|
+
incompleteAssets: string[];
|
|
24
|
+
freeBalances: BalanceQuantityMap;
|
|
25
|
+
lockedBalances: BalanceQuantityMap;
|
|
26
|
+
freezeBalances: BalanceQuantityMap;
|
|
27
|
+
withdrawingBalances: BalanceQuantityMap;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Normalizes a `POST /sapi/v3/asset/getUserAsset` response into one coherent
|
|
31
|
+
* snapshot.
|
|
32
|
+
*
|
|
33
|
+
* Fails closed on a response shape we cannot account for: a non-array body or a
|
|
34
|
+
* duplicate/unkeyed asset entry would each drop owned quantity on the floor, and
|
|
35
|
+
* an under-counted NAV is worse than an absent one. Per-bucket uncertainty is
|
|
36
|
+
* narrower and is reported rather than thrown: the asset is listed in
|
|
37
|
+
* `incompleteAssets` and its key is left out of the bucket map, so a reader sees
|
|
38
|
+
* "unknown", never an implicit zero.
|
|
39
|
+
*/
|
|
40
|
+
export declare function normalizeBinanceUserAssetsForArchive(response: unknown): NormalizedUserAssets;
|
|
41
|
+
export declare function buildUserAssetSnapshotRow(input: {
|
|
42
|
+
tags: BrokerArchiveCommonTags;
|
|
43
|
+
userAssets: NormalizedUserAssets;
|
|
44
|
+
}): BrokerArchiveRow;
|
|
21
45
|
export declare function buildCommonArchiveTags(input: {
|
|
22
46
|
source?: BrokerArchiveSource;
|
|
23
47
|
deploymentId: string;
|
|
@@ -46,6 +70,7 @@ export type TransferArchiveFields = {
|
|
|
46
70
|
eventKind: TransferEventKind;
|
|
47
71
|
lifecycleAction: TransferLifecycleAction;
|
|
48
72
|
status?: string;
|
|
73
|
+
assetSymbol?: string;
|
|
49
74
|
amount?: string;
|
|
50
75
|
address?: string;
|
|
51
76
|
network?: string;
|
|
@@ -2,7 +2,7 @@ export declare const BROKER_READ_SOURCE: "broker_read";
|
|
|
2
2
|
export declare const BROKER_WRITE_SOURCE: "broker_write";
|
|
3
3
|
export type BrokerArchiveSource = typeof BROKER_READ_SOURCE | typeof BROKER_WRITE_SOURCE;
|
|
4
4
|
export declare const ARCHIVE_SCHEMA_VERSION: "1";
|
|
5
|
-
export type BrokerArchiveTable = "broker_execution.order_events" | "broker_execution.market_metadata_snapshots" | "broker_execution.transfer_events" | "broker_execution.fill_events" | "broker_account.balance_snapshots" | "market_data.orderbook_snapshots" | "market_data.candles" | "market_data.cex_stream_events" | "market_data.cex_ticker_events" | "market_data.cex_trades" | "market_data.cex_ohlcv" | "market_data.cex_order_book_levels" | "market_data.cex_order_book_depth_summary";
|
|
5
|
+
export type BrokerArchiveTable = "broker_execution.order_events" | "broker_execution.market_metadata_snapshots" | "broker_execution.transfer_events" | "broker_execution.fill_events" | "broker_account.balance_snapshots" | "broker_account.user_asset_snapshots" | "market_data.orderbook_snapshots" | "market_data.candles" | "market_data.cex_stream_events" | "market_data.cex_ticker_events" | "market_data.cex_trades" | "market_data.cex_ohlcv" | "market_data.cex_order_book_levels" | "market_data.cex_order_book_depth_summary";
|
|
6
6
|
export type BrokerArchiveRow = {
|
|
7
7
|
table: BrokerArchiveTable;
|
|
8
8
|
row: Record<string, unknown>;
|
|
@@ -17,8 +17,10 @@ export type BrokerArchiveCommonTags = {
|
|
|
17
17
|
};
|
|
18
18
|
export type OrderArchiveAction = "CreateOrder" | "CancelOrder" | "GetOrderDetails";
|
|
19
19
|
export type SubscribeArchiveType = "ORDERS" | "BALANCE";
|
|
20
|
-
export type TransferEventKind = "withdrawal" | "deposit" | "internal_transfer";
|
|
21
|
-
export type TransferLifecycleAction = "submit_withdrawal" | "observe_withdrawal" | "observe_deposit" | "submit_internal_transfer";
|
|
20
|
+
export type TransferEventKind = "withdrawal" | "deposit" | "internal_transfer" | "balance_delta";
|
|
21
|
+
export type TransferLifecycleAction = "submit_withdrawal" | "observe_withdrawal" | "observe_deposit" | "observe_balance_update" | "submit_internal_transfer";
|
|
22
22
|
export declare const FILL_EVENT_KIND: "trade_history_fill";
|
|
23
23
|
export declare const ACCOUNT_BALANCE_SCOPE: "spot";
|
|
24
24
|
export declare const ACCOUNT_BALANCE_PRECISION_BASIS: "ccxt_normalized_number";
|
|
25
|
+
export declare const USER_ASSET_BALANCE_SCOPE: "user_asset";
|
|
26
|
+
export declare const USER_ASSET_PRECISION_BASIS: "venue_raw_string";
|
|
@@ -23,6 +23,19 @@ type ArchiverStats = {
|
|
|
23
23
|
flushed: number;
|
|
24
24
|
forwarderFailures: number;
|
|
25
25
|
};
|
|
26
|
+
export type BrokerExecutionArchiveHealthSnapshot = {
|
|
27
|
+
healthy: boolean;
|
|
28
|
+
queue_depth: number;
|
|
29
|
+
oldest_pending_age_ms: number;
|
|
30
|
+
shed_total: number;
|
|
31
|
+
last_failure_at: number | null;
|
|
32
|
+
last_shed_at: number | null;
|
|
33
|
+
last_success_at: number | null;
|
|
34
|
+
sink_latency_ms: number | null;
|
|
35
|
+
sink_errors_total: number;
|
|
36
|
+
in_flight: boolean;
|
|
37
|
+
in_flight_age_ms: number;
|
|
38
|
+
};
|
|
26
39
|
export declare function isArchiveOtelLogsEnabled(): boolean;
|
|
27
40
|
export declare function resolveArchiveForwarderUrlFromEnv(): string | undefined;
|
|
28
41
|
export declare function resolveArchiveSourceFromEnv(value?: string | undefined): BrokerArchiveSource;
|
|
@@ -39,9 +52,21 @@ export declare class BrokerExecutionArchiver {
|
|
|
39
52
|
private readonly flushIntervalMs;
|
|
40
53
|
private readonly forwarderTimeoutMs;
|
|
41
54
|
private readonly queue;
|
|
55
|
+
private readonly enqueueTimes;
|
|
42
56
|
private readonly stats;
|
|
57
|
+
private pendingRetry;
|
|
43
58
|
private flushTimer;
|
|
44
59
|
private flushInFlight;
|
|
60
|
+
private inFlightBatch;
|
|
61
|
+
private inFlightStartedAtMs;
|
|
62
|
+
private lastFailureAtMs;
|
|
63
|
+
private lastShedAtMs;
|
|
64
|
+
private lastSuccessAtMs;
|
|
65
|
+
private lastFailureEventSequence;
|
|
66
|
+
private lastShedEventSequence;
|
|
67
|
+
private lastSuccessEventSequence;
|
|
68
|
+
private lastSinkLatencyMs;
|
|
69
|
+
private healthEventSequence;
|
|
45
70
|
private lastShedWarnAtMs;
|
|
46
71
|
private closing;
|
|
47
72
|
private closed;
|
|
@@ -61,13 +86,18 @@ export declare class BrokerExecutionArchiver {
|
|
|
61
86
|
close(): Promise<void>;
|
|
62
87
|
getStats(): Readonly<ArchiverStats>;
|
|
63
88
|
getQueueDepth(): number;
|
|
89
|
+
getHealthSnapshot(): Readonly<BrokerExecutionArchiveHealthSnapshot>;
|
|
90
|
+
private oldestPendingEnqueueAtMs;
|
|
91
|
+
private recordHealthEvent;
|
|
92
|
+
private emitArchiveHealthMetrics;
|
|
64
93
|
private closeLossJournal;
|
|
65
|
-
private enforceQueueBound;
|
|
66
94
|
private appendLossRecords;
|
|
67
95
|
private flushBatch;
|
|
96
|
+
private clearInFlightBatch;
|
|
68
97
|
private recordFlushHealth;
|
|
69
98
|
private recordArchiveMetric;
|
|
70
99
|
private recordArchiveGauge;
|
|
100
|
+
private recordArchiveObservableGauge;
|
|
71
101
|
private emitOtelLog;
|
|
72
102
|
private postToForwarder;
|
|
73
103
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { BrokerPoolEntry } from "./broker";
|
|
2
|
+
import { type BrokerExecutionArchiver } from "./broker-execution-archive";
|
|
3
|
+
import type { OtelMetrics } from "./otel";
|
|
4
|
+
export type UserAssetArchivePollerConfig = {
|
|
5
|
+
pollIntervalMs: number;
|
|
6
|
+
};
|
|
7
|
+
export declare class UserAssetArchivePoller {
|
|
8
|
+
#private;
|
|
9
|
+
private readonly params;
|
|
10
|
+
constructor(params: {
|
|
11
|
+
brokers: Record<string, BrokerPoolEntry>;
|
|
12
|
+
archiver: BrokerExecutionArchiver;
|
|
13
|
+
metrics?: OtelMetrics;
|
|
14
|
+
config?: Partial<UserAssetArchivePollerConfig>;
|
|
15
|
+
});
|
|
16
|
+
start(): void;
|
|
17
|
+
stop(): Promise<void>;
|
|
18
|
+
pollAllOnce(): Promise<boolean>;
|
|
19
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -18,6 +18,8 @@ export default class CEXBroker {
|
|
|
18
18
|
private fillArchivePoller?;
|
|
19
19
|
private depositArchivePoller?;
|
|
20
20
|
private accountBalanceArchivePoller?;
|
|
21
|
+
private userAssetArchivePoller?;
|
|
22
|
+
private balanceUpdateArchiveConsumer?;
|
|
21
23
|
private userDataStreamSupervisor?;
|
|
22
24
|
/**
|
|
23
25
|
* Loads environment variables prefixed with CEX_BROKER_
|