@imbingox/acex 0.1.0-beta.0 → 0.1.0-beta.2
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 +575 -42
- package/index.ts +1 -0
- package/package.json +19 -24
- package/src/adapters/binance/adapter.ts +53 -0
- package/src/adapters/binance/book-ticker.ts +123 -0
- package/src/adapters/binance/market-catalog.ts +258 -0
- package/src/adapters/types.ts +43 -0
- package/src/client/context.ts +60 -0
- package/src/client/create-client.ts +6 -0
- package/src/client/runtime.ts +283 -0
- package/src/errors.ts +20 -0
- package/src/index.ts +5 -0
- package/src/internal/async-event-bus.ts +100 -0
- package/src/internal/filters.ts +119 -0
- package/src/internal/managed-websocket.ts +258 -0
- package/src/managers/account-manager.ts +315 -0
- package/src/managers/market-manager.ts +653 -0
- package/src/managers/order-manager.ts +304 -0
- package/src/types/account.ts +161 -0
- package/src/types/client.ts +79 -0
- package/src/types/index.ts +5 -0
- package/src/types/market.ts +138 -0
- package/src/types/order.ts +143 -0
- package/src/types/shared.ts +78 -0
- package/dist/adapters/ccxt/aster-ccxt-adapter.d.ts +0 -157
- package/dist/adapters/ccxt/aster-ccxt-adapter.js +0 -272
- package/dist/adapters/ccxt/binance-usdm-ccxt-adapter.d.ts +0 -179
- package/dist/adapters/ccxt/binance-usdm-ccxt-adapter.js +0 -537
- package/dist/adapters/fake/fake-aster-adapter.d.ts +0 -130
- package/dist/adapters/fake/fake-aster-adapter.js +0 -283
- package/dist/adapters/types.d.ts +0 -210
- package/dist/adapters/types.js +0 -1
- package/dist/core/client.d.ts +0 -37
- package/dist/core/client.js +0 -45
- package/dist/core/recovery.d.ts +0 -22
- package/dist/core/recovery.js +0 -18
- package/dist/core/runtime.d.ts +0 -26
- package/dist/core/runtime.js +0 -150
- package/dist/errors/acex-error.d.ts +0 -25
- package/dist/errors/acex-error.js +0 -54
- package/dist/index.d.ts +0 -5
- package/dist/index.js +0 -3
- package/dist/managers/account-manager.d.ts +0 -41
- package/dist/managers/account-manager.js +0 -80
- package/dist/managers/market-manager.d.ts +0 -16
- package/dist/managers/market-manager.js +0 -28
- package/dist/managers/order-manager.d.ts +0 -87
- package/dist/managers/order-manager.js +0 -122
- package/dist/runtime/async-queue.d.ts +0 -8
- package/dist/runtime/async-queue.js +0 -88
- package/dist/runtime/request-id.d.ts +0 -1
- package/dist/runtime/request-id.js +0 -5
- package/dist/store/account-store.d.ts +0 -52
- package/dist/store/account-store.js +0 -18
- package/dist/store/health-store.d.ts +0 -16
- package/dist/store/health-store.js +0 -29
- package/dist/store/market-store.d.ts +0 -42
- package/dist/store/market-store.js +0 -51
- package/dist/store/order-store.d.ts +0 -38
- package/dist/store/order-store.js +0 -49
- package/dist/testing/create-fake-runtime.d.ts +0 -5
- package/dist/testing/create-fake-runtime.js +0 -7
- package/dist/types/public.d.ts +0 -11
- package/dist/types/public.js +0 -1
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AccountAwareManager,
|
|
3
|
+
ClientContext,
|
|
4
|
+
HealthReporter,
|
|
5
|
+
ManagerLifecycle,
|
|
6
|
+
} from "../client/context.ts";
|
|
7
|
+
import { AsyncEventBus } from "../internal/async-event-bus.ts";
|
|
8
|
+
import { matchesOrderFilter } from "../internal/filters.ts";
|
|
9
|
+
import type {
|
|
10
|
+
Exchange,
|
|
11
|
+
GetOrderInput,
|
|
12
|
+
OrderDataStatus,
|
|
13
|
+
OrderEvent,
|
|
14
|
+
OrderEventStreams,
|
|
15
|
+
OrderManager,
|
|
16
|
+
OrderSnapshot,
|
|
17
|
+
OrderSnapshotReplacedEvent,
|
|
18
|
+
OrderStatusChangedEvent,
|
|
19
|
+
SubscribeOrdersInput,
|
|
20
|
+
UnsubscribeOrdersInput,
|
|
21
|
+
} from "../types/index.ts";
|
|
22
|
+
|
|
23
|
+
interface OrderRecord {
|
|
24
|
+
accountId: string;
|
|
25
|
+
exchange: Exchange;
|
|
26
|
+
subscribed: boolean;
|
|
27
|
+
snapshots: Map<string, OrderSnapshot>;
|
|
28
|
+
status: OrderDataStatus;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function cloneOrderStatus(status: OrderDataStatus): OrderDataStatus {
|
|
32
|
+
return { ...status };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export class OrderManagerImpl
|
|
36
|
+
implements
|
|
37
|
+
OrderManager,
|
|
38
|
+
ManagerLifecycle,
|
|
39
|
+
AccountAwareManager,
|
|
40
|
+
HealthReporter<OrderDataStatus>
|
|
41
|
+
{
|
|
42
|
+
readonly events: OrderEventStreams;
|
|
43
|
+
|
|
44
|
+
private readonly context: ClientContext;
|
|
45
|
+
private readonly orderBus = new AsyncEventBus<OrderEvent>();
|
|
46
|
+
private readonly orderStatusBus =
|
|
47
|
+
new AsyncEventBus<OrderStatusChangedEvent>();
|
|
48
|
+
private readonly records = new Map<string, OrderRecord>();
|
|
49
|
+
|
|
50
|
+
constructor(context: ClientContext) {
|
|
51
|
+
this.context = context;
|
|
52
|
+
|
|
53
|
+
this.events = {
|
|
54
|
+
status: (filter) =>
|
|
55
|
+
this.orderStatusBus.stream((event) =>
|
|
56
|
+
matchesOrderFilter(
|
|
57
|
+
{ accountId: event.accountId, exchange: event.exchange },
|
|
58
|
+
filter,
|
|
59
|
+
),
|
|
60
|
+
),
|
|
61
|
+
updates: (filter) =>
|
|
62
|
+
this.orderBus.stream((event) =>
|
|
63
|
+
matchesOrderFilter(
|
|
64
|
+
{
|
|
65
|
+
accountId: event.accountId,
|
|
66
|
+
exchange: event.exchange,
|
|
67
|
+
symbol: "symbol" in event ? event.symbol : undefined,
|
|
68
|
+
},
|
|
69
|
+
filter,
|
|
70
|
+
),
|
|
71
|
+
),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// --- OrderManager public API ---
|
|
76
|
+
|
|
77
|
+
async subscribeOrders(input: SubscribeOrdersInput): Promise<void> {
|
|
78
|
+
this.context.assertStarted();
|
|
79
|
+
const account = this.context.getRegisteredAccount(input.accountId);
|
|
80
|
+
this.context.ensurePrivateCredentials(input.accountId);
|
|
81
|
+
|
|
82
|
+
const record = this.getOrCreateRecord(input.accountId, account.exchange);
|
|
83
|
+
record.subscribed = true;
|
|
84
|
+
record.status = {
|
|
85
|
+
...this.createStatus(input.accountId, account.exchange, "active"),
|
|
86
|
+
ready: true,
|
|
87
|
+
runtimeStatus: "healthy",
|
|
88
|
+
lastReceivedAt: this.context.now(),
|
|
89
|
+
lastReadyAt: this.context.now(),
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const event: OrderSnapshotReplacedEvent = {
|
|
93
|
+
type: "order.snapshot_replaced",
|
|
94
|
+
accountId: record.accountId,
|
|
95
|
+
exchange: record.exchange,
|
|
96
|
+
snapshot: [...record.snapshots.values()],
|
|
97
|
+
ts: this.context.now(),
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
this.orderBus.publish(event);
|
|
101
|
+
this.publishStatus(record);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async unsubscribeOrders(input: UnsubscribeOrdersInput): Promise<void> {
|
|
105
|
+
const record = this.records.get(input.accountId);
|
|
106
|
+
if (!record?.subscribed) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
record.subscribed = false;
|
|
111
|
+
record.status = {
|
|
112
|
+
...record.status,
|
|
113
|
+
activity: "inactive",
|
|
114
|
+
runtimeStatus: "stopped",
|
|
115
|
+
inactiveSince: this.context.now(),
|
|
116
|
+
};
|
|
117
|
+
this.publishStatus(record);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
getOrder(input: GetOrderInput): OrderSnapshot | undefined {
|
|
121
|
+
const record = this.records.get(input.accountId);
|
|
122
|
+
if (!record) {
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (!input.orderId && !input.clientOrderId) {
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
for (const snapshot of record.snapshots.values()) {
|
|
131
|
+
if (input.orderId && snapshot.orderId === input.orderId) {
|
|
132
|
+
return snapshot;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (
|
|
136
|
+
input.clientOrderId &&
|
|
137
|
+
snapshot.clientOrderId === input.clientOrderId
|
|
138
|
+
) {
|
|
139
|
+
return snapshot;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
getOpenOrders(accountId: string, symbol?: string): OrderSnapshot[] {
|
|
147
|
+
const record = this.records.get(accountId);
|
|
148
|
+
if (!record) {
|
|
149
|
+
return [];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return [...record.snapshots.values()].filter((snapshot) => {
|
|
153
|
+
if (symbol && snapshot.symbol !== symbol) {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return (
|
|
158
|
+
snapshot.status === "open" || snapshot.status === "partially_filled"
|
|
159
|
+
);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
getOrderStatus(accountId: string): OrderDataStatus | undefined {
|
|
164
|
+
const status = this.records.get(accountId)?.status;
|
|
165
|
+
return status ? cloneOrderStatus(status) : undefined;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// --- ManagerLifecycle ---
|
|
169
|
+
|
|
170
|
+
onClientStarted(): void {
|
|
171
|
+
const now = this.context.now();
|
|
172
|
+
|
|
173
|
+
for (const [accountId, record] of this.records) {
|
|
174
|
+
if (!record.subscribed) {
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const account = this.context.getRegisteredAccount(accountId);
|
|
179
|
+
const creds = account.credentials;
|
|
180
|
+
if (!creds?.apiKey || !creds.secret) {
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
record.status = {
|
|
185
|
+
...this.createStatus(accountId, account.exchange, "active"),
|
|
186
|
+
ready: true,
|
|
187
|
+
runtimeStatus: "healthy",
|
|
188
|
+
lastReceivedAt: now,
|
|
189
|
+
lastReadyAt: now,
|
|
190
|
+
};
|
|
191
|
+
this.publishStatus(record);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
onClientStopping(now: number): void {
|
|
196
|
+
for (const record of this.records.values()) {
|
|
197
|
+
if (!record.subscribed) {
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
record.status = {
|
|
202
|
+
...record.status,
|
|
203
|
+
activity: "inactive",
|
|
204
|
+
runtimeStatus: "stopped",
|
|
205
|
+
inactiveSince: now,
|
|
206
|
+
};
|
|
207
|
+
this.publishStatus(record);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// --- AccountAwareManager ---
|
|
212
|
+
|
|
213
|
+
onAccountRemoved(accountId: string, now: number): void {
|
|
214
|
+
const record = this.records.get(accountId);
|
|
215
|
+
if (!record) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
record.subscribed = false;
|
|
220
|
+
record.status = {
|
|
221
|
+
...record.status,
|
|
222
|
+
activity: "inactive",
|
|
223
|
+
runtimeStatus: "stopped",
|
|
224
|
+
inactiveSince: now,
|
|
225
|
+
};
|
|
226
|
+
this.publishStatus(record);
|
|
227
|
+
this.records.delete(accountId);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
onCredentialsUpdated(accountId: string, exchange: Exchange): void {
|
|
231
|
+
const record = this.records.get(accountId);
|
|
232
|
+
if (!record?.subscribed) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
record.status = this.createStatus(accountId, exchange, "active");
|
|
237
|
+
record.status.ready = true;
|
|
238
|
+
record.status.runtimeStatus = "healthy";
|
|
239
|
+
record.status.lastReadyAt = this.context.now();
|
|
240
|
+
this.publishStatus(record);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// --- HealthReporter ---
|
|
244
|
+
|
|
245
|
+
getStatuses(): OrderDataStatus[] {
|
|
246
|
+
return [...this.records.values()]
|
|
247
|
+
.map((record) => cloneOrderStatus(record.status))
|
|
248
|
+
.sort((left, right) =>
|
|
249
|
+
`${left.exchange}:${left.accountId}`.localeCompare(
|
|
250
|
+
`${right.exchange}:${right.accountId}`,
|
|
251
|
+
),
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// --- Internal helpers ---
|
|
256
|
+
|
|
257
|
+
private getOrCreateRecord(
|
|
258
|
+
accountId: string,
|
|
259
|
+
exchange: Exchange,
|
|
260
|
+
): OrderRecord {
|
|
261
|
+
const existing = this.records.get(accountId);
|
|
262
|
+
if (existing) {
|
|
263
|
+
return existing;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const record: OrderRecord = {
|
|
267
|
+
accountId,
|
|
268
|
+
exchange,
|
|
269
|
+
subscribed: false,
|
|
270
|
+
snapshots: new Map(),
|
|
271
|
+
status: this.createStatus(accountId, exchange, "inactive"),
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
this.records.set(accountId, record);
|
|
275
|
+
return record;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
private createStatus(
|
|
279
|
+
accountId: string,
|
|
280
|
+
exchange: Exchange,
|
|
281
|
+
activity: "active" | "inactive",
|
|
282
|
+
): OrderDataStatus {
|
|
283
|
+
return {
|
|
284
|
+
accountId,
|
|
285
|
+
exchange,
|
|
286
|
+
activity,
|
|
287
|
+
ready: false,
|
|
288
|
+
runtimeStatus: activity === "active" ? "bootstrap_pending" : "stopped",
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
private publishStatus(record: OrderRecord): void {
|
|
293
|
+
const event: OrderStatusChangedEvent = {
|
|
294
|
+
type: "order.status_changed",
|
|
295
|
+
accountId: record.accountId,
|
|
296
|
+
exchange: record.exchange,
|
|
297
|
+
status: cloneOrderStatus(record.status),
|
|
298
|
+
ts: this.context.now(),
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
this.orderStatusBus.publish(event);
|
|
302
|
+
this.context.publishHealthEvent(event);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import type BigNumber from "bignumber.js";
|
|
2
|
+
import type {
|
|
3
|
+
Exchange,
|
|
4
|
+
PrivateRuntimeStatus,
|
|
5
|
+
SubscriptionActivity,
|
|
6
|
+
} from "./shared.ts";
|
|
7
|
+
|
|
8
|
+
export interface AccountDataStatus {
|
|
9
|
+
accountId: string;
|
|
10
|
+
exchange: Exchange;
|
|
11
|
+
activity: SubscriptionActivity;
|
|
12
|
+
ready: boolean;
|
|
13
|
+
runtimeStatus?: PrivateRuntimeStatus;
|
|
14
|
+
lastReceivedAt?: number;
|
|
15
|
+
lastReadyAt?: number;
|
|
16
|
+
inactiveSince?: number;
|
|
17
|
+
reason?:
|
|
18
|
+
| "credentials_missing"
|
|
19
|
+
| "auth_failed"
|
|
20
|
+
| "ws_disconnected"
|
|
21
|
+
| "heartbeat_timeout"
|
|
22
|
+
| "reconciling";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface AccountStatusChangedEvent {
|
|
26
|
+
type: "account.status_changed";
|
|
27
|
+
accountId: string;
|
|
28
|
+
exchange: Exchange;
|
|
29
|
+
status: AccountDataStatus;
|
|
30
|
+
ts: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type PositionSide = "long" | "short" | "net";
|
|
34
|
+
|
|
35
|
+
export interface SubscribeAccountInput {
|
|
36
|
+
accountId: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface UnsubscribeAccountInput {
|
|
40
|
+
accountId: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface PositionKeyInput {
|
|
44
|
+
accountId: string;
|
|
45
|
+
symbol: string;
|
|
46
|
+
side?: PositionSide;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface AccountEventFilter {
|
|
50
|
+
accountId?: string;
|
|
51
|
+
exchange?: Exchange;
|
|
52
|
+
symbol?: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface BalanceSnapshot {
|
|
56
|
+
accountId: string;
|
|
57
|
+
exchange: Exchange;
|
|
58
|
+
asset: string;
|
|
59
|
+
free: BigNumber;
|
|
60
|
+
used: BigNumber;
|
|
61
|
+
total: BigNumber;
|
|
62
|
+
exchangeTs?: number;
|
|
63
|
+
receivedAt: number;
|
|
64
|
+
updatedAt: number;
|
|
65
|
+
seq: number;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface PositionSnapshot {
|
|
69
|
+
accountId: string;
|
|
70
|
+
exchange: Exchange;
|
|
71
|
+
symbol: string;
|
|
72
|
+
side: PositionSide;
|
|
73
|
+
size: BigNumber;
|
|
74
|
+
entryPrice?: BigNumber;
|
|
75
|
+
markPrice?: BigNumber;
|
|
76
|
+
unrealizedPnl?: BigNumber;
|
|
77
|
+
leverage?: BigNumber;
|
|
78
|
+
liquidationPrice?: BigNumber;
|
|
79
|
+
exchangeTs?: number;
|
|
80
|
+
receivedAt: number;
|
|
81
|
+
updatedAt: number;
|
|
82
|
+
seq: number;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface RiskSnapshot {
|
|
86
|
+
accountId: string;
|
|
87
|
+
exchange: Exchange;
|
|
88
|
+
equity?: BigNumber;
|
|
89
|
+
marginRatio?: BigNumber;
|
|
90
|
+
initialMargin?: BigNumber;
|
|
91
|
+
maintenanceMargin?: BigNumber;
|
|
92
|
+
exchangeTs?: number;
|
|
93
|
+
receivedAt: number;
|
|
94
|
+
updatedAt: number;
|
|
95
|
+
seq: number;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface AccountSnapshot {
|
|
99
|
+
accountId: string;
|
|
100
|
+
exchange: Exchange;
|
|
101
|
+
balances: Record<string, BalanceSnapshot>;
|
|
102
|
+
positions: PositionSnapshot[];
|
|
103
|
+
risk?: RiskSnapshot;
|
|
104
|
+
exchangeTs?: number;
|
|
105
|
+
receivedAt: number;
|
|
106
|
+
updatedAt: number;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export interface AccountEventBase {
|
|
110
|
+
accountId: string;
|
|
111
|
+
exchange: Exchange;
|
|
112
|
+
ts: number;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export interface BalanceUpdatedEvent extends AccountEventBase {
|
|
116
|
+
type: "balance.updated";
|
|
117
|
+
asset: string;
|
|
118
|
+
snapshot: BalanceSnapshot;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export interface PositionUpdatedEvent extends AccountEventBase {
|
|
122
|
+
type: "position.updated";
|
|
123
|
+
symbol: string;
|
|
124
|
+
snapshot: PositionSnapshot;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface RiskUpdatedEvent extends AccountEventBase {
|
|
128
|
+
type: "risk.updated";
|
|
129
|
+
snapshot: RiskSnapshot;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export interface AccountSnapshotReplacedEvent extends AccountEventBase {
|
|
133
|
+
type: "account.snapshot_replaced";
|
|
134
|
+
snapshot: AccountSnapshot;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export type AccountEvent =
|
|
138
|
+
| BalanceUpdatedEvent
|
|
139
|
+
| PositionUpdatedEvent
|
|
140
|
+
| RiskUpdatedEvent
|
|
141
|
+
| AccountSnapshotReplacedEvent;
|
|
142
|
+
|
|
143
|
+
export interface AccountEventStreams {
|
|
144
|
+
updates(filter?: AccountEventFilter): AsyncIterable<AccountEvent>;
|
|
145
|
+
status(filter?: AccountEventFilter): AsyncIterable<AccountStatusChangedEvent>;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export interface AccountManager {
|
|
149
|
+
readonly events: AccountEventStreams;
|
|
150
|
+
|
|
151
|
+
subscribeAccount(input: SubscribeAccountInput): Promise<void>;
|
|
152
|
+
unsubscribeAccount(input: UnsubscribeAccountInput): Promise<void>;
|
|
153
|
+
|
|
154
|
+
getAccountSnapshot(accountId: string): AccountSnapshot | undefined;
|
|
155
|
+
getBalances(accountId: string): BalanceSnapshot[];
|
|
156
|
+
getBalance(accountId: string, asset: string): BalanceSnapshot | undefined;
|
|
157
|
+
getPositions(accountId: string, symbol?: string): PositionSnapshot[];
|
|
158
|
+
getPosition(input: PositionKeyInput): PositionSnapshot | undefined;
|
|
159
|
+
getRiskSnapshot(accountId: string): RiskSnapshot | undefined;
|
|
160
|
+
getAccountStatus(accountId: string): AccountDataStatus | undefined;
|
|
161
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AccountDataStatus,
|
|
3
|
+
AccountManager,
|
|
4
|
+
AccountStatusChangedEvent,
|
|
5
|
+
} from "./account.ts";
|
|
6
|
+
import type {
|
|
7
|
+
MarketDataStatus,
|
|
8
|
+
MarketManager,
|
|
9
|
+
MarketStatusChangedEvent,
|
|
10
|
+
} from "./market.ts";
|
|
11
|
+
import type {
|
|
12
|
+
OrderDataStatus,
|
|
13
|
+
OrderManager,
|
|
14
|
+
OrderStatusChangedEvent,
|
|
15
|
+
} from "./order.ts";
|
|
16
|
+
import type {
|
|
17
|
+
AccountCredentials,
|
|
18
|
+
AcexInternalError,
|
|
19
|
+
ClientStatus,
|
|
20
|
+
CreateClientOptions,
|
|
21
|
+
Exchange,
|
|
22
|
+
RegisterAccountInput,
|
|
23
|
+
RegisterAccountResult,
|
|
24
|
+
StopOptions,
|
|
25
|
+
} from "./shared.ts";
|
|
26
|
+
|
|
27
|
+
export interface ClientHealthSnapshot {
|
|
28
|
+
clientStatus: ClientStatus;
|
|
29
|
+
markets: MarketDataStatus[];
|
|
30
|
+
accounts: AccountDataStatus[];
|
|
31
|
+
orders: OrderDataStatus[];
|
|
32
|
+
updatedAt: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface ClientStatusChangedEvent {
|
|
36
|
+
type: "client.status_changed";
|
|
37
|
+
status: ClientStatus;
|
|
38
|
+
ts: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type HealthEvent =
|
|
42
|
+
| ClientStatusChangedEvent
|
|
43
|
+
| MarketStatusChangedEvent
|
|
44
|
+
| AccountStatusChangedEvent
|
|
45
|
+
| OrderStatusChangedEvent;
|
|
46
|
+
|
|
47
|
+
export interface HealthEventFilter {
|
|
48
|
+
scope?: "client" | "market" | "account" | "order";
|
|
49
|
+
exchange?: Exchange;
|
|
50
|
+
accountId?: string;
|
|
51
|
+
symbol?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface ClientEventStreams {
|
|
55
|
+
health(filter?: HealthEventFilter): AsyncIterable<HealthEvent>;
|
|
56
|
+
errors(): AsyncIterable<AcexInternalError>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface AcexClient {
|
|
60
|
+
readonly market: MarketManager;
|
|
61
|
+
readonly account: AccountManager;
|
|
62
|
+
readonly order: OrderManager;
|
|
63
|
+
readonly events: ClientEventStreams;
|
|
64
|
+
|
|
65
|
+
getStatus(): ClientStatus;
|
|
66
|
+
getHealth(): ClientHealthSnapshot;
|
|
67
|
+
|
|
68
|
+
registerAccount(input: RegisterAccountInput): Promise<RegisterAccountResult>;
|
|
69
|
+
updateAccountCredentials(
|
|
70
|
+
accountId: string,
|
|
71
|
+
credentials: AccountCredentials,
|
|
72
|
+
): Promise<void>;
|
|
73
|
+
removeAccount(accountId: string): Promise<void>;
|
|
74
|
+
|
|
75
|
+
start(): Promise<void>;
|
|
76
|
+
stop(options?: StopOptions): Promise<void>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export type CreateClient = (options?: CreateClientOptions) => AcexClient;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type BigNumber from "bignumber.js";
|
|
2
|
+
import type {
|
|
3
|
+
Exchange,
|
|
4
|
+
MarketFreshness,
|
|
5
|
+
SubscriptionActivity,
|
|
6
|
+
} from "./shared.ts";
|
|
7
|
+
|
|
8
|
+
export type MarketType = "spot" | "swap" | "future";
|
|
9
|
+
|
|
10
|
+
export interface MarketDefinition {
|
|
11
|
+
exchange: Exchange;
|
|
12
|
+
symbol: string;
|
|
13
|
+
id: string;
|
|
14
|
+
type: MarketType;
|
|
15
|
+
base: string;
|
|
16
|
+
quote: string;
|
|
17
|
+
settle?: string;
|
|
18
|
+
active: boolean;
|
|
19
|
+
contract: boolean;
|
|
20
|
+
linear?: boolean;
|
|
21
|
+
inverse?: boolean;
|
|
22
|
+
contractSize?: BigNumber;
|
|
23
|
+
pricePrecision: number;
|
|
24
|
+
amountPrecision: number;
|
|
25
|
+
priceStep: BigNumber;
|
|
26
|
+
amountStep: BigNumber;
|
|
27
|
+
minAmount?: BigNumber;
|
|
28
|
+
minNotional?: BigNumber;
|
|
29
|
+
expiry?: number;
|
|
30
|
+
raw: Record<string, unknown>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface MarketDataStatus {
|
|
34
|
+
exchange: Exchange;
|
|
35
|
+
symbol: string;
|
|
36
|
+
activity: SubscriptionActivity;
|
|
37
|
+
ready: boolean;
|
|
38
|
+
freshness?: MarketFreshness;
|
|
39
|
+
lastReceivedAt?: number;
|
|
40
|
+
lastReadyAt?: number;
|
|
41
|
+
inactiveSince?: number;
|
|
42
|
+
reason?: "ws_disconnected" | "heartbeat_timeout" | "reconciling";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface MarketKeyInput {
|
|
46
|
+
exchange: Exchange;
|
|
47
|
+
symbol: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface SubscribeL1BookInput extends MarketKeyInput {}
|
|
51
|
+
|
|
52
|
+
export interface SubscribeFundingRateInput extends MarketKeyInput {}
|
|
53
|
+
|
|
54
|
+
export interface MarketEventFilter {
|
|
55
|
+
exchange?: Exchange;
|
|
56
|
+
symbol?: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface L1Book {
|
|
60
|
+
exchange: Exchange;
|
|
61
|
+
symbol: string;
|
|
62
|
+
bidPrice: BigNumber;
|
|
63
|
+
bidSize: BigNumber;
|
|
64
|
+
askPrice: BigNumber;
|
|
65
|
+
askSize: BigNumber;
|
|
66
|
+
exchangeTs?: number;
|
|
67
|
+
receivedAt: number;
|
|
68
|
+
updatedAt: number;
|
|
69
|
+
version: number;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface FundingRateSnapshot {
|
|
73
|
+
exchange: Exchange;
|
|
74
|
+
symbol: string;
|
|
75
|
+
fundingRate: BigNumber;
|
|
76
|
+
nextFundingTime?: number;
|
|
77
|
+
markPrice?: BigNumber;
|
|
78
|
+
indexPrice?: BigNumber;
|
|
79
|
+
exchangeTs?: number;
|
|
80
|
+
receivedAt: number;
|
|
81
|
+
updatedAt: number;
|
|
82
|
+
version: number;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface MarketStatusChangedEvent {
|
|
86
|
+
type: "market.status_changed";
|
|
87
|
+
exchange: Exchange;
|
|
88
|
+
symbol: string;
|
|
89
|
+
status: MarketDataStatus;
|
|
90
|
+
ts: number;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface L1BookUpdatedEvent {
|
|
94
|
+
type: "l1_book.updated";
|
|
95
|
+
exchange: Exchange;
|
|
96
|
+
symbol: string;
|
|
97
|
+
snapshot: L1Book;
|
|
98
|
+
ts: number;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface FundingRateUpdatedEvent {
|
|
102
|
+
type: "funding_rate.updated";
|
|
103
|
+
exchange: Exchange;
|
|
104
|
+
symbol: string;
|
|
105
|
+
snapshot: FundingRateSnapshot;
|
|
106
|
+
ts: number;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export type MarketEvent =
|
|
110
|
+
| L1BookUpdatedEvent
|
|
111
|
+
| FundingRateUpdatedEvent
|
|
112
|
+
| MarketStatusChangedEvent;
|
|
113
|
+
|
|
114
|
+
export interface MarketEventStreams {
|
|
115
|
+
l1BookUpdates(filter?: MarketEventFilter): AsyncIterable<L1BookUpdatedEvent>;
|
|
116
|
+
fundingRateUpdates(
|
|
117
|
+
filter?: MarketEventFilter,
|
|
118
|
+
): AsyncIterable<FundingRateUpdatedEvent>;
|
|
119
|
+
status(filter?: MarketEventFilter): AsyncIterable<MarketStatusChangedEvent>;
|
|
120
|
+
all(filter?: MarketEventFilter): AsyncIterable<MarketEvent>;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface MarketManager {
|
|
124
|
+
readonly events: MarketEventStreams;
|
|
125
|
+
|
|
126
|
+
loadMarkets(): Promise<void>;
|
|
127
|
+
subscribeL1Book(input: SubscribeL1BookInput): Promise<void>;
|
|
128
|
+
unsubscribeL1Book(input: SubscribeL1BookInput): Promise<void>;
|
|
129
|
+
subscribeFundingRate(input: SubscribeFundingRateInput): Promise<void>;
|
|
130
|
+
unsubscribeFundingRate(input: SubscribeFundingRateInput): Promise<void>;
|
|
131
|
+
|
|
132
|
+
getMarket(exchange: Exchange, symbol: string): MarketDefinition | undefined;
|
|
133
|
+
findMarkets(symbol: string): MarketDefinition[];
|
|
134
|
+
listMarkets(exchange?: Exchange): MarketDefinition[];
|
|
135
|
+
getL1Book(key: MarketKeyInput): L1Book | undefined;
|
|
136
|
+
getFundingRate(key: MarketKeyInput): FundingRateSnapshot | undefined;
|
|
137
|
+
getMarketStatus(key: MarketKeyInput): MarketDataStatus | undefined;
|
|
138
|
+
}
|