@pear-protocol/symmio-client 0.3.27 → 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';
@@ -9,6 +9,7 @@ import { UseMutationOptions, useQueryClient, UseMutationResult, UseQueryResult }
9
9
  import * as node_modules_viem__types_actions_siwe_verifySiweMessage from 'node_modules/viem/_types/actions/siwe/verifySiweMessage';
10
10
  import * as abitype from 'abitype';
11
11
  import * as node_modules_viem__types_utils_ccip from 'node_modules/viem/_types/utils/ccip';
12
+ import * as _pear_protocol_symm_shared from '@pear-protocol/symm-shared';
12
13
  import * as _tanstack_query_core from '@tanstack/query-core';
13
14
  import * as node_modules__pear_protocol_symm_core_dist_types from 'node_modules/@pear-protocol/symm-core/dist/types';
14
15
  import * as zustand from 'zustand';
@@ -18,6 +19,7 @@ type SymmContextValue = {
18
19
  symmCoreClient: SymmSDK | null;
19
20
  chainId: number;
20
21
  address?: `0x${string}`;
22
+ accountAddress?: `0x${string}`;
21
23
  walletClient?: WalletClient;
22
24
  symmioConfig?: Partial<SymmioSDKConfig>;
23
25
  };
@@ -8390,7 +8392,7 @@ declare function useSymmCancelClose(options?: {
8390
8392
  accountAddress?: Address;
8391
8393
  overrideChainId?: number;
8392
8394
  }>;
8393
- }): _tanstack_react_query.UseMutationResult<_pear_protocol_symm_core.ApiResponse<unknown>, Error, {
8395
+ }): _tanstack_react_query.UseMutationResult<_pear_protocol_symm_shared.ApiResponse<unknown>, Error, {
8394
8396
  quoteId: string;
8395
8397
  authToken?: string;
8396
8398
  accountAddress?: Address;
@@ -9309,7 +9311,7 @@ declare function useSymmCloseAllPositionsMutation(options?: {
9309
9311
  */
9310
9312
  declare function useSymmCancelOpenMutation(options?: {
9311
9313
  mutation?: SymmMutationConfig<unknown, Error, CancelOpenRequest>;
9312
- }): _tanstack_react_query.UseMutationResult<_pear_protocol_symm_core.ApiResponse<unknown>, Error, CancelOpenRequest, unknown>;
9314
+ }): _tanstack_react_query.UseMutationResult<_pear_protocol_symm_shared.ApiResponse<unknown>, Error, CancelOpenRequest, unknown>;
9313
9315
  /**
9314
9316
  * Use case: Update position settings with automatic auth-token resolution.
9315
9317
  */
@@ -9365,7 +9367,7 @@ declare function useSymmSetTpslMutation(options?: {
9365
9367
  */
9366
9368
  declare function useSymmCancelTpslMutation(options?: {
9367
9369
  mutation?: SymmMutationConfig<unknown, Error, CancelTpslRequest>;
9368
- }): _tanstack_react_query.UseMutationResult<_pear_protocol_symm_core.ApiResponse<unknown>, Error, CancelTpslRequest, unknown>;
9370
+ }): _tanstack_react_query.UseMutationResult<_pear_protocol_symm_shared.ApiResponse<unknown>, Error, CancelTpslRequest, unknown>;
9369
9371
 
9370
9372
  type UseSymmTpslOrdersParams = {
9371
9373
  accountAddress?: Address;
@@ -9497,7 +9499,7 @@ declare function useSymmLockedParams(params: {
9497
9499
  leverage?: number;
9498
9500
  chainId?: number;
9499
9501
  query?: SymmQueryConfig;
9500
- }): _tanstack_react_query.UseQueryResult<node_modules__pear_protocol_symm_core_dist_types.LockedParamsResponse, Error>;
9502
+ }): _tanstack_react_query.UseQueryResult<_pear_protocol_symm_shared.LockedParams, Error>;
9501
9503
 
