@imbingox/acex 0.3.1-beta.0 → 0.4.0-beta.10
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/README.md +11 -10
- package/docs/api.md +502 -1030
- package/package.json +1 -1
- package/src/adapters/binance/adapter.ts +19 -1
- package/src/adapters/binance/market-catalog.ts +93 -22
- package/src/adapters/binance/private-adapter.ts +302 -59
- package/src/adapters/binance/rate-limit.ts +47 -0
- package/src/adapters/binance/server-time.ts +106 -0
- package/src/adapters/juplend/private-adapter.ts +97 -68
- package/src/adapters/types.ts +25 -1
- package/src/client/context.ts +26 -9
- package/src/client/private-subscription-coordinator.ts +898 -63
- package/src/client/runtime.ts +49 -11
- package/src/client/venue-capabilities.ts +1 -0
- package/src/errors.ts +156 -2
- package/src/index.ts +8 -1
- package/src/internal/decimal.ts +19 -0
- package/src/internal/http-client.ts +608 -0
- package/src/internal/rate-limiter.ts +181 -0
- package/src/internal/watermark.ts +83 -0
- package/src/managers/account-manager.ts +267 -55
- package/src/managers/market-manager.ts +261 -60
- package/src/managers/order-manager.ts +798 -84
- package/src/types/account.ts +27 -28
- package/src/types/client.ts +1 -0
- package/src/types/market.ts +37 -12
- package/src/types/order.ts +7 -7
- package/src/types/shared.ts +66 -0
package/src/types/account.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type BigNumber from "bignumber.js";
|
|
2
1
|
import type {
|
|
3
2
|
PrivateRuntimeReason,
|
|
4
3
|
PrivateRuntimeStatus,
|
|
@@ -52,9 +51,9 @@ export interface BalanceSnapshot {
|
|
|
52
51
|
accountId: string;
|
|
53
52
|
venue: Venue;
|
|
54
53
|
asset: string;
|
|
55
|
-
free:
|
|
56
|
-
used:
|
|
57
|
-
total:
|
|
54
|
+
free: string;
|
|
55
|
+
used: string;
|
|
56
|
+
total: string;
|
|
58
57
|
exchangeTs?: number;
|
|
59
58
|
receivedAt: number;
|
|
60
59
|
updatedAt: number;
|
|
@@ -63,12 +62,12 @@ export interface BalanceSnapshot {
|
|
|
63
62
|
}
|
|
64
63
|
|
|
65
64
|
export interface LendingBalanceFacet {
|
|
66
|
-
supplied:
|
|
67
|
-
borrowed:
|
|
68
|
-
interest:
|
|
69
|
-
netAsset:
|
|
70
|
-
supplyAPY?:
|
|
71
|
-
borrowAPY?:
|
|
65
|
+
supplied: string;
|
|
66
|
+
borrowed: string;
|
|
67
|
+
interest: string;
|
|
68
|
+
netAsset: string;
|
|
69
|
+
supplyAPY?: string;
|
|
70
|
+
borrowAPY?: string;
|
|
72
71
|
}
|
|
73
72
|
|
|
74
73
|
export interface PositionSnapshot {
|
|
@@ -76,12 +75,12 @@ export interface PositionSnapshot {
|
|
|
76
75
|
venue: Venue;
|
|
77
76
|
symbol: string;
|
|
78
77
|
side: PositionSide;
|
|
79
|
-
size:
|
|
80
|
-
entryPrice?:
|
|
81
|
-
markPrice?:
|
|
82
|
-
unrealizedPnl?:
|
|
83
|
-
leverage?:
|
|
84
|
-
liquidationPrice?:
|
|
78
|
+
size: string;
|
|
79
|
+
entryPrice?: string;
|
|
80
|
+
markPrice?: string;
|
|
81
|
+
unrealizedPnl?: string;
|
|
82
|
+
leverage?: string;
|
|
83
|
+
liquidationPrice?: string;
|
|
85
84
|
exchangeTs?: number;
|
|
86
85
|
receivedAt: number;
|
|
87
86
|
updatedAt: number;
|
|
@@ -91,12 +90,12 @@ export interface PositionSnapshot {
|
|
|
91
90
|
export interface RiskSnapshot {
|
|
92
91
|
accountId: string;
|
|
93
92
|
venue: Venue;
|
|
94
|
-
netEquity?:
|
|
95
|
-
riskEquity?:
|
|
96
|
-
riskRatio?:
|
|
97
|
-
riskLeverage?:
|
|
98
|
-
initialMargin?:
|
|
99
|
-
maintenanceMargin?:
|
|
93
|
+
netEquity?: string;
|
|
94
|
+
riskEquity?: string;
|
|
95
|
+
riskRatio?: string;
|
|
96
|
+
riskLeverage?: string;
|
|
97
|
+
initialMargin?: string;
|
|
98
|
+
maintenanceMargin?: string;
|
|
100
99
|
exchangeTs?: number;
|
|
101
100
|
receivedAt: number;
|
|
102
101
|
updatedAt: number;
|
|
@@ -105,12 +104,12 @@ export interface RiskSnapshot {
|
|
|
105
104
|
}
|
|
106
105
|
|
|
107
106
|
export interface LendingRiskFacet {
|
|
108
|
-
marginLevel?:
|
|
109
|
-
healthFactor?:
|
|
110
|
-
ltv?:
|
|
111
|
-
liquidationThreshold?:
|
|
112
|
-
totalCollateralUSD?:
|
|
113
|
-
totalDebtUSD?:
|
|
107
|
+
marginLevel?: string;
|
|
108
|
+
healthFactor?: string;
|
|
109
|
+
ltv?: string;
|
|
110
|
+
liquidationThreshold?: string;
|
|
111
|
+
totalCollateralUSD?: string;
|
|
112
|
+
totalDebtUSD?: string;
|
|
114
113
|
}
|
|
115
114
|
|
|
116
115
|
export interface AccountSnapshot {
|
package/src/types/client.ts
CHANGED
|
@@ -83,6 +83,7 @@ export type OrderTimeInForceCapability = "gtc" | "post_only";
|
|
|
83
83
|
|
|
84
84
|
export interface VenueMarketCapabilities {
|
|
85
85
|
catalog: VenueCapabilitySupport;
|
|
86
|
+
serverTime: VenueCapabilitySupport;
|
|
86
87
|
l1Book: VenueCapabilitySupport;
|
|
87
88
|
fundingRate: FundingRateCapability;
|
|
88
89
|
marketTypes: MarketType[];
|
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";
|
|
@@ -15,17 +16,39 @@ export interface MarketDefinition {
|
|
|
15
16
|
contract: boolean;
|
|
16
17
|
linear?: boolean;
|
|
17
18
|
inverse?: boolean;
|
|
18
|
-
contractSize?:
|
|
19
|
+
contractSize?: string;
|
|
19
20
|
pricePrecision: number;
|
|
20
21
|
amountPrecision: number;
|
|
21
|
-
priceStep:
|
|
22
|
-
amountStep:
|
|
23
|
-
minAmount?:
|
|
24
|
-
minNotional?:
|
|
22
|
+
priceStep: string;
|
|
23
|
+
amountStep: string;
|
|
24
|
+
minAmount?: string;
|
|
25
|
+
minNotional?: string;
|
|
25
26
|
expiry?: number;
|
|
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
|
+
|
|
39
|
+
export interface VenueServerTime {
|
|
40
|
+
/** Exchange server time in epoch milliseconds. Binance currently measures the USDM cluster. */
|
|
41
|
+
serverTime: number;
|
|
42
|
+
/** Local wall-clock timestamp captured immediately before the HTTP request is sent. */
|
|
43
|
+
requestSentAt: number;
|
|
44
|
+
/** Local wall-clock timestamp captured immediately after the HTTP response is received. */
|
|
45
|
+
responseReceivedAt: number;
|
|
46
|
+
/** Round trip duration measured with a monotonic clock, in milliseconds. */
|
|
47
|
+
roundTripMs: number;
|
|
48
|
+
/** NTP-style offset estimate: serverTime - midpoint(requestSentAt, responseReceivedAt). */
|
|
49
|
+
estimatedOffsetMs: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
29
52
|
export interface MarketDataStatus {
|
|
30
53
|
venue: Venue;
|
|
31
54
|
symbol: string;
|
|
@@ -92,10 +115,10 @@ export interface MarketEventFilter {
|
|
|
92
115
|
export interface L1Book {
|
|
93
116
|
venue: Venue;
|
|
94
117
|
symbol: string;
|
|
95
|
-
bidPrice:
|
|
96
|
-
bidSize:
|
|
97
|
-
askPrice:
|
|
98
|
-
askSize:
|
|
118
|
+
bidPrice: string;
|
|
119
|
+
bidSize: string;
|
|
120
|
+
askPrice: string;
|
|
121
|
+
askSize: string;
|
|
99
122
|
exchangeTs?: number;
|
|
100
123
|
receivedAt: number;
|
|
101
124
|
updatedAt: number;
|
|
@@ -106,10 +129,10 @@ export interface L1Book {
|
|
|
106
129
|
export interface FundingRateSnapshot {
|
|
107
130
|
venue: Venue;
|
|
108
131
|
symbol: string;
|
|
109
|
-
fundingRate:
|
|
132
|
+
fundingRate: string;
|
|
110
133
|
nextFundingTime?: number;
|
|
111
|
-
markPrice?:
|
|
112
|
-
indexPrice?:
|
|
134
|
+
markPrice?: string;
|
|
135
|
+
indexPrice?: string;
|
|
113
136
|
exchangeTs?: number;
|
|
114
137
|
receivedAt: number;
|
|
115
138
|
updatedAt: number;
|
|
@@ -159,6 +182,8 @@ export interface MarketManager {
|
|
|
159
182
|
readonly events: MarketEventStreams;
|
|
160
183
|
|
|
161
184
|
loadMarkets(): Promise<void>;
|
|
185
|
+
reloadMarkets(venue?: Venue): Promise<MarketCatalogReloadSummary[]>;
|
|
186
|
+
fetchServerTime(venue: Venue): Promise<VenueServerTime>;
|
|
162
187
|
subscribeL1Book(input: SubscribeL1BookInput): Promise<void>;
|
|
163
188
|
unsubscribeL1Book(input: SubscribeL1BookInput): Promise<void>;
|
|
164
189
|
subscribeFundingRate(input: SubscribeFundingRateInput): Promise<void>;
|
package/src/types/order.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type BigNumber from "bignumber.js";
|
|
2
1
|
import type { PositionSide } from "./account.ts";
|
|
3
2
|
import type {
|
|
4
3
|
PrivateRuntimeReason,
|
|
@@ -49,6 +48,7 @@ export interface UnsubscribeOrdersInput {
|
|
|
49
48
|
|
|
50
49
|
export interface GetOrderInput {
|
|
51
50
|
accountId: string;
|
|
51
|
+
symbol?: string;
|
|
52
52
|
orderId?: string;
|
|
53
53
|
clientOrderId?: string;
|
|
54
54
|
}
|
|
@@ -102,14 +102,14 @@ export interface OrderSnapshot {
|
|
|
102
102
|
side: OrderSide;
|
|
103
103
|
type: string;
|
|
104
104
|
status: OrderStatus;
|
|
105
|
-
price?:
|
|
106
|
-
triggerPrice?:
|
|
107
|
-
amount:
|
|
108
|
-
filled:
|
|
109
|
-
remaining?:
|
|
105
|
+
price?: string;
|
|
106
|
+
triggerPrice?: string;
|
|
107
|
+
amount: string;
|
|
108
|
+
filled: string;
|
|
109
|
+
remaining?: string;
|
|
110
110
|
reduceOnly?: boolean;
|
|
111
111
|
positionSide?: PositionSide;
|
|
112
|
-
avgFillPrice?:
|
|
112
|
+
avgFillPrice?: string;
|
|
113
113
|
exchangeTs?: number;
|
|
114
114
|
receivedAt: number;
|
|
115
115
|
updatedAt: number;
|
package/src/types/shared.ts
CHANGED
|
@@ -24,6 +24,63 @@ export interface Logger {
|
|
|
24
24
|
error(msg: string, context?: Record<string, unknown>): void;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
export interface TimeProvider {
|
|
28
|
+
/** Millisecond timestamp used for outbound request/signing timestamps. */
|
|
29
|
+
now(): number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface RateLimitScope {
|
|
33
|
+
venue: Venue;
|
|
34
|
+
accountId?: string;
|
|
35
|
+
endpointKey: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface RateLimitUsage {
|
|
39
|
+
/** Exchange request-weight usage by interval key, e.g. "1m". */
|
|
40
|
+
weight?: Record<string, number>;
|
|
41
|
+
/** Exchange order-count usage by interval key, separate from request weight. */
|
|
42
|
+
orderCount?: Record<string, number>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface RateLimitRequestContext {
|
|
46
|
+
scope: RateLimitScope;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface RateLimitResponseContext {
|
|
50
|
+
status: number;
|
|
51
|
+
headers?: Headers;
|
|
52
|
+
usage?: RateLimitUsage;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface RateLimitTransportErrorContext {
|
|
56
|
+
status?: number;
|
|
57
|
+
headers?: Headers;
|
|
58
|
+
retryAfterMs?: number;
|
|
59
|
+
usage?: RateLimitUsage;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface RateLimitSnapshot {
|
|
63
|
+
scope: RateLimitScope;
|
|
64
|
+
usage?: RateLimitUsage;
|
|
65
|
+
blockedUntil?: number;
|
|
66
|
+
retryAfterMs?: number;
|
|
67
|
+
state: "ok" | "rate_limited" | "banned";
|
|
68
|
+
updatedAt?: number;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface RateLimiter {
|
|
72
|
+
beforeRequest(ctx: RateLimitRequestContext): Promise<void> | void;
|
|
73
|
+
afterResponse(
|
|
74
|
+
ctx: RateLimitRequestContext,
|
|
75
|
+
response: RateLimitResponseContext,
|
|
76
|
+
): Promise<void> | void;
|
|
77
|
+
onTransportError(
|
|
78
|
+
ctx: RateLimitRequestContext,
|
|
79
|
+
error: RateLimitTransportErrorContext,
|
|
80
|
+
): Promise<void> | void;
|
|
81
|
+
getSnapshot(scope: RateLimitScope): RateLimitSnapshot | undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
27
84
|
export interface MarketRuntimeOptions {
|
|
28
85
|
l1InitialMessageTimeoutMs?: number;
|
|
29
86
|
l1StaleAfterMs?: number;
|
|
@@ -38,6 +95,7 @@ export interface AccountRuntimeOptions {
|
|
|
38
95
|
listenKeyKeepAliveMs?: number;
|
|
39
96
|
binance?: {
|
|
40
97
|
riskPollIntervalMs?: number;
|
|
98
|
+
privateReconcileIntervalMs?: number;
|
|
41
99
|
};
|
|
42
100
|
juplend?: {
|
|
43
101
|
pollIntervalMs?: number;
|
|
@@ -46,12 +104,20 @@ export interface AccountRuntimeOptions {
|
|
|
46
104
|
};
|
|
47
105
|
}
|
|
48
106
|
|
|
107
|
+
export interface OrderRuntimeOptions {
|
|
108
|
+
maxClosedOrdersPerSymbol?: number;
|
|
109
|
+
}
|
|
110
|
+
|
|
49
111
|
export interface CreateClientOptions {
|
|
50
112
|
sandbox?: boolean;
|
|
113
|
+
/** Request/signing clock; local receivedAt/freshness clocks stay independent. */
|
|
114
|
+
clock?: TimeProvider;
|
|
115
|
+
rateLimiter?: RateLimiter;
|
|
51
116
|
logger?: Logger;
|
|
52
117
|
logLevel?: LogLevel;
|
|
53
118
|
market?: MarketRuntimeOptions;
|
|
54
119
|
account?: AccountRuntimeOptions;
|
|
120
|
+
order?: OrderRuntimeOptions;
|
|
55
121
|
}
|
|
56
122
|
|
|
57
123
|
export interface AccountCredentials {
|