@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,537 @@
|
|
|
1
|
+
import { createAsyncQueue } from "../../runtime/async-queue.js";
|
|
2
|
+
export class CcxtBinanceUsdMAdapter {
|
|
3
|
+
exchange = "binance";
|
|
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
|
+
#exchange;
|
|
20
|
+
#started = false;
|
|
21
|
+
#privateWatchTasks = new Set();
|
|
22
|
+
#orderSymbolsByClientOrderId = new Map();
|
|
23
|
+
#orderSymbolsByOrderId = new Map();
|
|
24
|
+
#marketQueue = createAsyncQueue({ maxBufferSize: 100 });
|
|
25
|
+
#marketEventSink;
|
|
26
|
+
#internalErrorQueue = createAsyncQueue({ maxBufferSize: 20 });
|
|
27
|
+
#orderQueue = createAsyncQueue({ maxBufferSize: 100 });
|
|
28
|
+
#orderEventSink;
|
|
29
|
+
#accountQueue = createAsyncQueue({ maxBufferSize: 100 });
|
|
30
|
+
#accountEventSink;
|
|
31
|
+
constructor(input) {
|
|
32
|
+
this.#exchange = input.exchange;
|
|
33
|
+
}
|
|
34
|
+
async start() {
|
|
35
|
+
this.#started = true;
|
|
36
|
+
const privateStreams = this.#exchange;
|
|
37
|
+
if (privateStreams.watchOrders !== undefined) {
|
|
38
|
+
this.#trackPrivateWatchTask(this.#runWatchOrders(privateStreams));
|
|
39
|
+
}
|
|
40
|
+
if (privateStreams.watchBalance !== undefined) {
|
|
41
|
+
this.#trackPrivateWatchTask(this.#runWatchBalance(privateStreams));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
async stop() {
|
|
45
|
+
this.#started = false;
|
|
46
|
+
await this.#exchange.close?.();
|
|
47
|
+
await Promise.allSettled([...this.#privateWatchTasks]);
|
|
48
|
+
this.#marketQueue.close();
|
|
49
|
+
this.#internalErrorQueue.close();
|
|
50
|
+
this.#orderQueue.close();
|
|
51
|
+
this.#accountQueue.close();
|
|
52
|
+
}
|
|
53
|
+
async subscribeL1Book(input) {
|
|
54
|
+
try {
|
|
55
|
+
const snapshot = await this.mapTickerToL1(input.symbol);
|
|
56
|
+
this.#emitMarketEvent({
|
|
57
|
+
type: "l1_book.updated",
|
|
58
|
+
exchange: snapshot.exchange,
|
|
59
|
+
symbol: snapshot.symbol,
|
|
60
|
+
bidPrice: snapshot.bidPrice,
|
|
61
|
+
bidSize: snapshot.bidSize,
|
|
62
|
+
askPrice: snapshot.askPrice,
|
|
63
|
+
askSize: snapshot.askSize,
|
|
64
|
+
...(snapshot.exchangeTs === undefined ? {} : { exchangeTs: snapshot.exchangeTs }),
|
|
65
|
+
receivedAt: snapshot.receivedAt,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
this.#emitInternalError(asError(error));
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async subscribeFundingRate(_input) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
watchMarketEvents() {
|
|
77
|
+
return this.#marketQueue;
|
|
78
|
+
}
|
|
79
|
+
setMarketEventSink(sink) {
|
|
80
|
+
this.#marketEventSink = sink;
|
|
81
|
+
}
|
|
82
|
+
watchInternalErrors() {
|
|
83
|
+
return this.#internalErrorQueue;
|
|
84
|
+
}
|
|
85
|
+
async subscribeOrders(_input) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
watchOrderEvents() {
|
|
89
|
+
return this.#orderQueue;
|
|
90
|
+
}
|
|
91
|
+
setOrderEventSink(sink) {
|
|
92
|
+
this.#orderEventSink = sink;
|
|
93
|
+
}
|
|
94
|
+
watchAccountEvents() {
|
|
95
|
+
return this.#accountQueue;
|
|
96
|
+
}
|
|
97
|
+
setAccountEventSink(sink) {
|
|
98
|
+
this.#accountEventSink = sink;
|
|
99
|
+
}
|
|
100
|
+
async mapTickerToL1(symbol) {
|
|
101
|
+
const bidsAsks = await this.#exchange.watchBidsAsks([symbol]);
|
|
102
|
+
const ticker = bidsAsks[symbol];
|
|
103
|
+
if (ticker === undefined) {
|
|
104
|
+
throw new Error("missing required ticker fields");
|
|
105
|
+
}
|
|
106
|
+
if (ticker.bid === undefined ||
|
|
107
|
+
ticker.bidVolume === undefined ||
|
|
108
|
+
ticker.ask === undefined ||
|
|
109
|
+
ticker.askVolume === undefined) {
|
|
110
|
+
throw new Error("missing required ticker fields");
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
exchange: this.exchange,
|
|
114
|
+
symbol: ticker.symbol,
|
|
115
|
+
bidPrice: String(ticker.bid),
|
|
116
|
+
bidSize: String(ticker.bidVolume),
|
|
117
|
+
askPrice: String(ticker.ask),
|
|
118
|
+
askSize: String(ticker.askVolume),
|
|
119
|
+
...(ticker.timestamp === undefined ? {} : { exchangeTs: ticker.timestamp }),
|
|
120
|
+
receivedAt: Date.now(),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
async fetchMarketInfo() {
|
|
124
|
+
const markets = await this.#exchange.fetchMarkets();
|
|
125
|
+
return markets.map((market) => {
|
|
126
|
+
return {
|
|
127
|
+
exchange: this.exchange,
|
|
128
|
+
symbol: market.symbol,
|
|
129
|
+
baseAsset: market.base,
|
|
130
|
+
quoteAsset: market.quote,
|
|
131
|
+
marketType: market.type,
|
|
132
|
+
pricePrecision: market.precision.price,
|
|
133
|
+
amountPrecision: market.precision.amount,
|
|
134
|
+
active: market.active,
|
|
135
|
+
};
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
async fetchOpenOrdersBaseline(accountId) {
|
|
139
|
+
const now = Date.now();
|
|
140
|
+
const orders = await this.#exchange.fetchOpenOrders(undefined, undefined, undefined, {
|
|
141
|
+
type: "future",
|
|
142
|
+
});
|
|
143
|
+
return orders.map((order) => {
|
|
144
|
+
const symbol = requiredString(order.symbol, "symbol");
|
|
145
|
+
const side = requiredOrderSide(order.side);
|
|
146
|
+
const type = requiredString(order.type, "type");
|
|
147
|
+
const status = requiredString(order.status, "status");
|
|
148
|
+
const amount = valueToRequiredString(order.amount, "amount");
|
|
149
|
+
const filled = valueToRequiredString(order.filled, "filled");
|
|
150
|
+
const updatedAt = order.timestamp ?? now;
|
|
151
|
+
this.#rememberOrderSymbol(withOptionalOrderLocators({
|
|
152
|
+
symbol,
|
|
153
|
+
orderId: order.id,
|
|
154
|
+
clientOrderId: order.clientOrderId,
|
|
155
|
+
}));
|
|
156
|
+
return {
|
|
157
|
+
accountId,
|
|
158
|
+
exchange: this.exchange,
|
|
159
|
+
symbol,
|
|
160
|
+
side,
|
|
161
|
+
type,
|
|
162
|
+
status,
|
|
163
|
+
amount,
|
|
164
|
+
filled,
|
|
165
|
+
...(order.price === undefined ? {} : { price: String(order.price) }),
|
|
166
|
+
seq: 0,
|
|
167
|
+
receivedAt: now,
|
|
168
|
+
updatedAt,
|
|
169
|
+
...(order.id === undefined ? {} : { orderId: order.id }),
|
|
170
|
+
...(order.clientOrderId === undefined ? {} : { clientOrderId: order.clientOrderId }),
|
|
171
|
+
};
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
async fetchAccountBaseline(accountId) {
|
|
175
|
+
const now = Date.now();
|
|
176
|
+
const [balance, positions] = await Promise.all([
|
|
177
|
+
this.#exchange.fetchBalance({ type: "future" }),
|
|
178
|
+
this.#exchange.fetchPositions([], { type: "future" }),
|
|
179
|
+
]);
|
|
180
|
+
const assets = new Set([
|
|
181
|
+
...Object.keys(balance.free ?? {}),
|
|
182
|
+
...Object.keys(balance.used ?? {}),
|
|
183
|
+
...Object.keys(balance.total ?? {}),
|
|
184
|
+
]);
|
|
185
|
+
const balances = Object.fromEntries([...assets].map((asset) => [
|
|
186
|
+
asset,
|
|
187
|
+
{
|
|
188
|
+
accountId,
|
|
189
|
+
exchange: this.exchange,
|
|
190
|
+
asset,
|
|
191
|
+
free: valueToString(balance.free?.[asset]),
|
|
192
|
+
used: valueToString(balance.used?.[asset]),
|
|
193
|
+
total: valueToString(balance.total?.[asset]),
|
|
194
|
+
seq: 0,
|
|
195
|
+
receivedAt: now,
|
|
196
|
+
updatedAt: now,
|
|
197
|
+
},
|
|
198
|
+
]));
|
|
199
|
+
const positionsSnapshots = positions.map((position) => {
|
|
200
|
+
const symbol = requiredString(position.symbol, "symbol", "position fields");
|
|
201
|
+
const side = requiredString(position.side, "side", "position fields");
|
|
202
|
+
const size = valueToRequiredString(position.contracts ?? position.amount ?? position.positionAmt, "size", "position fields");
|
|
203
|
+
const updatedAt = position.timestamp ?? now;
|
|
204
|
+
return {
|
|
205
|
+
accountId,
|
|
206
|
+
exchange: this.exchange,
|
|
207
|
+
symbol,
|
|
208
|
+
side,
|
|
209
|
+
size,
|
|
210
|
+
seq: 0,
|
|
211
|
+
receivedAt: now,
|
|
212
|
+
updatedAt,
|
|
213
|
+
};
|
|
214
|
+
});
|
|
215
|
+
const totalWalletBalance = balance.info?.totalWalletBalance;
|
|
216
|
+
const risk = totalWalletBalance === undefined
|
|
217
|
+
? undefined
|
|
218
|
+
: {
|
|
219
|
+
accountId,
|
|
220
|
+
exchange: this.exchange,
|
|
221
|
+
seq: 0,
|
|
222
|
+
receivedAt: now,
|
|
223
|
+
updatedAt: now,
|
|
224
|
+
equity: valueToString(totalWalletBalance),
|
|
225
|
+
};
|
|
226
|
+
return {
|
|
227
|
+
balances,
|
|
228
|
+
positions: positionsSnapshots,
|
|
229
|
+
...(risk === undefined ? {} : { risk }),
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
async placeOrder(input) {
|
|
233
|
+
const amount = parseFiniteNumber(input.amount, "amount");
|
|
234
|
+
if (amount <= 0) {
|
|
235
|
+
throw new Error("invalid order amount");
|
|
236
|
+
}
|
|
237
|
+
const price = input.price === undefined ? undefined : parseFiniteNumber(input.price, "price");
|
|
238
|
+
if (price !== undefined && price <= 0) {
|
|
239
|
+
throw new Error("invalid order price");
|
|
240
|
+
}
|
|
241
|
+
const orderParams = {
|
|
242
|
+
type: "future",
|
|
243
|
+
clientOrderId: input.clientOrderId,
|
|
244
|
+
};
|
|
245
|
+
if (input.reduceOnly === true) {
|
|
246
|
+
orderParams.reduceOnly = true;
|
|
247
|
+
}
|
|
248
|
+
const order = await this.#exchange.createOrder(input.symbol, input.type, input.side, amount, price, orderParams);
|
|
249
|
+
this.#rememberOrderSymbol(withOptionalOrderLocators({
|
|
250
|
+
symbol: input.symbol,
|
|
251
|
+
orderId: order.id,
|
|
252
|
+
clientOrderId: order.clientOrderId ?? input.clientOrderId,
|
|
253
|
+
}));
|
|
254
|
+
return {
|
|
255
|
+
...(order.clientOrderId === undefined
|
|
256
|
+
? { clientOrderId: input.clientOrderId }
|
|
257
|
+
: { clientOrderId: String(order.clientOrderId) }),
|
|
258
|
+
...(order.id === undefined ? {} : { orderId: String(order.id) }),
|
|
259
|
+
receivedAt: Date.now(),
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
async amendOrder(input) {
|
|
263
|
+
return {
|
|
264
|
+
...(input.clientOrderId === undefined ? {} : { clientOrderId: input.clientOrderId }),
|
|
265
|
+
...(input.orderId === undefined ? {} : { orderId: input.orderId }),
|
|
266
|
+
receivedAt: Date.now(),
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
async cancelOrder(input) {
|
|
270
|
+
if (input.orderId === undefined && input.clientOrderId === undefined) {
|
|
271
|
+
return {
|
|
272
|
+
receivedAt: Date.now(),
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
const privateStreams = this.#exchange;
|
|
276
|
+
if (privateStreams.cancelOrder !== undefined) {
|
|
277
|
+
const symbol = this.#resolveOrderSymbol(withOptionalOrderLocators({
|
|
278
|
+
symbol: undefined,
|
|
279
|
+
orderId: input.orderId,
|
|
280
|
+
clientOrderId: input.clientOrderId,
|
|
281
|
+
}));
|
|
282
|
+
const params = { type: "future" };
|
|
283
|
+
if (input.clientOrderId !== undefined) {
|
|
284
|
+
params.clientOrderId = input.clientOrderId;
|
|
285
|
+
}
|
|
286
|
+
const result = await privateStreams.cancelOrder(input.orderId, symbol, params);
|
|
287
|
+
this.#rememberOrderSymbol(withOptionalOrderLocators({
|
|
288
|
+
symbol: result.symbol ?? symbol,
|
|
289
|
+
orderId: result.id ?? input.orderId,
|
|
290
|
+
clientOrderId: result.clientOrderId ?? input.clientOrderId,
|
|
291
|
+
}));
|
|
292
|
+
return {
|
|
293
|
+
...(result.clientOrderId === undefined
|
|
294
|
+
? input.clientOrderId === undefined
|
|
295
|
+
? {}
|
|
296
|
+
: { clientOrderId: input.clientOrderId }
|
|
297
|
+
: { clientOrderId: String(result.clientOrderId) }),
|
|
298
|
+
...(result.id === undefined
|
|
299
|
+
? input.orderId === undefined
|
|
300
|
+
? {}
|
|
301
|
+
: { orderId: input.orderId }
|
|
302
|
+
: { orderId: String(result.id) }),
|
|
303
|
+
receivedAt: Date.now(),
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
return {
|
|
307
|
+
...(input.clientOrderId === undefined ? {} : { clientOrderId: input.clientOrderId }),
|
|
308
|
+
...(input.orderId === undefined ? {} : { orderId: input.orderId }),
|
|
309
|
+
receivedAt: Date.now(),
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
async cancelAllOrders(_input) {
|
|
313
|
+
return {
|
|
314
|
+
accountId: _input.accountId,
|
|
315
|
+
exchange: this.exchange,
|
|
316
|
+
canceledCount: 0,
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
#emitMarketEvent(event) {
|
|
320
|
+
if (this.#marketEventSink !== undefined) {
|
|
321
|
+
this.#marketEventSink(event);
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
this.#marketQueue.push(event);
|
|
325
|
+
}
|
|
326
|
+
#emitOrderEvent(event) {
|
|
327
|
+
if (this.#orderEventSink !== undefined) {
|
|
328
|
+
this.#orderEventSink(event);
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
this.#orderQueue.push(event);
|
|
332
|
+
}
|
|
333
|
+
#emitAccountEvent(event) {
|
|
334
|
+
if (this.#accountEventSink !== undefined) {
|
|
335
|
+
this.#accountEventSink(event);
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
this.#accountQueue.push(event);
|
|
339
|
+
}
|
|
340
|
+
#emitInternalError(error) {
|
|
341
|
+
this.#internalErrorQueue.push(error);
|
|
342
|
+
}
|
|
343
|
+
#trackPrivateWatchTask(task) {
|
|
344
|
+
this.#privateWatchTasks.add(task);
|
|
345
|
+
void task.finally(() => {
|
|
346
|
+
this.#privateWatchTasks.delete(task);
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
#rememberOrderSymbol(input) {
|
|
350
|
+
if (input.symbol === undefined) {
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
if (input.orderId !== undefined) {
|
|
354
|
+
this.#orderSymbolsByOrderId.set(String(input.orderId), input.symbol);
|
|
355
|
+
}
|
|
356
|
+
if (input.clientOrderId !== undefined) {
|
|
357
|
+
this.#orderSymbolsByClientOrderId.set(String(input.clientOrderId), input.symbol);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
#resolveOrderSymbol(input) {
|
|
361
|
+
if (input.orderId !== undefined) {
|
|
362
|
+
const symbol = this.#orderSymbolsByOrderId.get(input.orderId);
|
|
363
|
+
if (symbol !== undefined) {
|
|
364
|
+
return symbol;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
if (input.clientOrderId !== undefined) {
|
|
368
|
+
return this.#orderSymbolsByClientOrderId.get(input.clientOrderId);
|
|
369
|
+
}
|
|
370
|
+
return undefined;
|
|
371
|
+
}
|
|
372
|
+
async #runWatchOrders(privateStreams) {
|
|
373
|
+
while (this.#started) {
|
|
374
|
+
try {
|
|
375
|
+
const orders = await privateStreams.watchOrders?.();
|
|
376
|
+
if (!this.#started) {
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
this.#emitOrdersFromWatch(orders ?? []);
|
|
380
|
+
}
|
|
381
|
+
catch (error) {
|
|
382
|
+
if (!this.#started) {
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
this.#started = false;
|
|
386
|
+
this.#emitInternalError(asError(error));
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
async #runWatchBalance(privateStreams) {
|
|
392
|
+
while (this.#started) {
|
|
393
|
+
try {
|
|
394
|
+
const balance = await privateStreams.watchBalance?.();
|
|
395
|
+
if (!this.#started) {
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
this.#emitBalanceFromWatch(balance);
|
|
399
|
+
}
|
|
400
|
+
catch (error) {
|
|
401
|
+
if (!this.#started) {
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
this.#started = false;
|
|
405
|
+
this.#emitInternalError(asError(error));
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
#emitOrdersFromWatch(orders) {
|
|
411
|
+
for (const order of orders) {
|
|
412
|
+
try {
|
|
413
|
+
const now = Date.now();
|
|
414
|
+
const symbol = requiredString(order.symbol, "symbol", "watchOrders");
|
|
415
|
+
const side = requiredOrderSide(order.side, "watchOrders");
|
|
416
|
+
const type = requiredString(order.type, "type", "watchOrders");
|
|
417
|
+
const status = requiredString(order.status, "status", "watchOrders");
|
|
418
|
+
const amount = valueToRequiredString(order.amount, "amount", "watchOrders");
|
|
419
|
+
const filled = valueToString(order.filled);
|
|
420
|
+
const normalizedStatus = status.toLowerCase();
|
|
421
|
+
this.#rememberOrderSymbol(withOptionalOrderLocators({
|
|
422
|
+
symbol,
|
|
423
|
+
orderId: order.id,
|
|
424
|
+
clientOrderId: order.clientOrderId,
|
|
425
|
+
}));
|
|
426
|
+
const eventType = normalizedStatus === "canceled" || normalizedStatus === "cancelled"
|
|
427
|
+
? "order.canceled"
|
|
428
|
+
: "order.updated";
|
|
429
|
+
this.#emitOrderEvent({
|
|
430
|
+
type: eventType,
|
|
431
|
+
accountId: "main",
|
|
432
|
+
exchange: this.exchange,
|
|
433
|
+
receivedAt: now,
|
|
434
|
+
snapshot: {
|
|
435
|
+
accountId: "main",
|
|
436
|
+
exchange: this.exchange,
|
|
437
|
+
symbol,
|
|
438
|
+
side,
|
|
439
|
+
type,
|
|
440
|
+
status,
|
|
441
|
+
amount,
|
|
442
|
+
filled,
|
|
443
|
+
...(order.price === undefined ? {} : { price: String(order.price) }),
|
|
444
|
+
seq: 0,
|
|
445
|
+
receivedAt: now,
|
|
446
|
+
updatedAt: Number(order.timestamp ?? now),
|
|
447
|
+
...(order.id === undefined ? {} : { orderId: String(order.id) }),
|
|
448
|
+
...(order.clientOrderId === undefined
|
|
449
|
+
? {}
|
|
450
|
+
: { clientOrderId: String(order.clientOrderId) }),
|
|
451
|
+
},
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
catch (error) {
|
|
455
|
+
this.#emitInternalError(asError(error));
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
#emitBalanceFromWatch(balance) {
|
|
460
|
+
if (balance === undefined) {
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
const now = Date.now();
|
|
464
|
+
const assets = new Set([
|
|
465
|
+
...Object.keys(balance.free ?? {}),
|
|
466
|
+
...Object.keys(balance.used ?? {}),
|
|
467
|
+
...Object.keys(balance.total ?? {}),
|
|
468
|
+
]);
|
|
469
|
+
for (const asset of assets) {
|
|
470
|
+
this.#emitAccountEvent({
|
|
471
|
+
type: "balance.updated",
|
|
472
|
+
accountId: "main",
|
|
473
|
+
exchange: this.exchange,
|
|
474
|
+
asset,
|
|
475
|
+
snapshot: {
|
|
476
|
+
accountId: "main",
|
|
477
|
+
exchange: this.exchange,
|
|
478
|
+
asset,
|
|
479
|
+
free: valueToString(balance.free?.[asset]),
|
|
480
|
+
used: valueToString(balance.used?.[asset]),
|
|
481
|
+
total: valueToString(balance.total?.[asset]),
|
|
482
|
+
seq: 0,
|
|
483
|
+
receivedAt: now,
|
|
484
|
+
updatedAt: now,
|
|
485
|
+
},
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
getHealth() {
|
|
490
|
+
return {
|
|
491
|
+
exchange: this.exchange,
|
|
492
|
+
status: this.#started ? "healthy" : "idle",
|
|
493
|
+
wsConnected: this.#started,
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
function withOptionalOrderLocators(input) {
|
|
498
|
+
return {
|
|
499
|
+
...(input.symbol === undefined ? {} : { symbol: input.symbol }),
|
|
500
|
+
...(input.orderId === undefined ? {} : { orderId: input.orderId }),
|
|
501
|
+
...(input.clientOrderId === undefined ? {} : { clientOrderId: input.clientOrderId }),
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
function requiredString(value, field, context = "order fields") {
|
|
505
|
+
if (value === undefined || value.trim().length === 0) {
|
|
506
|
+
throw new Error(`missing required ${context}: ${field}`);
|
|
507
|
+
}
|
|
508
|
+
return value;
|
|
509
|
+
}
|
|
510
|
+
function requiredOrderSide(value, context = "order fields") {
|
|
511
|
+
if (value !== "buy" && value !== "sell") {
|
|
512
|
+
throw new Error(`missing required ${context}: side`);
|
|
513
|
+
}
|
|
514
|
+
return value;
|
|
515
|
+
}
|
|
516
|
+
function valueToRequiredString(value, field, context = "order fields") {
|
|
517
|
+
if (value === undefined) {
|
|
518
|
+
throw new Error(`missing required ${context}: ${field}`);
|
|
519
|
+
}
|
|
520
|
+
return String(value);
|
|
521
|
+
}
|
|
522
|
+
function valueToString(value) {
|
|
523
|
+
return value === undefined ? "0" : String(value);
|
|
524
|
+
}
|
|
525
|
+
function parseFiniteNumber(value, field) {
|
|
526
|
+
if (value.trim().length === 0) {
|
|
527
|
+
throw new Error(`invalid order ${field}`);
|
|
528
|
+
}
|
|
529
|
+
const parsed = Number(value);
|
|
530
|
+
if (!Number.isFinite(parsed)) {
|
|
531
|
+
throw new Error(`invalid order ${field}`);
|
|
532
|
+
}
|
|
533
|
+
return parsed;
|
|
534
|
+
}
|
|
535
|
+
function asError(error) {
|
|
536
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
537
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import type { AdapterAccountBaseline, ExchangeAdapter, NormalizedAccountEvent, NormalizedMarketEvent, NormalizedOrderEvent } from "../types.js";
|
|
2
|
+
export declare class FakeAsterAdapter implements ExchangeAdapter {
|
|
3
|
+
#private;
|
|
4
|
+
readonly exchange = "aster";
|
|
5
|
+
readonly capabilities: {
|
|
6
|
+
publicWs: boolean;
|
|
7
|
+
privateWs: boolean;
|
|
8
|
+
l1BookStream: boolean;
|
|
9
|
+
fundingRateStream: boolean;
|
|
10
|
+
accountStream: boolean;
|
|
11
|
+
orderStream: boolean;
|
|
12
|
+
fetchMarketInfo: boolean;
|
|
13
|
+
fetchBalances: boolean;
|
|
14
|
+
fetchPositions: boolean;
|
|
15
|
+
fetchRisk: boolean;
|
|
16
|
+
fetchOpenOrders: boolean;
|
|
17
|
+
fetchMyTrades: boolean;
|
|
18
|
+
fetchOrderById: boolean;
|
|
19
|
+
};
|
|
20
|
+
start(): Promise<void>;
|
|
21
|
+
stop(): Promise<void>;
|
|
22
|
+
subscribeL1Book(): Promise<void>;
|
|
23
|
+
subscribeFundingRate(): Promise<void>;
|
|
24
|
+
watchMarketEvents(): import("../../runtime/async-queue.js").AsyncQueue<NormalizedMarketEvent>;
|
|
25
|
+
setMarketEventSink(sink: ((event: NormalizedMarketEvent) => void) | undefined): void;
|
|
26
|
+
emitL1Book(event: Extract<NormalizedMarketEvent, {
|
|
27
|
+
type: "l1_book.updated";
|
|
28
|
+
}>): void;
|
|
29
|
+
emitFundingRate(event: Extract<NormalizedMarketEvent, {
|
|
30
|
+
type: "funding_rate.updated";
|
|
31
|
+
}>): void;
|
|
32
|
+
emitMarketDisconnect(input: {
|
|
33
|
+
exchange: string;
|
|
34
|
+
symbol: string;
|
|
35
|
+
}): void;
|
|
36
|
+
emitMarketReconnect(input: {
|
|
37
|
+
exchange: string;
|
|
38
|
+
symbol: string;
|
|
39
|
+
}): void;
|
|
40
|
+
watchInternalErrors(): import("../../runtime/async-queue.js").AsyncQueue<Error>;
|
|
41
|
+
emitInternalError(error: Error): void;
|
|
42
|
+
subscribeOrders(): Promise<void>;
|
|
43
|
+
setAccountBaseline(accountId: string, baseline: AdapterAccountBaseline): void;
|
|
44
|
+
fetchAccountBaseline(accountId: string): Promise<AdapterAccountBaseline>;
|
|
45
|
+
watchAccountEvents(): import("../../runtime/async-queue.js").AsyncQueue<NormalizedAccountEvent>;
|
|
46
|
+
setAccountEventSink(sink: ((event: NormalizedAccountEvent) => void) | undefined): void;
|
|
47
|
+
emitBalanceUpdate(event: Omit<NormalizedAccountEvent, "type">): void;
|
|
48
|
+
fetchOpenOrdersBaseline(accountId: string): Promise<({
|
|
49
|
+
accountId: string;
|
|
50
|
+
exchange: string;
|
|
51
|
+
symbol: string;
|
|
52
|
+
side: "buy" | "sell";
|
|
53
|
+
type: string;
|
|
54
|
+
status: string;
|
|
55
|
+
amount: string;
|
|
56
|
+
filled: string;
|
|
57
|
+
price?: string;
|
|
58
|
+
seq: number;
|
|
59
|
+
receivedAt: number;
|
|
60
|
+
updatedAt: number;
|
|
61
|
+
orderId?: string;
|
|
62
|
+
clientOrderId?: string;
|
|
63
|
+
} | {
|
|
64
|
+
accountId: string;
|
|
65
|
+
exchange: string;
|
|
66
|
+
symbol: string;
|
|
67
|
+
side: "buy" | "sell";
|
|
68
|
+
type: string;
|
|
69
|
+
status: string;
|
|
70
|
+
amount: string;
|
|
71
|
+
filled: string;
|
|
72
|
+
price?: string;
|
|
73
|
+
seq: number;
|
|
74
|
+
receivedAt: number;
|
|
75
|
+
updatedAt: number;
|
|
76
|
+
orderId?: string;
|
|
77
|
+
clientOrderId?: string;
|
|
78
|
+
})[]>;
|
|
79
|
+
watchOrderEvents(): import("../../runtime/async-queue.js").AsyncQueue<NormalizedOrderEvent>;
|
|
80
|
+
setOrderEventSink(sink: ((event: NormalizedOrderEvent) => void) | undefined): void;
|
|
81
|
+
placeOrder(input: {
|
|
82
|
+
accountId: string;
|
|
83
|
+
exchange: string;
|
|
84
|
+
symbol: string;
|
|
85
|
+
side: "buy" | "sell";
|
|
86
|
+
amount: string;
|
|
87
|
+
clientOrderId: string;
|
|
88
|
+
type: string;
|
|
89
|
+
price?: string;
|
|
90
|
+
reduceOnly?: boolean;
|
|
91
|
+
}): Promise<{
|
|
92
|
+
receivedAt: number;
|
|
93
|
+
orderId?: string;
|
|
94
|
+
clientOrderId?: string;
|
|
95
|
+
}>;
|
|
96
|
+
amendOrder(input: {
|
|
97
|
+
accountId: string;
|
|
98
|
+
exchange: string;
|
|
99
|
+
clientOrderId?: string;
|
|
100
|
+
orderId?: string;
|
|
101
|
+
newPrice?: string;
|
|
102
|
+
}): Promise<{
|
|
103
|
+
receivedAt: number;
|
|
104
|
+
orderId?: string;
|
|
105
|
+
clientOrderId?: string;
|
|
106
|
+
}>;
|
|
107
|
+
cancelOrder(input: {
|
|
108
|
+
accountId: string;
|
|
109
|
+
exchange: string;
|
|
110
|
+
clientOrderId?: string;
|
|
111
|
+
orderId?: string;
|
|
112
|
+
}): Promise<{
|
|
113
|
+
receivedAt: number;
|
|
114
|
+
orderId?: string;
|
|
115
|
+
clientOrderId?: string;
|
|
116
|
+
}>;
|
|
117
|
+
cancelAllOrders(input: {
|
|
118
|
+
accountId: string;
|
|
119
|
+
exchange: string;
|
|
120
|
+
}): Promise<{
|
|
121
|
+
accountId: string;
|
|
122
|
+
exchange: string;
|
|
123
|
+
canceledCount: number;
|
|
124
|
+
}>;
|
|
125
|
+
getHealth(): {
|
|
126
|
+
exchange: string;
|
|
127
|
+
status: string;
|
|
128
|
+
wsConnected: boolean;
|
|
129
|
+
};
|
|
130
|
+
}
|