@pear-protocol/symmio-client 0.2.5 → 0.2.7

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.
@@ -4,6 +4,7 @@
4
4
  var react = require('react');
5
5
  var symmCore = require('@pear-protocol/symm-core');
6
6
  var reactQuery = require('@tanstack/react-query');
7
+ var zustand = require('zustand');
7
8
  var jsxRuntime = require('react/jsx-runtime');
8
9
  var viem = require('viem');
9
10
 
@@ -24,13 +25,13 @@ var symmKeys = {
24
25
  accountsLength: (address, chainId) => ["symm", "accountsLength", address, chainId],
25
26
  accountsWithPositions: (address, chainId) => ["symm", "accountsWithPositions", address, chainId],
26
27
  accountSummary: (address, chainId) => ["symm", "accountSummary", address, chainId],
27
- accountData: (address, chainId) => ["symm", "accountData", address, chainId],
28
+ accountData: (address, chainId, upnl) => ["symm", "accountData", address, chainId, upnl],
28
29
  signature: (address, chainId) => ["symm", "signature", address, chainId],
29
- approval: (owner, spender, chainId) => ["symm", "approval", owner, spender, chainId],
30
+ approval: (owner, spender, chainId, token) => ["symm", "approval", owner, spender, chainId, token],
30
31
  balances: (address, chainId) => ["symm", "balances", address, chainId],
31
- positions: (address, chainId) => ["symm", "positions", address, chainId],
32
- openOrders: (address, chainId) => ["symm", "openOrders", address, chainId],
33
- tradeHistory: (address, chainId) => ["symm", "tradeHistory", address, chainId],
32
+ positions: (params) => ["symm", "positions", params],
33
+ openOrders: (params) => ["symm", "openOrders", params],
34
+ tradeHistory: (params) => ["symm", "tradeHistory", params],
34
35
  tpslOrders: (address, chainId) => ["symm", "tpslOrders", address, chainId],
35
36
  tpslOrdersList: (params) => ["symm", "tpslOrders", params],
36
37
  twapOrders: (address, chainId) => ["symm", "twapOrders", address, chainId],
@@ -44,7 +45,7 @@ var symmKeys = {
44
45
  fundingRates: (chainId) => ["symm", "fundingRates", chainId],
45
46
  fundingPayments: (params) => ["symm", "fundingPayments", params],
46
47
  fundingHistory: (params) => ["symm", "fundingHistory", params],
47
- portfolio: (address, chainId) => ["symm", "portfolio", address, chainId],
48
+ portfolio: (params) => ["symm", "portfolio", params],
48
49
  notifications: (address, chainId) => ["symm", "notifications", address, chainId],
49
50
  unreadCount: (address, chainId) => ["symm", "unreadCount", address, chainId],
50
51
  availableMargin: (address, chainId) => ["symm", "availableMargin", address, chainId],
@@ -54,43 +55,46 @@ var symmKeys = {
54
55
  delegation: (account, target, selectors, chainId) => ["symm", "delegation", account, target, selectors, chainId],
55
56
  chartMetadata: (symbolsKey, positionKey) => ["symm", "chartMetadata", symbolsKey, positionKey]
56
57
  };
58
+ var useSymmWsStore = zustand.create((set) => ({
59
+ isConnected: false,
60
+ setConnected: (isConnected) => set({ isConnected })
61
+ }));
57
62
 
58
63
  // src/react/hooks/use-symm-ws.ts
59
64
  function asUnsubscribeFn(value) {
60
65
  return typeof value === "function" ? value : null;
61
66
  }
62
67
  function useSymmWs(params) {
63
- const { symmCoreClient, chainId: ctxChainId } = useSymmContext();
64
68
  const queryClient = reactQuery.useQueryClient();
65
- const { accountAddress } = params;
66
- const chainId = params.chainId ?? ctxChainId;
67
- const [isConnected, setIsConnected] = react.useState(false);
69
+ const isConnected = useSymmWsStore((state) => state.isConnected);
70
+ const setConnected = useSymmWsStore((state) => state.setConnected);
71
+ const { symmCoreClient, accountAddress, chainId } = params;
68
72
  react.useEffect(() => {
69
73
  if (!symmCoreClient || !accountAddress) {
70
- setIsConnected(false);
74
+ setConnected(false);
71
75
  return;
72
76
  }
73
77
  const ws = symmCoreClient.ws;
74
78
  const addr = accountAddress;
75
79
  const unsubscribers = [];
76
- const removeOnConnect = ws.onConnect(() => setIsConnected(true));
77
- const removeOnDisconnect = ws.onDisconnect(() => setIsConnected(false));
80
+ const removeOnConnect = ws.onConnect(() => setConnected(true));
81
+ const removeOnDisconnect = ws.onDisconnect(() => setConnected(false));
78
82
  unsubscribers.push(removeOnConnect, removeOnDisconnect);
79
83
  const positionsUnsub = asUnsubscribeFn(ws.subscribeToPositions(addr, chainId, () => {
80
84
  queryClient.invalidateQueries({
81
- queryKey: symmKeys.positions(accountAddress, chainId)
85
+ queryKey: ["symm", "positions"]
82
86
  });
83
87
  }));
84
88
  if (positionsUnsub) unsubscribers.push(positionsUnsub);
85
89
  const openOrdersUnsub = asUnsubscribeFn(ws.subscribeToOpenOrders(addr, chainId, () => {
86
90
  queryClient.invalidateQueries({
87
- queryKey: symmKeys.openOrders(accountAddress, chainId)
91
+ queryKey: ["symm", "openOrders"]
88
92
  });
89
93
  }));
90
94
  if (openOrdersUnsub) unsubscribers.push(openOrdersUnsub);
91
95
  const tradesUnsub = asUnsubscribeFn(ws.subscribeToTrades(addr, chainId, () => {
92
96
  queryClient.invalidateQueries({
93
- queryKey: symmKeys.tradeHistory(accountAddress, chainId)
97
+ queryKey: ["symm", "tradeHistory"]
94
98
  });
95
99
  }));
96
100
  if (tradesUnsub) unsubscribers.push(tradesUnsub);
@@ -129,10 +133,10 @@ function useSymmWs(params) {
129
133
  if (triggerOrdersUnsub) unsubscribers.push(triggerOrdersUnsub);
130
134
  const executionsUnsub = asUnsubscribeFn(ws.subscribeToExecutions(addr, chainId, () => {
131
135
  queryClient.invalidateQueries({
132
- queryKey: symmKeys.positions(accountAddress, chainId)
136
+ queryKey: ["symm", "positions"]
133
137
  });
134
138
  queryClient.invalidateQueries({
135
- queryKey: symmKeys.portfolio(accountAddress, chainId)
139
+ queryKey: ["symm", "portfolio"]
136
140
  });
137
141
  }));
138
142
  if (executionsUnsub) unsubscribers.push(executionsUnsub);
@@ -145,7 +149,7 @@ function useSymmWs(params) {
145
149
  ws.unsubscribeAll();
146
150
  }
147
151
  };
148
- }, [symmCoreClient, accountAddress, chainId, queryClient]);
152
+ }, [symmCoreClient, accountAddress, chainId, queryClient, setConnected]);
149
153
  return { isConnected };
150
154
  }
