@pear-protocol/symmio-client 0.3.28 → 0.3.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.
@@ -1,5 +1,5 @@
1
- import { S as SymmioSDKConfig } from './provider-CMBaM64o.mjs';
2
- export { a as SymmProvider, b as SymmProviderProps } from './provider-CMBaM64o.mjs';
1
+ import { S as SymmioSDKConfig } from './provider-DGrGzZoG.mjs';
2
+ export { a as SymmProvider, b as SymmProviderProps } from './provider-DGrGzZoG.mjs';
3
3
  import * as viem from 'viem';
4
4
  import { WalletClient, Address, Hex, PublicClient } from 'viem';
5
5
  import * as _pear_protocol_symm_core from '@pear-protocol/symm-core';
@@ -19,6 +19,7 @@ type SymmContextValue = {
19
19
  symmCoreClient: SymmSDK | null;
20
20
  chainId: number;
21
21
  address?: `0x${string}`;
22
+ accountAddress?: `0x${string}`;
22
23
  walletClient?: WalletClient;
23
24
  symmioConfig?: Partial<SymmioSDKConfig>;
24
25
  };
@@ -1,5 +1,5 @@
1
- import { S as SymmioSDKConfig } from './provider-CMBaM64o.js';
2
- export { a as SymmProvider, b as SymmProviderProps } from './provider-CMBaM64o.js';
1
+ import { S as SymmioSDKConfig } from './provider-DGrGzZoG.js';
2
+ export { a as SymmProvider, b as SymmProviderProps } from './provider-DGrGzZoG.js';
3
3
  import * as viem from 'viem';
4
4
  import { WalletClient, Address, Hex, PublicClient } from 'viem';
5
5
  import * as _pear_protocol_symm_core from '@pear-protocol/symm-core';
@@ -19,6 +19,7 @@ type SymmContextValue = {
19
19
  symmCoreClient: SymmSDK | null;
20
20
  chainId: number;
21
21
  address?: `0x${string}`;
22
+ accountAddress?: `0x${string}`;
22
23
  walletClient?: WalletClient;
23
24
  symmioConfig?: Partial<SymmioSDKConfig>;
24
25
  };
