@imbingox/acex 0.1.0-beta.0
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 +65 -0
- package/dist/adapters/ccxt/aster-ccxt-adapter.d.ts +157 -0
- package/dist/adapters/ccxt/aster-ccxt-adapter.js +272 -0
- package/dist/adapters/ccxt/binance-usdm-ccxt-adapter.d.ts +179 -0
- package/dist/adapters/ccxt/binance-usdm-ccxt-adapter.js +537 -0
- package/dist/adapters/fake/fake-aster-adapter.d.ts +130 -0
- package/dist/adapters/fake/fake-aster-adapter.js +283 -0
- package/dist/adapters/types.d.ts +210 -0
- package/dist/adapters/types.js +1 -0
- package/dist/core/client.d.ts +37 -0
- package/dist/core/client.js +45 -0
- package/dist/core/recovery.d.ts +22 -0
- package/dist/core/recovery.js +18 -0
- package/dist/core/runtime.d.ts +26 -0
- package/dist/core/runtime.js +150 -0
- package/dist/errors/acex-error.d.ts +25 -0
- package/dist/errors/acex-error.js +54 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +3 -0
- package/dist/managers/account-manager.d.ts +41 -0
- package/dist/managers/account-manager.js +80 -0
- package/dist/managers/market-manager.d.ts +16 -0
- package/dist/managers/market-manager.js +28 -0
- package/dist/managers/order-manager.d.ts +87 -0
- package/dist/managers/order-manager.js +122 -0
- package/dist/runtime/async-queue.d.ts +8 -0
- package/dist/runtime/async-queue.js +88 -0
- package/dist/runtime/request-id.d.ts +1 -0
- package/dist/runtime/request-id.js +5 -0
- package/dist/store/account-store.d.ts +52 -0
- package/dist/store/account-store.js +18 -0
- package/dist/store/health-store.d.ts +16 -0
- package/dist/store/health-store.js +29 -0
- package/dist/store/market-store.d.ts +42 -0
- package/dist/store/market-store.js +51 -0
- package/dist/store/order-store.d.ts +38 -0
- package/dist/store/order-store.js +49 -0
- package/dist/testing/create-fake-runtime.d.ts +5 -0
- package/dist/testing/create-fake-runtime.js +7 -0
- package/dist/types/public.d.ts +11 -0
- package/dist/types/public.js +1 -0
- package/package.json +34 -0
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { createAsyncQueue } from "../../runtime/async-queue.js";
|
|
2
|
+
export class FakeAsterAdapter {
|
|
3
|
+
exchange = "aster";
|
|
4
|
+
capabilities = {
|
|
5
|
+
publicWs: true,
|
|
6
|
+
privateWs: true,
|
|
7
|
+
l1BookStream: true,
|
|
8
|
+
fundingRateStream: true,
|
|
9
|
+
accountStream: true,
|
|
10
|
+
orderStream: true,
|
|
11
|
+
fetchMarketInfo: true,
|
|
12
|
+
fetchBalances: true,
|
|
13
|
+
fetchPositions: true,
|
|
14
|
+
fetchRisk: true,
|
|
15
|
+
fetchOpenOrders: true,
|
|
16
|
+
fetchMyTrades: true,
|
|
17
|
+
fetchOrderById: true,
|
|
18
|
+
};
|
|
19
|
+
#started = false;
|
|
20
|
+
#marketQueue = createAsyncQueue({ maxBufferSize: 100 });
|
|
21
|
+
#marketEventSink;
|
|
22
|
+
#internalErrorQueue = createAsyncQueue({ maxBufferSize: 10 });
|
|
23
|
+
#orderQueue = createAsyncQueue({ maxBufferSize: 100 });
|
|
24
|
+
#orderEventSink;
|
|
25
|
+
#orders = new Map();
|
|
26
|
+
#accountQueue = createAsyncQueue({ maxBufferSize: 100 });
|
|
27
|
+
#accountEventSink;
|
|
28
|
+
#accountBaselines = new Map();
|
|
29
|
+
async start() {
|
|
30
|
+
this.#started = true;
|
|
31
|
+
}
|
|
32
|
+
async stop() {
|
|
33
|
+
this.#started = false;
|
|
34
|
+
this.#marketQueue.close();
|
|
35
|
+
this.#internalErrorQueue.close();
|
|
36
|
+
this.#orderQueue.close();
|
|
37
|
+
this.#accountQueue.close();
|
|
38
|
+
}
|
|
39
|
+
async subscribeL1Book() { }
|
|
40
|
+
async subscribeFundingRate() { }
|
|
41
|
+
watchMarketEvents() {
|
|
42
|
+
return this.#marketQueue;
|
|
43
|
+
}
|
|
44
|
+
setMarketEventSink(sink) {
|
|
45
|
+
this.#marketEventSink = sink;
|
|
46
|
+
}
|
|
47
|
+
emitL1Book(event) {
|
|
48
|
+
this.#emitMarketEvent(event);
|
|
49
|
+
}
|
|
50
|
+
emitFundingRate(event) {
|
|
51
|
+
this.#emitMarketEvent(event);
|
|
52
|
+
}
|
|
53
|
+
emitMarketDisconnect(input) {
|
|
54
|
+
this.#emitMarketEvent({
|
|
55
|
+
type: "market.disconnected",
|
|
56
|
+
exchange: input.exchange,
|
|
57
|
+
symbol: input.symbol,
|
|
58
|
+
receivedAt: Date.now(),
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
emitMarketReconnect(input) {
|
|
62
|
+
this.#emitMarketEvent({
|
|
63
|
+
type: "market.reconnecting",
|
|
64
|
+
exchange: input.exchange,
|
|
65
|
+
symbol: input.symbol,
|
|
66
|
+
receivedAt: Date.now(),
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
watchInternalErrors() {
|
|
70
|
+
return this.#internalErrorQueue;
|
|
71
|
+
}
|
|
72
|
+
emitInternalError(error) {
|
|
73
|
+
this.#internalErrorQueue.push(error);
|
|
74
|
+
}
|
|
75
|
+
async subscribeOrders() { }
|
|
76
|
+
setAccountBaseline(accountId, baseline) {
|
|
77
|
+
this.#accountBaselines.set(accountId, baseline);
|
|
78
|
+
}
|
|
79
|
+
async fetchAccountBaseline(accountId) {
|
|
80
|
+
return (this.#accountBaselines.get(accountId) ?? {
|
|
81
|
+
balances: {},
|
|
82
|
+
positions: [],
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
watchAccountEvents() {
|
|
86
|
+
return this.#accountQueue;
|
|
87
|
+
}
|
|
88
|
+
setAccountEventSink(sink) {
|
|
89
|
+
this.#accountEventSink = sink;
|
|
90
|
+
}
|
|
91
|
+
emitBalanceUpdate(event) {
|
|
92
|
+
const nextEvent = {
|
|
93
|
+
type: "balance.updated",
|
|
94
|
+
...event,
|
|
95
|
+
};
|
|
96
|
+
const baseline = this.#accountBaselines.get(event.accountId);
|
|
97
|
+
this.#accountBaselines.set(event.accountId, {
|
|
98
|
+
balances: {
|
|
99
|
+
...(baseline?.balances ?? {}),
|
|
100
|
+
[event.asset]: event.snapshot,
|
|
101
|
+
},
|
|
102
|
+
positions: baseline?.positions ?? [],
|
|
103
|
+
...(baseline?.risk === undefined ? {} : { risk: baseline.risk }),
|
|
104
|
+
});
|
|
105
|
+
if (this.#accountEventSink !== undefined) {
|
|
106
|
+
this.#accountEventSink(nextEvent);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
this.#accountQueue.push(nextEvent);
|
|
110
|
+
}
|
|
111
|
+
async fetchOpenOrdersBaseline(accountId) {
|
|
112
|
+
return [...this.#orders.values()].filter((order) => {
|
|
113
|
+
return order.accountId === accountId && order.status === "open";
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
watchOrderEvents() {
|
|
117
|
+
return this.#orderQueue;
|
|
118
|
+
}
|
|
119
|
+
setOrderEventSink(sink) {
|
|
120
|
+
this.#orderEventSink = sink;
|
|
121
|
+
}
|
|
122
|
+
async placeOrder(input) {
|
|
123
|
+
const receivedAt = Date.now();
|
|
124
|
+
const snapshot = {
|
|
125
|
+
...input,
|
|
126
|
+
status: "open",
|
|
127
|
+
filled: "0",
|
|
128
|
+
seq: 1,
|
|
129
|
+
receivedAt,
|
|
130
|
+
updatedAt: receivedAt,
|
|
131
|
+
};
|
|
132
|
+
this.#orders.set(this.#orderKey(input.accountId, input.clientOrderId), snapshot);
|
|
133
|
+
this.#emitOrderEvent({
|
|
134
|
+
type: "order.updated",
|
|
135
|
+
accountId: input.accountId,
|
|
136
|
+
exchange: input.exchange,
|
|
137
|
+
receivedAt,
|
|
138
|
+
snapshot,
|
|
139
|
+
});
|
|
140
|
+
return {
|
|
141
|
+
...(snapshot.clientOrderId === undefined ? {} : { clientOrderId: snapshot.clientOrderId }),
|
|
142
|
+
...(snapshot.orderId === undefined ? {} : { orderId: snapshot.orderId }),
|
|
143
|
+
receivedAt,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
async amendOrder(input) {
|
|
147
|
+
const orderKey = this.#resolveOrderKey(input.accountId, input);
|
|
148
|
+
if (orderKey === undefined) {
|
|
149
|
+
return {
|
|
150
|
+
...(input.clientOrderId === undefined ? {} : { clientOrderId: input.clientOrderId }),
|
|
151
|
+
...(input.orderId === undefined ? {} : { orderId: input.orderId }),
|
|
152
|
+
receivedAt: Date.now(),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
const current = this.#orders.get(orderKey);
|
|
156
|
+
if (current === undefined) {
|
|
157
|
+
return {
|
|
158
|
+
...(input.clientOrderId === undefined ? {} : { clientOrderId: input.clientOrderId }),
|
|
159
|
+
...(input.orderId === undefined ? {} : { orderId: input.orderId }),
|
|
160
|
+
receivedAt: Date.now(),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
const amended = {
|
|
164
|
+
...current,
|
|
165
|
+
updatedAt: Date.now(),
|
|
166
|
+
...(input.newPrice === undefined ? {} : { price: input.newPrice }),
|
|
167
|
+
};
|
|
168
|
+
this.#orders.set(orderKey, amended);
|
|
169
|
+
this.#emitOrderEvent({
|
|
170
|
+
type: "order.updated",
|
|
171
|
+
accountId: input.accountId,
|
|
172
|
+
exchange: input.exchange,
|
|
173
|
+
receivedAt: amended.updatedAt,
|
|
174
|
+
snapshot: amended,
|
|
175
|
+
});
|
|
176
|
+
return {
|
|
177
|
+
...(amended.clientOrderId === undefined ? {} : { clientOrderId: amended.clientOrderId }),
|
|
178
|
+
...(amended.orderId === undefined ? {} : { orderId: amended.orderId }),
|
|
179
|
+
receivedAt: amended.updatedAt,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
async cancelOrder(input) {
|
|
183
|
+
const orderKey = this.#resolveOrderKey(input.accountId, input);
|
|
184
|
+
if (orderKey === undefined) {
|
|
185
|
+
return {
|
|
186
|
+
...(input.clientOrderId === undefined ? {} : { clientOrderId: input.clientOrderId }),
|
|
187
|
+
...(input.orderId === undefined ? {} : { orderId: input.orderId }),
|
|
188
|
+
receivedAt: Date.now(),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
const current = this.#orders.get(orderKey);
|
|
192
|
+
if (current === undefined) {
|
|
193
|
+
return {
|
|
194
|
+
...(input.clientOrderId === undefined ? {} : { clientOrderId: input.clientOrderId }),
|
|
195
|
+
...(input.orderId === undefined ? {} : { orderId: input.orderId }),
|
|
196
|
+
receivedAt: Date.now(),
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
const canceled = {
|
|
200
|
+
...current,
|
|
201
|
+
status: "canceled",
|
|
202
|
+
updatedAt: Date.now(),
|
|
203
|
+
};
|
|
204
|
+
this.#orders.set(orderKey, canceled);
|
|
205
|
+
this.#emitOrderEvent({
|
|
206
|
+
type: "order.canceled",
|
|
207
|
+
accountId: input.accountId,
|
|
208
|
+
exchange: input.exchange,
|
|
209
|
+
receivedAt: canceled.updatedAt,
|
|
210
|
+
snapshot: canceled,
|
|
211
|
+
});
|
|
212
|
+
return {
|
|
213
|
+
...(canceled.clientOrderId === undefined ? {} : { clientOrderId: canceled.clientOrderId }),
|
|
214
|
+
...(canceled.orderId === undefined ? {} : { orderId: canceled.orderId }),
|
|
215
|
+
receivedAt: canceled.updatedAt,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
async cancelAllOrders(input) {
|
|
219
|
+
let canceledCount = 0;
|
|
220
|
+
for (const [key, snapshot] of this.#orders.entries()) {
|
|
221
|
+
if (!key.startsWith(`${input.accountId}:`) || snapshot.status === "canceled") {
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
canceledCount += 1;
|
|
225
|
+
const canceled = {
|
|
226
|
+
...snapshot,
|
|
227
|
+
status: "canceled",
|
|
228
|
+
updatedAt: Date.now(),
|
|
229
|
+
};
|
|
230
|
+
this.#orders.set(key, canceled);
|
|
231
|
+
this.#emitOrderEvent({
|
|
232
|
+
type: "order.canceled",
|
|
233
|
+
accountId: input.accountId,
|
|
234
|
+
exchange: input.exchange,
|
|
235
|
+
receivedAt: canceled.updatedAt,
|
|
236
|
+
snapshot: canceled,
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
return {
|
|
240
|
+
accountId: input.accountId,
|
|
241
|
+
exchange: input.exchange,
|
|
242
|
+
canceledCount,
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
getHealth() {
|
|
246
|
+
return {
|
|
247
|
+
exchange: this.exchange,
|
|
248
|
+
status: this.#started ? "healthy" : "idle",
|
|
249
|
+
wsConnected: this.#started,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
#emitMarketEvent(event) {
|
|
253
|
+
if (this.#marketEventSink !== undefined) {
|
|
254
|
+
this.#marketEventSink(event);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
this.#marketQueue.push(event);
|
|
258
|
+
}
|
|
259
|
+
#emitOrderEvent(event) {
|
|
260
|
+
if (this.#orderEventSink !== undefined) {
|
|
261
|
+
this.#orderEventSink(event);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
this.#orderQueue.push(event);
|
|
265
|
+
}
|
|
266
|
+
#orderKey(accountId, clientOrderId) {
|
|
267
|
+
return `${accountId}:${clientOrderId}`;
|
|
268
|
+
}
|
|
269
|
+
#resolveOrderKey(accountId, locator) {
|
|
270
|
+
if (locator.clientOrderId !== undefined) {
|
|
271
|
+
return this.#orderKey(accountId, locator.clientOrderId);
|
|
272
|
+
}
|
|
273
|
+
if (locator.orderId === undefined) {
|
|
274
|
+
return undefined;
|
|
275
|
+
}
|
|
276
|
+
for (const [key, order] of this.#orders.entries()) {
|
|
277
|
+
if (key.startsWith(`${accountId}:`) && order.orderId === locator.orderId) {
|
|
278
|
+
return key;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return undefined;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
export interface ExchangeCapabilities {
|
|
2
|
+
publicWs: boolean;
|
|
3
|
+
privateWs: boolean;
|
|
4
|
+
l1BookStream: boolean;
|
|
5
|
+
fundingRateStream: boolean;
|
|
6
|
+
accountStream: boolean;
|
|
7
|
+
orderStream: boolean;
|
|
8
|
+
fetchMarketInfo: boolean;
|
|
9
|
+
fetchBalances: boolean;
|
|
10
|
+
fetchPositions: boolean;
|
|
11
|
+
fetchRisk: boolean;
|
|
12
|
+
fetchOpenOrders: boolean;
|
|
13
|
+
fetchMyTrades: boolean;
|
|
14
|
+
fetchOrderById: boolean;
|
|
15
|
+
}
|
|
16
|
+
export type NormalizedMarketEvent = {
|
|
17
|
+
type: "l1_book.updated";
|
|
18
|
+
exchange: string;
|
|
19
|
+
symbol: string;
|
|
20
|
+
bidPrice: string;
|
|
21
|
+
bidSize: string;
|
|
22
|
+
askPrice: string;
|
|
23
|
+
askSize: string;
|
|
24
|
+
exchangeTs?: number;
|
|
25
|
+
receivedAt: number;
|
|
26
|
+
} | {
|
|
27
|
+
type: "market.disconnected";
|
|
28
|
+
exchange: string;
|
|
29
|
+
symbol: string;
|
|
30
|
+
receivedAt: number;
|
|
31
|
+
} | {
|
|
32
|
+
type: "market.reconnecting";
|
|
33
|
+
exchange: string;
|
|
34
|
+
symbol: string;
|
|
35
|
+
receivedAt: number;
|
|
36
|
+
} | {
|
|
37
|
+
type: "funding_rate.updated";
|
|
38
|
+
exchange: string;
|
|
39
|
+
symbol: string;
|
|
40
|
+
fundingRate: string;
|
|
41
|
+
exchangeTs?: number;
|
|
42
|
+
receivedAt: number;
|
|
43
|
+
};
|
|
44
|
+
export type NormalizedOrderEvent = {
|
|
45
|
+
type: "order.updated";
|
|
46
|
+
accountId: string;
|
|
47
|
+
exchange: string;
|
|
48
|
+
receivedAt: number;
|
|
49
|
+
snapshot: {
|
|
50
|
+
accountId: string;
|
|
51
|
+
exchange: string;
|
|
52
|
+
symbol: string;
|
|
53
|
+
side: "buy" | "sell";
|
|
54
|
+
type: string;
|
|
55
|
+
status: string;
|
|
56
|
+
amount: string;
|
|
57
|
+
filled: string;
|
|
58
|
+
price?: string;
|
|
59
|
+
seq: number;
|
|
60
|
+
receivedAt: number;
|
|
61
|
+
updatedAt: number;
|
|
62
|
+
orderId?: string;
|
|
63
|
+
clientOrderId?: string;
|
|
64
|
+
};
|
|
65
|
+
} | {
|
|
66
|
+
type: "order.canceled";
|
|
67
|
+
accountId: string;
|
|
68
|
+
exchange: string;
|
|
69
|
+
receivedAt: number;
|
|
70
|
+
snapshot: {
|
|
71
|
+
accountId: string;
|
|
72
|
+
exchange: string;
|
|
73
|
+
symbol: string;
|
|
74
|
+
side: "buy" | "sell";
|
|
75
|
+
type: string;
|
|
76
|
+
status: string;
|
|
77
|
+
amount: string;
|
|
78
|
+
filled: string;
|
|
79
|
+
price?: string;
|
|
80
|
+
seq: number;
|
|
81
|
+
receivedAt: number;
|
|
82
|
+
updatedAt: number;
|
|
83
|
+
orderId?: string;
|
|
84
|
+
clientOrderId?: string;
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
export interface AdapterAccountBaseline {
|
|
88
|
+
balances: Record<string, {
|
|
89
|
+
accountId: string;
|
|
90
|
+
exchange: string;
|
|
91
|
+
asset: string;
|
|
92
|
+
free: string;
|
|
93
|
+
used: string;
|
|
94
|
+
total: string;
|
|
95
|
+
seq: number;
|
|
96
|
+
receivedAt: number;
|
|
97
|
+
updatedAt: number;
|
|
98
|
+
}>;
|
|
99
|
+
positions: Array<{
|
|
100
|
+
accountId: string;
|
|
101
|
+
exchange: string;
|
|
102
|
+
symbol: string;
|
|
103
|
+
side: string;
|
|
104
|
+
size: string;
|
|
105
|
+
seq: number;
|
|
106
|
+
receivedAt: number;
|
|
107
|
+
updatedAt: number;
|
|
108
|
+
}>;
|
|
109
|
+
risk?: {
|
|
110
|
+
accountId: string;
|
|
111
|
+
exchange: string;
|
|
112
|
+
seq: number;
|
|
113
|
+
receivedAt: number;
|
|
114
|
+
updatedAt: number;
|
|
115
|
+
equity: string;
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
export type NormalizedAccountEvent = {
|
|
119
|
+
type: "balance.updated";
|
|
120
|
+
accountId: string;
|
|
121
|
+
exchange: string;
|
|
122
|
+
asset: string;
|
|
123
|
+
snapshot: {
|
|
124
|
+
accountId: string;
|
|
125
|
+
exchange: string;
|
|
126
|
+
asset: string;
|
|
127
|
+
free: string;
|
|
128
|
+
used: string;
|
|
129
|
+
total: string;
|
|
130
|
+
seq: number;
|
|
131
|
+
receivedAt: number;
|
|
132
|
+
updatedAt: number;
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
export interface ExchangeAdapter {
|
|
136
|
+
readonly exchange: string;
|
|
137
|
+
readonly capabilities: ExchangeCapabilities;
|
|
138
|
+
start(): Promise<void>;
|
|
139
|
+
stop(): Promise<void>;
|
|
140
|
+
subscribeL1Book(input: {
|
|
141
|
+
exchange: string;
|
|
142
|
+
symbol: string;
|
|
143
|
+
}): Promise<void>;
|
|
144
|
+
subscribeFundingRate(input: {
|
|
145
|
+
exchange: string;
|
|
146
|
+
symbol: string;
|
|
147
|
+
}): Promise<void>;
|
|
148
|
+
watchMarketEvents(): AsyncIterable<NormalizedMarketEvent>;
|
|
149
|
+
setMarketEventSink?(sink: ((event: NormalizedMarketEvent) => void) | undefined): void;
|
|
150
|
+
watchInternalErrors(): AsyncIterable<Error>;
|
|
151
|
+
subscribeOrders(input: {
|
|
152
|
+
accountId: string;
|
|
153
|
+
}): Promise<void>;
|
|
154
|
+
fetchOpenOrdersBaseline(accountId: string): Promise<NormalizedOrderEvent["snapshot"][]>;
|
|
155
|
+
watchOrderEvents(): AsyncIterable<NormalizedOrderEvent>;
|
|
156
|
+
setOrderEventSink?(sink: ((event: NormalizedOrderEvent) => void) | undefined): void;
|
|
157
|
+
fetchAccountBaseline(accountId: string): Promise<AdapterAccountBaseline>;
|
|
158
|
+
watchAccountEvents(): AsyncIterable<NormalizedAccountEvent>;
|
|
159
|
+
setAccountEventSink?(sink: ((event: NormalizedAccountEvent) => void) | undefined): void;
|
|
160
|
+
placeOrder(input: {
|
|
161
|
+
accountId: string;
|
|
162
|
+
exchange: string;
|
|
163
|
+
symbol: string;
|
|
164
|
+
side: "buy" | "sell";
|
|
165
|
+
amount: string;
|
|
166
|
+
clientOrderId: string;
|
|
167
|
+
type: string;
|
|
168
|
+
price?: string;
|
|
169
|
+
reduceOnly?: boolean;
|
|
170
|
+
}): Promise<{
|
|
171
|
+
clientOrderId?: string;
|
|
172
|
+
orderId?: string;
|
|
173
|
+
receivedAt: number;
|
|
174
|
+
}>;
|
|
175
|
+
amendOrder(input: {
|
|
176
|
+
accountId: string;
|
|
177
|
+
exchange: string;
|
|
178
|
+
clientOrderId?: string;
|
|
179
|
+
orderId?: string;
|
|
180
|
+
symbol?: string;
|
|
181
|
+
newPrice?: string;
|
|
182
|
+
}): Promise<{
|
|
183
|
+
clientOrderId?: string;
|
|
184
|
+
orderId?: string;
|
|
185
|
+
receivedAt: number;
|
|
186
|
+
}>;
|
|
187
|
+
cancelOrder(input: {
|
|
188
|
+
accountId: string;
|
|
189
|
+
exchange: string;
|
|
190
|
+
clientOrderId?: string;
|
|
191
|
+
orderId?: string;
|
|
192
|
+
}): Promise<{
|
|
193
|
+
clientOrderId?: string;
|
|
194
|
+
orderId?: string;
|
|
195
|
+
receivedAt: number;
|
|
196
|
+
}>;
|
|
197
|
+
cancelAllOrders(input: {
|
|
198
|
+
accountId: string;
|
|
199
|
+
exchange: string;
|
|
200
|
+
}): Promise<{
|
|
201
|
+
accountId: string;
|
|
202
|
+
exchange: string;
|
|
203
|
+
canceledCount: number;
|
|
204
|
+
}>;
|
|
205
|
+
getHealth(): {
|
|
206
|
+
exchange: string;
|
|
207
|
+
status: string;
|
|
208
|
+
wsConnected: boolean;
|
|
209
|
+
};
|
|
210
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { ExchangeAdapter } from "../adapters/types.js";
|
|
2
|
+
import { type AccountManager } from "../managers/account-manager.js";
|
|
3
|
+
import { type MarketManager } from "../managers/market-manager.js";
|
|
4
|
+
import { type OrderManager } from "../managers/order-manager.js";
|
|
5
|
+
import { createHealthStore } from "../store/health-store.js";
|
|
6
|
+
import type { ClientStatus, Exchange } from "../types/public.js";
|
|
7
|
+
import { createRuntime } from "./runtime.js";
|
|
8
|
+
export interface RegisterAccountInput {
|
|
9
|
+
accountId: string;
|
|
10
|
+
exchange: Exchange;
|
|
11
|
+
credentials: {
|
|
12
|
+
apiKey: string;
|
|
13
|
+
secret: string;
|
|
14
|
+
password?: string;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
interface InternalCreateClientOptions {
|
|
18
|
+
adapter?: ExchangeAdapter;
|
|
19
|
+
}
|
|
20
|
+
export interface AcexClient {
|
|
21
|
+
readonly market: MarketManager;
|
|
22
|
+
readonly account: AccountManager;
|
|
23
|
+
readonly order: OrderManager;
|
|
24
|
+
getStatus(): ClientStatus;
|
|
25
|
+
getHealth(): ReturnType<ReturnType<typeof createHealthStore>["snapshot"]>;
|
|
26
|
+
registerAccount(input: RegisterAccountInput): Promise<{
|
|
27
|
+
accountId: string;
|
|
28
|
+
exchange: Exchange;
|
|
29
|
+
}>;
|
|
30
|
+
start(): Promise<void>;
|
|
31
|
+
stop(): Promise<void>;
|
|
32
|
+
watchErrors(): ReturnType<ReturnType<typeof createRuntime>["watchErrors"]>;
|
|
33
|
+
watchHealth(): ReturnType<ReturnType<typeof createRuntime>["watchHealth"]>;
|
|
34
|
+
}
|
|
35
|
+
export declare function createInternalClient(options?: InternalCreateClientOptions): AcexClient;
|
|
36
|
+
export declare function createClient(): AcexClient;
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { createIdleAccountManager } from "../managers/account-manager.js";
|
|
2
|
+
import { createIdleMarketManager } from "../managers/market-manager.js";
|
|
3
|
+
import { createIdleOrderManager } from "../managers/order-manager.js";
|
|
4
|
+
import { createHealthStore } from "../store/health-store.js";
|
|
5
|
+
import { createRuntime } from "./runtime.js";
|
|
6
|
+
async function* emptyAsyncIterable() { }
|
|
7
|
+
export function createInternalClient(options = {}) {
|
|
8
|
+
let status = "idle";
|
|
9
|
+
const healthStore = createHealthStore();
|
|
10
|
+
const accounts = new Map();
|
|
11
|
+
const adapter = options.adapter;
|
|
12
|
+
const runtime = adapter === undefined ? undefined : createRuntime(adapter);
|
|
13
|
+
return {
|
|
14
|
+
market: runtime?.market ?? createIdleMarketManager(),
|
|
15
|
+
account: runtime?.account ?? createIdleAccountManager(),
|
|
16
|
+
order: runtime?.order ?? createIdleOrderManager(),
|
|
17
|
+
getStatus: () => status,
|
|
18
|
+
getHealth: () => healthStore.snapshot(),
|
|
19
|
+
watchErrors: () => runtime?.watchErrors() ?? emptyAsyncIterable(),
|
|
20
|
+
watchHealth: () => runtime?.watchHealth() ?? emptyAsyncIterable(),
|
|
21
|
+
async registerAccount(input) {
|
|
22
|
+
accounts.set(input.accountId, input);
|
|
23
|
+
return {
|
|
24
|
+
accountId: input.accountId,
|
|
25
|
+
exchange: input.exchange,
|
|
26
|
+
};
|
|
27
|
+
},
|
|
28
|
+
async start() {
|
|
29
|
+
status = "running";
|
|
30
|
+
healthStore.setClientStatus("running");
|
|
31
|
+
if (adapter !== undefined) {
|
|
32
|
+
await adapter.start();
|
|
33
|
+
healthStore.setExchangeHealth(adapter.exchange, adapter.getHealth());
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
async stop() {
|
|
37
|
+
status = "stopped";
|
|
38
|
+
healthStore.setClientStatus("stopped");
|
|
39
|
+
await adapter?.stop();
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export function createClient() {
|
|
44
|
+
return createInternalClient();
|
|
45
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export declare function markMarketStale(input: {
|
|
2
|
+
exchange: string;
|
|
3
|
+
symbol: string;
|
|
4
|
+
receivedAt: number;
|
|
5
|
+
}): {
|
|
6
|
+
exchange: string;
|
|
7
|
+
symbol: string;
|
|
8
|
+
freshness: "stale";
|
|
9
|
+
staleSince: number;
|
|
10
|
+
reason: "ws_disconnected";
|
|
11
|
+
};
|
|
12
|
+
export declare function markMarketReconciling(input: {
|
|
13
|
+
exchange: string;
|
|
14
|
+
symbol: string;
|
|
15
|
+
receivedAt: number;
|
|
16
|
+
}): {
|
|
17
|
+
exchange: string;
|
|
18
|
+
symbol: string;
|
|
19
|
+
freshness: "reconciling";
|
|
20
|
+
lastStreamReceivedAt: number;
|
|
21
|
+
reason: "reconciling";
|
|
22
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function markMarketStale(input) {
|
|
2
|
+
return {
|
|
3
|
+
exchange: input.exchange,
|
|
4
|
+
symbol: input.symbol,
|
|
5
|
+
freshness: "stale",
|
|
6
|
+
staleSince: input.receivedAt,
|
|
7
|
+
reason: "ws_disconnected",
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export function markMarketReconciling(input) {
|
|
11
|
+
return {
|
|
12
|
+
exchange: input.exchange,
|
|
13
|
+
symbol: input.symbol,
|
|
14
|
+
freshness: "reconciling",
|
|
15
|
+
lastStreamReceivedAt: input.receivedAt,
|
|
16
|
+
reason: "reconciling",
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ExchangeAdapter } from "../adapters/types.js";
|
|
2
|
+
import { type AccountManagerRuntime } from "../managers/account-manager.js";
|
|
3
|
+
import { createMarketManager } from "../managers/market-manager.js";
|
|
4
|
+
import { createOrderManager } from "../managers/order-manager.js";
|
|
5
|
+
import { markMarketReconciling, markMarketStale } from "./recovery.js";
|
|
6
|
+
export interface HealthEvent {
|
|
7
|
+
type: "market.status_changed";
|
|
8
|
+
exchange: string;
|
|
9
|
+
symbol: string;
|
|
10
|
+
status: ReturnType<typeof markMarketStale> | ReturnType<typeof markMarketReconciling>;
|
|
11
|
+
ts: number;
|
|
12
|
+
}
|
|
13
|
+
export interface RuntimeErrorEvent {
|
|
14
|
+
source: "adapter";
|
|
15
|
+
exchange: string;
|
|
16
|
+
error: Error;
|
|
17
|
+
ts: number;
|
|
18
|
+
}
|
|
19
|
+
export interface Runtime {
|
|
20
|
+
account: AccountManagerRuntime;
|
|
21
|
+
market: ReturnType<typeof createMarketManager>;
|
|
22
|
+
order: ReturnType<typeof createOrderManager>;
|
|
23
|
+
watchErrors(): AsyncIterable<RuntimeErrorEvent>;
|
|
24
|
+
watchHealth(): AsyncIterable<HealthEvent>;
|
|
25
|
+
}
|
|
26
|
+
export declare function createRuntime(adapter: ExchangeAdapter): Runtime;
|