@pear-protocol/symmio-client 0.2.28 → 0.2.29

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.
@@ -69,7 +69,7 @@ function toBinanceSymbol(symmSymbol) {
69
69
  }
70
70
 
71
71
  // src/utils/binance-ws.ts
72
- var BINANCE_WS_URL = "wss://fstream.binance.com/ws";
72
+ var BINANCE_WS_URL = "wss://fstream.binance.com/market/ws";
73
73
  var RECONNECT_DELAYS = [1e3, 2e3, 4e3, 8e3, 16e3, 3e4];
74
74
  var STABLE_QUOTES = ["USDT0", "USDT", "USDC", "USDE", "USDH", "USD"];
75
75
  function normalizeBaseSymbol(symbol) {
@@ -99,6 +99,22 @@ function extractTickers(payload) {
99
99
  }
100
100
  return [];
101
101
  }
102
+ function extractWsPayload(payload) {
103
+ if (payload && typeof payload === "object" && "data" in payload) {
104
+ return payload.data ?? payload;
105
+ }
106
+ return payload;
107
+ }
108
+ function isKlinePayload(payload) {
109
+ return Boolean(
110
+ payload && typeof payload === "object" && payload.e === "kline" && typeof payload.s === "string" && payload.k && typeof payload.k.i === "string"
111
+ );
112
+ }
113
+ function isMarkPricePayload(payload) {
114
+ return Boolean(
115
+ payload && typeof payload === "object" && payload.e === "markPriceUpdate" && typeof payload.s === "string"
116
+ );
117
+ }
102
118
  var BinanceWsManager = class {
103
119
  ws = null;
104
120
  streams = /* @__PURE__ */ new Map();
@@ -252,17 +268,18 @@ var BinanceWsManager = class {
252
268
  };
253
269
  }
