@pear-protocol/symmio-client 0.2.32 → 0.2.34

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.
@@ -39,8 +39,12 @@ declare function useSymmAuth(params?: UseSymmAuthParams): {
39
39
  accessToken: string | null;
40
40
  authToken: string | null;
41
41
  isAuthenticated: boolean;
42
- refresh: (accountAddress?: Address) => Promise<string | null>;
43
- refreshAuth: (accountAddress?: Address) => Promise<string | null>;
42
+ refresh: (accountAddress?: Address, options?: {
43
+ force?: boolean;
44
+ }) => Promise<string | null>;
45
+ refreshAuth: (accountAddress?: Address, options?: {
46
+ force?: boolean;
47
+ }) => Promise<string | null>;
44
48
  clear: () => void;
45
49
  };
46
50
 
@@ -8504,6 +8508,12 @@ declare function useSymmBalances(params: {
8504
8508
  query?: SymmQueryConfig;
8505
8509
  }): _tanstack_react_query.UseQueryResult<node_modules__pear_protocol_symm_core_dist_types.BalanceInfoResponse, Error>;
8506
8510
 
8511
+ type SymmTradeAuthParams = {
8512
+ address?: Address;
8513
+ chainId?: number;
8514
+ walletClient?: WalletClient;
8515
+ siweDomain?: string;
8516
+ };
8507
8517
  /**
8508
8518
  * Use case: Open a basket position through SYMM core API.
8509
8519
  */
