@blackcube/xgate-sdk 0.22.0 → 0.23.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 +177 -17
- package/dist/index.cjs +419 -134
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +198 -5
- package/dist/index.d.ts +198 -5
- package/dist/index.js +417 -135
- package/dist/index.js.map +1 -1
- package/package.json +10 -10
package/dist/index.js
CHANGED
|
@@ -19,131 +19,6 @@ var __decorateClass = (decorators, target, key, kind) => {
|
|
|
19
19
|
return result;
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
-
// src/enums/timeframe.enums.ts
|
|
23
|
-
var Timeframe = /* @__PURE__ */ ((Timeframe2) => {
|
|
24
|
-
Timeframe2["M1"] = "1m";
|
|
25
|
-
Timeframe2["M5"] = "5m";
|
|
26
|
-
Timeframe2["M15"] = "15m";
|
|
27
|
-
Timeframe2["H1"] = "1h";
|
|
28
|
-
Timeframe2["H4"] = "4h";
|
|
29
|
-
Timeframe2["D"] = "1d";
|
|
30
|
-
Timeframe2["W"] = "1w";
|
|
31
|
-
return Timeframe2;
|
|
32
|
-
})(Timeframe || {});
|
|
33
|
-
var TIMEFRAME_MINUTES = {
|
|
34
|
-
["1m" /* M1 */]: 1,
|
|
35
|
-
["5m" /* M5 */]: 5,
|
|
36
|
-
["15m" /* M15 */]: 15,
|
|
37
|
-
["1h" /* H1 */]: 60,
|
|
38
|
-
["4h" /* H4 */]: 240,
|
|
39
|
-
["1d" /* D */]: 1440,
|
|
40
|
-
["1w" /* W */]: 10080
|
|
41
|
-
};
|
|
42
|
-
function timeframeMs(timeframe) {
|
|
43
|
-
return TIMEFRAME_MINUTES[timeframe] * 6e4;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// src/helpers/decimal.ts
|
|
47
|
-
function parse(value) {
|
|
48
|
-
const trimmed = value.trim();
|
|
49
|
-
if (/^-?\d+(\.\d+)?$/.test(trimmed) === false) {
|
|
50
|
-
throw new Error(`D\xE9cimal illisible : "${value}".`);
|
|
51
|
-
}
|
|
52
|
-
const dot = trimmed.indexOf(".");
|
|
53
|
-
if (dot < 0) {
|
|
54
|
-
return { digits: BigInt(trimmed), scale: 0 };
|
|
55
|
-
}
|
|
56
|
-
return {
|
|
57
|
-
digits: BigInt(trimmed.slice(0, dot) + trimmed.slice(dot + 1)),
|
|
58
|
-
scale: trimmed.length - dot - 1
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
function format(digits, scale) {
|
|
62
|
-
const negative = digits < 0n;
|
|
63
|
-
const plain = (negative === true ? -digits : digits).toString().padStart(scale + 1, "0");
|
|
64
|
-
const sign = negative === true ? "-" : "";
|
|
65
|
-
if (scale === 0) {
|
|
66
|
-
return sign + plain;
|
|
67
|
-
}
|
|
68
|
-
const whole = plain.slice(0, plain.length - scale);
|
|
69
|
-
const fraction = plain.slice(plain.length - scale).replace(/0+$/, "");
|
|
70
|
-
return fraction === "" ? sign + whole : `${sign + whole}.${fraction}`;
|
|
71
|
-
}
|
|
72
|
-
function addDecimals(left, right) {
|
|
73
|
-
const a = parse(left);
|
|
74
|
-
const b = parse(right);
|
|
75
|
-
const scale = Math.max(a.scale, b.scale);
|
|
76
|
-
const digits = a.digits * 10n ** BigInt(scale - a.scale) + b.digits * 10n ** BigInt(scale - b.scale);
|
|
77
|
-
return format(digits, scale);
|
|
78
|
-
}
|
|
79
|
-
function compareDecimals(left, right) {
|
|
80
|
-
const a = parse(left);
|
|
81
|
-
const b = parse(right);
|
|
82
|
-
const scale = Math.max(a.scale, b.scale);
|
|
83
|
-
const x = a.digits * 10n ** BigInt(scale - a.scale);
|
|
84
|
-
const y = b.digits * 10n ** BigInt(scale - b.scale);
|
|
85
|
-
if (x === y) {
|
|
86
|
-
return 0;
|
|
87
|
-
}
|
|
88
|
-
return x > y ? 1 : -1;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// src/helpers/candle-aggregator.ts
|
|
92
|
-
var MONDAY_OFFSET_MS = 4 * 24 * 60 * 60 * 1e3;
|
|
93
|
-
function aggregate(source, target, now = Date.now()) {
|
|
94
|
-
if (source.length === 0) {
|
|
95
|
-
return [];
|
|
96
|
-
}
|
|
97
|
-
const sourceMs = timeframeMs(source[0]?.interval ?? "1m" /* M1 */);
|
|
98
|
-
const targetMs = timeframeMs(target);
|
|
99
|
-
if (targetMs <= sourceMs) {
|
|
100
|
-
return [];
|
|
101
|
-
}
|
|
102
|
-
const offsetMs = target === "1w" /* W */ ? MONDAY_OFFSET_MS : 0;
|
|
103
|
-
const expected = targetMs / sourceMs;
|
|
104
|
-
const buckets = /* @__PURE__ */ new Map();
|
|
105
|
-
const sorted = [...source].sort(
|
|
106
|
-
(left, right) => left.openedAt.getTime() - right.openedAt.getTime()
|
|
107
|
-
);
|
|
108
|
-
for (const candle of sorted) {
|
|
109
|
-
const start = Math.floor((candle.openedAt.getTime() - offsetMs) / targetMs) * targetMs + offsetMs;
|
|
110
|
-
const bucket = buckets.get(start);
|
|
111
|
-
if (bucket === void 0) {
|
|
112
|
-
buckets.set(start, {
|
|
113
|
-
sources: 1,
|
|
114
|
-
candle: {
|
|
115
|
-
...candle,
|
|
116
|
-
interval: target,
|
|
117
|
-
openedAt: new Date(start),
|
|
118
|
-
// Dernière milliseconde du créneau, comme les bougies servies par les venues.
|
|
119
|
-
closedAt: new Date(start + targetMs - 1),
|
|
120
|
-
derived: true
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
} else {
|
|
124
|
-
bucket.sources += 1;
|
|
125
|
-
const built = bucket.candle;
|
|
126
|
-
built.high = compareDecimals(candle.high, built.high) > 0 ? candle.high : built.high;
|
|
127
|
-
built.low = compareDecimals(candle.low, built.low) < 0 ? candle.low : built.low;
|
|
128
|
-
built.close = candle.close;
|
|
129
|
-
built.volume = sum(built.volume, candle.volume);
|
|
130
|
-
built.quoteVolume = sum(built.quoteVolume, candle.quoteVolume);
|
|
131
|
-
built.takerBuyQuoteVolume = sum(built.takerBuyQuoteVolume, candle.takerBuyQuoteVolume);
|
|
132
|
-
built.trades = built.trades === null || candle.trades === null ? null : built.trades + candle.trades;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
return [...buckets.keys()].sort((left, right) => left - right).map((start) => buckets.get(start)).filter((bucket) => bucket !== void 0).filter((bucket) => {
|
|
136
|
-
const stillOpen = bucket.candle.openedAt.getTime() + targetMs > now;
|
|
137
|
-
return bucket.sources >= expected || stillOpen === true;
|
|
138
|
-
}).map((bucket) => bucket.candle);
|
|
139
|
-
}
|
|
140
|
-
function sum(left, right) {
|
|
141
|
-
if (left === null || right === null) {
|
|
142
|
-
return null;
|
|
143
|
-
}
|
|
144
|
-
return addDecimals(left, right);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
22
|
// src/helpers/symbol-naming.ts
|
|
148
23
|
var CONCAT_QUOTE_XEXS = /* @__PURE__ */ new Set(["aster", "binance", "bybit"]);
|
|
149
24
|
var K_MULTIPLIER = /^k(?=[A-Z])/u;
|
|
@@ -219,15 +94,13 @@ function toCandle(wire, xex, interval, quote) {
|
|
|
219
94
|
};
|
|
220
95
|
}
|
|
221
96
|
|
|
222
|
-
// src/helpers/
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
limit: query.limit
|
|
230
|
-
};
|
|
97
|
+
// src/helpers/ws-limits.ts
|
|
98
|
+
var WS_SUBSCRIPTION_LIMITS = {
|
|
99
|
+
aster: 200,
|
|
100
|
+
hyperliquid: 1e3
|
|
101
|
+
};
|
|
102
|
+
function subscriptionLimitOf(xex) {
|
|
103
|
+
return WS_SUBSCRIPTION_LIMITS[xex];
|
|
231
104
|
}
|
|
232
105
|
|
|
233
106
|
// src/enums/xgate-ex.enums.ts
|
|
@@ -375,6 +248,282 @@ function createPublicSpotXex(xex) {
|
|
|
375
248
|
}
|
|
376
249
|
}
|
|
377
250
|
|
|
251
|
+
// src/services/candles-stream.service.ts
|
|
252
|
+
var CandlesStreamService = class {
|
|
253
|
+
constructor(xex, kind = "perp") {
|
|
254
|
+
this.xex = xex;
|
|
255
|
+
this.kind = kind;
|
|
256
|
+
this.logger = new Logger(`CandlesStream:${xex}${kind === "spot" ? ":spot" : ""}`);
|
|
257
|
+
this.maxSubscriptions = subscriptionLimitOf(xex) ?? null;
|
|
258
|
+
}
|
|
259
|
+
xex;
|
|
260
|
+
kind;
|
|
261
|
+
logger;
|
|
262
|
+
source = null;
|
|
263
|
+
unsubscribes = [];
|
|
264
|
+
/** Le plafond de souscriptions documenté par la venue, ou `null` si personne ne l'a publié. */
|
|
265
|
+
maxSubscriptions;
|
|
266
|
+
/**
|
|
267
|
+
* Ouvre le flux sur une LISTE de marchés, sur une seule socket.
|
|
268
|
+
*
|
|
269
|
+
* **UNE ERREUR DE FLUX COUPE TOUT, PUIS LÈVE.** Elle n'arrive pas à la souscription : mesuré le
|
|
270
|
+
* 2026-08-07, la venue accepte un symbole inconnu sans broncher, puis ferme la connexion en le
|
|
271
|
+
* découvrant. Le SDK reconnecte, elle referme — cinq cycles en douze secondes, et sur une socket
|
|
272
|
+
* partagée **tous** les marchés valides cessent de recevoir, en silence.
|
|
273
|
+
*
|
|
274
|
+
* On refuse cette boucle : à la première erreur, les souscriptions sont coupées, l'incident est
|
|
275
|
+
* journalisé en `error`, et l'exception part. Un flux à moitié mort qui se tait coûte plus cher
|
|
276
|
+
* qu'un arrêt franc — on ne découvre le premier qu'en constatant l'absence de données.
|
|
277
|
+
*
|
|
278
|
+
* ⚠️ L'exception naît dans un rappel de socket : elle ne remonte pas à l'appelant de `start()`,
|
|
279
|
+
* elle sort en erreur non capturée. C'est délibéré — elle doit être impossible à ignorer.
|
|
280
|
+
*
|
|
281
|
+
* **binance et bybit ne remontent pas encore leurs erreurs** : leur client gère la fermeture en
|
|
282
|
+
* interne sans l'exposer. Sur ces deux venues, la boucle silencieuse reste possible.
|
|
283
|
+
*/
|
|
284
|
+
start(symbolsXex, interval, onCandle) {
|
|
285
|
+
const ws = this.wire();
|
|
286
|
+
if ("onError" in ws) {
|
|
287
|
+
ws.onError = (error) => {
|
|
288
|
+
const raison = lisible(error);
|
|
289
|
+
this.logger.error(`${this.xex} : erreur de flux \u2014 ${raison}. Souscriptions coup\xE9es.`);
|
|
290
|
+
this.stop();
|
|
291
|
+
throw new Error(`stream(${this.xex}) : flux interrompu \u2014 ${raison}`);
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
for (const symbolXex of symbolsXex) {
|
|
295
|
+
this.unsubscribes.push(
|
|
296
|
+
ws.subscribeCandles({ name: symbolXex, interval }, (wire) => {
|
|
297
|
+
onCandle(toCandle(wire, this.xex, interval, this.quoteOf(symbolXex)));
|
|
298
|
+
})
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
this.logger.log(`${this.unsubscribes.length}/${symbolsXex.length} souscription(s) ouvertes`);
|
|
302
|
+
}
|
|
303
|
+
/** Coupe tout. Un désabonnement qui échoue ne doit pas empêcher les autres de se fermer. */
|
|
304
|
+
stop() {
|
|
305
|
+
for (const unsubscribe of this.unsubscribes.splice(0)) {
|
|
306
|
+
try {
|
|
307
|
+
unsubscribe();
|
|
308
|
+
} catch {
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
/** Le nombre de souscriptions ouvertes. La liste des désabonnements EST le compteur. */
|
|
313
|
+
subscriptionCount() {
|
|
314
|
+
return this.unsubscribes.length;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Le client temps réel, sur le bon marché.
|
|
318
|
+
*
|
|
319
|
+
* Les deux contrats ne signent pas `ws()` pareil, et c'est voulu : côté comptant le marché est
|
|
320
|
+
* **obligatoire** (`ws('spot')`), parce que les façades ouvrent du perpétuel par défaut — un
|
|
321
|
+
* `ws()` nu y diffuserait des prix de perpétuel à qui croit écouter le comptant.
|
|
322
|
+
*/
|
|
323
|
+
wire() {
|
|
324
|
+
const source = this.instance();
|
|
325
|
+
return this.kind === "spot" ? source.ws("spot") : source.ws();
|
|
326
|
+
}
|
|
327
|
+
/** La façade de la venue, créée une seule fois : c'est elle qui porte la socket partagée. */
|
|
328
|
+
instance() {
|
|
329
|
+
if (this.source === null) {
|
|
330
|
+
if (this.kind === "spot" && servesSpot(this.xex) === false) {
|
|
331
|
+
throw new Error(`stream(${this.xex}) : cette venue ne sert pas de comptant.`);
|
|
332
|
+
}
|
|
333
|
+
this.source = this.kind === "spot" ? createPublicSpotXex(this.xex) : createPublicXex(this.xex);
|
|
334
|
+
}
|
|
335
|
+
return this.source;
|
|
336
|
+
}
|
|
337
|
+
/** Voir `CandlesService.quoteOf` : la cotation se déduit du symbole natif, faute d'être publiée. */
|
|
338
|
+
quoteOf(symbolXex) {
|
|
339
|
+
const parts = symbolXex.split("-");
|
|
340
|
+
const last = parts[parts.length - 1];
|
|
341
|
+
return parts.length > 1 && last !== void 0 ? last : "";
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
var CandlesStreamRegistry = class {
|
|
345
|
+
streams = /* @__PURE__ */ new Map();
|
|
346
|
+
/** Le flux d'une venue, créé au premier appel puis réutilisé. */
|
|
347
|
+
of(xex, kind = "perp") {
|
|
348
|
+
const cle = `${xex}:${kind}`;
|
|
349
|
+
let stream = this.streams.get(cle);
|
|
350
|
+
if (stream === void 0) {
|
|
351
|
+
stream = new CandlesStreamService(xex, kind);
|
|
352
|
+
this.streams.set(cle, stream);
|
|
353
|
+
}
|
|
354
|
+
return stream;
|
|
355
|
+
}
|
|
356
|
+
/** Coupe tous les flux ouverts, toutes venues confondues. */
|
|
357
|
+
stopAll() {
|
|
358
|
+
for (const stream of this.streams.values()) {
|
|
359
|
+
stream.stop();
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
/** Ce qui est ouvert, par venue — pour surveiller sans avoir à tenir de compteur soi-même. */
|
|
363
|
+
counts() {
|
|
364
|
+
const total = {};
|
|
365
|
+
for (const [cle, stream] of this.streams) {
|
|
366
|
+
total[cle] = stream.subscriptionCount();
|
|
367
|
+
}
|
|
368
|
+
return total;
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
CandlesStreamRegistry = __decorateClass([
|
|
372
|
+
Injectable()
|
|
373
|
+
], CandlesStreamRegistry);
|
|
374
|
+
function lisible(error) {
|
|
375
|
+
if (error instanceof Error) {
|
|
376
|
+
return error.message;
|
|
377
|
+
}
|
|
378
|
+
const event = error;
|
|
379
|
+
if (event !== null && typeof event === "object") {
|
|
380
|
+
const message = event.message ?? event.error?.message;
|
|
381
|
+
if (typeof message === "string" && message !== "") {
|
|
382
|
+
return message;
|
|
383
|
+
}
|
|
384
|
+
if (typeof event.type === "string" && event.type !== "") {
|
|
385
|
+
return `\xE9v\xE9nement \xAB ${event.type} \xBB`;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return String(error);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// src/enums/timeframe.enums.ts
|
|
392
|
+
var Timeframe = /* @__PURE__ */ ((Timeframe2) => {
|
|
393
|
+
Timeframe2["M1"] = "1m";
|
|
394
|
+
Timeframe2["M5"] = "5m";
|
|
395
|
+
Timeframe2["M15"] = "15m";
|
|
396
|
+
Timeframe2["H1"] = "1h";
|
|
397
|
+
Timeframe2["H4"] = "4h";
|
|
398
|
+
Timeframe2["D"] = "1d";
|
|
399
|
+
Timeframe2["W"] = "1w";
|
|
400
|
+
return Timeframe2;
|
|
401
|
+
})(Timeframe || {});
|
|
402
|
+
var TIMEFRAME_MINUTES = {
|
|
403
|
+
["1m" /* M1 */]: 1,
|
|
404
|
+
["5m" /* M5 */]: 5,
|
|
405
|
+
["15m" /* M15 */]: 15,
|
|
406
|
+
["1h" /* H1 */]: 60,
|
|
407
|
+
["4h" /* H4 */]: 240,
|
|
408
|
+
["1d" /* D */]: 1440,
|
|
409
|
+
["1w" /* W */]: 10080
|
|
410
|
+
};
|
|
411
|
+
function timeframeMs(timeframe) {
|
|
412
|
+
return TIMEFRAME_MINUTES[timeframe] * 6e4;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// src/helpers/decimal.ts
|
|
416
|
+
function parse(value) {
|
|
417
|
+
const trimmed = value.trim();
|
|
418
|
+
if (/^-?\d+(\.\d+)?$/.test(trimmed) === false) {
|
|
419
|
+
throw new Error(`D\xE9cimal illisible : "${value}".`);
|
|
420
|
+
}
|
|
421
|
+
const dot = trimmed.indexOf(".");
|
|
422
|
+
if (dot < 0) {
|
|
423
|
+
return { digits: BigInt(trimmed), scale: 0 };
|
|
424
|
+
}
|
|
425
|
+
return {
|
|
426
|
+
digits: BigInt(trimmed.slice(0, dot) + trimmed.slice(dot + 1)),
|
|
427
|
+
scale: trimmed.length - dot - 1
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
function format(digits, scale) {
|
|
431
|
+
const negative = digits < 0n;
|
|
432
|
+
const plain = (negative === true ? -digits : digits).toString().padStart(scale + 1, "0");
|
|
433
|
+
const sign = negative === true ? "-" : "";
|
|
434
|
+
if (scale === 0) {
|
|
435
|
+
return sign + plain;
|
|
436
|
+
}
|
|
437
|
+
const whole = plain.slice(0, plain.length - scale);
|
|
438
|
+
const fraction = plain.slice(plain.length - scale).replace(/0+$/, "");
|
|
439
|
+
return fraction === "" ? sign + whole : `${sign + whole}.${fraction}`;
|
|
440
|
+
}
|
|
441
|
+
function addDecimals(left, right) {
|
|
442
|
+
const a = parse(left);
|
|
443
|
+
const b = parse(right);
|
|
444
|
+
const scale = Math.max(a.scale, b.scale);
|
|
445
|
+
const digits = a.digits * 10n ** BigInt(scale - a.scale) + b.digits * 10n ** BigInt(scale - b.scale);
|
|
446
|
+
return format(digits, scale);
|
|
447
|
+
}
|
|
448
|
+
function compareDecimals(left, right) {
|
|
449
|
+
const a = parse(left);
|
|
450
|
+
const b = parse(right);
|
|
451
|
+
const scale = Math.max(a.scale, b.scale);
|
|
452
|
+
const x = a.digits * 10n ** BigInt(scale - a.scale);
|
|
453
|
+
const y = b.digits * 10n ** BigInt(scale - b.scale);
|
|
454
|
+
if (x === y) {
|
|
455
|
+
return 0;
|
|
456
|
+
}
|
|
457
|
+
return x > y ? 1 : -1;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// src/helpers/candle-aggregator.ts
|
|
461
|
+
var MONDAY_OFFSET_MS = 4 * 24 * 60 * 60 * 1e3;
|
|
462
|
+
function aggregate(source, target, now = Date.now()) {
|
|
463
|
+
if (source.length === 0) {
|
|
464
|
+
return [];
|
|
465
|
+
}
|
|
466
|
+
const sourceMs = timeframeMs(source[0]?.interval ?? "1m" /* M1 */);
|
|
467
|
+
const targetMs = timeframeMs(target);
|
|
468
|
+
if (targetMs <= sourceMs) {
|
|
469
|
+
return [];
|
|
470
|
+
}
|
|
471
|
+
const offsetMs = target === "1w" /* W */ ? MONDAY_OFFSET_MS : 0;
|
|
472
|
+
const expected = targetMs / sourceMs;
|
|
473
|
+
const buckets = /* @__PURE__ */ new Map();
|
|
474
|
+
const sorted = [...source].sort(
|
|
475
|
+
(left, right) => left.openedAt.getTime() - right.openedAt.getTime()
|
|
476
|
+
);
|
|
477
|
+
for (const candle of sorted) {
|
|
478
|
+
const start = Math.floor((candle.openedAt.getTime() - offsetMs) / targetMs) * targetMs + offsetMs;
|
|
479
|
+
const bucket = buckets.get(start);
|
|
480
|
+
if (bucket === void 0) {
|
|
481
|
+
buckets.set(start, {
|
|
482
|
+
sources: 1,
|
|
483
|
+
candle: {
|
|
484
|
+
...candle,
|
|
485
|
+
interval: target,
|
|
486
|
+
openedAt: new Date(start),
|
|
487
|
+
// Dernière milliseconde du créneau, comme les bougies servies par les venues.
|
|
488
|
+
closedAt: new Date(start + targetMs - 1),
|
|
489
|
+
derived: true
|
|
490
|
+
}
|
|
491
|
+
});
|
|
492
|
+
} else {
|
|
493
|
+
bucket.sources += 1;
|
|
494
|
+
const built = bucket.candle;
|
|
495
|
+
built.high = compareDecimals(candle.high, built.high) > 0 ? candle.high : built.high;
|
|
496
|
+
built.low = compareDecimals(candle.low, built.low) < 0 ? candle.low : built.low;
|
|
497
|
+
built.close = candle.close;
|
|
498
|
+
built.volume = sum(built.volume, candle.volume);
|
|
499
|
+
built.quoteVolume = sum(built.quoteVolume, candle.quoteVolume);
|
|
500
|
+
built.takerBuyQuoteVolume = sum(built.takerBuyQuoteVolume, candle.takerBuyQuoteVolume);
|
|
501
|
+
built.trades = built.trades === null || candle.trades === null ? null : built.trades + candle.trades;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
return [...buckets.keys()].sort((left, right) => left - right).map((start) => buckets.get(start)).filter((bucket) => bucket !== void 0).filter((bucket) => {
|
|
505
|
+
const stillOpen = bucket.candle.openedAt.getTime() + targetMs > now;
|
|
506
|
+
return bucket.sources >= expected || stillOpen === true;
|
|
507
|
+
}).map((bucket) => bucket.candle);
|
|
508
|
+
}
|
|
509
|
+
function sum(left, right) {
|
|
510
|
+
if (left === null || right === null) {
|
|
511
|
+
return null;
|
|
512
|
+
}
|
|
513
|
+
return addDecimals(left, right);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// src/helpers/candle-query.ts
|
|
517
|
+
function toXexQuery(query) {
|
|
518
|
+
return {
|
|
519
|
+
name: query.symbol,
|
|
520
|
+
interval: query.interval,
|
|
521
|
+
startTime: query.startTime,
|
|
522
|
+
endTime: query.endTime,
|
|
523
|
+
limit: query.limit
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
|
|
378
527
|
// src/helpers/xex-timeframes.ts
|
|
379
528
|
var HYPERLIQUID_NATIVE = Object.values(Timeframe).filter(
|
|
380
529
|
(interval) => interval !== "1w" /* W */
|
|
@@ -796,6 +945,133 @@ var SpotCatalogService = class {
|
|
|
796
945
|
SpotCatalogService = __decorateClass([
|
|
797
946
|
Injectable()
|
|
798
947
|
], SpotCatalogService);
|
|
948
|
+
var SpotPricesService = class {
|
|
949
|
+
logger = new Logger(SpotPricesService.name);
|
|
950
|
+
/** Les venues dont XGate sert les prix comptant. */
|
|
951
|
+
venues() {
|
|
952
|
+
return SPOT_XEXES;
|
|
953
|
+
}
|
|
954
|
+
/**
|
|
955
|
+
* Les prix comptant d'une ou plusieurs venues, en une seule liste.
|
|
956
|
+
*
|
|
957
|
+
* Même politique d'échec que partout : seule, une venue en échec fait échouer l'appel ; parmi
|
|
958
|
+
* d'autres, elle est journalisée et ignorée. Chaque prix porte son `xex`.
|
|
959
|
+
*/
|
|
960
|
+
async prices(...xexes) {
|
|
961
|
+
if (xexes.length === 0) {
|
|
962
|
+
throw new Error("spot prices() : aucune venue demand\xE9e \u2014 pr\xE9cise au moins un XgateEx.");
|
|
963
|
+
}
|
|
964
|
+
const solo = xexes.length === 1;
|
|
965
|
+
const collected = await Promise.all(
|
|
966
|
+
xexes.map(async (xex) => {
|
|
967
|
+
try {
|
|
968
|
+
return await this.pricesOf(xex);
|
|
969
|
+
} catch (error) {
|
|
970
|
+
if (solo === true) {
|
|
971
|
+
throw error;
|
|
972
|
+
}
|
|
973
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
974
|
+
this.logger.error(`spot prices(${xex}) a \xE9chou\xE9, venue ignor\xE9e : ${message}`);
|
|
975
|
+
return [];
|
|
976
|
+
}
|
|
977
|
+
})
|
|
978
|
+
);
|
|
979
|
+
return collected.flat();
|
|
980
|
+
}
|
|
981
|
+
/** Les prix comptant d'UNE venue, traduits vers {@link IPrice}. */
|
|
982
|
+
async pricesOf(xex) {
|
|
983
|
+
if (servesSpot(xex) === false) {
|
|
984
|
+
throw new Error(
|
|
985
|
+
`spot prices(${xex}) : cette venue ne sert pas de comptant. Disponibles : ${SPOT_XEXES.join(", ")}.`
|
|
986
|
+
);
|
|
987
|
+
}
|
|
988
|
+
const spot = createPublicSpotXex(xex).spot();
|
|
989
|
+
if (spot.getPrices === void 0) {
|
|
990
|
+
throw new Error(`spot prices(${xex}) : cette venue ne sert pas de prix comptant.`);
|
|
991
|
+
}
|
|
992
|
+
const prices = await spot.getPrices();
|
|
993
|
+
return prices.map((price) => ({
|
|
994
|
+
// `canonicalFromXex`, et non `canonicalFromBase` : les prix ne portent QUE le symbole
|
|
995
|
+
// concaténé (`BTCUSDT`), jamais la base — contrairement au catalogue, où la venue déclare
|
|
996
|
+
// `baseAsset`. Passer le symbole à `canonicalFromBase` rendrait `BTCUSDT` tel quel, puisque
|
|
997
|
+
// cette fonction ne strippe volontairement aucune cotation.
|
|
998
|
+
//
|
|
999
|
+
// ⚠️ La limite est connue et assumée : le strip ne retire que `USDT`/`USDC` finaux, donc une
|
|
1000
|
+
// paire cotée en BTC (`ROSEBTC`, `ETHBTC`) garde son nom entier. Le catalogue comptant, lui,
|
|
1001
|
+
// résout ces cas — c'est là qu'il faut aller chercher `symbol` et `quote` justes.
|
|
1002
|
+
symbol: canonicalFromXex(price.name, xex),
|
|
1003
|
+
symbolXex: price.name,
|
|
1004
|
+
xex,
|
|
1005
|
+
kind: price.kind,
|
|
1006
|
+
mark: price.mark,
|
|
1007
|
+
oracle: price.oracle,
|
|
1008
|
+
mid: price.mid,
|
|
1009
|
+
bid: price.bid,
|
|
1010
|
+
ask: price.ask,
|
|
1011
|
+
last: price.last,
|
|
1012
|
+
funding: price.funding,
|
|
1013
|
+
openInterest: price.openInterest,
|
|
1014
|
+
volume24h: price.volume24h,
|
|
1015
|
+
prevDayPrice: price.prevDayPrice,
|
|
1016
|
+
quotedAt: price.quotedAt,
|
|
1017
|
+
xtras: price.xtras
|
|
1018
|
+
}));
|
|
1019
|
+
}
|
|
1020
|
+
};
|
|
1021
|
+
SpotPricesService = __decorateClass([
|
|
1022
|
+
Injectable()
|
|
1023
|
+
], SpotPricesService);
|
|
1024
|
+
var SpotWsCandlesService = class {
|
|
1025
|
+
logger = new Logger(SpotWsCandlesService.name);
|
|
1026
|
+
/** Les venues dont XGate diffuse le comptant en temps réel. */
|
|
1027
|
+
venues() {
|
|
1028
|
+
return SPOT_XEXES;
|
|
1029
|
+
}
|
|
1030
|
+
/**
|
|
1031
|
+
* Souscrit aux bougies **comptant** d'un marché. Rend la fonction de désabonnement.
|
|
1032
|
+
*
|
|
1033
|
+
* Le handler reçoit une bougie à la fois, au format unifié ; la bougie en cours est repoussée à
|
|
1034
|
+
* chaque mise à jour tant qu'elle n'est pas close.
|
|
1035
|
+
*/
|
|
1036
|
+
subscribe(xex, query, handler) {
|
|
1037
|
+
if (servesSpot(xex) === false) {
|
|
1038
|
+
throw new Error(
|
|
1039
|
+
`spot subscribe(${xex}) : cette venue ne sert pas de comptant. Disponibles : ${SPOT_XEXES.join(", ")}.`
|
|
1040
|
+
);
|
|
1041
|
+
}
|
|
1042
|
+
this.logger.log(`souscription bougies COMPTANT ${query.symbol} ${query.interval} sur ${xex}`);
|
|
1043
|
+
return createPublicSpotXex(xex).ws("spot").subscribeCandles({ name: query.symbol, interval: query.interval }, (wire) => {
|
|
1044
|
+
handler(toCandle(wire, xex, query.interval, ""));
|
|
1045
|
+
});
|
|
1046
|
+
}
|
|
1047
|
+
/**
|
|
1048
|
+
* Souscrit au MÊME marché comptant chez plusieurs venues, avec un seul handler.
|
|
1049
|
+
*
|
|
1050
|
+
* Chaque bougie porte son `xex`. La fonction rendue coupe tous les flux d'un coup.
|
|
1051
|
+
*/
|
|
1052
|
+
subscribeAll(xexes, query, handler) {
|
|
1053
|
+
if (xexes.length === 0) {
|
|
1054
|
+
throw new Error("spot subscribeAll() : aucune venue demand\xE9e \u2014 pr\xE9cise au moins un XgateEx.");
|
|
1055
|
+
}
|
|
1056
|
+
const stops = [];
|
|
1057
|
+
for (const xex of xexes) {
|
|
1058
|
+
try {
|
|
1059
|
+
stops.push(this.subscribe(xex, query, handler));
|
|
1060
|
+
} catch (error) {
|
|
1061
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1062
|
+
this.logger.error(`souscription comptant ${xex} refus\xE9e, venue ignor\xE9e : ${message}`);
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
return () => {
|
|
1066
|
+
for (const stop of stops) {
|
|
1067
|
+
stop();
|
|
1068
|
+
}
|
|
1069
|
+
};
|
|
1070
|
+
}
|
|
1071
|
+
};
|
|
1072
|
+
SpotWsCandlesService = __decorateClass([
|
|
1073
|
+
Injectable()
|
|
1074
|
+
], SpotWsCandlesService);
|
|
799
1075
|
|
|
800
1076
|
// src/helpers/protection.ts
|
|
801
1077
|
var SLIPPAGE_DEFAUT = 5e-3;
|
|
@@ -1340,24 +1616,30 @@ XgateModule = __decorateClass([
|
|
|
1340
1616
|
providers: [
|
|
1341
1617
|
CatalogService,
|
|
1342
1618
|
CandlesService,
|
|
1619
|
+
CandlesStreamRegistry,
|
|
1343
1620
|
WsCandlesService,
|
|
1344
1621
|
PricesService,
|
|
1345
1622
|
SpotCatalogService,
|
|
1346
1623
|
SpotCandlesService,
|
|
1624
|
+
SpotPricesService,
|
|
1625
|
+
SpotWsCandlesService,
|
|
1347
1626
|
WalletService
|
|
1348
1627
|
],
|
|
1349
1628
|
exports: [
|
|
1350
1629
|
CatalogService,
|
|
1351
1630
|
CandlesService,
|
|
1631
|
+
CandlesStreamRegistry,
|
|
1352
1632
|
WsCandlesService,
|
|
1353
1633
|
PricesService,
|
|
1354
1634
|
SpotCatalogService,
|
|
1355
1635
|
SpotCandlesService,
|
|
1636
|
+
SpotPricesService,
|
|
1637
|
+
SpotWsCandlesService,
|
|
1356
1638
|
WalletService
|
|
1357
1639
|
]
|
|
1358
1640
|
})
|
|
1359
1641
|
], XgateModule);
|
|
1360
1642
|
|
|
1361
|
-
export { CandlesService, CatalogService, ORDER_STATUSES, PricesService, SPOT_XEXES, SpotCandlesService, SpotCatalogService, TIMEFRAME_MINUTES, Timeframe, TradingService, WALLET_XEXES, WalletService, WsCandlesService, XGATE_EXCHANGES, XgateEx, XgateModule, buildLevels, buildProtection, canonicalFromBase, canonicalFromXex, expiryOf, isDated, multiplierFromXex, roundPrice, roundToStep, servesNatively, servesSpot, sourceFor, timeframeMs };
|
|
1643
|
+
export { CandlesService, CandlesStreamRegistry, CandlesStreamService, CatalogService, ORDER_STATUSES, PricesService, SPOT_XEXES, SpotCandlesService, SpotCatalogService, SpotPricesService, SpotWsCandlesService, TIMEFRAME_MINUTES, Timeframe, TradingService, WALLET_XEXES, WS_SUBSCRIPTION_LIMITS, WalletService, WsCandlesService, XGATE_EXCHANGES, XgateEx, XgateModule, buildLevels, buildProtection, canonicalFromBase, canonicalFromXex, expiryOf, isDated, multiplierFromXex, roundPrice, roundToStep, servesNatively, servesSpot, sourceFor, subscriptionLimitOf, timeframeMs };
|
|
1362
1644
|
//# sourceMappingURL=index.js.map
|
|
1363
1645
|
//# sourceMappingURL=index.js.map
|