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