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