@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.
- package/dist/react/index.d.mts +21 -4
- package/dist/react/index.d.ts +21 -4
- package/dist/react/index.js +165 -44
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +165 -44
- package/dist/react/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/react/index.mjs
CHANGED
|
@@ -614,7 +614,6 @@ async function login(chainId, params) {
|
|
|
614
614
|
// src/react/auth.ts
|
|
615
615
|
var TOKEN_STORAGE_PREFIX = "symm_access_token";
|
|
616
616
|
var TOKEN_STORAGE_VERSION = 1;
|
|
617
|
-
var tokenCache = /* @__PURE__ */ new Map();
|
|
618
617
|
function cacheKey(address, chainId) {
|
|
619
618
|
return `${address}:${chainId}`;
|
|
620
619
|
}
|
|
@@ -652,20 +651,11 @@ function writeStoredToken(address, chainId, cached) {
|
|
|
652
651
|
}
|
|
653
652
|
}
|
|
654
653
|
function getCachedToken(address, chainId) {
|
|
655
|
-
const cached = tokenCache.get(cacheKey(address, chainId));
|
|
656
|
-
if (cached && Date.now() < cached.expiresAt) {
|
|
657
|
-
return cached.token;
|
|
658
|
-
}
|
|
659
|
-
if (cached) {
|
|
660
|
-
tokenCache.delete(cacheKey(address, chainId));
|
|
661
|
-
}
|
|
662
654
|
const stored = readStoredToken(address, chainId);
|
|
663
655
|
if (!stored) return null;
|
|
664
|
-
tokenCache.set(cacheKey(address, chainId), stored);
|
|
665
656
|
return stored.token;
|
|
666
657
|
}
|
|
667
658
|
function clearCachedToken(address, chainId) {
|
|
668
|
-
tokenCache.delete(cacheKey(address, chainId));
|
|
669
659
|
if (typeof window !== "undefined") {
|
|
670
660
|
window.localStorage.removeItem(storageKey(address, chainId));
|
|
671
661
|
}
|
|
@@ -698,7 +688,6 @@ async function fetchAccessToken(walletClient, signerAddress, accountAddress, cha
|
|
|
698
688
|
token: accessToken,
|
|
699
689
|
expiresAt: expiresAt - 6e4
|
|
700
690
|
};
|
|
701
|
-
tokenCache.set(cacheKey(accountAddress, chainId), cachedToken);
|
|
702
691
|
writeStoredToken(accountAddress, chainId, cachedToken);
|
|
703
692
|
return accessToken;
|
|
704
693
|
}
|
|
@@ -735,13 +724,31 @@ var useSymmAuthStore = create((set, get) => ({
|
|
|
735
724
|
}));
|
|
736
725
|
|
|
737
726
|
// src/react/hooks/use-symm-auth.ts
|
|
727
|
+
function logSymmAuth(event, details) {
|
|
728
|
+
if (typeof window === "undefined") {
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
console.debug("[symm-auth]", event, details ?? {});
|
|
732
|
+
}
|
|
733
|
+
function extractSubaccountAddresses(summary) {
|
|
734
|
+
const accounts = summary?.data?.accounts;
|
|
735
|
+
if (!Array.isArray(accounts)) {
|
|
736
|
+
return [];
|
|
737
|
+
}
|
|
738
|
+
return accounts.flatMap((account) => {
|
|
739
|
+
const accountAddress = account.accountAddress ?? account.address;
|
|
740
|
+
return accountAddress ? [accountAddress] : [];
|
|
741
|
+
});
|
|
742
|
+
}
|
|
738
743
|
function useSymmAuth(params) {
|
|
739
|
-
const
|
|
744
|
+
const context = useSymmContext();
|
|
745
|
+
const ctx = context;
|
|
746
|
+
const { symmCoreClient } = context;
|
|
740
747
|
const address = params?.address ?? ctx.address;
|
|
741
748
|
const chainId = params?.chainId ?? ctx.chainId ?? 42161;
|
|
742
749
|
const walletClient = params?.walletClient ?? ctx.walletClient;
|
|
743
750
|
const siweDomain = params?.siweDomain;
|
|
744
|
-
const
|
|
751
|
+
const token = useSymmAuthStore((state) => {
|
|
745
752
|
if (address) {
|
|
746
753
|
return state.tokensByKey[symmAuthTokenKey(address, chainId)] ?? null;
|
|
747
754
|
}
|
|
@@ -752,36 +759,65 @@ function useSymmAuth(params) {
|
|
|
752
759
|
const clearToken = useSymmAuthStore((state) => state.clearToken);
|
|
753
760
|
const previousAddressRef = useRef(address);
|
|
754
761
|
const previousChainIdRef = useRef(chainId);
|
|
762
|
+
const refreshAuthRef = useRef(async () => null);
|
|
755
763
|
const refreshAuth = useCallback(
|
|
756
|
-
async (accountAddress) => {
|
|
764
|
+
async (accountAddress, options) => {
|
|
757
765
|
if (!walletClient || !address) return null;
|
|
758
766
|
const resolvedAccountAddress = accountAddress ?? address;
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
+
if (!options?.force) {
|
|
768
|
+
const inStore = getToken(resolvedAccountAddress, chainId);
|
|
769
|
+
if (inStore) {
|
|
770
|
+
logSymmAuth("refresh:store-hit", {
|
|
771
|
+
accountAddress: resolvedAccountAddress,
|
|
772
|
+
chainId
|
|
773
|
+
});
|
|
774
|
+
return inStore;
|
|
775
|
+
}
|
|
776
|
+
const cached = getCachedToken(resolvedAccountAddress, chainId);
|
|
777
|
+
if (cached) {
|
|
778
|
+
setToken(resolvedAccountAddress, chainId, cached);
|
|
779
|
+
logSymmAuth("refresh:local-storage-hit", {
|
|
780
|
+
accountAddress: resolvedAccountAddress,
|
|
781
|
+
chainId
|
|
782
|
+
});
|
|
783
|
+
return cached;
|
|
784
|
+
}
|
|
767
785
|
}
|
|
768
786
|
try {
|
|
769
|
-
|
|
787
|
+
logSymmAuth("refresh:fetch-start", {
|
|
788
|
+
signerAddress: address,
|
|
789
|
+
accountAddress: resolvedAccountAddress,
|
|
790
|
+
chainId,
|
|
791
|
+
force: options?.force ?? false
|
|
792
|
+
});
|
|
793
|
+
const token2 = await fetchAccessToken(
|
|
770
794
|
walletClient,
|
|
771
795
|
address,
|
|
772
796
|
resolvedAccountAddress,
|
|
773
797
|
chainId,
|
|
774
798
|
siweDomain
|
|
775
799
|
);
|
|
776
|
-
setToken(resolvedAccountAddress, chainId,
|
|
777
|
-
|
|
778
|
-
|
|
800
|
+
setToken(resolvedAccountAddress, chainId, token2);
|
|
801
|
+
logSymmAuth("refresh:fetch-success", {
|
|
802
|
+
signerAddress: address,
|
|
803
|
+
accountAddress: resolvedAccountAddress,
|
|
804
|
+
chainId
|
|
805
|
+
});
|
|
806
|
+
return token2;
|
|
807
|
+
} catch (error) {
|
|
779
808
|
clearToken(resolvedAccountAddress, chainId);
|
|
809
|
+
logSymmAuth("refresh:fetch-failed", {
|
|
810
|
+
signerAddress: address,
|
|
811
|
+
accountAddress: resolvedAccountAddress,
|
|
812
|
+
chainId,
|
|
813
|
+
error: error instanceof Error ? error.message : String(error)
|
|
814
|
+
});
|
|
780
815
|
return null;
|
|
781
816
|
}
|
|
782
817
|
},
|
|
783
818
|
[walletClient, address, chainId, siweDomain, getToken, setToken, clearToken]
|
|
784
819
|
);
|
|
820
|
+
refreshAuthRef.current = refreshAuth;
|
|
785
821
|
const clearAuth = useCallback(() => {
|
|
786
822
|
if (address) {
|
|
787
823
|
clearCachedToken(address, chainId);
|
|
@@ -793,26 +829,67 @@ function useSymmAuth(params) {
|
|
|
793
829
|
const previousChainId = previousChainIdRef.current;
|
|
794
830
|
const addressChanged = previousAddress !== address;
|
|
795
831
|
const chainChanged = previousChainId !== chainId;
|
|
832
|
+
let cancelled = false;
|
|
796
833
|
previousAddressRef.current = address;
|
|
797
834
|
previousChainIdRef.current = chainId;
|
|
798
|
-
if (!address
|
|
835
|
+
if (!address) {
|
|
799
836
|
return;
|
|
800
837
|
}
|
|
838
|
+
const userAddress = address;
|
|
801
839
|
if (previousAddress && (addressChanged || chainChanged)) {
|
|
802
840
|
clearCachedToken(previousAddress, previousChainId);
|
|
803
841
|
clearToken(previousAddress, previousChainId);
|
|
804
842
|
}
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
843
|
+
async function bootstrapAuth() {
|
|
844
|
+
let targets = [userAddress];
|
|
845
|
+
if (symmCoreClient) {
|
|
846
|
+
try {
|
|
847
|
+
const summary = await symmCoreClient.accounts.getSummary({
|
|
848
|
+
userAddress,
|
|
849
|
+
chainId
|
|
850
|
+
});
|
|
851
|
+
const subaccounts = extractSubaccountAddresses(summary);
|
|
852
|
+
if (subaccounts.length > 0) {
|
|
853
|
+
targets = subaccounts;
|
|
854
|
+
}
|
|
855
|
+
logSymmAuth("bootstrap:subaccounts-resolved", {
|
|
856
|
+
userAddress,
|
|
857
|
+
chainId,
|
|
858
|
+
subaccounts
|
|
859
|
+
});
|
|
860
|
+
} catch (error) {
|
|
861
|
+
logSymmAuth("bootstrap:subaccounts-failed", {
|
|
862
|
+
userAddress,
|
|
863
|
+
chainId,
|
|
864
|
+
error: error instanceof Error ? error.message : String(error)
|
|
865
|
+
});
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
const uniqueTargets = [...new Set(targets)].filter(
|
|
869
|
+
(accountAddress) => !!accountAddress
|
|
870
|
+
);
|
|
871
|
+
logSymmAuth("bootstrap:start", {
|
|
872
|
+
userAddress,
|
|
873
|
+
chainId,
|
|
874
|
+
targets: uniqueTargets,
|
|
875
|
+
walletReady: !!walletClient
|
|
876
|
+
});
|
|
877
|
+
for (const accountAddress of uniqueTargets) {
|
|
878
|
+
if (cancelled) {
|
|
879
|
+
return;
|
|
880
|
+
}
|
|
881
|
+
await refreshAuthRef.current(accountAddress);
|
|
882
|
+
}
|
|
810
883
|
}
|
|
811
|
-
|
|
884
|
+
void bootstrapAuth();
|
|
885
|
+
return () => {
|
|
886
|
+
cancelled = true;
|
|
887
|
+
};
|
|
888
|
+
}, [address, walletClient, chainId, clearToken, symmCoreClient]);
|
|
812
889
|
return {
|
|
813
|
-
accessToken,
|
|
814
|
-
authToken:
|
|
815
|
-
isAuthenticated: !!
|
|
890
|
+
accessToken: token,
|
|
891
|
+
authToken: token,
|
|
892
|
+
isAuthenticated: !!token,
|
|
816
893
|
refresh: refreshAuth,
|
|
817
894
|
refreshAuth,
|
|
818
895
|
clear: clearAuth
|
|
@@ -1921,10 +1998,12 @@ function useSymmCloseOrder(options) {
|
|
|
1921
1998
|
if (!symmCoreClient) {
|
|
1922
1999
|
throw new Error("symm-core client not available");
|
|
1923
2000
|
}
|
|
2001
|
+
const typedRequest = request;
|
|
2002
|
+
const resolvedAccountAddress = typedRequest.accountAddress ?? address;
|
|
1924
2003
|
return symmCoreClient.orders.close(request.id, {
|
|
1925
2004
|
kind: request.kind,
|
|
1926
2005
|
type: request.type,
|
|
1927
|
-
authToken: request.authToken ?? (
|
|
2006
|
+
authToken: request.authToken ?? (resolvedAccountAddress ? useSymmAuthStore.getState().getToken(resolvedAccountAddress, chainId) ?? void 0 : void 0)
|
|
1928
2007
|
});
|
|
1929
2008
|
}
|
|
1930
2009
|
});
|
|
@@ -25034,14 +25113,42 @@ function useSymmBalances(params) {
|
|
|
25034
25113
|
enabled: internalEnabled && (params.query?.enabled ?? true)
|
|
25035
25114
|
});
|
|
25036
25115
|
}
|
|
25037
|
-
function
|
|
25116
|
+
function logTradeAuth(event, details) {
|
|
25117
|
+
if (typeof window === "undefined") {
|
|
25118
|
+
return;
|
|
25119
|
+
}
|
|
25120
|
+
console.debug("[symm-trade]", event, details ?? {});
|
|
25121
|
+
}
|
|
25122
|
+
function splitTradeHookArgs(paramsOrOptions, options) {
|
|
25123
|
+
if (paramsOrOptions && "mutation" in paramsOrOptions) {
|
|
25124
|
+
return {
|
|
25125
|
+
params: {},
|
|
25126
|
+
options: paramsOrOptions
|
|
25127
|
+
};
|
|
25128
|
+
}
|
|
25129
|
+
return {
|
|
25130
|
+
params: paramsOrOptions ?? {},
|
|
25131
|
+
options
|
|
25132
|
+
};
|
|
25133
|
+
}
|
|
25134
|
+
function useResolveTradeAuthToken(params = {}) {
|
|
25038
25135
|
const context = useSymmContext();
|
|
25039
|
-
const
|
|
25040
|
-
const
|
|
25136
|
+
const address = params.address ?? context.address;
|
|
25137
|
+
const chainId = params.chainId ?? context.chainId;
|
|
25138
|
+
const { refreshAuth } = useSymmAuth({
|
|
25139
|
+
address,
|
|
25140
|
+
chainId,
|
|
25141
|
+
walletClient: params.walletClient,
|
|
25142
|
+
siweDomain: params.siweDomain
|
|
25143
|
+
});
|
|
25041
25144
|
const refreshAuthFromContext = context.refreshAuth;
|
|
25042
25145
|
return useCallback(
|
|
25043
25146
|
async (providedAuthToken, accountAddress) => {
|
|
25044
25147
|
if (providedAuthToken) {
|
|
25148
|
+
logTradeAuth("resolve-auth:provided", {
|
|
25149
|
+
accountAddress,
|
|
25150
|
+
chainId
|
|
25151
|
+
});
|
|
25045
25152
|
return providedAuthToken;
|
|
25046
25153
|
}
|
|
25047
25154
|
const resolvedAccountAddress = accountAddress ?? address;
|
|
@@ -25050,11 +25157,23 @@ function useResolveTradeAuthToken() {
|
|
|
25050
25157
|
}
|
|
25051
25158
|
const inMemoryToken = useSymmAuthStore.getState().getToken(resolvedAccountAddress, chainId);
|
|
25052
25159
|
if (inMemoryToken) {
|
|
25160
|
+
logTradeAuth("resolve-auth:store-hit", {
|
|
25161
|
+
accountAddress: resolvedAccountAddress,
|
|
25162
|
+
chainId
|
|
25163
|
+
});
|
|
25053
25164
|
return inMemoryToken;
|
|
25054
25165
|
}
|
|
25055
25166
|
if (refreshAuthFromContext) {
|
|
25167
|
+
logTradeAuth("resolve-auth:context-refresh", {
|
|
25168
|
+
accountAddress: resolvedAccountAddress,
|
|
25169
|
+
chainId
|
|
25170
|
+
});
|
|
25056
25171
|
return refreshAuthFromContext(resolvedAccountAddress);
|
|
25057
25172
|
}
|
|
25173
|
+
logTradeAuth("resolve-auth:hook-refresh", {
|
|
25174
|
+
accountAddress: resolvedAccountAddress,
|
|
25175
|
+
chainId
|
|
25176
|
+
});
|
|
25058
25177
|
return refreshAuth(resolvedAccountAddress);
|
|
25059
25178
|
},
|
|
25060
25179
|
[address, chainId, refreshAuth, refreshAuthFromContext]
|
|
@@ -25075,10 +25194,11 @@ function useSymmOpenBasketMutation(options) {
|
|
|
25075
25194
|
}
|
|
25076
25195
|
});
|
|
25077
25196
|
}
|
|
25078
|
-
function useSymmClosePositionMutation(
|
|
25197
|
+
function useSymmClosePositionMutation(paramsOrOptions, maybeOptions) {
|
|
25079
25198
|
const { symmCoreClient } = useSymmContext();
|
|
25080
25199
|
const queryClient = useQueryClient();
|
|
25081
|
-
const
|
|
25200
|
+
const { params, options } = splitTradeHookArgs(paramsOrOptions, maybeOptions);
|
|
25201
|
+
const resolveAuthToken = useResolveTradeAuthToken(params);
|
|
25082
25202
|
return useMutation({
|
|
25083
25203
|
...withSymmMutationConfig(options?.mutation, {
|
|
25084
25204
|
onSuccess: () => {
|
|
@@ -25132,10 +25252,11 @@ function useSymmCancelOpenMutation(options) {
|
|
|
25132
25252
|
}
|
|
25133
25253
|
});
|
|
25134
25254
|
}
|
|
25135
|
-
function useSymmUpdatePositionMutation(
|
|
25255
|
+
function useSymmUpdatePositionMutation(paramsOrOptions, maybeOptions) {
|
|
25136
25256
|
const { symmCoreClient } = useSymmContext();
|
|
25137
25257
|
const queryClient = useQueryClient();
|
|
25138
|
-
const
|
|
25258
|
+
const { params, options } = splitTradeHookArgs(paramsOrOptions, maybeOptions);
|
|
25259
|
+
const resolveAuthToken = useResolveTradeAuthToken(params);
|
|
25139
25260
|
return useMutation({
|
|
25140
25261
|
...withSymmMutationConfig(options?.mutation, {
|
|
25141
25262
|
onSuccess: () => {
|