254
270
  handleMessage(data) {
255
- if (Array.isArray(data)) {
256
- this.dispatchToStream("!markPrice@arr@1s", data);
271
+ const payload = extractWsPayload(data);
272
+ if (Array.isArray(payload)) {
273
+ this.dispatchToStream("!markPrice@arr@1s", payload);
257
274
  return;
258
275
  }
259
- if (data.e === "kline") {
260
- const k = data.k;
261
- const streamName = `${data.s.toLowerCase()}@kline_${k.i}`;
262
- this.dispatchToStream(streamName, data);
263
- } else if (data.e === "markPriceUpdate") {
264
- const streamName = `${data.s.toLowerCase()}@markPrice@1s`;
265
- this.dispatchToStream(streamName, data);
276
+ if (isKlinePayload(payload)) {
277
+ const k = payload.k;
278
+ const streamName = `${payload.s.toLowerCase()}@kline_${k.i}`;
279
+ this.dispatchToStream(streamName, payload);
280
+ } else if (isMarkPricePayload(payload)) {
281
+ const streamName = `${payload.s.toLowerCase()}@markPrice@1s`;
282
+ this.dispatchToStream(streamName, payload);
266
283
  }
267
284
  }
268
285
  dispatchToStream(streamName, data) {
@@ -25435,7 +25452,7 @@ function useSymmHedgerMarkets(params) {
25435
25452
 
25436
25453
  // src/utils/binance-api.ts
25437
25454
  var BINANCE_FAPI_BASE = "https://fapi.binance.com";
25438
- async function fetchMarkPriceKlines(symbol, interval, startTime, endTime, limit = 1500) {
25455
+ async function fetchKlines(symbol, interval, startTime, endTime, limit = 1500) {
25439
25456
  const params = new URLSearchParams({
25440
25457
  symbol,
25441
25458
  interval,
@@ -25443,9 +25460,9 @@ async function fetchMarkPriceKlines(symbol, interval, startTime, endTime, limit
25443
25460
  endTime: String(endTime),
25444
25461
  limit: String(Math.min(limit, 1500))
25445
25462
  });
25446
- const response = await fetch(`${BINANCE_FAPI_BASE}/fapi/v1/markPriceKlines?${params}`);
25463
+ const response = await fetch(`${BINANCE_FAPI_BASE}/fapi/v1/klines?${params}`);
25447
25464
  if (!response.ok) {
25448
- throw new Error(`Binance markPriceKlines failed: ${response.status}`);
25465
+ throw new Error(`Binance klines failed: ${response.status}`);
25449
25466
  }
25450
25467
  const data = await response.json();
25451
25468
  return data.map((k) => ({
@@ -25465,7 +25482,7 @@ async function fetch24hrTicker(symbol) {
25465
25482
  const data = await response.json();
25466
25483
  return {
25467
25484
  lastPrice: parseFloat(data.lastPrice),
25468
- prevClosePrice: parseFloat(data.prevClosePrice),
25485
+ openPrice: parseFloat(data.openPrice),
25469
25486
  priceChangePercent: parseFloat(data.priceChangePercent)
25470
25487
  };
25471
25488
  }
@@ -25479,7 +25496,7 @@ async function fetch24hrTickers() {
25479
25496
  for (const item of data) {
25480
25497
  result[item.symbol] = {
25481
25498
  lastPrice: parseFloat(item.lastPrice),
25482
- prevClosePrice: parseFloat(item.prevClosePrice),
25499
+ openPrice: parseFloat(item.openPrice),
25483
25500
  priceChangePercent: parseFloat(item.priceChangePercent)
25484
25501
  };
25485
25502
  }
@@ -25515,7 +25532,7 @@ function useSymmTokenSelectionMarkets(params) {
25515
25532
  return;
25516
25533
  }
25517
25534
  const ticker = allTickers[binanceSymbol];
25518
- tickerSnapshots[symbol] = ticker ? { prevClosePrice: ticker.prevClosePrice } : null;
25535
+ tickerSnapshots[symbol] = ticker ? { openPrice: ticker.openPrice } : null;
25519
25536
  });
25520
25537
  return { tickerSnapshots };
25521
25538
  },
@@ -25528,7 +25545,7 @@ function useSymmTokenSelectionMarkets(params) {
25528
25545
  return baseMarkets.map((market) => {
25529
25546
  const symbol = market.symbol ?? "";
25530
25547
  const markPrice = symbol ? liveMarkPrices[symbol] ?? null : null;
25531
- const prevDayPrice = symbol ? snapshots[symbol]?.prevClosePrice ?? null : null;
25548
+ const prevDayPrice = symbol ? snapshots[symbol]?.openPrice ?? null : null;
25532
25549
  const priceChange24h = markPrice != null && prevDayPrice != null ? markPrice - prevDayPrice : null;
25533
25550
  const priceChange24hPercent = markPrice != null && prevDayPrice != null && prevDayPrice !== 0 ? (markPrice - prevDayPrice) / prevDayPrice * 100 : null;
25534
25551
  return {
@@ -26146,7 +26163,7 @@ async function fetchTickerSnapshot(symbol) {
26146
26163
  const ticker = await fetch24hrTicker(resolution.binanceSymbol);
26147
26164
  if (!ticker) return null;
26148
26165
  return {
26149
- prevClosePrice: ticker.prevClosePrice
26166
+ openPrice: ticker.openPrice
26150
26167
  };
26151
26168
  }
26152
26169
  function toSymmTokenMetadata(currentPrice, prevDayPrice) {
@@ -26206,12 +26223,12 @@ function useSymmTokenSelectionMetadata(selection, options = {}) {
26206
26223
  for (const token of longTokens) {
26207
26224
  const currentPrice = liveMarkPrices[token.symbol];
26208
26225
  const ticker = tickerSnapshots[token.symbol];
26209
- longMeta[token.symbol] = currentPrice != null && ticker ? toSymmTokenMetadata(currentPrice, ticker.prevClosePrice) : null;
26226
+ longMeta[token.symbol] = currentPrice != null && ticker ? toSymmTokenMetadata(currentPrice, ticker.openPrice) : null;
26210
26227
  }
26211
26228
  for (const token of shortTokens) {
26212
26229
  const currentPrice = liveMarkPrices[token.symbol];
26213
26230
  const ticker = tickerSnapshots[token.symbol];
26214
- shortMeta[token.symbol] = currentPrice != null && ticker ? toSymmTokenMetadata(currentPrice, ticker.prevClosePrice) : null;
26231
+ shortMeta[token.symbol] = currentPrice != null && ticker ? toSymmTokenMetadata(currentPrice, ticker.openPrice) : null;
26215
26232
  }
26216
26233
  const allLongReady = longTokens.every(
26217
26234
  (t) => longMeta[t.symbol]?.currentPrice != null
@@ -26332,10 +26349,13 @@ function toBinanceInterval(interval) {
26332
26349
  }
26333
26350
 
26334
26351
  // src/react/hooks/use-symm-chart-candles.ts
26352
+ function areIntervalsEqual(currentInterval, nextInterval) {
26353
+ return currentInterval === toBinanceInterval(nextInterval);
26354
+ }
26335
26355
  async function fetchSymbolKlines(symbol, interval, start, end) {
26336
26356
  const binanceSymbol = toBinanceSymbol(symbol);
26337
26357
  if (!binanceSymbol) return [];
26338
- const klines = await fetchMarkPriceKlines(
26358
+ const klines = await fetchKlines(
26339
26359
  binanceSymbol,
26340
26360
  toBinanceInterval(interval),
26341
26361
  start,
@@ -26496,6 +26516,15 @@ function useSymmChartCandles(selection) {
26496
26516
  wsUnsubsRef.current.push(unsub);
26497
26517
  }
26498
26518
  }, [emitRealtimeBar, isUnsupported, selectedSymbols]);
26519
+ const setRealtimeInterval = useCallback((interval) => {
26520
+ if (areIntervalsEqual(activeIntervalRef.current, interval)) {
26521
+ return;
26522
+ }
26523
+ activeIntervalRef.current = toBinanceInterval(interval);
26524
+ if (listenersRef.current.size > 0) {
26525
+ setupWsSubscriptions();
26526
+ }
26527
+ }, [setupWsSubscriptions]);
26499
26528
  useEffect(() => {
26500
26529
  if (listenersRef.current.size > 0) {
26501
26530
  setupWsSubscriptions();
@@ -26507,6 +26536,7 @@ function useSymmChartCandles(selection) {
26507
26536
  }, [setupWsSubscriptions]);
26508
26537
  const fetchBasketCandles = useCallback(
26509
26538
  async (start, end, interval) => {
26539
+ setRealtimeInterval(interval);
26510
26540
  const longSymbol = longTokens[0]?.symbol;
26511
26541
  const shortSymbol = shortTokens[0]?.symbol;
26512
26542
  if (isUnsupported) {
@@ -26532,18 +26562,20 @@ function useSymmChartCandles(selection) {
26532
26562
  Object.fromEntries(entries)
26533
26563
  );
26534
26564
  },
26535
- [isUnsupported, longTokens, shortTokens]
26565
+ [isUnsupported, longTokens, setRealtimeInterval, shortTokens]
26536
26566
  );
26537
26567
  const fetchPerformanceCandles = useCallback(
26538
26568
  async (start, end, interval, symbol) => {
26569
+ setRealtimeInterval(interval);
26539
26570
  const parts = symbol.split(" ");
26540
26571
  const assetSymbol = parts.length >= 2 ? parts.slice(1).join(" ") : symbol;
26541
26572
  return fetchSymbolKlines(assetSymbol, interval, start, end);
26542
26573
  },
26543
- []
26574
+ [setRealtimeInterval]
26544
26575
  );
26545
26576
  const fetchOverallPerformanceCandles = useCallback(
26546
26577
  async (start, end, interval) => {
26578
+ setRealtimeInterval(interval);
26547
26579
  const longSymbol = longTokens[0]?.symbol;
26548
26580
  const shortSymbol = shortTokens[0]?.symbol;
26549
26581
  if (isUnsupported) return [];
@@ -26600,7 +26632,7 @@ function useSymmChartCandles(selection) {
26600
26632
  }
26601
26633
  return result;
26602
26634
  },
26603
- [isUnsupported, longTokens, shortTokens]
26635
+ [isUnsupported, longTokens, setRealtimeInterval, shortTokens]
26604
26636
  );
26605
26637
  const addRealtimeListener = useCallback((cb) => {
26606
26638
  const id = Math.random().toString(36).slice(2);