@@ -699,7 +699,7 @@ function useSymmWs(params = {}) {
699
699
  const isConnected = useSymmWsStore((state) => state.isConnected);
700
700
  const setConnected = useSymmWsStore((state) => state.setConnected);
701
701
  const symmCoreClient = params.symmCoreClient ?? ctx?.symmCoreClient ?? null;
702
- const accountAddress = params.accountAddress ?? ctx?.address;
702
+ const accountAddress = params.accountAddress ?? ctx?.accountAddress;
703
703
  const chainId = params.chainId ?? ctx?.chainId ?? 42161;
704
704
  react.useEffect(() => {
705
705
  if (!symmCoreClient || !accountAddress) {
@@ -709,14 +709,61 @@ function useSymmWs(params = {}) {
709
709
  const ws = symmCoreClient.ws;
710
710
  const addr = accountAddress;
711
711
  let cancelled = false;
712
+ const subscribedChannelKeys = /* @__PURE__ */ new Set();
712
713
  addWsOwner(ws);
713
- const removeOnConnect = ws.onConnect(() => setConnected(true));
714
- const removeOnDisconnect = ws.onDisconnect(() => setConnected(false));
715
714
  const subscriptions = [];
716
715
  const addSubscription = (channel, handler) => {
717
- ws.subscribe(channel, addr, chainId, handler);
718
716
  subscriptions.push({ channel, handler });
719
717
  };
718
+ const sendWsMessage = (message) => {
719
+ if (!ws.isConnected()) return;
720
+ ws.ws?.send(JSON.stringify(message));
721
+ };
722
+ const getHandlers = (channelKey) => {
723
+ const internals = ws;
724
+ if (!internals.subscriptions) {
725
+ internals.subscriptions = /* @__PURE__ */ new Map();
726
+ }
727
+ let handlers = internals.subscriptions.get(channelKey);
728
+ if (!handlers) {
729
+ handlers = /* @__PURE__ */ new Set();
730
+ internals.subscriptions.set(channelKey, handlers);
731
+ }
732
+ return handlers;
733
+ };
734
+ const subscribeAll = () => {
735
+ subscriptions.forEach(({ channel, handler }) => {
736
+ const channelKey = symmCore.formatChannel(channel, addr, chainId);
737
+ getHandlers(channelKey).add(handler);
738
+ if (!subscribedChannelKeys.has(channelKey)) {
739
+ sendWsMessage({ type: "subscribe", channel: channelKey });
740
+ subscribedChannelKeys.add(channelKey);
741
+ }
742
+ });
743
+ };
744
+ const unsubscribeAll = (sendUnsubscribe) => {
745
+ subscriptions.forEach(({ channel, handler }) => {
746
+ const channelKey = symmCore.formatChannel(channel, addr, chainId);
747
+ const internals = ws;
748
+ const handlers = internals.subscriptions?.get(channelKey);
749
+ handlers?.delete(handler);
750
+ if (handlers?.size === 0) {
751
+ internals.subscriptions?.delete(channelKey);
752
+ }
753
+ if (sendUnsubscribe && subscribedChannelKeys.has(channelKey)) {
754
+ sendWsMessage({ type: "unsubscribe", channel: channelKey });
755
+ }
756
+ subscribedChannelKeys.delete(channelKey);
757
+ });
758
+ };
759
+ const removeOnConnect = ws.onConnect(() => {
760
+ setConnected(true);
761
+ subscribeAll();
762
+ });
763
+ const removeOnDisconnect = ws.onDisconnect(() => {
764
+ unsubscribeAll(false);
765
+ setConnected(false);
766
+ });
720
767
  addSubscription("positions", () => {
721
768
  queryClient.invalidateQueries({ queryKey: symmKeys.positionsRoot });
722
769
  });
@@ -758,10 +805,11 @@ function useSymmWs(params = {}) {
758
805
  queryClient.invalidateQueries({ queryKey: symmKeys.positionsRoot });
759
806
  queryClient.invalidateQueries({ queryKey: symmKeys.portfolioRoot });
760
807
  });
761
- void connectShared(ws).then(() => {
808
+ connectShared(ws).then(() => {
762
809
  if (cancelled) return;
763
810
  if (ws.isConnected()) {
764
811
  setConnected(true);
812
+ subscribeAll();
765
813
  }
766
814
  }).catch(() => {
767
815
  if (cancelled) return;
@@ -771,15 +819,13 @@ function useSymmWs(params = {}) {
771
819
  cancelled = true;
772
820
  removeOnConnect();
773
821
  removeOnDisconnect();
774
- subscriptions.forEach(({ channel, handler }) => {
775
- ws.unsubscribe(channel, addr, chainId, handler);
776
- });
822
+ unsubscribeAll(true);
777
823
  if (removeWsOwner(ws) === 0) {
778
824
  ws.disconnect();
779
825
  setConnected(false);
780
826
  }
781
827
  };
782
- }, [symmCoreClient, accountAddress, chainId, queryClient, setConnected]);
828
+ }, [symmCoreClient, accountAddress, chainId]);
783
829
  return { isConnected };
784
830
  }
785
831
  function trimTrailingSlashes(value) {
@@ -788,6 +834,7 @@ function trimTrailingSlashes(value) {
788
834
  function SymmProvider({
789
835
  chainId = 42161,
790
836
  address,
837
+ accountAddress,
791
838
  walletClient,
792
839
  symmCoreConfig = {
793
840
  apiUrl: "https://nginx-server-staging.up.railway.app",
@@ -816,10 +863,11 @@ function SymmProvider({
816
863
  symmCoreClient,
817
864
  chainId,
818
865
  address,
866
+ accountAddress,
819
867
  walletClient,
820
868
  symmioConfig
821
869
  }),
822
- [symmCoreClient, chainId, address, walletClient, symmioConfig]
870
+ [symmCoreClient, chainId, address, accountAddress, walletClient, symmioConfig]
823
871
  );
824
872
  useBinanceWs({
825
873
  symmCoreClient,
@@ -827,7 +875,7 @@ function SymmProvider({
827
875
  });
828
876
  useSymmWs({
829
877
  symmCoreClient,
830
- accountAddress: address,
878
+ accountAddress,
831
879
  chainId
832
880
  });
833
881
  return /* @__PURE__ */ jsxRuntime.jsx(SymmContext.Provider, { value, children });