@@ -8513,7 +8523,9 @@ declare function useSymmOpenBasketMutation(options?: {
8513
8523
  /**
8514
8524
  * Use case: Close a position with automatic auth-token resolution.
8515
8525
  */
8516
- declare function useSymmClosePositionMutation(options?: {
8526
+ declare function useSymmClosePositionMutation(paramsOrOptions?: SymmTradeAuthParams | {
8527
+ mutation?: SymmMutationConfig<unknown, Error, ClosePositionRequest>;
8528
+ }, maybeOptions?: {
8517
8529
  mutation?: SymmMutationConfig<unknown, Error, ClosePositionRequest>;
8518
8530
  }): _tanstack_react_query.UseMutationResult<_pear_protocol_symm_core.ClosePositionResponse | _pear_protocol_symm_core.CloseCommandResult, Error, ClosePositionRequest, unknown>;
8519
8531
  /**
@@ -8533,7 +8545,12 @@ declare function useSymmCancelOpenMutation(options?: {
8533
8545
  /**
8534
8546
  * Use case: Update position settings with automatic auth-token resolution.
8535
8547
  */
8536
- declare function useSymmUpdatePositionMutation(options?: {
8548
+ declare function useSymmUpdatePositionMutation(paramsOrOptions?: SymmTradeAuthParams | {
8549
+ mutation?: SymmMutationConfig<unknown, Error, {
8550
+ positionId: string;
8551
+ request: UpdatePositionRequest;
8552
+ }>;
8553
+ }, maybeOptions?: {
8537
8554
  mutation?: SymmMutationConfig<unknown, Error, {
8538
8555
  positionId: string;
8539
8556
  request: UpdatePositionRequest;
@@ -39,8 +39,12 @@ declare function useSymmAuth(params?: UseSymmAuthParams): {
39
39
  accessToken: string | null;
40
40
  authToken: string | null;
41
41
  isAuthenticated: boolean;
42
- refresh: (accountAddress?: Address) => Promise<string | null>;
43
- refreshAuth: (accountAddress?: Address) => Promise<string | null>;
42
+ refresh: (accountAddress?: Address, options?: {
43
+ force?: boolean;
44
+ }) => Promise<string | null>;
45
+ refreshAuth: (accountAddress?: Address, options?: {
46
+ force?: boolean;
47
+ }) => Promise<string | null>;
44
48
  clear: () => void;
45
49
  };
46
50
 
@@ -8504,6 +8508,12 @@ declare function useSymmBalances(params: {
8504
8508
  query?: SymmQueryConfig;
8505
8509
  }): _tanstack_react_query.UseQueryResult<node_modules__pear_protocol_symm_core_dist_types.BalanceInfoResponse, Error>;
8506
8510
 
8511
+ type SymmTradeAuthParams = {
8512
+ address?: Address;
8513
+ chainId?: number;
8514
+ walletClient?: WalletClient;
8515
+ siweDomain?: string;
8516
+ };
8507
8517
  /**
8508
8518
  * Use case: Open a basket position through SYMM core API.
8509
8519
  */
@@ -8513,7 +8523,9 @@ declare function useSymmOpenBasketMutation(options?: {
8513
8523
  /**
8514
8524
  * Use case: Close a position with automatic auth-token resolution.
8515
8525
  */
8516
- declare function useSymmClosePositionMutation(options?: {
8526
+ declare function useSymmClosePositionMutation(paramsOrOptions?: SymmTradeAuthParams | {
8527
+ mutation?: SymmMutationConfig<unknown, Error, ClosePositionRequest>;
8528
+ }, maybeOptions?: {
8517
8529
  mutation?: SymmMutationConfig<unknown, Error, ClosePositionRequest>;
8518
8530
  }): _tanstack_react_query.UseMutationResult<_pear_protocol_symm_core.ClosePositionResponse | _pear_protocol_symm_core.CloseCommandResult, Error, ClosePositionRequest, unknown>;
8519
8531
  /**
@@ -8533,7 +8545,12 @@ declare function useSymmCancelOpenMutation(options?: {
8533
8545
  /**
8534
8546
  * Use case: Update position settings with automatic auth-token resolution.
8535
8547
  */
8536
- declare function useSymmUpdatePositionMutation(options?: {
8548
+ declare function useSymmUpdatePositionMutation(paramsOrOptions?: SymmTradeAuthParams | {
8549
+ mutation?: SymmMutationConfig<unknown, Error, {
8550
+ positionId: string;
8551
+ request: UpdatePositionRequest;
8552
+ }>;
8553
+ }, maybeOptions?: {
8537
8554
  mutation?: SymmMutationConfig<unknown, Error, {
8538
8555
  positionId: string;
8539
8556
  request: UpdatePositionRequest;
@@ -616,7 +616,6 @@ async function login(chainId, params) {
616
616
  // src/react/auth.ts
617
617
  var TOKEN_STORAGE_PREFIX = "symm_access_token";
618
618
  var TOKEN_STORAGE_VERSION = 1;
619
- var tokenCache = /* @__PURE__ */ new Map();
620
619
  function cacheKey(address, chainId) {
621
620
  return `${address}:${chainId}`;
622
621
  }
@@ -654,20 +653,11 @@ function writeStoredToken(address, chainId, cached) {
654
653
  }
655
654
  }
656
655
  function getCachedToken(address, chainId) {
657
- const cached = tokenCache.get(cacheKey(address, chainId));
658
- if (cached && Date.now() < cached.expiresAt) {
659
- return cached.token;
660
- }
661
- if (cached) {
662
- tokenCache.delete(cacheKey(address, chainId));
663
- }
664
656
  const stored = readStoredToken(address, chainId);
665
657
  if (!stored) return null;
666
- tokenCache.set(cacheKey(address, chainId), stored);
667
658
  return stored.token;
668
659
  }
669
660
  function clearCachedToken(address, chainId) {
670
- tokenCache.delete(cacheKey(address, chainId));
671
661
  if (typeof window !== "undefined") {
672
662
  window.localStorage.removeItem(storageKey(address, chainId));
673
663
  }
@@ -700,7 +690,6 @@ async function fetchAccessToken(walletClient, signerAddress, accountAddress, cha
700
690
  token: accessToken,
701
691
  expiresAt: expiresAt - 6e4
702
692
  };
703
- tokenCache.set(cacheKey(accountAddress, chainId), cachedToken);
704
693
  writeStoredToken(accountAddress, chainId, cachedToken);
705
694
  return accessToken;
706
695
  }
@@ -737,13 +726,31 @@ var useSymmAuthStore = zustand.create((set, get) => ({
737
726
  }));
738
727
 
739
728
  // src/react/hooks/use-symm-auth.ts
729
+ function logSymmAuth(event, details) {
730
+ if (typeof window === "undefined") {
731
+ return;
732
+ }
733
+ console.debug("[symm-auth]", event, details ?? {});
734
+ }
735
+ function extractSubaccountAddresses(summary) {
736
+ const accounts = summary?.data?.accounts;
737
+ if (!Array.isArray(accounts)) {
738
+ return [];
739
+ }
740
+ return accounts.flatMap((account) => {
741
+ const accountAddress = account.accountAddress ?? account.address;
742
+ return accountAddress ? [accountAddress] : [];
743
+ });
744
+ }
740
745
  function useSymmAuth(params) {
741
- const ctx = useSymmContext();
746
+ const context = useSymmContext();
747
+ const ctx = context;
748
+ const { symmCoreClient } = context;
742
749
  const address = params?.address ?? ctx.address;
743
750
  const chainId = params?.chainId ?? ctx.chainId ?? 42161;
744
751
  const walletClient = params?.walletClient ?? ctx.walletClient;
745
752
  const siweDomain = params?.siweDomain;
746
- const accessToken = useSymmAuthStore((state) => {
753
+ const token = useSymmAuthStore((state) => {
747
754
  if (address) {
748
755
  return state.tokensByKey[symmAuthTokenKey(address, chainId)] ?? null;
749
756
  }
@@ -754,36 +761,65 @@ function useSymmAuth(params) {
754
761
  const clearToken = useSymmAuthStore((state) => state.clearToken);
755
762
  const previousAddressRef = react.useRef(address);
756
763
  const previousChainIdRef = react.useRef(chainId);
764
+ const refreshAuthRef = react.useRef(async () => null);
757
765
  const refreshAuth = react.useCallback(
758
- async (accountAddress) => {
766
+ async (accountAddress, options) => {
759
767
  if (!walletClient || !address) return null;
760
768
  const resolvedAccountAddress = accountAddress ?? address;
761
- const inMemory = getToken(resolvedAccountAddress, chainId);
762
- if (inMemory) {
763
- return inMemory;
764
- }
765
- const cached = getCachedToken(resolvedAccountAddress, chainId);
766
- if (cached) {
767
- setToken(resolvedAccountAddress, chainId, cached);
768
- return cached;
769
+ if (!options?.force) {
770
+ const inStore = getToken(resolvedAccountAddress, chainId);
771
+ if (inStore) {
772
+ logSymmAuth("refresh:store-hit", {
773
+ accountAddress: resolvedAccountAddress,
774
+ chainId
775
+ });
776
+ return inStore;
777
+ }
778
+ const cached = getCachedToken(resolvedAccountAddress, chainId);
779
+ if (cached) {
780
+ setToken(resolvedAccountAddress, chainId, cached);
781
+ logSymmAuth("refresh:local-storage-hit", {
782
+ accountAddress: resolvedAccountAddress,
783
+ chainId
784
+ });
785
+ return cached;
786
+ }
769
787
  }
770
788
  try {
771
- const token = await fetchAccessToken(
789
+ logSymmAuth("refresh:fetch-start", {
790
+ signerAddress: address,
791
+ accountAddress: resolvedAccountAddress,
792
+ chainId,
793
+ force: options?.force ?? false
794
+ });
795
+ const token2 = await fetchAccessToken(
772
796
  walletClient,
773
797
  address,
774
798
  resolvedAccountAddress,
775
799
  chainId,
776
800
  siweDomain
777
801
  );
778
- setToken(resolvedAccountAddress, chainId, token);
779
- return token;
780
- } catch {
802
+ setToken(resolvedAccountAddress, chainId, token2);
803
+ logSymmAuth("refresh:fetch-success", {
804
+ signerAddress: address,
805
+ accountAddress: resolvedAccountAddress,
806
+ chainId
807
+ });
808
+ return token2;
809
+ } catch (error) {
781
810
  clearToken(resolvedAccountAddress, chainId);
811
+ logSymmAuth("refresh:fetch-failed", {
812
+ signerAddress: address,
813
+ accountAddress: resolvedAccountAddress,
814
+ chainId,
815
+ error: error instanceof Error ? error.message : String(error)
816
+ });
782
817
  return null;
783
818
  }
784
819
  },
785
820
  [walletClient, address, chainId, siweDomain, getToken, setToken, clearToken]
786
821
  );
822
+ refreshAuthRef.current = refreshAuth;
787
823
  const clearAuth = react.useCallback(() => {
788
824
  if (address) {
789
825
  clearCachedToken(address, chainId);
@@ -795,26 +831,67 @@ function useSymmAuth(params) {
795
831
  const previousChainId = previousChainIdRef.current;
796
832
  const addressChanged = previousAddress !== address;
797
833
  const chainChanged = previousChainId !== chainId;
834
+ let cancelled = false;
798
835
  previousAddressRef.current = address;
799
836
  previousChainIdRef.current = chainId;
800
- if (!address || !walletClient) {
837
+ if (!address) {
801
838
  return;
802
839
  }
840
+ const userAddress = address;
803
841
  if (previousAddress && (addressChanged || chainChanged)) {
804
842
  clearCachedToken(previousAddress, previousChainId);
805
843
  clearToken(previousAddress, previousChainId);
806
844
  }
807
- const cached = getCachedToken(address, chainId);
808
- if (cached) {
809
- setToken(address, chainId, cached);
810
- } else {
811
- clearToken(address, chainId);
845
+ async function bootstrapAuth() {
846
+ let targets = [userAddress];
847
+ if (symmCoreClient) {
848
+ try {
849
+ const summary = await symmCoreClient.accounts.getSummary({
850
+ userAddress,
851
+ chainId
852
+ });
853
+ const subaccounts = extractSubaccountAddresses(summary);
854
+ if (subaccounts.length > 0) {
855
+ targets = subaccounts;
856
+ }
857
+ logSymmAuth("bootstrap:subaccounts-resolved", {
858
+ userAddress,
859
+ chainId,
860
+ subaccounts
861
+ });
862
+ } catch (error) {
863
+ logSymmAuth("bootstrap:subaccounts-failed", {
864
+ userAddress,
865
+ chainId,
866
+ error: error instanceof Error ? error.message : String(error)
867
+ });
868
+ }
869
+ }
870
+ const uniqueTargets = [...new Set(targets)].filter(
871
+ (accountAddress) => !!accountAddress
872
+ );
873
+ logSymmAuth("bootstrap:start", {
874
+ userAddress,
875
+ chainId,
876
+ targets: uniqueTargets,
877
+ walletReady: !!walletClient
878
+ });
879
+ for (const accountAddress of uniqueTargets) {
880
+ if (cancelled) {
881
+ return;
882
+ }
883
+ await refreshAuthRef.current(accountAddress);
884
+ }
812
885
  }
813
- }, [address, walletClient, chainId, setToken, clearToken]);
886
+ void bootstrapAuth();
887
+ return () => {
888
+ cancelled = true;
889
+ };
890
+ }, [address, walletClient, chainId, clearToken, symmCoreClient]);
814
891
  return {
815
- accessToken,
816
- authToken: accessToken,
817
- isAuthenticated: !!accessToken,
892
+ accessToken: token,
893
+ authToken: token,
894
+ isAuthenticated: !!token,
818
895
  refresh: refreshAuth,
819
896
  refreshAuth,
820
897
  clear: clearAuth
@@ -1923,10 +2000,12 @@ function useSymmCloseOrder(options) {
1923
2000
  if (!symmCoreClient) {
1924
2001
  throw new Error("symm-core client not available");
1925
2002
  }
2003
+ const typedRequest = request;
2004
+ const resolvedAccountAddress = typedRequest.accountAddress ?? address;
1926
2005
  return symmCoreClient.orders.close(request.id, {
1927
2006
  kind: request.kind,
1928
2007
  type: request.type,
1929
- authToken: request.authToken ?? (address ? useSymmAuthStore.getState().getToken(address, chainId) ?? void 0 : void 0)
2008
+ authToken: request.authToken ?? (resolvedAccountAddress ? useSymmAuthStore.getState().getToken(resolvedAccountAddress, chainId) ?? void 0 : void 0)
1930
2009
  });
1931
2010
  }
1932
2011
  });
@@ -25036,14 +25115,42 @@ function useSymmBalances(params) {
25036
25115
  enabled: internalEnabled && (params.query?.enabled ?? true)
25037
25116
  });
25038
25117
  }
25039
- function useResolveTradeAuthToken() {
25118
+ function logTradeAuth(event, details) {
25119
+ if (typeof window === "undefined") {
25120
+ return;
25121
+ }
25122
+ console.debug("[symm-trade]", event, details ?? {});
25123
+ }
25124
+ function splitTradeHookArgs(paramsOrOptions, options) {
25125
+ if (paramsOrOptions && "mutation" in paramsOrOptions) {
25126
+ return {
25127
+ params: {},
25128
+ options: paramsOrOptions
25129
+ };
25130
+ }
25131
+ return {
25132
+ params: paramsOrOptions ?? {},
25133
+ options
25134
+ };
25135
+ }
25136
+ function useResolveTradeAuthToken(params = {}) {
25040
25137
  const context = useSymmContext();
25041
- const { address, chainId } = context;
25042
- const { refreshAuth } = useSymmAuth({ address, chainId });
25138
+ const address = params.address ?? context.address;
25139
+ const chainId = params.chainId ?? context.chainId;
25140
+ const { refreshAuth } = useSymmAuth({
25141
+ address,
25142
+ chainId,
25143
+ walletClient: params.walletClient,
25144
+ siweDomain: params.siweDomain
25145
+ });
25043
25146
  const refreshAuthFromContext = context.refreshAuth;
25044
25147
  return react.useCallback(
25045
25148
  async (providedAuthToken, accountAddress) => {
25046
25149
  if (providedAuthToken) {
25150
+ logTradeAuth("resolve-auth:provided", {
25151
+ accountAddress,
25152
+ chainId
25153
+ });
25047
25154
  return providedAuthToken;
25048
25155
  }
25049
25156
  const resolvedAccountAddress = accountAddress ?? address;
@@ -25052,11 +25159,23 @@ function useResolveTradeAuthToken() {
25052
25159
  }
25053
25160
  const inMemoryToken = useSymmAuthStore.getState().getToken(resolvedAccountAddress, chainId);
25054
25161
  if (inMemoryToken) {
25162
+ logTradeAuth("resolve-auth:store-hit", {
25163
+ accountAddress: resolvedAccountAddress,
25164
+ chainId
25165
+ });
25055
25166
  return inMemoryToken;
25056
25167
  }
25057
25168
  if (refreshAuthFromContext) {
25169
+ logTradeAuth("resolve-auth:context-refresh", {
25170
+ accountAddress: resolvedAccountAddress,
25171
+ chainId
25172
+ });
25058
25173
  return refreshAuthFromContext(resolvedAccountAddress);
25059
25174
  }
25175
+ logTradeAuth("resolve-auth:hook-refresh", {
25176
+ accountAddress: resolvedAccountAddress,
25177
+ chainId
25178
+ });
25060
25179
  return refreshAuth(resolvedAccountAddress);
25061
25180
  },
25062
25181
  [address, chainId, refreshAuth, refreshAuthFromContext]
@@ -25077,10 +25196,11 @@ function useSymmOpenBasketMutation(options) {
25077
25196
  }
25078
25197
  });
25079
25198
  }
25080
- function useSymmClosePositionMutation(options) {
25199
+ function useSymmClosePositionMutation(paramsOrOptions, maybeOptions) {
25081
25200
  const { symmCoreClient } = useSymmContext();
25082
25201
  const queryClient = reactQuery.useQueryClient();
25083
- const resolveAuthToken = useResolveTradeAuthToken();
25202
+ const { params, options } = splitTradeHookArgs(paramsOrOptions, maybeOptions);
25203
+ const resolveAuthToken = useResolveTradeAuthToken(params);
25084
25204
  return reactQuery.useMutation({
25085
25205
  ...withSymmMutationConfig(options?.mutation, {
25086
25206
  onSuccess: () => {
@@ -25134,10 +25254,11 @@ function useSymmCancelOpenMutation(options) {
25134
25254
  }
25135
25255
  });
25136
25256
  }
25137
- function useSymmUpdatePositionMutation(options) {
25257
+ function useSymmUpdatePositionMutation(paramsOrOptions, maybeOptions) {
25138
25258
  const { symmCoreClient } = useSymmContext();
25139
25259
  const queryClient = reactQuery.useQueryClient();
25140
- const resolveAuthToken = useResolveTradeAuthToken();
25260
+ const { params, options } = splitTradeHookArgs(paramsOrOptions, maybeOptions);
25261
+ const resolveAuthToken = useResolveTradeAuthToken(params);
25141
25262
  return reactQuery.useMutation({
25142
25263
  ...withSymmMutationConfig(options?.mutation, {
25143
25264
  onSuccess: () => {