151
155
  var noopRefreshAuth = async () => null;
@@ -167,7 +171,7 @@ function SymmProvider({
167
171
  });
168
172
  }, [chainId, symmCoreConfig.apiUrl, symmCoreConfig.wsUrl]);
169
173
  const resolvedAuthToken = authToken ?? accessToken;
170
- useSymmWs({ chainId });
174
+ useSymmWs({ symmCoreClient, accountAddress: address, chainId });
171
175
  const value = react.useMemo(
172
176
  () => ({
173
177
  symmCoreClient,
@@ -373,20 +377,60 @@ async function fetchAccessToken(walletClient, signerAddress, accountAddress, cha
373
377
  writeStoredToken(accountAddress, chainId, cachedToken);
374
378
  return accessToken;
375
379
  }
380
+ function symmAuthTokenKey(address, chainId) {
381
+ return `${address.toLowerCase()}:${chainId}`;
382
+ }
383
+ var useSymmAuthStore = zustand.create((set, get) => ({
384
+ tokensByKey: {},
385
+ setToken: (address, chainId, token) => {
386
+ const key = symmAuthTokenKey(address, chainId);
387
+ set((state) => ({
388
+ tokensByKey: {
389
+ ...state.tokensByKey,
390
+ [key]: token
391
+ }
392
+ }));
393
+ },
394
+ getToken: (address, chainId) => {
395
+ const key = symmAuthTokenKey(address, chainId);
396
+ return get().tokensByKey[key] ?? null;
397
+ },
398
+ clearToken: (address, chainId) => {
399
+ const key = symmAuthTokenKey(address, chainId);
400
+ set((state) => {
401
+ if (!(key in state.tokensByKey)) return state;
402
+ const next = { ...state.tokensByKey };
403
+ delete next[key];
404
+ return { tokensByKey: next };
405
+ });
406
+ },
407
+ clearAll: () => {
408
+ set({ tokensByKey: {} });
409
+ }
410
+ }));
376
411
 
377
412
  // src/react/hooks/use-symm-auth.ts
378
413
  function useSymmAuth(params) {
379
414
  const { address, chainId, walletClient, siweDomain } = params;
380
- const [accessToken, setAccessToken] = react.useState(null);
415
+ const accessToken = useSymmAuthStore(
416
+ (state) => address ? state.tokensByKey[symmAuthTokenKey(address, chainId)] ?? null : null
417
+ );
418
+ const setToken = useSymmAuthStore((state) => state.setToken);
419
+ const getToken = useSymmAuthStore((state) => state.getToken);
420
+ const clearToken = useSymmAuthStore((state) => state.clearToken);
381
421
  const previousAddressRef = react.useRef(address);
382
422
  const previousChainIdRef = react.useRef(chainId);
383
423
  const refreshAuth = react.useCallback(
384
424
  async (accountAddress) => {
385
425
  if (!walletClient || !address) return null;
386
426
  const resolvedAccountAddress = accountAddress ?? address;
427
+ const inMemory = getToken(resolvedAccountAddress, chainId);
428
+ if (inMemory) {
429
+ return inMemory;
430
+ }
387
431
  const cached = getCachedToken(resolvedAccountAddress, chainId);
388
432
  if (cached) {
389
- setAccessToken(cached);
433
+ setToken(resolvedAccountAddress, chainId, cached);
390
434
  return cached;
391
435
  }
392
436
  try {
@@ -397,21 +441,21 @@ function useSymmAuth(params) {
397
441
  chainId,
398
442
  siweDomain
399
443
  );
400
- setAccessToken(token);
444
+ setToken(resolvedAccountAddress, chainId, token);
401
445
  return token;
402
446
  } catch {
403
- setAccessToken(null);
447
+ clearToken(resolvedAccountAddress, chainId);
404
448
  return null;
405
449
  }
406
450
  },
407
- [walletClient, address, chainId, siweDomain]
451
+ [walletClient, address, chainId, siweDomain, getToken, setToken, clearToken]
408
452
  );
409
453
  const clearAuth = react.useCallback(() => {
410
454
  if (address) {
411
455
  clearCachedToken(address, chainId);
456
+ clearToken(address, chainId);
412
457
  }
413
- setAccessToken(null);
414
- }, [address, chainId]);
458
+ }, [address, chainId, clearToken]);
415
459
  react.useEffect(() => {
416
460
  const previousAddress = previousAddressRef.current;
417
461
  const previousChainId = previousChainIdRef.current;
@@ -420,15 +464,19 @@ function useSymmAuth(params) {
420
464
  previousAddressRef.current = address;
421
465
  previousChainIdRef.current = chainId;
422
466
  if (!address || !walletClient) {
423
- setAccessToken(null);
424
467
  return;
425
468
  }
426
469
  if (previousAddress && (addressChanged || chainChanged)) {
427
470
  clearCachedToken(previousAddress, previousChainId);
471
+ clearToken(previousAddress, previousChainId);
428
472
  }
429
473
  const cached = getCachedToken(address, chainId);
430
- setAccessToken(cached);
431
- }, [address, walletClient, chainId]);
474
+ if (cached) {
475
+ setToken(address, chainId, cached);
476
+ } else {
477
+ clearToken(address, chainId);
478
+ }
479
+ }, [address, walletClient, chainId, setToken, clearToken]);
432
480
  return {
433
481
  accessToken,
434
482
  authToken: accessToken,
@@ -1042,7 +1090,7 @@ function useSymmInstantTrade(params) {
1042
1090
  if (selectors.length === 0) {
1043
1091
  throw new Error("at least one delegation selector is required");
1044
1092
  }
1045
- const accessToken = await refreshAuth(accountAddress);
1093
+ const accessToken = useSymmAuthStore.getState().getToken(accountAddress, chainId) ?? await refreshAuth(accountAddress);
1046
1094
  if (!accessToken) {
1047
1095
  throw new Error("failed to refresh instant-trading auth");
1048
1096
  }
@@ -1398,7 +1446,7 @@ function useSymmApproval(params) {
1398
1446
  const internalEnabled = !!publicClient && !!owner && !!resolvedSpender && !!resolvedToken;
1399
1447
  const approvalQuery = reactQuery.useQuery({
1400
1448
  ...params.query,
1401
- queryKey: symmKeys.approval(owner, resolvedSpender, chainId),
1449
+ queryKey: symmKeys.approval(owner, resolvedSpender, chainId, resolvedToken),
1402
1450
  queryFn: async () => {
1403
1451
  const [allowanceVal, balanceVal] = await Promise.all([
1404
1452
  getAllowance(publicClient, resolvedToken, owner, resolvedSpender),
@@ -1416,7 +1464,7 @@ function useSymmApproval(params) {
1416
1464
  },
1417
1465
  onSuccess: () => {
1418
1466
  queryClient.invalidateQueries({
1419
- queryKey: symmKeys.approval(owner, resolvedSpender, chainId)
1467
+ queryKey: symmKeys.approval(owner, resolvedSpender, chainId, resolvedToken)
1420
1468
  });
1421
1469
  }
1422
1470
  });
@@ -1441,7 +1489,7 @@ function useSymmCancelClose() {
1441
1489
  if (!symmCoreClient) {
1442
1490
  throw new Error("symm-core client not available");
1443
1491
  }
1444
- const resolvedAuthToken = authToken ?? ctxAuthToken ?? (accountAddress ? await refreshAuth(accountAddress) : null);
1492
+ const resolvedAuthToken = authToken ?? (accountAddress ? useSymmAuthStore.getState().getToken(accountAddress, chainId) : null) ?? ctxAuthToken ?? (accountAddress ? await refreshAuth(accountAddress) : null);
1445
1493
  if (!resolvedAuthToken) {
1446
1494
  throw new Error("auth token is required to cancel a pending close");
1447
1495
  }
@@ -24551,7 +24599,7 @@ function useSymmAccountData(params) {
24551
24599
  const internalEnabled = !!symmCoreClient && !!address;
24552
24600
  const query = reactQuery.useQuery({
24553
24601
  ...params.query,
24554
- queryKey: symmKeys.accountData(address, chainId),
24602
+ queryKey: symmKeys.accountData(address, chainId, upnl),
24555
24603
  queryFn: () => symmCoreClient.accounts.getData({
24556
24604
  address,
24557
24605
  chainId,
@@ -24641,13 +24689,17 @@ function useSymmTrade() {
24641
24689
  }
24642
24690
  function useSymmPositions(params) {
24643
24691
  const { symmCoreClient, chainId: ctxChainId } = useSymmContext();
24644
- const { accountAddress, mainAddress, address } = params;
24645
- const resolvedAddress = address ?? mainAddress;
24692
+ const { accountAddress, address } = params;
24693
+ const resolvedAddress = accountAddress ? void 0 : address;
24646
24694
  const chainId = params.chainId ?? ctxChainId;
24647
24695
  const internalEnabled = !!symmCoreClient && !!(accountAddress || resolvedAddress);
24648
24696
  const query = reactQuery.useQuery({
24649
24697
  ...params.query,
24650
- queryKey: symmKeys.positions(accountAddress ?? resolvedAddress, chainId),
24698
+ queryKey: symmKeys.positions({
24699
+ accountAddress,
24700
+ address: resolvedAddress,
24701
+ chainId
24702
+ }),
24651
24703
  queryFn: () => symmCoreClient.positions.getOpen({
24652
24704
  accountAddress,
24653
24705
  address: resolvedAddress,
@@ -24663,15 +24715,19 @@ function useSymmPositions(params) {
24663
24715
  }
24664
24716
  function useSymmOpenOrders(params) {
24665
24717
  const { symmCoreClient, chainId: ctxChainId } = useSymmContext();
24666
- const { accountAddress, mainAddress } = params;
24718
+ const { accountAddress, address } = params;
24719
+ const resolvedAddress = accountAddress ? void 0 : address;
24667
24720
  const chainId = params.chainId ?? ctxChainId;
24668
- const internalEnabled = !!symmCoreClient && !!(accountAddress || mainAddress);
24721
+ const internalEnabled = !!symmCoreClient && !!(accountAddress || resolvedAddress);
24669
24722
  const query = reactQuery.useQuery({
24670
24723
  ...params.query,
24671
- queryKey: symmKeys.openOrders(accountAddress ?? mainAddress, chainId),
24724
+ queryKey: symmKeys.openOrders({
24725
+ accountAddress,
24726
+ address: resolvedAddress,
24727
+ chainId
24728
+ }),
24672
24729
  queryFn: () => symmCoreClient.orders.list({
24673
- address: accountAddress,
24674
- mainAddress,
24730
+ address: accountAddress ?? resolvedAddress,
24675
24731
  chainId
24676
24732
  }),
24677
24733
  enabled: internalEnabled && (params.query?.enabled ?? true)
@@ -24684,17 +24740,25 @@ function useSymmOpenOrders(params) {
24684
24740
  }
24685
24741
  function useSymmTradeHistory(params) {
24686
24742
  const { symmCoreClient, chainId: ctxChainId } = useSymmContext();
24687
- const { accountAddress, mainAddress } = params;
24743
+ const { accountAddress, address } = params;
24744
+ const resolvedAddress = accountAddress ? void 0 : address;
24688
24745
  const chainId = params.chainId ?? ctxChainId;
24689
- const internalEnabled = !!symmCoreClient && !!(accountAddress || mainAddress);
24746
+ const internalEnabled = !!symmCoreClient && !!(accountAddress || resolvedAddress);
24690
24747
  const query = reactQuery.useQuery({
24691
24748
  ...params.query,
24692
- queryKey: symmKeys.tradeHistory(accountAddress ?? mainAddress, chainId),
24693
- queryFn: () => symmCoreClient.positions.getTradeHistory({
24749
+ queryKey: symmKeys.tradeHistory({
24694
24750
  accountAddress,
24695
- mainAddress,
24751
+ address: resolvedAddress,
24696
24752
  chainId
24697
24753
  }),
24754
+ queryFn: () => {
24755
+ const request = {
24756
+ accountAddress,
24757
+ address: resolvedAddress,
24758
+ chainId
24759
+ };
24760
+ return symmCoreClient.positions.getTradeHistory(request);
24761
+ },
24698
24762
  enabled: internalEnabled && (params.query?.enabled ?? true)
24699
24763
  });
24700
24764
  return {
@@ -24730,12 +24794,12 @@ function useSymmTpsl() {
24730
24794
  }
24731
24795
  function useSymmTpslOrders(params) {
24732
24796
  const { symmCoreClient, chainId: ctxChainId } = useSymmContext();
24733
- const { accountAddress, mainAddress, positionId, type, status } = params;
24797
+ const { accountAddress, address, positionId, type, status } = params;
24798
+ const resolvedAddress = accountAddress ? void 0 : address;
24734
24799
  const chainId = params.chainId ?? ctxChainId;
24735
- const internalEnabled = !!symmCoreClient && !!(accountAddress || mainAddress || positionId);
24800
+ const internalEnabled = !!symmCoreClient && !!(accountAddress || resolvedAddress || positionId);
24736
24801
  const request = {
24737
- address: accountAddress,
24738
- mainAddress,
24802
+ address: accountAddress ?? resolvedAddress,
24739
24803
  positionId,
24740
24804
  type,
24741
24805
  status,
@@ -24745,7 +24809,7 @@ function useSymmTpslOrders(params) {
24745
24809
  ...params.query,
24746
24810
  queryKey: symmKeys.tpslOrdersList({
24747
24811
  accountAddress,
24748
- mainAddress,
24812
+ address: resolvedAddress,
24749
24813
  positionId,
24750
24814
  type,
24751
24815
  status,
@@ -24764,15 +24828,15 @@ function useSymmTpslOrders(params) {
24764
24828
  function useSymmTwap(params) {
24765
24829
  const { symmCoreClient, chainId: ctxChainId } = useSymmContext();
24766
24830
  const queryClient = reactQuery.useQueryClient();
24767
- const { accountAddress, mainAddress } = params;
24831
+ const { accountAddress, address } = params;
24832
+ const resolvedAddress = accountAddress ? void 0 : address;
24768
24833
  const chainId = params.chainId ?? ctxChainId;
24769
- const internalEnabled = !!symmCoreClient && !!(accountAddress || mainAddress);
24834
+ const internalEnabled = !!symmCoreClient && !!(accountAddress || resolvedAddress);
24770
24835
  const query = reactQuery.useQuery({
24771
24836
  ...params.query,
24772
- queryKey: symmKeys.twapOrders(accountAddress ?? mainAddress, chainId),
24837
+ queryKey: symmKeys.twapOrders(accountAddress ?? resolvedAddress, chainId),
24773
24838
  queryFn: () => symmCoreClient.orders.getTwapOrders({
24774
- address: accountAddress,
24775
- mainAddress,
24839
+ address: accountAddress ?? resolvedAddress,
24776
24840
  chainId
24777
24841
  }),
24778
24842
  enabled: internalEnabled && (params.query?.enabled ?? true)
@@ -24841,12 +24905,12 @@ function useSymmTriggerConfig(params) {
24841
24905
  }
24842
24906
  function useSymmTriggerOrders(params) {
24843
24907
  const { symmCoreClient, chainId: ctxChainId } = useSymmContext();
24844
- const { accountAddress, mainAddress, status, limit, offset } = params;
24908
+ const { accountAddress, address, status, limit, offset } = params;
24909
+ const resolvedAddress = accountAddress ? void 0 : address;
24845
24910
  const chainId = params.chainId ?? ctxChainId;
24846
- const internalEnabled = !!symmCoreClient && !!(accountAddress || mainAddress);
24911
+ const internalEnabled = !!symmCoreClient && !!(accountAddress || resolvedAddress);
24847
24912
  const request = {
24848
- address: accountAddress,
24849
- mainAddress,
24913
+ address: accountAddress ?? resolvedAddress,
24850
24914
  chainId,
24851
24915
  status,
24852
24916
  limit,
@@ -24856,7 +24920,7 @@ function useSymmTriggerOrders(params) {
24856
24920
  ...params.query,
24857
24921
  queryKey: symmKeys.triggerOrders({
24858
24922
  accountAddress,
24859
- mainAddress,
24923
+ address: resolvedAddress,
24860
24924
  chainId,
24861
24925
  status,
24862
24926
  limit,
@@ -25073,17 +25137,25 @@ function useSymmFundingPayments(params) {
25073
25137
  }
25074
25138
  function useSymmPortfolio(params) {
25075
25139
  const { symmCoreClient, chainId: ctxChainId } = useSymmContext();
25076
- const { accountAddress, mainAddress } = params;
25140
+ const { accountAddress, address } = params;
25141
+ const resolvedAddress = accountAddress ? void 0 : address;
25077
25142
  const chainId = params.chainId ?? ctxChainId;
25078
- const internalEnabled = !!symmCoreClient && !!(accountAddress || mainAddress);
25143
+ const internalEnabled = !!symmCoreClient && !!(accountAddress || resolvedAddress);
25079
25144
  const query = reactQuery.useQuery({
25080
25145
  ...params.query,
25081
- queryKey: symmKeys.portfolio(accountAddress ?? mainAddress, chainId),
25082
- queryFn: () => symmCoreClient.portfolio.getMetrics({
25146
+ queryKey: symmKeys.portfolio({
25083
25147
  accountAddress,
25084
- mainAddress,
25148
+ address: resolvedAddress,
25085
25149
  chainId
25086
25150
  }),
25151
+ queryFn: () => {
25152
+ const request = {
25153
+ accountAddress,
25154
+ address: resolvedAddress,
25155
+ chainId
25156
+ };
25157
+ return symmCoreClient.portfolio.getMetrics(request);
25158
+ },
25087
25159
  enabled: internalEnabled && (params.query?.enabled ?? true)
25088
25160
  });
25089
25161
  return {
@@ -25178,7 +25250,7 @@ function useSymmPendingInstantOpens(params) {
25178
25250
  ...params.query,
25179
25251
  queryKey: symmKeys.pendingInstantOpens(accountAddress, chainId),
25180
25252
  queryFn: async () => {
25181
- const authToken = providedAuthToken ?? ctxAuthToken ?? await refreshAuth(accountAddress);
25253
+ const authToken = providedAuthToken ?? useSymmAuthStore.getState().getToken(accountAddress, chainId) ?? ctxAuthToken ?? await refreshAuth(accountAddress);
25182
25254
  if (!authToken) {
25183
25255
  throw new Error("failed to acquire auth token for pending instant opens");
25184
25256
  }
@@ -25690,6 +25762,74 @@ function getBinanceWsManager() {
25690
25762
  return _instance;
25691
25763
  }
25692
25764
 
25765
+ // src/react/stores/use-binance-mark-price-store.ts
25766
+ var refCounts = /* @__PURE__ */ new Map();
25767
+ var streamUnsubs = /* @__PURE__ */ new Map();
25768
+ var streamSymbols = /* @__PURE__ */ new Map();
25769
+ function normalizeBinanceSymbol(symbol) {
25770
+ return symbol.toUpperCase().trim();
25771
+ }
25772
+ function getNextRefCount(binanceSymbol) {
25773
+ return (refCounts.get(binanceSymbol) ?? 0) + 1;
25774
+ }
25775
+ function getPrevRefCount(binanceSymbol) {
25776
+ return Math.max(0, (refCounts.get(binanceSymbol) ?? 0) - 1);
25777
+ }
25778
+ var useBinanceMarkPriceStore = zustand.create((set) => ({
25779
+ markPrices: {},
25780
+ subscribeSymbol: (symmSymbol, rawBinanceSymbol) => {
25781
+ const binanceSymbol = normalizeBinanceSymbol(rawBinanceSymbol);
25782
+ const nextRefCount = getNextRefCount(binanceSymbol);
25783
+ refCounts.set(binanceSymbol, nextRefCount);
25784
+ const symbols = streamSymbols.get(binanceSymbol) ?? /* @__PURE__ */ new Set();
25785
+ symbols.add(symmSymbol);
25786
+ streamSymbols.set(binanceSymbol, symbols);
25787
+ if (nextRefCount === 1) {
25788
+ const wsManager = getBinanceWsManager();
25789
+ const unsubscribe = wsManager.subscribeMarkPrice(binanceSymbol, (data) => {
25790
+ const canonicalSymbol = normalizeBinanceSymbol(data.symbol);
25791
+ const mappedSymbols = streamSymbols.get(canonicalSymbol);
25792
+ if (!mappedSymbols || mappedSymbols.size === 0) return;
25793
+ set((state) => {
25794
+ const nextMarkPrices = { ...state.markPrices };
25795
+ mappedSymbols.forEach((mappedSymbol) => {
25796
+ nextMarkPrices[mappedSymbol] = data.markPrice;
25797
+ });
25798
+ return { markPrices: nextMarkPrices };
25799
+ });
25800
+ });
25801
+ streamUnsubs.set(binanceSymbol, unsubscribe);
25802
+ }
25803
+ },
25804
+ unsubscribeSymbol: (symmSymbol, rawBinanceSymbol) => {
25805
+ const binanceSymbol = normalizeBinanceSymbol(rawBinanceSymbol);
25806
+ const symbols = streamSymbols.get(binanceSymbol);
25807
+ if (symbols) {
25808
+ symbols.delete(symmSymbol);
25809
+ if (symbols.size === 0) {
25810
+ streamSymbols.delete(binanceSymbol);
25811
+ } else {
25812
+ streamSymbols.set(binanceSymbol, symbols);
25813
+ }
25814
+ }
25815
+ const nextRefCount = getPrevRefCount(binanceSymbol);
25816
+ if (nextRefCount === 0) {
25817
+ const unsubscribe = streamUnsubs.get(binanceSymbol);
25818
+ if (unsubscribe) unsubscribe();
25819
+ streamUnsubs.delete(binanceSymbol);
25820
+ refCounts.delete(binanceSymbol);
25821
+ } else {
25822
+ refCounts.set(binanceSymbol, nextRefCount);
25823
+ }
25824
+ set((state) => {
25825
+ if (state.markPrices[symmSymbol] == null) return state;
25826
+ const nextMarkPrices = { ...state.markPrices };
25827
+ delete nextMarkPrices[symmSymbol];
25828
+ return { markPrices: nextMarkPrices };
25829
+ });
25830
+ }
25831
+ }));
25832
+
25693
25833
  // src/react/hooks/use-symm-token-selection-metadata.ts
25694
25834
  async function fetchTickerSnapshot(symbol) {
25695
25835
  const resolution = resolveBinanceSymbol(symbol);
@@ -25731,9 +25871,9 @@ function useSymmTokenSelectionMetadata(selection, options = {}) {
25731
25871
  ),
25732
25872
  [selectedSymbols]
25733
25873
  );
25734
- const [liveMarkPrices, setLiveMarkPrices] = react.useState(
25735
- {}
25736
- );
25874
+ const liveMarkPrices = useBinanceMarkPriceStore((state) => state.markPrices);
25875
+ const subscribeSymbol = useBinanceMarkPriceStore((state) => state.subscribeSymbol);
25876
+ const unsubscribeSymbol = useBinanceMarkPriceStore((state) => state.unsubscribeSymbol);
25737
25877
  const query = reactQuery.useQuery({
25738
25878
  queryKey: ["symm", "chart-metadata", symbolsKey],
25739
25879
  queryFn: async () => {
@@ -25761,36 +25901,25 @@ function useSymmTokenSelectionMetadata(selection, options = {}) {
25761
25901
  });
25762
25902
  react.useEffect(() => {
25763
25903
  if (!enabled || isUnsupported || selectedSymbols.length === 0) {
25764
- setLiveMarkPrices({});
25765
25904
  return;
25766
25905
  }
25767
- setLiveMarkPrices((current) => {
25768
- const next = {};
25769
- for (const symbol of selectedSymbols) {
25770
- if (current[symbol] != null) {
25771
- next[symbol] = current[symbol];
25772
- }
25773
- }
25774
- return next;
25775
- });
25776
- const wsManager = getBinanceWsManager();
25777
- const unsubscribes = Array.from(symbolToBinanceMap.entries()).map(
25778
- ([symbol, binanceSymbol]) => wsManager.subscribeMarkPrice(binanceSymbol, (data) => {
25779
- setLiveMarkPrices((current) => {
25780
- if (current[symbol] === data.markPrice) return current;
25781
- return {
25782
- ...current,
25783
- [symbol]: data.markPrice
25784
- };
25785
- });
25786
- })
25906
+ const symbolEntries = Array.from(symbolToBinanceMap.entries());
25907
+ symbolEntries.forEach(
25908
+ ([symbol, binanceSymbol]) => subscribeSymbol(symbol, binanceSymbol)
25787
25909
  );
25788
25910
  return () => {
25789
- for (const unsubscribe of unsubscribes) {
25790
- unsubscribe();
25791
- }
25911
+ symbolEntries.forEach(
25912
+ ([symbol, binanceSymbol]) => unsubscribeSymbol(symbol, binanceSymbol)
25913
+ );
25792
25914
  };
25793
- }, [enabled, isUnsupported, selectedSymbols, symbolToBinanceMap]);
25915
+ }, [
25916
+ enabled,
25917
+ isUnsupported,
25918
+ selectedSymbols.length,
25919
+ symbolToBinanceMap,
25920
+ subscribeSymbol,
25921
+ unsubscribeSymbol
25922
+ ]);
25794
25923
  return react.useMemo(() => {
25795
25924
  const tickerSnapshots = query.data?.tickerSnapshots ?? {};
25796
25925
  const longMeta = {};
@@ -26233,6 +26362,7 @@ function getSymmErrorMessage(error) {
26233
26362
  exports.SymmProvider = SymmProvider;
26234
26363
  exports.getSymmErrorMessage = getSymmErrorMessage;
26235
26364
  exports.symmKeys = symmKeys;
26365
+ exports.useBinanceMarkPriceStore = useBinanceMarkPriceStore;
26236
26366
  exports.useSymmAccountData = useSymmAccountData;
26237
26367
  exports.useSymmAccountSummary = useSymmAccountSummary;
26238
26368
  exports.useSymmAccounts = useSymmAccounts;
@@ -26241,6 +26371,7 @@ exports.useSymmAccountsLength = useSymmAccountsLength;
26241
26371
  exports.useSymmAccountsWithPositions = useSymmAccountsWithPositions;
26242
26372
  exports.useSymmApproval = useSymmApproval;
26243
26373
  exports.useSymmAuth = useSymmAuth;
26374
+ exports.useSymmAuthStore = useSymmAuthStore;
26244
26375
  exports.useSymmAvailableMargin = useSymmAvailableMargin;
26245
26376
  exports.useSymmBalances = useSymmBalances;
26246
26377
  exports.useSymmCancelClose = useSymmCancelClose;
@@ -26280,5 +26411,6 @@ exports.useSymmTwap = useSymmTwap;
26280
26411
  exports.useSymmTwapOrder = useSymmTwapOrder;
26281
26412
  exports.useSymmWithdraw = useSymmWithdraw;
26282
26413
  exports.useSymmWs = useSymmWs;
26414
+ exports.useSymmWsStore = useSymmWsStore;
26283
26415
  //# sourceMappingURL=index.js.map
26284
26416
  //# sourceMappingURL=index.js.map