@pear-protocol/symmio-client 0.2.33 → 0.2.35
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.js +117 -7
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +117 -7
- package/dist/react/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/react/index.mjs
CHANGED
|
@@ -724,8 +724,26 @@ var useSymmAuthStore = create((set, get) => ({
|
|
|
724
724
|
}));
|
|
725
725
|
|
|
726
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
|
+
}
|
|
727
743
|
function useSymmAuth(params) {
|
|
728
|
-
const
|
|
744
|
+
const context = useSymmContext();
|
|
745
|
+
const ctx = context;
|
|
746
|
+
const { symmCoreClient } = context;
|
|
729
747
|
const address = params?.address ?? ctx.address;
|
|
730
748
|
const chainId = params?.chainId ?? ctx.chainId ?? 42161;
|
|
731
749
|
const walletClient = params?.walletClient ?? ctx.walletClient;
|
|
@@ -749,15 +767,29 @@ function useSymmAuth(params) {
|
|
|
749
767
|
if (!options?.force) {
|
|
750
768
|
const inStore = getToken(resolvedAccountAddress, chainId);
|
|
751
769
|
if (inStore) {
|
|
770
|
+
logSymmAuth("refresh:store-hit", {
|
|
771
|
+
accountAddress: resolvedAccountAddress,
|
|
772
|
+
chainId
|
|
773
|
+
});
|
|
752
774
|
return inStore;
|
|
753
775
|
}
|
|
754
776
|
const cached = getCachedToken(resolvedAccountAddress, chainId);
|
|
755
777
|
if (cached) {
|
|
756
778
|
setToken(resolvedAccountAddress, chainId, cached);
|
|
779
|
+
logSymmAuth("refresh:local-storage-hit", {
|
|
780
|
+
accountAddress: resolvedAccountAddress,
|
|
781
|
+
chainId
|
|
782
|
+
});
|
|
757
783
|
return cached;
|
|
758
784
|
}
|
|
759
785
|
}
|
|
760
786
|
try {
|
|
787
|
+
logSymmAuth("refresh:fetch-start", {
|
|
788
|
+
signerAddress: address,
|
|
789
|
+
accountAddress: resolvedAccountAddress,
|
|
790
|
+
chainId,
|
|
791
|
+
force: options?.force ?? false
|
|
792
|
+
});
|
|
761
793
|
const token2 = await fetchAccessToken(
|
|
762
794
|
walletClient,
|
|
763
795
|
address,
|
|
@@ -766,9 +798,20 @@ function useSymmAuth(params) {
|
|
|
766
798
|
siweDomain
|
|
767
799
|
);
|
|
768
800
|
setToken(resolvedAccountAddress, chainId, token2);
|
|
801
|
+
logSymmAuth("refresh:fetch-success", {
|
|
802
|
+
signerAddress: address,
|
|
803
|
+
accountAddress: resolvedAccountAddress,
|
|
804
|
+
chainId
|
|
805
|
+
});
|
|
769
806
|
return token2;
|
|
770
|
-
} catch {
|
|
807
|
+
} catch (error) {
|
|
771
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
|
+
});
|
|
772
815
|
return null;
|
|
773
816
|
}
|
|
774
817
|
},
|
|
@@ -786,20 +829,63 @@ function useSymmAuth(params) {
|
|
|
786
829
|
const previousChainId = previousChainIdRef.current;
|
|
787
830
|
const addressChanged = previousAddress !== address;
|
|
788
831
|
const chainChanged = previousChainId !== chainId;
|
|
832
|
+
let cancelled = false;
|
|
789
833
|
previousAddressRef.current = address;
|
|
790
834
|
previousChainIdRef.current = chainId;
|
|
791
835
|
if (!address) {
|
|
792
836
|
return;
|
|
793
837
|
}
|
|
838
|
+
const userAddress = address;
|
|
794
839
|
if (previousAddress && (addressChanged || chainChanged)) {
|
|
795
840
|
clearCachedToken(previousAddress, previousChainId);
|
|
796
841
|
clearToken(previousAddress, previousChainId);
|
|
797
842
|
}
|
|
798
|
-
|
|
799
|
-
|
|
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
|
+
}
|
|
800
883
|
}
|
|
801
|
-
|
|
802
|
-
|
|
884
|
+
bootstrapAuth();
|
|
885
|
+
return () => {
|
|
886
|
+
cancelled = true;
|
|
887
|
+
};
|
|
888
|
+
}, [address, walletClient, chainId]);
|
|
803
889
|
return {
|
|
804
890
|
accessToken: token,
|
|
805
891
|
authToken: token,
|
|
@@ -1912,10 +1998,12 @@ function useSymmCloseOrder(options) {
|
|
|
1912
1998
|
if (!symmCoreClient) {
|
|
1913
1999
|
throw new Error("symm-core client not available");
|
|
1914
2000
|
}
|
|
2001
|
+
const typedRequest = request;
|
|
2002
|
+
const resolvedAccountAddress = typedRequest.accountAddress ?? address;
|
|
1915
2003
|
return symmCoreClient.orders.close(request.id, {
|
|
1916
2004
|
kind: request.kind,
|
|
1917
2005
|
type: request.type,
|
|
1918
|
-
authToken: request.authToken ?? (
|
|
2006
|
+
authToken: request.authToken ?? (resolvedAccountAddress ? useSymmAuthStore.getState().getToken(resolvedAccountAddress, chainId) ?? void 0 : void 0)
|
|
1919
2007
|
});
|
|
1920
2008
|
}
|
|
1921
2009
|
});
|
|
@@ -25025,6 +25113,12 @@ function useSymmBalances(params) {
|
|
|
25025
25113
|
enabled: internalEnabled && (params.query?.enabled ?? true)
|
|
25026
25114
|
});
|
|
25027
25115
|
}
|
|
25116
|
+
function logTradeAuth(event, details) {
|
|
25117
|
+
if (typeof window === "undefined") {
|
|
25118
|
+
return;
|
|
25119
|
+
}
|
|
25120
|
+
console.debug("[symm-trade]", event, details ?? {});
|
|
25121
|
+
}
|
|
25028
25122
|
function splitTradeHookArgs(paramsOrOptions, options) {
|
|
25029
25123
|
if (paramsOrOptions && "mutation" in paramsOrOptions) {
|
|
25030
25124
|
return {
|
|
@@ -25051,6 +25145,10 @@ function useResolveTradeAuthToken(params = {}) {
|
|
|
25051
25145
|
return useCallback(
|
|
25052
25146
|
async (providedAuthToken, accountAddress) => {
|
|
25053
25147
|
if (providedAuthToken) {
|
|
25148
|
+
logTradeAuth("resolve-auth:provided", {
|
|
25149
|
+
accountAddress,
|
|
25150
|
+
chainId
|
|
25151
|
+
});
|
|
25054
25152
|
return providedAuthToken;
|
|
25055
25153
|
}
|
|
25056
25154
|
const resolvedAccountAddress = accountAddress ?? address;
|
|
@@ -25059,11 +25157,23 @@ function useResolveTradeAuthToken(params = {}) {
|
|
|
25059
25157
|
}
|
|
25060
25158
|
const inMemoryToken = useSymmAuthStore.getState().getToken(resolvedAccountAddress, chainId);
|
|
25061
25159
|
if (inMemoryToken) {
|
|
25160
|
+
logTradeAuth("resolve-auth:store-hit", {
|
|
25161
|
+
accountAddress: resolvedAccountAddress,
|
|
25162
|
+
chainId
|
|
25163
|
+
});
|
|
25062
25164
|
return inMemoryToken;
|
|
25063
25165
|
}
|
|
25064
25166
|
if (refreshAuthFromContext) {
|
|
25167
|
+
logTradeAuth("resolve-auth:context-refresh", {
|
|
25168
|
+
accountAddress: resolvedAccountAddress,
|
|
25169
|
+
chainId
|
|
25170
|
+
});
|
|
25065
25171
|
return refreshAuthFromContext(resolvedAccountAddress);
|
|
25066
25172
|
}
|
|
25173
|
+
logTradeAuth("resolve-auth:hook-refresh", {
|
|
25174
|
+
accountAddress: resolvedAccountAddress,
|
|
25175
|
+
chainId
|
|
25176
|
+
});
|
|
25067
25177
|
return refreshAuth(resolvedAccountAddress);
|
|
25068
25178
|
},
|
|
25069
25179
|
[address, chainId, refreshAuth, refreshAuthFromContext]
|