@pear-protocol/symmio-client 0.2.41 → 0.2.43
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 +87 -62
- package/dist/react/index.d.ts +87 -62
- package/dist/react/index.js +380 -213
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +379 -214
- package/dist/react/index.mjs.map +1 -1
- package/dist/react/{provider-BoNiSPy9.d.mts → provider-CMBaM64o.d.mts} +3 -2
- package/dist/react/{provider-BoNiSPy9.d.ts → provider-CMBaM64o.d.ts} +3 -2
- package/dist/react/provider.d.mts +1 -1
- package/dist/react/provider.d.ts +1 -1
- package/dist/react/provider.js +3 -1
- package/dist/react/provider.js.map +1 -1
- package/dist/react/provider.mjs +3 -1
- package/dist/react/provider.mjs.map +1 -1
- package/package.json +1 -1
package/dist/react/index.js
CHANGED
|
@@ -5,8 +5,8 @@ var react = require('react');
|
|
|
5
5
|
var symmCore = require('@pear-protocol/symm-core');
|
|
6
6
|
var zustand = require('zustand');
|
|
7
7
|
var jsxRuntime = require('react/jsx-runtime');
|
|
8
|
-
var siwe = require('siwe');
|
|
9
8
|
var reactQuery = require('@tanstack/react-query');
|
|
9
|
+
var siwe = require('siwe');
|
|
10
10
|
var viem = require('viem');
|
|
11
11
|
|
|
12
12
|
var SymmContext = react.createContext(null);
|
|
@@ -493,6 +493,7 @@ function useBinanceWs(params) {
|
|
|
493
493
|
function SymmProvider({
|
|
494
494
|
chainId = 42161,
|
|
495
495
|
address,
|
|
496
|
+
walletClient,
|
|
496
497
|
symmCoreConfig = {
|
|
497
498
|
apiUrl: "https://nginx-server-staging.up.railway.app",
|
|
498
499
|
wsUrl: "wss://nginx-server-staging.up.railway.app"
|
|
@@ -512,9 +513,10 @@ function SymmProvider({
|
|
|
512
513
|
symmCoreClient,
|
|
513
514
|
chainId,
|
|
514
515
|
address,
|
|
516
|
+
walletClient,
|
|
515
517
|
symmioConfig
|
|
516
518
|
}),
|
|
517
|
-
[symmCoreClient, chainId, address, symmioConfig]
|
|
519
|
+
[symmCoreClient, chainId, address, walletClient, symmioConfig]
|
|
518
520
|
);
|
|
519
521
|
useBinanceWs({
|
|
520
522
|
symmCoreClient,
|
|
@@ -635,7 +637,7 @@ function cacheKey(address, chainId) {
|
|
|
635
637
|
function storageKey(address, chainId) {
|
|
636
638
|
return `${TOKEN_STORAGE_PREFIX}:${TOKEN_STORAGE_VERSION}:${cacheKey(address, chainId)}`;
|
|
637
639
|
}
|
|
638
|
-
function
|
|
640
|
+
function getCachedTokenEntry(address, chainId) {
|
|
639
641
|
if (typeof window === "undefined") return null;
|
|
640
642
|
try {
|
|
641
643
|
const raw = window.localStorage.getItem(storageKey(address, chainId));
|
|
@@ -665,17 +667,12 @@ function writeStoredToken(address, chainId, cached) {
|
|
|
665
667
|
} catch {
|
|
666
668
|
}
|
|
667
669
|
}
|
|
668
|
-
function getCachedToken(address, chainId) {
|
|
669
|
-
const stored = readStoredToken(address, chainId);
|
|
670
|
-
if (!stored) return null;
|
|
671
|
-
return stored.token;
|
|
672
|
-
}
|
|
673
670
|
function clearCachedToken(address, chainId) {
|
|
674
671
|
if (typeof window !== "undefined") {
|
|
675
672
|
window.localStorage.removeItem(storageKey(address, chainId));
|
|
676
673
|
}
|
|
677
674
|
}
|
|
678
|
-
async function
|
|
675
|
+
async function fetchAccessTokenEntry(walletClient, signerAddress, accountAddress, chainId, domain) {
|
|
679
676
|
const resolvedDomain = domain ?? (typeof window !== "undefined" ? window.location.host : "localhost");
|
|
680
677
|
const hedgerLoginUrl = new URL("login", getHedgerBaseUrl(chainId)).href;
|
|
681
678
|
const nonce = await getNonce(chainId, accountAddress);
|
|
@@ -704,39 +701,114 @@ async function fetchAccessToken(walletClient, signerAddress, accountAddress, cha
|
|
|
704
701
|
expiresAt: expiresAt - 6e4
|
|
705
702
|
};
|
|
706
703
|
writeStoredToken(accountAddress, chainId, cachedToken);
|
|
707
|
-
return
|
|
704
|
+
return cachedToken;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
// src/react/query-keys.ts
|
|
708
|
+
var symmKeys = {
|
|
709
|
+
all: ["symm"],
|
|
710
|
+
accounts: (address, chainId) => ["symm", "accounts", address, chainId],
|
|
711
|
+
accountsApi: (address, chainId) => ["symm", "accountsApi", address, chainId],
|
|
712
|
+
accountsLength: (address, chainId) => ["symm", "accountsLength", address, chainId],
|
|
713
|
+
accountsWithPositions: (address, chainId) => ["symm", "accountsWithPositions", address, chainId],
|
|
714
|
+
accountSummary: (address, chainId) => ["symm", "accountSummary", address, chainId],
|
|
715
|
+
accountData: (address, chainId, upnl) => ["symm", "accountData", address, chainId, upnl],
|
|
716
|
+
signature: (address, chainId) => ["symm", "signature", address, chainId],
|
|
717
|
+
auth: (accountAddress, chainId) => ["symm", "auth", accountAddress, chainId],
|
|
718
|
+
approval: (owner, spender, chainId, token) => ["symm", "approval", owner, spender, chainId, token],
|
|
719
|
+
balances: (address, chainId) => ["symm", "balances", address, chainId],
|
|
720
|
+
positions: (params) => ["symm", "positions", params],
|
|
721
|
+
openOrders: (params) => ["symm", "openOrders", params],
|
|
722
|
+
tradeHistory: (params) => ["symm", "tradeHistory", params],
|
|
723
|
+
tpslOrders: (address, chainId) => ["symm", "tpslOrders", address, chainId],
|
|
724
|
+
tpslOrdersList: (params) => ["symm", "tpslOrders", params],
|
|
725
|
+
twapOrders: (address, chainId) => ["symm", "twapOrders", address, chainId],
|
|
726
|
+
triggerOrders: (params) => ["symm", "triggerOrders", params],
|
|
727
|
+
triggerConfig: (orderId) => ["symm", "triggerConfig", orderId],
|
|
728
|
+
markets: (chainId, search) => ["symm", "markets", chainId, search],
|
|
729
|
+
hedgerMarketById: (id, chainId) => ["symm", "hedgerMarketById", id, chainId],
|
|
730
|
+
hedgerMarketBySymbol: (symbol, chainId) => ["symm", "hedgerMarketBySymbol", symbol, chainId],
|
|
731
|
+
lockedParams: (marketName, leverage, chainId) => ["symm", "lockedParams", marketName, leverage, chainId],
|
|
732
|
+
hedgerMarkets: (request) => ["symm", "hedgerMarkets", request],
|
|
733
|
+
fundingRates: (chainId) => ["symm", "fundingRates", chainId],
|
|
734
|
+
fundingPayments: (params) => ["symm", "fundingPayments", params],
|
|
735
|
+
fundingHistory: (params) => ["symm", "fundingHistory", params],
|
|
736
|
+
portfolio: (params) => ["symm", "portfolio", params],
|
|
737
|
+
notifications: (address, chainId) => ["symm", "notifications", address, chainId],
|
|
738
|
+
unreadCount: (address, chainId) => ["symm", "unreadCount", address, chainId],
|
|
739
|
+
availableMargin: (address, chainId) => ["symm", "availableMargin", address, chainId],
|
|
740
|
+
pendingIds: (address, chainId) => ["symm", "pendingIds", address, chainId],
|
|
741
|
+
pendingInstantOpens: (accountAddress, chainId) => ["symm", "pendingInstantOpens", accountAddress, chainId],
|
|
742
|
+
twapOrder: (orderId) => ["symm", "twapOrder", orderId],
|
|
743
|
+
delegation: (account, target, selectors, chainId) => ["symm", "delegation", account, target, selectors, chainId],
|
|
744
|
+
chartMetadata: (symbolsKey, positionKey) => ["symm", "chartMetadata", symbolsKey, positionKey]
|
|
745
|
+
};
|
|
746
|
+
|
|
747
|
+
// src/react/auth-cache.ts
|
|
748
|
+
function getAuthQueryData(queryClient, accountAddress, chainId) {
|
|
749
|
+
return queryClient.getQueryData(symmKeys.auth(accountAddress, chainId)) ?? null;
|
|
708
750
|
}
|
|
709
|
-
function
|
|
710
|
-
|
|
751
|
+
function setAuthQueryData(queryClient, accountAddress, chainId, entry) {
|
|
752
|
+
queryClient.setQueryData(symmKeys.auth(accountAddress, chainId), entry);
|
|
711
753
|
}
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
return get().tokensByKey[key] ?? null;
|
|
726
|
-
},
|
|
727
|
-
clearToken: (address, chainId) => {
|
|
728
|
-
const key = symmAuthTokenKey(address, chainId);
|
|
729
|
-
set((state) => {
|
|
730
|
-
if (!(key in state.tokensByKey)) return state;
|
|
731
|
-
const next = { ...state.tokensByKey };
|
|
732
|
-
delete next[key];
|
|
733
|
-
return { tokensByKey: next };
|
|
734
|
-
});
|
|
735
|
-
},
|
|
736
|
-
clearAll: () => {
|
|
737
|
-
set({ tokensByKey: {} });
|
|
754
|
+
function clearAuthQueryData(queryClient, accountAddress, chainId) {
|
|
755
|
+
queryClient.removeQueries({
|
|
756
|
+
queryKey: symmKeys.auth(accountAddress, chainId),
|
|
757
|
+
exact: true
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
function clearPersistedAuthState(accountAddress, chainId) {
|
|
761
|
+
clearCachedToken(accountAddress, chainId);
|
|
762
|
+
}
|
|
763
|
+
function getAuthTokenFromRuntimeCache(queryClient, accountAddress, chainId) {
|
|
764
|
+
const inQuery = getAuthQueryData(queryClient, accountAddress, chainId);
|
|
765
|
+
if (inQuery && inQuery.expiresAt > Date.now()) {
|
|
766
|
+
return inQuery.token;
|
|
738
767
|
}
|
|
739
|
-
|
|
768
|
+
const persisted = getCachedTokenEntry(accountAddress, chainId);
|
|
769
|
+
if (!persisted) {
|
|
770
|
+
return null;
|
|
771
|
+
}
|
|
772
|
+
setAuthQueryData(queryClient, accountAddress, chainId, persisted);
|
|
773
|
+
return persisted.token;
|
|
774
|
+
}
|
|
775
|
+
async function resolveAuthTokenEntry({
|
|
776
|
+
queryClient,
|
|
777
|
+
walletClient,
|
|
778
|
+
signerAddress,
|
|
779
|
+
accountAddress,
|
|
780
|
+
chainId,
|
|
781
|
+
siweDomain,
|
|
782
|
+
force = false
|
|
783
|
+
}) {
|
|
784
|
+
if (!walletClient || !signerAddress || !accountAddress) {
|
|
785
|
+
return null;
|
|
786
|
+
}
|
|
787
|
+
if (!force) {
|
|
788
|
+
const inQuery = getAuthQueryData(queryClient, accountAddress, chainId);
|
|
789
|
+
if (inQuery && inQuery.expiresAt > Date.now()) {
|
|
790
|
+
return inQuery;
|
|
791
|
+
}
|
|
792
|
+
const persisted = getCachedTokenEntry(accountAddress, chainId);
|
|
793
|
+
if (persisted) {
|
|
794
|
+
setAuthQueryData(queryClient, accountAddress, chainId, persisted);
|
|
795
|
+
return persisted;
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
const fresh = await fetchAccessTokenEntry(
|
|
799
|
+
walletClient,
|
|
800
|
+
signerAddress,
|
|
801
|
+
accountAddress,
|
|
802
|
+
chainId,
|
|
803
|
+
siweDomain
|
|
804
|
+
);
|
|
805
|
+
setAuthQueryData(queryClient, accountAddress, chainId, fresh);
|
|
806
|
+
return fresh;
|
|
807
|
+
}
|
|
808
|
+
function clearAuthState(queryClient, accountAddress, chainId) {
|
|
809
|
+
clearPersistedAuthState(accountAddress, chainId);
|
|
810
|
+
clearAuthQueryData(queryClient, accountAddress, chainId);
|
|
811
|
+
}
|
|
740
812
|
|
|
741
813
|
// src/react/hooks/use-symm-auth.ts
|
|
742
814
|
function logSymmAuth(event, details) {
|
|
@@ -747,47 +819,24 @@ function logSymmAuth(event, details) {
|
|
|
747
819
|
}
|
|
748
820
|
function useSymmAuth(params) {
|
|
749
821
|
const context = useSymmContext();
|
|
750
|
-
const
|
|
751
|
-
const address = params?.address ??
|
|
752
|
-
const chainId = params?.chainId ??
|
|
753
|
-
const walletClient = params?.walletClient ??
|
|
822
|
+
const queryClient = reactQuery.useQueryClient();
|
|
823
|
+
const address = params?.address ?? context.address;
|
|
824
|
+
const chainId = params?.chainId ?? context.chainId ?? 42161;
|
|
825
|
+
const walletClient = params?.walletClient ?? context.walletClient;
|
|
754
826
|
const siweDomain = params?.siweDomain;
|
|
755
|
-
const activeAccountAddress = params?.activeAccountAddress
|
|
756
|
-
const
|
|
757
|
-
if (activeAccountAddress) {
|
|
758
|
-
return state.tokensByKey[symmAuthTokenKey(activeAccountAddress, chainId)] ?? null;
|
|
759
|
-
}
|
|
760
|
-
return ctx.accessToken ?? ctx.authToken ?? null;
|
|
761
|
-
});
|
|
762
|
-
const setToken = useSymmAuthStore((state) => state.setToken);
|
|
763
|
-
const getToken = useSymmAuthStore((state) => state.getToken);
|
|
764
|
-
const clearToken = useSymmAuthStore((state) => state.clearToken);
|
|
765
|
-
const previousAddressRef = react.useRef(activeAccountAddress);
|
|
766
|
-
const previousChainIdRef = react.useRef(chainId);
|
|
767
|
-
const refreshAuthRef = react.useRef(async () => null);
|
|
827
|
+
const activeAccountAddress = params?.activeAccountAddress;
|
|
828
|
+
const canBootstrap = !!walletClient && !!address && !!activeAccountAddress;
|
|
768
829
|
const refreshAuth = react.useCallback(
|
|
769
830
|
async (accountAddress, options) => {
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
chainId
|
|
778
|
-
});
|
|
779
|
-
return inStore;
|
|
780
|
-
}
|
|
781
|
-
const cached = getCachedToken(resolvedAccountAddress, chainId);
|
|
782
|
-
if (cached) {
|
|
783
|
-
setToken(resolvedAccountAddress, chainId, cached);
|
|
784
|
-
logSymmAuth("refresh:local-storage-hit", {
|
|
785
|
-
accountAddress: resolvedAccountAddress,
|
|
786
|
-
chainId
|
|
787
|
-
});
|
|
788
|
-
return cached;
|
|
789
|
-
}
|
|
831
|
+
const resolvedAccountAddress = accountAddress ?? activeAccountAddress;
|
|
832
|
+
if (!resolvedAccountAddress) {
|
|
833
|
+
logSymmAuth("refresh:skip-no-active-account", {
|
|
834
|
+
signerAddress: address,
|
|
835
|
+
chainId
|
|
836
|
+
});
|
|
837
|
+
return null;
|
|
790
838
|
}
|
|
839
|
+
if (!walletClient || !address) return null;
|
|
791
840
|
try {
|
|
792
841
|
logSymmAuth("refresh:fetch-start", {
|
|
793
842
|
signerAddress: address,
|
|
@@ -795,14 +844,16 @@ function useSymmAuth(params) {
|
|
|
795
844
|
chainId,
|
|
796
845
|
force: options?.force ?? false
|
|
797
846
|
});
|
|
798
|
-
const
|
|
847
|
+
const tokenEntry = await resolveAuthTokenEntry({
|
|
848
|
+
queryClient,
|
|
799
849
|
walletClient,
|
|
800
|
-
address,
|
|
801
|
-
resolvedAccountAddress,
|
|
850
|
+
signerAddress: address,
|
|
851
|
+
accountAddress: resolvedAccountAddress,
|
|
802
852
|
chainId,
|
|
803
|
-
siweDomain
|
|
804
|
-
|
|
805
|
-
|
|
853
|
+
siweDomain,
|
|
854
|
+
force: options?.force
|
|
855
|
+
});
|
|
856
|
+
const token2 = tokenEntry?.token ?? null;
|
|
806
857
|
logSymmAuth("refresh:fetch-success", {
|
|
807
858
|
signerAddress: address,
|
|
808
859
|
accountAddress: resolvedAccountAddress,
|
|
@@ -810,7 +861,7 @@ function useSymmAuth(params) {
|
|
|
810
861
|
});
|
|
811
862
|
return token2;
|
|
812
863
|
} catch (error) {
|
|
813
|
-
|
|
864
|
+
clearPersistedAuthState(resolvedAccountAddress, chainId);
|
|
814
865
|
logSymmAuth("refresh:fetch-failed", {
|
|
815
866
|
signerAddress: address,
|
|
816
867
|
accountAddress: resolvedAccountAddress,
|
|
@@ -820,74 +871,49 @@ function useSymmAuth(params) {
|
|
|
820
871
|
return null;
|
|
821
872
|
}
|
|
822
873
|
},
|
|
823
|
-
[
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
const clearAuth = react.useCallback(() => {
|
|
827
|
-
if (activeAccountAddress) {
|
|
828
|
-
clearCachedToken(activeAccountAddress, chainId);
|
|
829
|
-
clearToken(activeAccountAddress, chainId);
|
|
830
|
-
}
|
|
831
|
-
}, [activeAccountAddress, chainId, clearToken]);
|
|
832
|
-
react.useEffect(() => {
|
|
833
|
-
const previousAddress = previousAddressRef.current;
|
|
834
|
-
const previousChainId = previousChainIdRef.current;
|
|
835
|
-
const addressChanged = previousAddress !== activeAccountAddress;
|
|
836
|
-
const chainChanged = previousChainId !== chainId;
|
|
837
|
-
let cancelled = false;
|
|
838
|
-
previousAddressRef.current = activeAccountAddress;
|
|
839
|
-
previousChainIdRef.current = chainId;
|
|
840
|
-
logSymmAuth("bootstrap:dependencies", {
|
|
841
|
-
signerAddress: address,
|
|
874
|
+
[
|
|
875
|
+
walletClient,
|
|
876
|
+
address,
|
|
842
877
|
activeAccountAddress,
|
|
843
878
|
chainId,
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
chainId,
|
|
855
|
-
hasWalletClient: !!walletClient
|
|
856
|
-
});
|
|
857
|
-
return;
|
|
858
|
-
}
|
|
859
|
-
if (previousAddress && (addressChanged || chainChanged)) {
|
|
860
|
-
clearCachedToken(previousAddress, previousChainId);
|
|
861
|
-
clearToken(previousAddress, previousChainId);
|
|
862
|
-
}
|
|
863
|
-
async function bootstrapAuth() {
|
|
864
|
-
logSymmAuth("bootstrap:run", {
|
|
865
|
-
signerAddress: address,
|
|
866
|
-
activeAccountAddress,
|
|
867
|
-
chainId,
|
|
868
|
-
hasWalletClient: !!walletClient,
|
|
869
|
-
hasRefreshAuth: !!refreshAuthRef.current
|
|
870
|
-
});
|
|
871
|
-
logSymmAuth("bootstrap:start", {
|
|
879
|
+
siweDomain,
|
|
880
|
+
queryClient
|
|
881
|
+
]
|
|
882
|
+
);
|
|
883
|
+
const authQuery = reactQuery.useQuery({
|
|
884
|
+
queryKey: symmKeys.auth(activeAccountAddress, chainId),
|
|
885
|
+
queryFn: async () => {
|
|
886
|
+
const tokenEntry = await resolveAuthTokenEntry({
|
|
887
|
+
queryClient,
|
|
888
|
+
walletClient,
|
|
872
889
|
signerAddress: address,
|
|
890
|
+
accountAddress: activeAccountAddress,
|
|
873
891
|
chainId,
|
|
874
|
-
|
|
875
|
-
walletReady: !!walletClient
|
|
892
|
+
siweDomain
|
|
876
893
|
});
|
|
877
|
-
if (
|
|
878
|
-
return;
|
|
894
|
+
if (!tokenEntry) {
|
|
895
|
+
return null;
|
|
879
896
|
}
|
|
880
|
-
|
|
897
|
+
return tokenEntry;
|
|
898
|
+
},
|
|
899
|
+
enabled: canBootstrap,
|
|
900
|
+
retry: false,
|
|
901
|
+
refetchOnWindowFocus: false,
|
|
902
|
+
refetchOnReconnect: false
|
|
903
|
+
});
|
|
904
|
+
const clearAuth = react.useCallback(() => {
|
|
905
|
+
if (activeAccountAddress) {
|
|
906
|
+
clearAuthState(queryClient, activeAccountAddress, chainId);
|
|
881
907
|
}
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
cancelled = true;
|
|
885
|
-
};
|
|
886
|
-
}, [address, activeAccountAddress, walletClient, chainId, clearToken]);
|
|
908
|
+
}, [activeAccountAddress, chainId, queryClient]);
|
|
909
|
+
const token = authQuery.data?.token ?? null;
|
|
887
910
|
return {
|
|
888
911
|
accessToken: token,
|
|
889
912
|
authToken: token,
|
|
890
913
|
isAuthenticated: !!token,
|
|
914
|
+
isLoading: authQuery.isLoading,
|
|
915
|
+
isFetching: authQuery.isFetching,
|
|
916
|
+
error: authQuery.error,
|
|
891
917
|
refresh: refreshAuth,
|
|
892
918
|
refreshAuth,
|
|
893
919
|
clear: clearAuth
|
|
@@ -1379,6 +1405,30 @@ function prepareDelegateAccess(multiAccount, account, params) {
|
|
|
1379
1405
|
config: { account, to: multiAccount, data, value: 0n }
|
|
1380
1406
|
};
|
|
1381
1407
|
}
|
|
1408
|
+
function prepareProposeRevoke(multiAccount, account, params) {
|
|
1409
|
+
const data = viem.encodeFunctionData({
|
|
1410
|
+
abi: MultiAccountABI,
|
|
1411
|
+
functionName: "proposeToRevokeAccesses",
|
|
1412
|
+
args: [params.account, params.target, params.selectors]
|
|
1413
|
+
});
|
|
1414
|
+
return {
|
|
1415
|
+
functionName: "proposeToRevokeAccesses",
|
|
1416
|
+
args: [params.account, params.target, params.selectors],
|
|
1417
|
+
config: { account, to: multiAccount, data, value: 0n }
|
|
1418
|
+
};
|
|
1419
|
+
}
|
|
1420
|
+
function prepareRevokeAccess(multiAccount, account, params) {
|
|
1421
|
+
const data = viem.encodeFunctionData({
|
|
1422
|
+
abi: MultiAccountABI,
|
|
1423
|
+
functionName: "revokeAccesses",
|
|
1424
|
+
args: [params.account, params.target, params.selectors]
|
|
1425
|
+
});
|
|
1426
|
+
return {
|
|
1427
|
+
functionName: "revokeAccesses",
|
|
1428
|
+
args: [params.account, params.target, params.selectors],
|
|
1429
|
+
config: { account, to: multiAccount, data, value: 0n }
|
|
1430
|
+
};
|
|
1431
|
+
}
|
|
1382
1432
|
async function delegateAccess(walletClient, publicClient, multiAccount, params) {
|
|
1383
1433
|
const account = walletClient.account?.address;
|
|
1384
1434
|
if (!account) throw new Error("Wallet client has no account");
|
|
@@ -1390,6 +1440,28 @@ async function delegateAccess(walletClient, publicClient, multiAccount, params)
|
|
|
1390
1440
|
chain: walletClient.chain
|
|
1391
1441
|
});
|
|
1392
1442
|
}
|
|
1443
|
+
async function proposeRevoke(walletClient, publicClient, multiAccount, params) {
|
|
1444
|
+
const account = walletClient.account?.address;
|
|
1445
|
+
if (!account) throw new Error("Wallet client has no account");
|
|
1446
|
+
const prepared = prepareProposeRevoke(multiAccount, account, params);
|
|
1447
|
+
const gas = await publicClient.estimateGas(prepared.config);
|
|
1448
|
+
return walletClient.sendTransaction({
|
|
1449
|
+
...prepared.config,
|
|
1450
|
+
gas: calculateGasMargin(gas),
|
|
1451
|
+
chain: walletClient.chain
|
|
1452
|
+
});
|
|
1453
|
+
}
|
|
1454
|
+
async function revokeAccess(walletClient, publicClient, multiAccount, params) {
|
|
1455
|
+
const account = walletClient.account?.address;
|
|
1456
|
+
if (!account) throw new Error("Wallet client has no account");
|
|
1457
|
+
const prepared = prepareRevokeAccess(multiAccount, account, params);
|
|
1458
|
+
const gas = await publicClient.estimateGas(prepared.config);
|
|
1459
|
+
return walletClient.sendTransaction({
|
|
1460
|
+
...prepared.config,
|
|
1461
|
+
gas: calculateGasMargin(gas),
|
|
1462
|
+
chain: walletClient.chain
|
|
1463
|
+
});
|
|
1464
|
+
}
|
|
1393
1465
|
async function hasDelegatedAccess(publicClient, multiAccount, params) {
|
|
1394
1466
|
validateAddress(params.account, "subAccount");
|
|
1395
1467
|
validateAddress(params.target, "target");
|
|
@@ -1402,52 +1474,61 @@ async function hasDelegatedAccess(publicClient, multiAccount, params) {
|
|
|
1402
1474
|
return Boolean(result);
|
|
1403
1475
|
}
|
|
1404
1476
|
|
|
1405
|
-
// src/react/
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
hedgerMarkets: (request) => ["symm", "hedgerMarkets", request],
|
|
1430
|
-
fundingRates: (chainId) => ["symm", "fundingRates", chainId],
|
|
1431
|
-
fundingPayments: (params) => ["symm", "fundingPayments", params],
|
|
1432
|
-
fundingHistory: (params) => ["symm", "fundingHistory", params],
|
|
1433
|
-
portfolio: (params) => ["symm", "portfolio", params],
|
|
1434
|
-
notifications: (address, chainId) => ["symm", "notifications", address, chainId],
|
|
1435
|
-
unreadCount: (address, chainId) => ["symm", "unreadCount", address, chainId],
|
|
1436
|
-
availableMargin: (address, chainId) => ["symm", "availableMargin", address, chainId],
|
|
1437
|
-
pendingIds: (address, chainId) => ["symm", "pendingIds", address, chainId],
|
|
1438
|
-
pendingInstantOpens: (accountAddress, chainId) => ["symm", "pendingInstantOpens", accountAddress, chainId],
|
|
1439
|
-
twapOrder: (orderId) => ["symm", "twapOrder", orderId],
|
|
1440
|
-
delegation: (account, target, selectors, chainId) => ["symm", "delegation", account, target, selectors, chainId],
|
|
1441
|
-
chartMetadata: (symbolsKey, positionKey) => ["symm", "chartMetadata", symbolsKey, positionKey]
|
|
1442
|
-
};
|
|
1477
|
+
// src/react/mutation-config.ts
|
|
1478
|
+
function withSymmMutationConfig(config, internal) {
|
|
1479
|
+
const {
|
|
1480
|
+
onSuccess: configOnSuccess,
|
|
1481
|
+
onError: configOnError,
|
|
1482
|
+
onSettled: configOnSettled,
|
|
1483
|
+
...rest
|
|
1484
|
+
} = config ?? {};
|
|
1485
|
+
return {
|
|
1486
|
+
...rest,
|
|
1487
|
+
onSuccess: async (...args) => {
|
|
1488
|
+
await internal?.onSuccess?.(...args);
|
|
1489
|
+
await configOnSuccess?.(...args);
|
|
1490
|
+
},
|
|
1491
|
+
onError: async (...args) => {
|
|
1492
|
+
await internal?.onError?.(...args);
|
|
1493
|
+
await configOnError?.(...args);
|
|
1494
|
+
},
|
|
1495
|
+
onSettled: async (...args) => {
|
|
1496
|
+
await internal?.onSettled?.(...args);
|
|
1497
|
+
await configOnSettled?.(...args);
|
|
1498
|
+
}
|
|
1499
|
+
};
|
|
1500
|
+
}
|
|
1443
1501
|
|
|
1444
1502
|
// src/react/hooks/use-symm-delegation.ts
|
|
1445
|
-
function
|
|
1503
|
+
function useResolvedDelegationConfig() {
|
|
1446
1504
|
const { chainId, address, symmioConfig } = useSymmContext();
|
|
1447
1505
|
const multiAccount = symmioConfig?.multiAccountAddress ?? getAddress(MULTI_ACCOUNT_ADDRESS, chainId, "MultiAccount");
|
|
1506
|
+
const target = DEFAULT_PARTY_B_ADDRESS[chainId];
|
|
1507
|
+
return {
|
|
1508
|
+
address,
|
|
1509
|
+
chainId,
|
|
1510
|
+
defaultTarget: target,
|
|
1511
|
+
multiAccount
|
|
1512
|
+
};
|
|
1513
|
+
}
|
|
1514
|
+
function resolveDelegationRequest(variables, defaultTarget, defaultSelectors) {
|
|
1515
|
+
const target = variables.target ?? defaultTarget;
|
|
1516
|
+
const selectors = variables.selectors ?? defaultSelectors ?? ALL_TRADING_SELECTORS;
|
|
1517
|
+
if (!target) throw new Error("delegation target is not configured");
|
|
1518
|
+
if (selectors.length === 0) {
|
|
1519
|
+
throw new Error("at least one delegation selector is required");
|
|
1520
|
+
}
|
|
1521
|
+
return {
|
|
1522
|
+
account: variables.account,
|
|
1523
|
+
selectors: [...selectors],
|
|
1524
|
+
target
|
|
1525
|
+
};
|
|
1526
|
+
}
|
|
1527
|
+
function useSymmDelegation(params) {
|
|
1528
|
+
const { chainId, address, multiAccount, defaultTarget } = useResolvedDelegationConfig();
|
|
1448
1529
|
const publicClient = params?.publicClient;
|
|
1449
1530
|
const accountAddress = params?.accountAddress ?? address;
|
|
1450
|
-
const target = params?.target ??
|
|
1531
|
+
const target = params?.target ?? defaultTarget;
|
|
1451
1532
|
const selectors = params?.selectors ?? ALL_TRADING_SELECTORS;
|
|
1452
1533
|
const consumerEnabled = params?.query?.enabled ?? params?.enabled ?? true;
|
|
1453
1534
|
const internalEnabled = !!publicClient && !!accountAddress && !!target && selectors.length > 0;
|
|
@@ -1482,6 +1563,110 @@ function useSymmDelegation(params) {
|
|
|
1482
1563
|
}
|
|
1483
1564
|
});
|
|
1484
1565
|
}
|
|
1566
|
+
function useSymmDelegateAccessMutation(params = {}, options) {
|
|
1567
|
+
const queryClient = reactQuery.useQueryClient();
|
|
1568
|
+
const { chainId, multiAccount, defaultTarget } = useResolvedDelegationConfig();
|
|
1569
|
+
const { publicClient, walletClient } = params;
|
|
1570
|
+
return reactQuery.useMutation({
|
|
1571
|
+
...withSymmMutationConfig(options?.mutation, {
|
|
1572
|
+
onSuccess: async (_data, variables) => {
|
|
1573
|
+
const request = resolveDelegationRequest(
|
|
1574
|
+
variables,
|
|
1575
|
+
params.target ?? defaultTarget,
|
|
1576
|
+
params.selectors
|
|
1577
|
+
);
|
|
1578
|
+
await queryClient.invalidateQueries({
|
|
1579
|
+
queryKey: symmKeys.delegation(
|
|
1580
|
+
request.account,
|
|
1581
|
+
request.target,
|
|
1582
|
+
request.selectors,
|
|
1583
|
+
chainId
|
|
1584
|
+
)
|
|
1585
|
+
});
|
|
1586
|
+
}
|
|
1587
|
+
}),
|
|
1588
|
+
mutationFn: async (variables) => {
|
|
1589
|
+
if (!walletClient || !publicClient) throw new Error("Clients not available");
|
|
1590
|
+
const request = resolveDelegationRequest(
|
|
1591
|
+
variables,
|
|
1592
|
+
params.target ?? defaultTarget,
|
|
1593
|
+
params.selectors
|
|
1594
|
+
);
|
|
1595
|
+
return delegateAccess(walletClient, publicClient, multiAccount, {
|
|
1596
|
+
account: request.account,
|
|
1597
|
+
target: request.target,
|
|
1598
|
+
selectors: request.selectors,
|
|
1599
|
+
activate: true
|
|
1600
|
+
});
|
|
1601
|
+
}
|
|
1602
|
+
});
|
|
1603
|
+
}
|
|
1604
|
+
function useSymmProposeRevokeDelegationMutation(params = {}, options) {
|
|
1605
|
+
const queryClient = reactQuery.useQueryClient();
|
|
1606
|
+
const { chainId, multiAccount, defaultTarget } = useResolvedDelegationConfig();
|
|
1607
|
+
const { publicClient, walletClient } = params;
|
|
1608
|
+
return reactQuery.useMutation({
|
|
1609
|
+
...withSymmMutationConfig(options?.mutation, {
|
|
1610
|
+
onSuccess: async (_data, variables) => {
|
|
1611
|
+
const request = resolveDelegationRequest(
|
|
1612
|
+
variables,
|
|
1613
|
+
params.target ?? defaultTarget,
|
|
1614
|
+
params.selectors
|
|
1615
|
+
);
|
|
1616
|
+
await queryClient.invalidateQueries({
|
|
1617
|
+
queryKey: symmKeys.delegation(
|
|
1618
|
+
request.account,
|
|
1619
|
+
request.target,
|
|
1620
|
+
request.selectors,
|
|
1621
|
+
chainId
|
|
1622
|
+
)
|
|
1623
|
+
});
|
|
1624
|
+
}
|
|
1625
|
+
}),
|
|
1626
|
+
mutationFn: async (variables) => {
|
|
1627
|
+
if (!walletClient || !publicClient) throw new Error("Clients not available");
|
|
1628
|
+
const request = resolveDelegationRequest(
|
|
1629
|
+
variables,
|
|
1630
|
+
params.target ?? defaultTarget,
|
|
1631
|
+
params.selectors
|
|
1632
|
+
);
|
|
1633
|
+
return proposeRevoke(walletClient, publicClient, multiAccount, request);
|
|
1634
|
+
}
|
|
1635
|
+
});
|
|
1636
|
+
}
|
|
1637
|
+
function useSymmRevokeDelegationMutation(params = {}, options) {
|
|
1638
|
+
const queryClient = reactQuery.useQueryClient();
|
|
1639
|
+
const { chainId, multiAccount, defaultTarget } = useResolvedDelegationConfig();
|
|
1640
|
+
const { publicClient, walletClient } = params;
|
|
1641
|
+
return reactQuery.useMutation({
|
|
1642
|
+
...withSymmMutationConfig(options?.mutation, {
|
|
1643
|
+
onSuccess: async (_data, variables) => {
|
|
1644
|
+
const request = resolveDelegationRequest(
|
|
1645
|
+
variables,
|
|
1646
|
+
params.target ?? defaultTarget,
|
|
1647
|
+
params.selectors
|
|
1648
|
+
);
|
|
1649
|
+
await queryClient.invalidateQueries({
|
|
1650
|
+
queryKey: symmKeys.delegation(
|
|
1651
|
+
request.account,
|
|
1652
|
+
request.target,
|
|
1653
|
+
request.selectors,
|
|
1654
|
+
chainId
|
|
1655
|
+
)
|
|
1656
|
+
});
|
|
1657
|
+
}
|
|
1658
|
+
}),
|
|
1659
|
+
mutationFn: async (variables) => {
|
|
1660
|
+
if (!walletClient || !publicClient) throw new Error("Clients not available");
|
|
1661
|
+
const request = resolveDelegationRequest(
|
|
1662
|
+
variables,
|
|
1663
|
+
params.target ?? defaultTarget,
|
|
1664
|
+
params.selectors
|
|
1665
|
+
);
|
|
1666
|
+
return revokeAccess(walletClient, publicClient, multiAccount, request);
|
|
1667
|
+
}
|
|
1668
|
+
});
|
|
1669
|
+
}
|
|
1485
1670
|
|
|
1486
1671
|
// src/react/cache.ts
|
|
1487
1672
|
function invalidateBalances(qc) {
|
|
@@ -1503,31 +1688,6 @@ function invalidateOrders(qc) {
|
|
|
1503
1688
|
qc.invalidateQueries({ queryKey: ["symm", "triggerOrders"] });
|
|
1504
1689
|
}
|
|
1505
1690
|
|
|
1506
|
-
// src/react/mutation-config.ts
|
|
1507
|
-
function withSymmMutationConfig(config, internal) {
|
|
1508
|
-
const {
|
|
1509
|
-
onSuccess: configOnSuccess,
|
|
1510
|
-
onError: configOnError,
|
|
1511
|
-
onSettled: configOnSettled,
|
|
1512
|
-
...rest
|
|
1513
|
-
} = config ?? {};
|
|
1514
|
-
return {
|
|
1515
|
-
...rest,
|
|
1516
|
-
onSuccess: async (...args) => {
|
|
1517
|
-
await internal?.onSuccess?.(...args);
|
|
1518
|
-
await configOnSuccess?.(...args);
|
|
1519
|
-
},
|
|
1520
|
-
onError: async (...args) => {
|
|
1521
|
-
await internal?.onError?.(...args);
|
|
1522
|
-
await configOnError?.(...args);
|
|
1523
|
-
},
|
|
1524
|
-
onSettled: async (...args) => {
|
|
1525
|
-
await internal?.onSettled?.(...args);
|
|
1526
|
-
await configOnSettled?.(...args);
|
|
1527
|
-
}
|
|
1528
|
-
};
|
|
1529
|
-
}
|
|
1530
|
-
|
|
1531
1691
|
// src/react/hooks/use-symm-instant-trade.ts
|
|
1532
1692
|
function useInstantTradeDeps(params) {
|
|
1533
1693
|
const { symmCoreClient, chainId, address, symmioConfig } = useSymmContext();
|
|
@@ -1557,7 +1717,7 @@ async function ensureInstantTradeReady(deps, queryClient, request) {
|
|
|
1557
1717
|
if (selectors.length === 0) {
|
|
1558
1718
|
throw new Error("at least one delegation selector is required");
|
|
1559
1719
|
}
|
|
1560
|
-
const accessToken =
|
|
1720
|
+
const accessToken = getAuthTokenFromRuntimeCache(queryClient, accountAddress, chainId);
|
|
1561
1721
|
if (!accessToken) {
|
|
1562
1722
|
throw new Error("auth token is required for instant trading");
|
|
1563
1723
|
}
|
|
@@ -1970,7 +2130,7 @@ function useSymmCancelClose(options) {
|
|
|
1970
2130
|
}
|
|
1971
2131
|
const resolvedAccountAddress = accountAddress ?? address;
|
|
1972
2132
|
const resolvedChainId = overrideChainId ?? chainId;
|
|
1973
|
-
const resolvedAuthToken = authToken ?? (resolvedAccountAddress ?
|
|
2133
|
+
const resolvedAuthToken = authToken ?? (resolvedAccountAddress ? getAuthTokenFromRuntimeCache(queryClient, resolvedAccountAddress, resolvedChainId) : null);
|
|
1974
2134
|
if (!resolvedAuthToken) {
|
|
1975
2135
|
throw new Error("auth token is required to cancel a pending close");
|
|
1976
2136
|
}
|
|
@@ -2001,7 +2161,7 @@ function useSymmCloseOrder(options) {
|
|
|
2001
2161
|
return symmCoreClient.orders.close(request.id, {
|
|
2002
2162
|
kind: request.kind,
|
|
2003
2163
|
type: request.type,
|
|
2004
|
-
authToken: request.authToken ?? (resolvedAccountAddress ?
|
|
2164
|
+
authToken: request.authToken ?? (resolvedAccountAddress ? getAuthTokenFromRuntimeCache(queryClient, resolvedAccountAddress, chainId) ?? void 0 : void 0)
|
|
2005
2165
|
});
|
|
2006
2166
|
}
|
|
2007
2167
|
});
|
|
@@ -25131,11 +25291,11 @@ function splitTradeHookArgs(paramsOrOptions, options) {
|
|
|
25131
25291
|
}
|
|
25132
25292
|
function useResolveTradeAuthToken(params = {}) {
|
|
25133
25293
|
const context = useSymmContext();
|
|
25294
|
+
const queryClient = reactQuery.useQueryClient();
|
|
25134
25295
|
const address = params.address ?? context.address;
|
|
25135
25296
|
const chainId = params.chainId ?? context.chainId;
|
|
25136
25297
|
const { refreshAuth } = useSymmAuth({
|
|
25137
25298
|
address,
|
|
25138
|
-
activeAccountAddress: address,
|
|
25139
25299
|
chainId,
|
|
25140
25300
|
walletClient: params.walletClient,
|
|
25141
25301
|
siweDomain: params.siweDomain
|
|
@@ -25154,7 +25314,11 @@ function useResolveTradeAuthToken(params = {}) {
|
|
|
25154
25314
|
if (!resolvedAccountAddress) {
|
|
25155
25315
|
return null;
|
|
25156
25316
|
}
|
|
25157
|
-
const inMemoryToken =
|
|
25317
|
+
const inMemoryToken = getAuthTokenFromRuntimeCache(
|
|
25318
|
+
queryClient,
|
|
25319
|
+
resolvedAccountAddress,
|
|
25320
|
+
chainId
|
|
25321
|
+
);
|
|
25158
25322
|
if (inMemoryToken) {
|
|
25159
25323
|
logTradeAuth("resolve-auth:store-hit", {
|
|
25160
25324
|
accountAddress: resolvedAccountAddress,
|
|
@@ -25175,7 +25339,7 @@ function useResolveTradeAuthToken(params = {}) {
|
|
|
25175
25339
|
});
|
|
25176
25340
|
return refreshAuth(resolvedAccountAddress);
|
|
25177
25341
|
},
|
|
25178
|
-
[address, chainId, refreshAuth, refreshAuthFromContext]
|
|
25342
|
+
[address, chainId, queryClient, refreshAuth, refreshAuthFromContext]
|
|
25179
25343
|
);
|
|
25180
25344
|
}
|
|
25181
25345
|
function useSymmOpenBasketMutation(options) {
|
|
@@ -25914,6 +26078,7 @@ function useSymmPendingInstantOpens(params) {
|
|
|
25914
26078
|
symmCoreClient,
|
|
25915
26079
|
chainId: ctxChainId
|
|
25916
26080
|
} = useSymmContext();
|
|
26081
|
+
const queryClient = reactQuery.useQueryClient();
|
|
25917
26082
|
const { accountAddress, authToken: providedAuthToken } = params;
|
|
25918
26083
|
const chainId = params.chainId ?? ctxChainId;
|
|
25919
26084
|
const internalEnabled = !!symmCoreClient && !!accountAddress;
|
|
@@ -25921,7 +26086,7 @@ function useSymmPendingInstantOpens(params) {
|
|
|
25921
26086
|
...params.query,
|
|
25922
26087
|
queryKey: symmKeys.pendingInstantOpens(accountAddress, chainId),
|
|
25923
26088
|
queryFn: async () => {
|
|
25924
|
-
const authToken = providedAuthToken ??
|
|
26089
|
+
const authToken = providedAuthToken ?? getAuthTokenFromRuntimeCache(queryClient, accountAddress, chainId);
|
|
25925
26090
|
if (!authToken) {
|
|
25926
26091
|
throw new Error("failed to acquire auth token for pending instant opens");
|
|
25927
26092
|
}
|
|
@@ -26816,7 +26981,6 @@ exports.useSymmAllocateCollateralMutation = useSymmAllocateCollateralMutation;
|
|
|
26816
26981
|
exports.useSymmApprovalQuery = useSymmApprovalQuery;
|
|
26817
26982
|
exports.useSymmApproveMutation = useSymmApproveMutation;
|
|
26818
26983
|
exports.useSymmAuth = useSymmAuth;
|
|
26819
|
-
exports.useSymmAuthStore = useSymmAuthStore;
|
|
26820
26984
|
exports.useSymmAvailableMargin = useSymmAvailableMargin;
|
|
26821
26985
|
exports.useSymmBalances = useSymmBalances;
|
|
26822
26986
|
exports.useSymmCancelClose = useSymmCancelClose;
|
|
@@ -26833,6 +26997,7 @@ exports.useSymmContext = useSymmContext;
|
|
|
26833
26997
|
exports.useSymmCoreClient = useSymmCoreClient;
|
|
26834
26998
|
exports.useSymmCreateAccountMutation = useSymmCreateAccountMutation;
|
|
26835
26999
|
exports.useSymmDeallocateCollateralMutation = useSymmDeallocateCollateralMutation;
|
|
27000
|
+
exports.useSymmDelegateAccessMutation = useSymmDelegateAccessMutation;
|
|
26836
27001
|
exports.useSymmDelegation = useSymmDelegation;
|
|
26837
27002
|
exports.useSymmDepositAndAllocateMutation = useSymmDepositAndAllocateMutation;
|
|
26838
27003
|
exports.useSymmDepositMutation = useSymmDepositMutation;
|
|
@@ -26857,6 +27022,8 @@ exports.useSymmPendingInstantOpens = useSymmPendingInstantOpens;
|
|
|
26857
27022
|
exports.useSymmPerformanceOverlays = useSymmPerformanceOverlays;
|
|
26858
27023
|
exports.useSymmPortfolio = useSymmPortfolio;
|
|
26859
27024
|
exports.useSymmPositions = useSymmPositions;
|
|
27025
|
+
exports.useSymmProposeRevokeDelegationMutation = useSymmProposeRevokeDelegationMutation;
|
|
27026
|
+
exports.useSymmRevokeDelegationMutation = useSymmRevokeDelegationMutation;
|
|
26860
27027
|
exports.useSymmSetTpslMutation = useSymmSetTpslMutation;
|
|
26861
27028
|
exports.useSymmSetTriggerConfigMutation = useSymmSetTriggerConfigMutation;
|
|
26862
27029
|
exports.useSymmSignTermsMutation = useSymmSignTermsMutation;
|