@pear-protocol/symmio-client 0.3.15 → 0.3.16

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.
@@ -75,6 +75,7 @@ var RECONNECT_DELAYS = [1e3, 2e3, 4e3, 8e3, 16e3, 3e4];
75
75
  var IDLE_CLOSE_DELAY_MS = 3e4;
76
76
  var STALE_CONNECTION_MS = 3e4;
77
77
  var STALE_CHECK_INTERVAL_MS = 1e4;
78
+ var RECONNECT_RESET_MS = 6e4;
78
79
  var STABLE_QUOTES = ["USDT0", "USDT", "USDC", "USDE", "USDH", "USD"];
79
80
  function normalizeBaseSymbol(symbol) {
80
81
  const normalized = symbol.toUpperCase().trim();
@@ -126,6 +127,7 @@ var BinanceWsManager = class {
126
127
  reconnectTimer = null;
127
128
  idleCloseTimer = null;
128
129
  staleCheckTimer = null;
130
+ reconnectResetTimer = null;
129
131
  intentionalClose = false;
130
132
  pendingSubscribes = /* @__PURE__ */ new Set();
131
133
  lastMessageAt = 0;
@@ -164,6 +166,7 @@ var BinanceWsManager = class {
164
166
  const wrappedCb = (raw) => {
165
167
  cb({
166
168
  symbol: normalizeBaseSymbol(raw.s),
169
+ binanceSymbol: raw.s,
167
170
  markPrice: parseFloat(raw.p),
168
171
  indexPrice: parseFloat(raw.i),
169
172
  time: raw.E,
@@ -185,6 +188,7 @@ var BinanceWsManager = class {
185
188
  cb(
186
189
  extractTickers(raw).map((entry) => ({
187
190
  symbol: normalizeBaseSymbol(entry.s),
191
+ binanceSymbol: entry.s,
188
192
  markPrice: parseFloat(entry.p),
189
193
  indexPrice: parseFloat(entry.i),
190
194
  time: entry.E,
@@ -204,6 +208,7 @@ var BinanceWsManager = class {
204
208
  this.clearReconnectTimer();
205
209
  this.clearIdleCloseTimer();
206
210
  this.clearStaleCheckTimer();
211
+ this.clearReconnectResetTimer();
207
212
  this.pendingSubscribes.clear();
208
213
  if (this.ws) {
209
214
  this.ws.close();
@@ -257,9 +262,9 @@ var BinanceWsManager = class {
257
262
  this.intentionalClose = false;
258
263
  this.ws = new WebSocket(BINANCE_WS_URL);
259
264
  this.ws.onopen = () => {
260
- this.reconnectAttempt = 0;
261
265
  this.lastMessageAt = Date.now();
262
266
  this.startStaleCheck();
267
+ this.scheduleReconnectReset();
263
268
  const activeStreams = Array.from(
264
269
  /* @__PURE__ */ new Set([...this.streams.keys(), ...this.pendingSubscribes])
265
270
  );
@@ -272,6 +277,7 @@ var BinanceWsManager = class {
272
277
  try {
273
278
  const data = JSON.parse(event.data);
274
279
  this.lastMessageAt = Date.now();
280
+ this.resetReconnectAttempt();
275
281
  this.handleMessage(data);
276
282
  } catch {
277
283
  }
@@ -281,6 +287,7 @@ var BinanceWsManager = class {
281
287
  this.intentionalClose = false;
282
288
  this.ws = null;
283
289
  this.clearStaleCheckTimer();
290
+ this.clearReconnectResetTimer();
284
291
  if (this.streams.size === 0) {
285
292
  return;
286
293
  }
@@ -374,6 +381,17 @@ var BinanceWsManager = class {
374
381
  this.ws.close();
375
382
  }, STALE_CHECK_INTERVAL_MS);
376
383
  }
384
+ scheduleReconnectReset() {
385
+ this.clearReconnectResetTimer();
386
+ this.reconnectResetTimer = setTimeout(() => {
387
+ this.reconnectResetTimer = null;
388
+ this.resetReconnectAttempt();
389
+ }, RECONNECT_RESET_MS);
390
+ }
391
+ resetReconnectAttempt() {
392
+ this.reconnectAttempt = 0;
393
+ this.clearReconnectResetTimer();
394
+ }
377
395
  clearReconnectTimer() {
378
396
  if (!this.reconnectTimer) return;
379
397
  clearTimeout(this.reconnectTimer);
@@ -389,6 +407,11 @@ var BinanceWsManager = class {
389
407
  clearInterval(this.staleCheckTimer);
390
408
  this.staleCheckTimer = null;
391
409
  }
410
+ clearReconnectResetTimer() {
411
+ if (!this.reconnectResetTimer) return;
412
+ clearTimeout(this.reconnectResetTimer);
413
+ this.reconnectResetTimer = null;
414
+ }
392
415
  };
393
416
  var BINANCE_WS_SINGLETON_KEY = "__pearBinanceWsManager__";
394
417
  function getBinanceWsManager() {
@@ -406,15 +429,8 @@ var refCounts = /* @__PURE__ */ new Map();
406
429
  var streamSymbols = /* @__PURE__ */ new Map();
407
430
  var allMarkPricesRefCount = 0;
408
431
  var allMarkPricesUnsubscribe = null;
409
- var STABLE_QUOTES2 = ["USDT0", "USDT", "USDC", "USDE", "USDH", "USD"];
410
- function normalizeBinanceSymbol(symbol) {
411
- const normalized = symbol.toUpperCase().trim();
412
- for (const quote of STABLE_QUOTES2) {
413
- if (normalized.endsWith(quote) && normalized.length > quote.length) {
414
- return normalized.slice(0, -quote.length);
415
- }
416
- }
417
- return normalized;
432
+ function normalizeBinanceSymbolKey(symbol) {
433
+ return symbol.toUpperCase().trim();
418
434
  }
419
435
  function getNextRefCount(binanceSymbol) {
420
436
  return (refCounts.get(binanceSymbol) ?? 0) + 1;
@@ -449,7 +465,7 @@ var useBinanceMarkPriceStore = zustand.create((set) => ({
449
465
  fundingRates: {},
450
466
  nextFundingTimes: {},
451
467
  subscribeSymbol: (symmSymbol, rawBinanceSymbol) => {
452
- const binanceSymbol = normalizeBinanceSymbol(rawBinanceSymbol);
468
+ const binanceSymbol = normalizeBinanceSymbolKey(rawBinanceSymbol);
453
469
  const nextRefCount = getNextRefCount(binanceSymbol);
454
470
  refCounts.set(binanceSymbol, nextRefCount);
455
471
  addMappedSymbol(binanceSymbol, symmSymbol);
@@ -461,8 +477,10 @@ var useBinanceMarkPriceStore = zustand.create((set) => ({
461
477
  let nextFundingRates = null;
462
478
  let nextFundingTimes = null;
463
479
  entries.forEach((entry) => {
464
- const canonicalSymbol = normalizeBinanceSymbol(entry.symbol);
465
- const mappedSymbols = streamSymbols.get(canonicalSymbol);
480
+ const binanceSymbolKey = normalizeBinanceSymbolKey(
481
+ entry.binanceSymbol ?? entry.symbol
482
+ );
483
+ const mappedSymbols = streamSymbols.get(binanceSymbolKey);
466
484
  if (!mappedSymbols || mappedSymbols.size === 0) return;
467
485
  nextMarkPrices ??= { ...state.markPrices };
468
486
  nextFundingRates ??= { ...state.fundingRates };
@@ -487,7 +505,7 @@ var useBinanceMarkPriceStore = zustand.create((set) => ({
487
505
  allMarkPricesRefCount += 1;
488
506
  },
489
507
  unsubscribeSymbol: (symmSymbol, rawBinanceSymbol) => {
490
- const binanceSymbol = normalizeBinanceSymbol(rawBinanceSymbol);
508
+ const binanceSymbol = normalizeBinanceSymbolKey(rawBinanceSymbol);
491
509
  const removedSubscription = removeMappedSymbol(binanceSymbol, symmSymbol);
492
510
  if (!removedSubscription) {
493
511
  return;
@@ -527,6 +545,7 @@ var useBinanceMarkPriceStore = zustand.create((set) => ({
527
545
  }));
528
546
 
529
547
  // src/react/hooks/use-binance-ws.ts
548
+ var BOOTSTRAP_RETRY_DELAYS_MS = [1e3, 2e3, 5e3];
530
549
  function useBinanceWs(params) {
531
550
  const { symmCoreClient, chainId } = params;
532
551
  const subscribeSymbol = useBinanceMarkPriceStore((state) => state.subscribeSymbol);
@@ -537,7 +556,8 @@ function useBinanceWs(params) {
537
556
  }
538
557
  let cancelled = false;
539
558
  let subscribedPairs = [];
540
- const run = async () => {
559
+ let retryTimer = null;
560
+ const run = async (attempt = 0) => {
541
561
  try {
542
562
  const result = await symmCoreClient.markets.listSymmHedger({ chainId });
543
563
  if (cancelled) {
@@ -557,11 +577,22 @@ function useBinanceWs(params) {
557
577
  subscribeSymbol(symbol, binanceSymbol);
558
578
  });
559
579
  } catch {
580
+ const delay = BOOTSTRAP_RETRY_DELAYS_MS[attempt];
581
+ if (cancelled || delay == null) {
582
+ return;
583
+ }
584
+ retryTimer = setTimeout(() => {
585
+ retryTimer = null;
586
+ void run(attempt + 1);
587
+ }, delay);
560
588
  }
561
589
  };
562
590
  void run();
563
591
  return () => {
564
592
  cancelled = true;
593
+ if (retryTimer) {
594
+ clearTimeout(retryTimer);
595
+ }
565
596
  subscribedPairs.forEach(([symbol, binanceSymbol]) => {
566
597
  unsubscribeSymbol(symbol, binanceSymbol);
567
598
  });
@@ -25895,15 +25926,24 @@ function readTimestamp(source) {
25895
25926
  const parsed = Number(timestamp);
25896
25927
  return Number.isFinite(parsed) ? parsed : void 0;
25897
25928
  }
25929
+ function hasAccountDataShape(value) {
25930
+ return isRecord(value) && ("equity" in value || "maintenanceMargin" in value || "availableForOrder" in value || "totalLocked" in value);
25931
+ }
25898
25932
  function getSymmAccountData(response) {
25899
25933
  if (!isRecord(response)) {
25900
25934
  return void 0;
25901
25935
  }
25902
25936
  const data = response.data;
25903
25937
  if (isRecord(data)) {
25904
- return data;
25938
+ if (hasAccountDataShape(data.accountData)) {
25939
+ return data.accountData;
25940
+ }
25941
+ if (hasAccountDataShape(data)) {
25942
+ return data;
25943
+ }
25944
+ return void 0;
25905
25945
  }
25906
- if ("equity" in response || "maintenanceMargin" in response || "availableForOrder" in response || "totalLocked" in response) {
25946
+ if (hasAccountDataShape(response)) {
25907
25947
  return response;
25908
25948
  }
25909
25949
  return void 0;