9502
9504
  type UseSymmHedgerMarketsParams = GetSymmHedgerMarketsRequest & {
9503
9505
  /** @deprecated Use `query.enabled` instead. */
@@ -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';
@@ -9,6 +9,7 @@ import { UseMutationOptions, useQueryClient, UseMutationResult, UseQueryResult }
9
9
  import * as node_modules_viem__types_actions_siwe_verifySiweMessage from 'node_modules/viem/_types/actions/siwe/verifySiweMessage';
10
10
  import * as abitype from 'abitype';
11
11
  import * as node_modules_viem__types_utils_ccip from 'node_modules/viem/_types/utils/ccip';
12
+ import * as _pear_protocol_symm_shared from '@pear-protocol/symm-shared';
12
13
  import * as _tanstack_query_core from '@tanstack/query-core';
13
14
  import * as node_modules__pear_protocol_symm_core_dist_types from 'node_modules/@pear-protocol/symm-core/dist/types';
14
15
  import * as zustand from 'zustand';
@@ -18,6 +19,7 @@ type SymmContextValue = {
18
19
  symmCoreClient: SymmSDK | null;
19
20
  chainId: number;
20
21
  address?: `0x${string}`;
22
+ accountAddress?: `0x${string}`;
21
23
  walletClient?: WalletClient;
22
24
  symmioConfig?: Partial<SymmioSDKConfig>;
23
25
  };
@@ -8390,7 +8392,7 @@ declare function useSymmCancelClose(options?: {
8390
8392
  accountAddress?: Address;
8391
8393
  overrideChainId?: number;
8392
8394
  }>;
8393
- }): _tanstack_react_query.UseMutationResult<_pear_protocol_symm_core.ApiResponse<unknown>, Error, {
8395
+ }): _tanstack_react_query.UseMutationResult<_pear_protocol_symm_shared.ApiResponse<unknown>, Error, {
8394
8396
  quoteId: string;
8395
8397
  authToken?: string;
8396
8398
  accountAddress?: Address;
@@ -9309,7 +9311,7 @@ declare function useSymmCloseAllPositionsMutation(options?: {
9309
9311
  */
9310
9312
  declare function useSymmCancelOpenMutation(options?: {
9311
9313
  mutation?: SymmMutationConfig<unknown, Error, CancelOpenRequest>;
9312
- }): _tanstack_react_query.UseMutationResult<_pear_protocol_symm_core.ApiResponse<unknown>, Error, CancelOpenRequest, unknown>;
9314
+ }): _tanstack_react_query.UseMutationResult<_pear_protocol_symm_shared.ApiResponse<unknown>, Error, CancelOpenRequest, unknown>;
9313
9315
  /**
9314
9316
  * Use case: Update position settings with automatic auth-token resolution.
9315
9317
  */
@@ -9365,7 +9367,7 @@ declare function useSymmSetTpslMutation(options?: {
9365
9367
  */
9366
9368
  declare function useSymmCancelTpslMutation(options?: {
9367
9369
  mutation?: SymmMutationConfig<unknown, Error, CancelTpslRequest>;
9368
- }): _tanstack_react_query.UseMutationResult<_pear_protocol_symm_core.ApiResponse<unknown>, Error, CancelTpslRequest, unknown>;
9370
+ }): _tanstack_react_query.UseMutationResult<_pear_protocol_symm_shared.ApiResponse<unknown>, Error, CancelTpslRequest, unknown>;
9369
9371
 
9370
9372
  type UseSymmTpslOrdersParams = {
9371
9373
  accountAddress?: Address;
@@ -9497,7 +9499,7 @@ declare function useSymmLockedParams(params: {
9497
9499
  leverage?: number;
9498
9500
  chainId?: number;
9499
9501
  query?: SymmQueryConfig;
9500
- }): _tanstack_react_query.UseQueryResult<node_modules__pear_protocol_symm_core_dist_types.LockedParamsResponse, Error>;
9502
+ }): _tanstack_react_query.UseQueryResult<_pear_protocol_symm_shared.LockedParams, Error>;
9501
9503
 
9502
9504
  type UseSymmHedgerMarketsParams = GetSymmHedgerMarketsRequest & {
9503
9505
  /** @deprecated Use `query.enabled` instead. */
@@ -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 });
@@ -27108,6 +27156,24 @@ function useSymmBalances(params) {
27108
27156
  enabled: internalEnabled && (params.query?.enabled ?? true)
27109
27157
  });
27110
27158
  }
27159
+ function normalizeSideWeights(legs) {
27160
+ if (!legs?.length) return legs;
27161
+ const allWeighted = legs.every(
27162
+ (leg) => typeof leg.weight === "number" && Number.isFinite(leg.weight) && leg.weight > 0
27163
+ );
27164
+ if (!allWeighted) return legs;
27165
+ const total = legs.reduce((sum, leg) => sum + (leg.weight ?? 0), 0);
27166
+ if (total <= 0) return legs;
27167
+ if (Math.abs(total - 1) < 1e-9) return legs;
27168
+ return legs.map((leg) => ({ ...leg, weight: (leg.weight ?? 0) / total }));
27169
+ }
27170
+ function normalizeBasketWeights(request) {
27171
+ return {
27172
+ ...request,
27173
+ longPositions: normalizeSideWeights(request.longPositions),
27174
+ shortPositions: normalizeSideWeights(request.shortPositions)
27175
+ };
27176
+ }
27111
27177
  function useSymmOpenBasketMutation(options) {
27112
27178
  const { symmCoreClient } = useSymmContext();
27113
27179
  const queryClient = reactQuery.useQueryClient();
@@ -27128,10 +27194,12 @@ function useSymmOpenBasketMutation(options) {
27128
27194
  if (!authToken) {
27129
27195
  throw new Error("auth token is required to open a position");
27130
27196
  }
27131
- return symmCoreClient.positions.openBasket({
27132
- ...request,
27133
- authToken
27134
- });
27197
+ return symmCoreClient.positions.openBasket(
27198
+ normalizeBasketWeights({
27199
+ ...request,
27200
+ authToken
27201
+ })
27202
+ );
27135
27203
  }
27136
27204
  });
27137
27205
  }
@@ -27603,18 +27671,14 @@ function useSymmHedgerMarketBySymbol(params) {
27603
27671
  });
27604
27672
  }
27605
27673
  function useSymmLockedParams(params) {
27606
- const { symmCoreClient, chainId: ctxChainId } = useSymmContext();
27674
+ const { chainId: ctxChainId } = useSymmContext();
27607
27675
  const { marketName, leverage } = params;
27608
27676
  const chainId = params.chainId ?? ctxChainId;
27609
- const internalEnabled = !!symmCoreClient && !!marketName && leverage != null;
27677
+ const internalEnabled = !!marketName && leverage != null && typeof chainId === "number";
27610
27678
  return reactQuery.useQuery({
27611
27679
  ...params.query,
27612
27680
  queryKey: symmKeys.lockedParams(marketName, leverage, chainId),
27613
- queryFn: () => symmCoreClient.markets.getLockedParams({
27614
- marketName,
27615
- leverage,
27616
- chainId
27617
- }),
27681
+ queryFn: () => symmCore.HedgerClient.getLockedParams(chainId, marketName, leverage),
27618
27682
  enabled: internalEnabled && (params.query?.enabled ?? true)
27619
27683
  });
27620
27684
  }