@sodax/dapp-kit 2.0.0 → 2.1.0-rc.2
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/README.md +128 -115
- package/dist/index.d.ts +342 -12
- package/dist/index.mjs +405 -47
- package/package.json +2 -2
- package/src/hooks/_mutationContract.test.ts +5 -0
- package/src/hooks/backend/types.ts +1 -1
- package/src/hooks/bridge/useBridgeApprove.ts +2 -1
- package/src/hooks/bridgeApi/index.ts +13 -0
- package/src/hooks/bridgeApi/useBridgeApiAllowance.ts +42 -0
- package/src/hooks/bridgeApi/useBridgeApiApprove.ts +42 -0
- package/src/hooks/bridgeApi/useBridgeApiBridgeableAmount.ts +45 -0
- package/src/hooks/bridgeApi/useBridgeApiCreateBridgeIntent.ts +42 -0
- package/src/hooks/bridgeApi/useBridgeApiFee.ts +44 -0
- package/src/hooks/bridgeApi/useBridgeApiIsBridgeable.ts +45 -0
- package/src/hooks/bridgeApi/useBridgeApiSubmitTx.ts +50 -0
- package/src/hooks/bridgeApi/useBridgeApiSubmitTxStatus.ts +60 -0
- package/src/hooks/bridgeApi/useBridgeApiTokens.ts +35 -0
- package/src/hooks/bridgeApi/useBridgeApiTokensByChain.ts +40 -0
- package/src/hooks/index.ts +2 -0
- package/src/hooks/migrate/useMigrationApprove.ts +1 -3
- package/src/hooks/mm/useWithdraw.ts +5 -1
- package/src/hooks/partner/useApproveToken.ts +1 -2
- package/src/hooks/shared/index.ts +2 -0
- package/src/hooks/shared/useEstablishTrustline.test.ts +89 -0
- package/src/hooks/shared/useEstablishTrustline.ts +72 -0
- package/src/hooks/shared/useRequestTrustline.test.ts +115 -0
- package/src/hooks/shared/useRequestTrustline.ts +41 -56
- package/src/hooks/shared/useSafeMutation.ts +1 -4
- package/src/hooks/shared/useStellarGate.test.ts +99 -0
- package/src/hooks/shared/useStellarGate.ts +102 -0
- package/src/hooks/shared/useStellarTrustlineCheck.test.ts +2 -2
- package/src/hooks/sponsoring/index.ts +4 -0
- package/src/hooks/sponsoring/useActivateStellarAccount.test.ts +74 -0
- package/src/hooks/sponsoring/useActivateStellarAccount.ts +38 -0
- package/src/hooks/sponsoring/useSponsorConfig.test.ts +46 -0
- package/src/hooks/sponsoring/useSponsorConfig.ts +31 -0
- package/src/hooks/sponsoring/useStellarAccountActive.test.ts +50 -0
- package/src/hooks/sponsoring/useStellarAccountActive.ts +45 -0
- package/src/hooks/sponsoring/useStellarAccountStatus.test.ts +90 -0
- package/src/hooks/sponsoring/useStellarAccountStatus.ts +57 -0
- package/src/hooks/staking/useStakingConfig.ts +1 -3
- package/src/hooks/staking/useStakingInfo.ts +4 -4
- package/src/hooks/staking/useUnstake.ts +5 -1
- package/src/hooks/swap/useCreateLimitOrder.ts +1 -2
- package/src/hooks/swap/useSwap.ts +1 -5
- package/src/hooks/swapsApi/useSwapsApiSubmitTxStatus.ts +6 -3
- package/src/index.ts +1 -1
- package/src/providers/createSodaxQueryClient.ts +1 -5
- package/src/utils/dex-utils.ts +1 -4
- package/src/utils/index.ts +1 -0
- package/src/utils/stellarGate.test.ts +196 -0
- package/src/utils/stellarGate.ts +84 -0
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { isSodaxError, ChainKeys, RadfiApiError, normalizePsbtToBase64, ClService, Sodax } from '@sodax/sdk';
|
|
1
|
+
import { isSodaxError, ChainKeys, STELLAR_TRUSTLINE_MIN_XLM_STROOPS, RadfiApiError, normalizePsbtToBase64, ClService, SPONSOR_CONFIG_TTL_MS, Sodax } from '@sodax/sdk';
|
|
2
2
|
export * from '@sodax/sdk';
|
|
3
3
|
import { useMutation, useQuery, useQueryClient, useQueries, QueryClient, MutationCache } from '@tanstack/react-query';
|
|
4
|
-
import { createContext, useCallback, useContext,
|
|
4
|
+
import { createContext, useCallback, useContext, useRef, useState, useEffect, useMemo } from 'react';
|
|
5
5
|
import { isAddress, erc20Abi, parseUnits } from 'viem';
|
|
6
6
|
import { jsx } from 'react/jsx-runtime';
|
|
7
7
|
|
|
@@ -111,52 +111,64 @@ function useStellarTrustlineCheck({
|
|
|
111
111
|
...queryOptions
|
|
112
112
|
});
|
|
113
113
|
}
|
|
114
|
-
function
|
|
114
|
+
function useEstablishTrustline({
|
|
115
|
+
mutationOptions
|
|
116
|
+
} = {}) {
|
|
115
117
|
const { sodax } = useSodaxContext();
|
|
116
118
|
const queryClient = useQueryClient();
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
token: token2,
|
|
124
|
-
amount,
|
|
125
|
-
srcChainKey,
|
|
126
|
-
walletProvider
|
|
127
|
-
}) => {
|
|
128
|
-
if (!token2 || !amount) {
|
|
129
|
-
const error2 = new Error("Token and amount are required");
|
|
130
|
-
setError(error2);
|
|
131
|
-
throw error2;
|
|
119
|
+
return useSafeMutation({
|
|
120
|
+
mutationKey: ["shared", "establishTrustline"],
|
|
121
|
+
...mutationOptions,
|
|
122
|
+
mutationFn: async ({ token, amount, srcChainKey, walletProvider }) => {
|
|
123
|
+
if (!token || !amount) {
|
|
124
|
+
throw new Error("Token and amount are required");
|
|
132
125
|
}
|
|
133
|
-
|
|
134
|
-
|
|
126
|
+
const srcAddress = await walletProvider.getWalletAddress();
|
|
127
|
+
return sodax.spoke.stellar.requestTrustline({
|
|
128
|
+
raw: false,
|
|
129
|
+
srcChainKey,
|
|
130
|
+
srcAddress,
|
|
131
|
+
token,
|
|
132
|
+
amount,
|
|
133
|
+
walletProvider
|
|
134
|
+
});
|
|
135
|
+
},
|
|
136
|
+
onSuccess: async (data, vars, ctx) => {
|
|
137
|
+
const srcAddress = await vars.walletProvider.getWalletAddress().catch(() => void 0);
|
|
138
|
+
queryClient.invalidateQueries({
|
|
139
|
+
queryKey: srcAddress ? ["shared", "stellarTrustlineCheck", vars.srcChainKey, vars.token, srcAddress] : ["shared", "stellarTrustlineCheck"]
|
|
140
|
+
});
|
|
141
|
+
queryClient.invalidateQueries({
|
|
142
|
+
queryKey: srcAddress ? ["sponsoring", "stellarAccountStatus", srcAddress] : ["sponsoring", "stellarAccountStatus"]
|
|
143
|
+
});
|
|
144
|
+
queryClient.invalidateQueries({ queryKey: ["shared", "xBalances", ChainKeys.STELLAR_MAINNET] });
|
|
145
|
+
await mutationOptions?.onSuccess?.(data, vars, ctx);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
function useRequestTrustline(token) {
|
|
150
|
+
const { mutateAsync, isPending, isSuccess, error, data } = useEstablishTrustline();
|
|
151
|
+
const requested = useRef(false);
|
|
152
|
+
const lastData = useRef(null);
|
|
153
|
+
if (isSuccess) requested.current = true;
|
|
154
|
+
if (data !== void 0) lastData.current = data;
|
|
155
|
+
const requestTrustline = useCallback(
|
|
156
|
+
async (params) => {
|
|
135
157
|
try {
|
|
136
|
-
|
|
137
|
-
const result = await sodax.spoke.stellar.requestTrustline({
|
|
138
|
-
raw: false,
|
|
139
|
-
srcChainKey,
|
|
140
|
-
srcAddress,
|
|
141
|
-
token: token2,
|
|
142
|
-
amount,
|
|
143
|
-
walletProvider
|
|
144
|
-
});
|
|
145
|
-
setData(result);
|
|
146
|
-
setIsRequested(true);
|
|
147
|
-
queryClient.invalidateQueries({ queryKey: ["shared", "stellarTrustlineCheck", srcChainKey, token2, srcAddress] });
|
|
148
|
-
return result;
|
|
158
|
+
return await mutateAsync(params);
|
|
149
159
|
} catch (err) {
|
|
150
|
-
|
|
151
|
-
setError(error2);
|
|
152
|
-
throw error2;
|
|
153
|
-
} finally {
|
|
154
|
-
setIsLoading(false);
|
|
160
|
+
throw err instanceof Error ? err : new Error("Unknown error occurred");
|
|
155
161
|
}
|
|
156
162
|
},
|
|
157
|
-
[
|
|
163
|
+
[mutateAsync]
|
|
158
164
|
);
|
|
159
|
-
return {
|
|
165
|
+
return {
|
|
166
|
+
requestTrustline,
|
|
167
|
+
isLoading: isPending,
|
|
168
|
+
isRequested: requested.current,
|
|
169
|
+
error,
|
|
170
|
+
data: lastData.current
|
|
171
|
+
};
|
|
160
172
|
}
|
|
161
173
|
function useNearStorageCheck({
|
|
162
174
|
params,
|
|
@@ -229,6 +241,140 @@ function useNearStorageGate({
|
|
|
229
241
|
registerStorage
|
|
230
242
|
};
|
|
231
243
|
}
|
|
244
|
+
function unresolved(check) {
|
|
245
|
+
return check.isLoading || check.data === void 0;
|
|
246
|
+
}
|
|
247
|
+
var NOT_STELLAR = {
|
|
248
|
+
isStellar: false,
|
|
249
|
+
needsActivation: false,
|
|
250
|
+
needsFunding: false,
|
|
251
|
+
needsTrustline: false,
|
|
252
|
+
checkFailed: false,
|
|
253
|
+
blocksAction: false
|
|
254
|
+
};
|
|
255
|
+
function resolveStellarGate(chainKey, inputs) {
|
|
256
|
+
const { statusCheck, trustlineCheck, isNativeToken } = inputs;
|
|
257
|
+
if (chainKey !== ChainKeys.STELLAR_MAINNET) return NOT_STELLAR;
|
|
258
|
+
const base = {
|
|
259
|
+
isStellar: true,
|
|
260
|
+
needsActivation: false,
|
|
261
|
+
needsFunding: false,
|
|
262
|
+
needsTrustline: false,
|
|
263
|
+
checkFailed: false
|
|
264
|
+
};
|
|
265
|
+
if (statusCheck.isError) return { ...base, checkFailed: true, blocksAction: true };
|
|
266
|
+
const status = statusCheck.data;
|
|
267
|
+
if (statusCheck.isLoading || status === void 0) return { ...base, blocksAction: true };
|
|
268
|
+
if (!status.exists) return { ...base, needsActivation: true, blocksAction: true };
|
|
269
|
+
if (isNativeToken) return { ...base, blocksAction: false };
|
|
270
|
+
if (trustlineCheck.isError) return { ...base, checkFailed: true, blocksAction: true };
|
|
271
|
+
if (unresolved(trustlineCheck)) return { ...base, blocksAction: true };
|
|
272
|
+
if (trustlineCheck.data === true) return { ...base, blocksAction: false };
|
|
273
|
+
if (!status.canAffordTrustline) return { ...base, needsFunding: true, blocksAction: true };
|
|
274
|
+
return { ...base, needsTrustline: true, blocksAction: true };
|
|
275
|
+
}
|
|
276
|
+
function useActivateStellarAccount({
|
|
277
|
+
mutationOptions
|
|
278
|
+
} = {}) {
|
|
279
|
+
const { sodax } = useSodaxContext();
|
|
280
|
+
const queryClient = useQueryClient();
|
|
281
|
+
return useSafeMutation({
|
|
282
|
+
mutationKey: ["sponsoring", "activateStellarAccount"],
|
|
283
|
+
...mutationOptions,
|
|
284
|
+
mutationFn: async (vars) => unwrapResult(await sodax.sponsoring.activateStellarAccount(vars)),
|
|
285
|
+
onSuccess: async (data, vars, ctx) => {
|
|
286
|
+
queryClient.invalidateQueries({ queryKey: ["sponsoring", "stellarAccountActive", vars.address] });
|
|
287
|
+
queryClient.invalidateQueries({ queryKey: ["sponsoring", "stellarAccountStatus", vars.address] });
|
|
288
|
+
queryClient.invalidateQueries({ queryKey: ["shared", "stellarTrustlineCheck"] });
|
|
289
|
+
queryClient.invalidateQueries({ queryKey: ["shared", "xBalances", ChainKeys.STELLAR_MAINNET] });
|
|
290
|
+
await mutationOptions?.onSuccess?.(data, vars, ctx);
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
function getStellarAccountStatusQueryOptions({
|
|
295
|
+
sodax,
|
|
296
|
+
address
|
|
297
|
+
}) {
|
|
298
|
+
return {
|
|
299
|
+
queryKey: ["sponsoring", "stellarAccountStatus", address],
|
|
300
|
+
queryFn: async () => {
|
|
301
|
+
if (!address) {
|
|
302
|
+
return {
|
|
303
|
+
exists: false,
|
|
304
|
+
nativeBalanceStroops: 0n,
|
|
305
|
+
availableBalanceStroops: 0n,
|
|
306
|
+
canAffordTrustline: false,
|
|
307
|
+
// No account to read the network reserve for; the published value is the best guess.
|
|
308
|
+
trustlineMinXlmStroops: STELLAR_TRUSTLINE_MIN_XLM_STROOPS
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
const result = await sodax.sponsoring.getStellarAccountStatus({ address });
|
|
312
|
+
if (!result.ok) throw result.error;
|
|
313
|
+
return result.value;
|
|
314
|
+
},
|
|
315
|
+
enabled: !!address
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
function useStellarAccountStatus({
|
|
319
|
+
params,
|
|
320
|
+
queryOptions
|
|
321
|
+
} = {}) {
|
|
322
|
+
const { sodax } = useSodaxContext();
|
|
323
|
+
return useQuery({
|
|
324
|
+
...getStellarAccountStatusQueryOptions({ sodax, address: params?.address }),
|
|
325
|
+
...queryOptions
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// src/hooks/shared/useStellarGate.ts
|
|
330
|
+
function useStellarGate({
|
|
331
|
+
dstChainKey,
|
|
332
|
+
token,
|
|
333
|
+
amount,
|
|
334
|
+
address,
|
|
335
|
+
walletProvider
|
|
336
|
+
}) {
|
|
337
|
+
const { sodax } = useSodaxContext();
|
|
338
|
+
const isStellarDestination = dstChainKey === ChainKeys.STELLAR_MAINNET;
|
|
339
|
+
const stellarWalletProvider = walletProvider?.chainType === "STELLAR" ? walletProvider : void 0;
|
|
340
|
+
const gatedAddress = isStellarDestination ? address : void 0;
|
|
341
|
+
const statusCheck = useStellarAccountStatus({ params: { address: gatedAddress } });
|
|
342
|
+
const trustlineAddress = statusCheck.data?.exists === true ? gatedAddress : void 0;
|
|
343
|
+
const trustlineCheck = useStellarTrustlineCheck({
|
|
344
|
+
params: { token, amount, chainId: dstChainKey, walletAddress: trustlineAddress }
|
|
345
|
+
});
|
|
346
|
+
const isNativeToken = isStellarDestination && !!token && !sodax.spoke.stellar.requiresTrustline(token);
|
|
347
|
+
const state = resolveStellarGate(dstChainKey, { statusCheck, trustlineCheck, isNativeToken });
|
|
348
|
+
const { mutateAsyncSafe: activateAccount, isPending: isActivating } = useActivateStellarAccount();
|
|
349
|
+
const { mutateAsyncSafe: establishTrustline, isPending: isRequestingTrustline } = useEstablishTrustline();
|
|
350
|
+
const activate = async () => {
|
|
351
|
+
if (!stellarWalletProvider || !address) return void 0;
|
|
352
|
+
return activateAccount({ address, walletProvider: stellarWalletProvider });
|
|
353
|
+
};
|
|
354
|
+
const requestTrustline = async () => {
|
|
355
|
+
if (!stellarWalletProvider || !token || amount === void 0) return void 0;
|
|
356
|
+
return establishTrustline({
|
|
357
|
+
token,
|
|
358
|
+
amount,
|
|
359
|
+
srcChainKey: ChainKeys.STELLAR_MAINNET,
|
|
360
|
+
walletProvider: stellarWalletProvider
|
|
361
|
+
});
|
|
362
|
+
};
|
|
363
|
+
const retry = () => {
|
|
364
|
+
void statusCheck.refetch();
|
|
365
|
+
void trustlineCheck.refetch();
|
|
366
|
+
};
|
|
367
|
+
return {
|
|
368
|
+
...state,
|
|
369
|
+
isChecking: statusCheck.isLoading || trustlineCheck.isLoading,
|
|
370
|
+
error: statusCheck.error ?? trustlineCheck.error ?? void 0,
|
|
371
|
+
retry,
|
|
372
|
+
isActivating,
|
|
373
|
+
isRequestingTrustline,
|
|
374
|
+
activate,
|
|
375
|
+
requestTrustline
|
|
376
|
+
};
|
|
377
|
+
}
|
|
232
378
|
var SESSION_KEY = (address) => `radfi_session_${address}`;
|
|
233
379
|
function saveRadfiSession(address, session) {
|
|
234
380
|
try {
|
|
@@ -1596,8 +1742,8 @@ var useSwapsApiSubmitTxStatus = ({
|
|
|
1596
1742
|
enabled: !!txHash && txHash.length > 0 && !!srcChainKey,
|
|
1597
1743
|
retry: 3,
|
|
1598
1744
|
refetchInterval: (query) => {
|
|
1599
|
-
const
|
|
1600
|
-
if (status === "solved" || status === "failed") return false;
|
|
1745
|
+
const data = query.state.data?.data;
|
|
1746
|
+
if (data?.status === "solved" || data?.status === "failed" || data?.abandonedAt) return false;
|
|
1601
1747
|
return 1e3;
|
|
1602
1748
|
},
|
|
1603
1749
|
...queryOptions
|
|
@@ -1716,6 +1862,172 @@ function useGetBridgeableTokens({
|
|
|
1716
1862
|
...queryOptions
|
|
1717
1863
|
});
|
|
1718
1864
|
}
|
|
1865
|
+
var useBridgeApiAllowance = ({
|
|
1866
|
+
params,
|
|
1867
|
+
queryOptions
|
|
1868
|
+
} = {}) => {
|
|
1869
|
+
const { sodax } = useSodaxContext();
|
|
1870
|
+
const body = params?.body;
|
|
1871
|
+
const apiConfig = params?.apiConfig;
|
|
1872
|
+
return useQuery({
|
|
1873
|
+
queryKey: ["bridgeApi", "allowance", body?.srcChainKey, body?.inputToken, body?.inputAmount, body?.srcAddress],
|
|
1874
|
+
queryFn: async () => {
|
|
1875
|
+
if (!body) return void 0;
|
|
1876
|
+
return unwrapResult(await sodax.api.bridge.checkAllowance(body, apiConfig));
|
|
1877
|
+
},
|
|
1878
|
+
enabled: !!body,
|
|
1879
|
+
retry: 3,
|
|
1880
|
+
...queryOptions
|
|
1881
|
+
});
|
|
1882
|
+
};
|
|
1883
|
+
|
|
1884
|
+
// src/hooks/bridgeApi/useBridgeApiApprove.ts
|
|
1885
|
+
var useBridgeApiApprove = ({
|
|
1886
|
+
mutationOptions
|
|
1887
|
+
} = {}) => {
|
|
1888
|
+
const { sodax } = useSodaxContext();
|
|
1889
|
+
return useSafeMutation({
|
|
1890
|
+
mutationKey: ["bridgeApi", "approve"],
|
|
1891
|
+
retry: 3,
|
|
1892
|
+
...mutationOptions,
|
|
1893
|
+
mutationFn: async ({ body, apiConfig }) => unwrapResult(await sodax.api.bridge.approve(body, apiConfig))
|
|
1894
|
+
});
|
|
1895
|
+
};
|
|
1896
|
+
var useBridgeApiBridgeableAmount = ({
|
|
1897
|
+
params,
|
|
1898
|
+
queryOptions
|
|
1899
|
+
} = {}) => {
|
|
1900
|
+
const { sodax } = useSodaxContext();
|
|
1901
|
+
const body = params?.body;
|
|
1902
|
+
const apiConfig = params?.apiConfig;
|
|
1903
|
+
return useQuery({
|
|
1904
|
+
queryKey: ["bridgeApi", "bridgeableAmount", body?.srcChainKey, body?.dstChainKey, body?.inputToken, body?.outputToken],
|
|
1905
|
+
queryFn: async () => {
|
|
1906
|
+
if (!body) return void 0;
|
|
1907
|
+
return unwrapResult(await sodax.api.bridge.getBridgeableAmount(body, apiConfig));
|
|
1908
|
+
},
|
|
1909
|
+
enabled: !!body,
|
|
1910
|
+
retry: 3,
|
|
1911
|
+
...queryOptions
|
|
1912
|
+
});
|
|
1913
|
+
};
|
|
1914
|
+
|
|
1915
|
+
// src/hooks/bridgeApi/useBridgeApiCreateBridgeIntent.ts
|
|
1916
|
+
var useBridgeApiCreateBridgeIntent = ({
|
|
1917
|
+
mutationOptions
|
|
1918
|
+
} = {}) => {
|
|
1919
|
+
const { sodax } = useSodaxContext();
|
|
1920
|
+
return useSafeMutation({
|
|
1921
|
+
mutationKey: ["bridgeApi", "createBridgeIntent"],
|
|
1922
|
+
retry: 3,
|
|
1923
|
+
...mutationOptions,
|
|
1924
|
+
mutationFn: async ({ body, apiConfig }) => unwrapResult(await sodax.api.bridge.createBridgeIntent(body, apiConfig))
|
|
1925
|
+
});
|
|
1926
|
+
};
|
|
1927
|
+
var useBridgeApiFee = ({
|
|
1928
|
+
params,
|
|
1929
|
+
queryOptions
|
|
1930
|
+
} = {}) => {
|
|
1931
|
+
const { sodax } = useSodaxContext();
|
|
1932
|
+
const body = params?.body;
|
|
1933
|
+
const apiConfig = params?.apiConfig;
|
|
1934
|
+
return useQuery({
|
|
1935
|
+
queryKey: ["bridgeApi", "fee", body?.inputAmount, body?.partnerFee],
|
|
1936
|
+
queryFn: async () => {
|
|
1937
|
+
if (!body) return void 0;
|
|
1938
|
+
return unwrapResult(await sodax.api.bridge.getFee(body, apiConfig));
|
|
1939
|
+
},
|
|
1940
|
+
enabled: !!body,
|
|
1941
|
+
retry: 3,
|
|
1942
|
+
...queryOptions
|
|
1943
|
+
});
|
|
1944
|
+
};
|
|
1945
|
+
var useBridgeApiIsBridgeable = ({
|
|
1946
|
+
params,
|
|
1947
|
+
queryOptions
|
|
1948
|
+
} = {}) => {
|
|
1949
|
+
const { sodax } = useSodaxContext();
|
|
1950
|
+
const body = params?.body;
|
|
1951
|
+
const apiConfig = params?.apiConfig;
|
|
1952
|
+
return useQuery({
|
|
1953
|
+
queryKey: ["bridgeApi", "bridgeable", body?.srcChainKey, body?.dstChainKey, body?.inputToken, body?.outputToken],
|
|
1954
|
+
queryFn: async () => {
|
|
1955
|
+
if (!body) return void 0;
|
|
1956
|
+
return unwrapResult(await sodax.api.bridge.isBridgeable(body, apiConfig));
|
|
1957
|
+
},
|
|
1958
|
+
enabled: !!body,
|
|
1959
|
+
retry: 3,
|
|
1960
|
+
...queryOptions
|
|
1961
|
+
});
|
|
1962
|
+
};
|
|
1963
|
+
|
|
1964
|
+
// src/hooks/bridgeApi/useBridgeApiSubmitTx.ts
|
|
1965
|
+
var useBridgeApiSubmitTx = ({
|
|
1966
|
+
mutationOptions
|
|
1967
|
+
} = {}) => {
|
|
1968
|
+
const { sodax } = useSodaxContext();
|
|
1969
|
+
return useSafeMutation({
|
|
1970
|
+
mutationKey: ["bridgeApi", "submitTx"],
|
|
1971
|
+
retry: 3,
|
|
1972
|
+
...mutationOptions,
|
|
1973
|
+
mutationFn: async ({ request, apiConfig }) => unwrapResult(await sodax.api.bridge.submitTx(request, apiConfig))
|
|
1974
|
+
});
|
|
1975
|
+
};
|
|
1976
|
+
var useBridgeApiSubmitTxStatus = ({
|
|
1977
|
+
params,
|
|
1978
|
+
queryOptions
|
|
1979
|
+
} = {}) => {
|
|
1980
|
+
const { sodax } = useSodaxContext();
|
|
1981
|
+
const txHash = params?.txHash;
|
|
1982
|
+
const srcChainKey = params?.srcChainKey;
|
|
1983
|
+
const apiConfig = params?.apiConfig;
|
|
1984
|
+
return useQuery({
|
|
1985
|
+
queryKey: ["bridgeApi", "submitTx", "status", txHash, srcChainKey],
|
|
1986
|
+
queryFn: async () => {
|
|
1987
|
+
if (!txHash || !srcChainKey) return void 0;
|
|
1988
|
+
return unwrapResult(await sodax.api.bridge.getSubmitTxStatus({ txHash, srcChainKey }, apiConfig));
|
|
1989
|
+
},
|
|
1990
|
+
enabled: !!txHash && txHash.length > 0 && !!srcChainKey,
|
|
1991
|
+
retry: 3,
|
|
1992
|
+
refetchInterval: (query) => {
|
|
1993
|
+
const data = query.state.data?.data;
|
|
1994
|
+
if (data?.status === "executed" || data?.status === "failed" || data?.abandonedAt) return false;
|
|
1995
|
+
return 1e3;
|
|
1996
|
+
},
|
|
1997
|
+
...queryOptions
|
|
1998
|
+
});
|
|
1999
|
+
};
|
|
2000
|
+
var useBridgeApiTokens = ({
|
|
2001
|
+
params,
|
|
2002
|
+
queryOptions
|
|
2003
|
+
} = {}) => {
|
|
2004
|
+
const { sodax } = useSodaxContext();
|
|
2005
|
+
const apiConfig = params?.apiConfig;
|
|
2006
|
+
return useQuery({
|
|
2007
|
+
queryKey: ["bridgeApi", "tokens"],
|
|
2008
|
+
queryFn: async () => unwrapResult(await sodax.api.bridge.getTokens(apiConfig)),
|
|
2009
|
+
retry: 3,
|
|
2010
|
+
...queryOptions
|
|
2011
|
+
});
|
|
2012
|
+
};
|
|
2013
|
+
var useBridgeApiTokensByChain = ({
|
|
2014
|
+
params,
|
|
2015
|
+
queryOptions
|
|
2016
|
+
} = {}) => {
|
|
2017
|
+
const { sodax } = useSodaxContext();
|
|
2018
|
+
const chainKey = params?.chainKey;
|
|
2019
|
+
const apiConfig = params?.apiConfig;
|
|
2020
|
+
return useQuery({
|
|
2021
|
+
queryKey: ["bridgeApi", "tokens", chainKey],
|
|
2022
|
+
queryFn: async () => {
|
|
2023
|
+
if (!chainKey) return void 0;
|
|
2024
|
+
return unwrapResult(await sodax.api.bridge.getTokensByChain(chainKey, apiConfig));
|
|
2025
|
+
},
|
|
2026
|
+
enabled: !!chainKey && chainKey.length > 0,
|
|
2027
|
+
retry: 3,
|
|
2028
|
+
...queryOptions
|
|
2029
|
+
});
|
|
2030
|
+
};
|
|
1719
2031
|
function useStake({
|
|
1720
2032
|
mutationOptions
|
|
1721
2033
|
} = {}) {
|
|
@@ -1843,7 +2155,10 @@ function useCancelUnstake({
|
|
|
1843
2155
|
}
|
|
1844
2156
|
});
|
|
1845
2157
|
}
|
|
1846
|
-
function useStakingInfo({
|
|
2158
|
+
function useStakingInfo({
|
|
2159
|
+
params,
|
|
2160
|
+
queryOptions
|
|
2161
|
+
} = {}) {
|
|
1847
2162
|
const { sodax } = useSodaxContext();
|
|
1848
2163
|
const srcAddress = params?.srcAddress;
|
|
1849
2164
|
const srcChainKey = params?.srcChainKey;
|
|
@@ -1884,9 +2199,7 @@ function useUnstakingInfoWithPenalty({
|
|
|
1884
2199
|
...queryOptions
|
|
1885
2200
|
});
|
|
1886
2201
|
}
|
|
1887
|
-
function useStakingConfig({
|
|
1888
|
-
queryOptions
|
|
1889
|
-
} = {}) {
|
|
2202
|
+
function useStakingConfig({ queryOptions } = {}) {
|
|
1890
2203
|
const { sodax } = useSodaxContext();
|
|
1891
2204
|
return useQuery({
|
|
1892
2205
|
queryKey: ["staking", "config"],
|
|
@@ -3196,6 +3509,51 @@ function useLeverageYieldWithdraw({
|
|
|
3196
3509
|
mutationFn: async (vars) => unwrapResult(await sodax.leverageYield.withdraw(vars))
|
|
3197
3510
|
});
|
|
3198
3511
|
}
|
|
3512
|
+
function getSponsorConfigQueryOptions({ sodax }) {
|
|
3513
|
+
return {
|
|
3514
|
+
queryKey: ["sponsoring", "sponsorConfig"],
|
|
3515
|
+
queryFn: async () => {
|
|
3516
|
+
const result = await sodax.sponsoring.getStellarSponsorConfig();
|
|
3517
|
+
if (!result.ok) throw result.error;
|
|
3518
|
+
return result.value;
|
|
3519
|
+
},
|
|
3520
|
+
staleTime: SPONSOR_CONFIG_TTL_MS
|
|
3521
|
+
};
|
|
3522
|
+
}
|
|
3523
|
+
function useSponsorConfig({
|
|
3524
|
+
queryOptions
|
|
3525
|
+
} = {}) {
|
|
3526
|
+
const { sodax } = useSodaxContext();
|
|
3527
|
+
return useQuery({
|
|
3528
|
+
...getSponsorConfigQueryOptions({ sodax }),
|
|
3529
|
+
...queryOptions
|
|
3530
|
+
});
|
|
3531
|
+
}
|
|
3532
|
+
function getStellarAccountActiveQueryOptions({
|
|
3533
|
+
sodax,
|
|
3534
|
+
address
|
|
3535
|
+
}) {
|
|
3536
|
+
return {
|
|
3537
|
+
queryKey: ["sponsoring", "stellarAccountActive", address],
|
|
3538
|
+
queryFn: async () => {
|
|
3539
|
+
if (!address) return false;
|
|
3540
|
+
const result = await sodax.sponsoring.isStellarAccountActive({ address });
|
|
3541
|
+
if (!result.ok) throw result.error;
|
|
3542
|
+
return result.value;
|
|
3543
|
+
},
|
|
3544
|
+
enabled: !!address
|
|
3545
|
+
};
|
|
3546
|
+
}
|
|
3547
|
+
function useStellarAccountActive({
|
|
3548
|
+
params,
|
|
3549
|
+
queryOptions
|
|
3550
|
+
} = {}) {
|
|
3551
|
+
const { sodax } = useSodaxContext();
|
|
3552
|
+
return useQuery({
|
|
3553
|
+
...getStellarAccountActiveQueryOptions({ sodax, address: params?.address }),
|
|
3554
|
+
...queryOptions
|
|
3555
|
+
});
|
|
3556
|
+
}
|
|
3199
3557
|
var SodaxProvider = ({ children, config }) => {
|
|
3200
3558
|
const sodax = useMemo(() => new Sodax(config), [config]);
|
|
3201
3559
|
const contextValue = useMemo(() => ({ sodax }), [sodax]);
|
|
@@ -3227,4 +3585,4 @@ function createSodaxQueryClient({
|
|
|
3227
3585
|
});
|
|
3228
3586
|
}
|
|
3229
3587
|
|
|
3230
|
-
export { SodaxProvider, clearRadfiSession, createDecreaseLiquidityParamsProps, createDepositParamsProps, createSodaxQueryClient, createSupplyLiquidityParamsProps, createWithdrawParamsProps, getStellarTrustlineCheckQueryOptions, getXBalancesQueryOptions, isTerminalSwapIntentStatus, isUserRejectedError, loadRadfiSession, resolveNearStorageGate, saveRadfiSession, toResult, unwrapResult, useAToken, useATokensBalances, useApproveToken, useBackendAllMoneyMarketAssets, useBackendAllMoneyMarketBorrowers, useBackendIntentByHash, useBackendIntentByTxHash, useBackendMoneyMarketAsset, useBackendMoneyMarketAssetBorrowers, useBackendMoneyMarketAssetSuppliers, useBackendMoneyMarketPosition, useBackendOrderbook, useBackendUserIntents, useBitcoinBalance, useBitcoinTradingSetup, useBorrow, useBridge, useBridgeAllowance, useBridgeApprove, useCancelLimitOrder, useCancelSwap, useCancelUnstake, useClaim, useClaimRewards, useConvertedAssets, useCreateDecreaseLiquidityParams, useCreateDepositParams, useCreateLimitOrder, useCreateSupplyLiquidityParams, useCreateWithdrawParams, useDecreaseLiquidity, useDeriveUserWalletAddress, useDexAllowance, useDexApprove, useDexDeposit, useDexWithdraw, useEnsureRadfiAccessToken, useEstimateGas, useExpiredUtxos, useFeeClaimSwap, useFeeClaimWithdraw, useFetchAssetsBalances, useFundTradingWallet, useGetAutoSwapPreferences, useGetBridgeableAmount, useGetBridgeableTokens, useGetIntentDetails, useGetUserHubWalletAddress, useGetUserIntent, useHubAssetBalances, useHubProvider, useInstantUnstake, useInstantUnstakeAllowance, useInstantUnstakeApprove, useInstantUnstakeRatio, useIsTokenApproved, useLeverageYieldDeposit, useLeverageYieldEffectiveApr, useLeverageYieldNotifySolver, useLeverageYieldPosition, useLeverageYieldPreviewRedeem, useLeverageYieldShareBalances, useLeverageYieldTotalAssets, useLeverageYieldVaultSwap, useLeverageYieldWithdraw, useLiquidityAmounts, useMMAllowance, useMMApprove, useMigrateBaln, useMigrateIcxToSoda, useMigratebnUSD, useMigrationAllowance, useMigrationApprove, useNearStorageCheck, useNearStorageGate, usePartnerCancelIntent, usePoolBalances, usePoolData, usePools, usePositionInfo, useQuote, useRadfiAuth, useRadfiSession, useRadfiWithdraw, useRegisterNearStorage, useRenewUtxos, useRepay, useRequestTrustline, useReservesData, useReservesHumanized, useReservesList, useReservesUsdFormat, useRevertMigrateSodaToIcx, useSafeMutation, useSetSwapPreference, useSodaxContext, useStake, useStakeAllowance, useStakeApprove, useStakeRatio, useStakingConfig, useStakingInfo, useStatus, useStellarTrustlineCheck, useSupply, useSupplyLiquidity, useSwap, useSwapAllowance, useSwapApprove, useSwapsApiAllowance, useSwapsApiApprove, useSwapsApiCancelIntent, useSwapsApiCreateIntent, useSwapsApiCreateLimitOrder, useSwapsApiDeadline, useSwapsApiEstimateGas, useSwapsApiFilledIntent, useSwapsApiIntent, useSwapsApiIntentExtraData, useSwapsApiIntentHash, useSwapsApiIntentPacket, useSwapsApiPartnerFee, useSwapsApiQuote, useSwapsApiSolverFee, useSwapsApiStatus, useSwapsApiSubmitIntent, useSwapsApiSubmitTx, useSwapsApiSubmitTxStatus, useSwapsApiTokens, useSwapsApiTokensByChain, useTradingWallet, useTradingWalletBalance, useUnstake, useUnstakeAllowance, useUnstakeApprove, useUnstakingInfo, useUnstakingInfoWithPenalty, useUserFormattedSummary, useUserReservesData, useWithdraw, useWithdrawHubAsset, useXBalances };
|
|
3588
|
+
export { SodaxProvider, clearRadfiSession, createDecreaseLiquidityParamsProps, createDepositParamsProps, createSodaxQueryClient, createSupplyLiquidityParamsProps, createWithdrawParamsProps, getSponsorConfigQueryOptions, getStellarAccountActiveQueryOptions, getStellarAccountStatusQueryOptions, getStellarTrustlineCheckQueryOptions, getXBalancesQueryOptions, isTerminalSwapIntentStatus, isUserRejectedError, loadRadfiSession, resolveNearStorageGate, resolveStellarGate, saveRadfiSession, toResult, unwrapResult, useAToken, useATokensBalances, useActivateStellarAccount, useApproveToken, useBackendAllMoneyMarketAssets, useBackendAllMoneyMarketBorrowers, useBackendIntentByHash, useBackendIntentByTxHash, useBackendMoneyMarketAsset, useBackendMoneyMarketAssetBorrowers, useBackendMoneyMarketAssetSuppliers, useBackendMoneyMarketPosition, useBackendOrderbook, useBackendUserIntents, useBitcoinBalance, useBitcoinTradingSetup, useBorrow, useBridge, useBridgeAllowance, useBridgeApiAllowance, useBridgeApiApprove, useBridgeApiBridgeableAmount, useBridgeApiCreateBridgeIntent, useBridgeApiFee, useBridgeApiIsBridgeable, useBridgeApiSubmitTx, useBridgeApiSubmitTxStatus, useBridgeApiTokens, useBridgeApiTokensByChain, useBridgeApprove, useCancelLimitOrder, useCancelSwap, useCancelUnstake, useClaim, useClaimRewards, useConvertedAssets, useCreateDecreaseLiquidityParams, useCreateDepositParams, useCreateLimitOrder, useCreateSupplyLiquidityParams, useCreateWithdrawParams, useDecreaseLiquidity, useDeriveUserWalletAddress, useDexAllowance, useDexApprove, useDexDeposit, useDexWithdraw, useEnsureRadfiAccessToken, useEstablishTrustline, useEstimateGas, useExpiredUtxos, useFeeClaimSwap, useFeeClaimWithdraw, useFetchAssetsBalances, useFundTradingWallet, useGetAutoSwapPreferences, useGetBridgeableAmount, useGetBridgeableTokens, useGetIntentDetails, useGetUserHubWalletAddress, useGetUserIntent, useHubAssetBalances, useHubProvider, useInstantUnstake, useInstantUnstakeAllowance, useInstantUnstakeApprove, useInstantUnstakeRatio, useIsTokenApproved, useLeverageYieldDeposit, useLeverageYieldEffectiveApr, useLeverageYieldNotifySolver, useLeverageYieldPosition, useLeverageYieldPreviewRedeem, useLeverageYieldShareBalances, useLeverageYieldTotalAssets, useLeverageYieldVaultSwap, useLeverageYieldWithdraw, useLiquidityAmounts, useMMAllowance, useMMApprove, useMigrateBaln, useMigrateIcxToSoda, useMigratebnUSD, useMigrationAllowance, useMigrationApprove, useNearStorageCheck, useNearStorageGate, usePartnerCancelIntent, usePoolBalances, usePoolData, usePools, usePositionInfo, useQuote, useRadfiAuth, useRadfiSession, useRadfiWithdraw, useRegisterNearStorage, useRenewUtxos, useRepay, useRequestTrustline, useReservesData, useReservesHumanized, useReservesList, useReservesUsdFormat, useRevertMigrateSodaToIcx, useSafeMutation, useSetSwapPreference, useSodaxContext, useSponsorConfig, useStake, useStakeAllowance, useStakeApprove, useStakeRatio, useStakingConfig, useStakingInfo, useStatus, useStellarAccountActive, useStellarAccountStatus, useStellarGate, useStellarTrustlineCheck, useSupply, useSupplyLiquidity, useSwap, useSwapAllowance, useSwapApprove, useSwapsApiAllowance, useSwapsApiApprove, useSwapsApiCancelIntent, useSwapsApiCreateIntent, useSwapsApiCreateLimitOrder, useSwapsApiDeadline, useSwapsApiEstimateGas, useSwapsApiFilledIntent, useSwapsApiIntent, useSwapsApiIntentExtraData, useSwapsApiIntentHash, useSwapsApiIntentPacket, useSwapsApiPartnerFee, useSwapsApiQuote, useSwapsApiSolverFee, useSwapsApiStatus, useSwapsApiSubmitIntent, useSwapsApiSubmitTx, useSwapsApiSubmitTxStatus, useSwapsApiTokens, useSwapsApiTokensByChain, useTradingWallet, useTradingWalletBalance, useUnstake, useUnstakeAllowance, useUnstakeApprove, useUnstakingInfo, useUnstakingInfoWithPenalty, useUserFormattedSummary, useUserReservesData, useWithdraw, useWithdrawHubAsset, useXBalances };
|
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
7
|
"license": "MIT",
|
|
8
|
-
"version": "2.0.
|
|
8
|
+
"version": "2.1.0-rc.2",
|
|
9
9
|
"description": "React hooks for building dApps on the SODAX cross-chain DeFi platform",
|
|
10
10
|
"keywords": [
|
|
11
11
|
"sodax",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"viem": "2.29.2",
|
|
37
|
-
"@sodax/sdk": "2.0.
|
|
37
|
+
"@sodax/sdk": "2.1.0-rc.2"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@arethetypeswrong/cli": "0.17.4",
|
|
@@ -46,8 +46,10 @@ const HOOKS: Array<{ path: string; nativeThrow?: true }> = [
|
|
|
46
46
|
{ path: 'partner/usePartnerCancelIntent.ts' },
|
|
47
47
|
{ path: 'partner/useSetSwapPreference.ts' },
|
|
48
48
|
{ path: 'recovery/useWithdrawHubAsset.ts' },
|
|
49
|
+
{ path: 'shared/useEstablishTrustline.ts', nativeThrow: true },
|
|
49
50
|
{ path: 'shared/useEstimateGas.ts' },
|
|
50
51
|
{ path: 'shared/useRegisterNearStorage.ts', nativeThrow: true },
|
|
52
|
+
{ path: 'sponsoring/useActivateStellarAccount.ts' },
|
|
51
53
|
{ path: 'staking/useCancelUnstake.ts' },
|
|
52
54
|
{ path: 'staking/useClaim.ts' },
|
|
53
55
|
{ path: 'staking/useInstantUnstake.ts' },
|
|
@@ -67,6 +69,9 @@ const HOOKS: Array<{ path: string; nativeThrow?: true }> = [
|
|
|
67
69
|
{ path: 'swapsApi/useSwapsApiCreateLimitOrder.ts' },
|
|
68
70
|
{ path: 'swapsApi/useSwapsApiSubmitIntent.ts' },
|
|
69
71
|
{ path: 'swapsApi/useSwapsApiSubmitTx.ts' },
|
|
72
|
+
{ path: 'bridgeApi/useBridgeApiApprove.ts' },
|
|
73
|
+
{ path: 'bridgeApi/useBridgeApiCreateBridgeIntent.ts' },
|
|
74
|
+
{ path: 'bridgeApi/useBridgeApiSubmitTx.ts' },
|
|
70
75
|
];
|
|
71
76
|
|
|
72
77
|
describe.each(HOOKS)('mutation hook contract: $path', ({ path, nativeThrow }) => {
|
|
@@ -34,7 +34,8 @@ export function useBridgeApprove<K extends SpokeChainKey = SpokeChainKey>({
|
|
|
34
34
|
return useSafeMutation<TxReturnType<K, false>, Error, UseBridgeApproveVars<K>>({
|
|
35
35
|
mutationKey: ['bridge', 'approve'],
|
|
36
36
|
...mutationOptions,
|
|
37
|
-
mutationFn: async vars =>
|
|
37
|
+
mutationFn: async vars =>
|
|
38
|
+
unwrapResult(await sodax.bridge.approve({ ...vars, raw: false } as BridgeParams<K, false>)),
|
|
38
39
|
onSuccess: async (data, vars, ctx) => {
|
|
39
40
|
queryClient.invalidateQueries({ queryKey: ['bridge', 'allowance'] });
|
|
40
41
|
await mutationOptions?.onSuccess?.(data, vars, ctx);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// bridgeApi/ — React Query hooks over the HTTP Bridge API (`sodax.api.bridge.*`).
|
|
2
|
+
// Distinct from the on-chain `bridge/` hooks (which call `sodax.bridge.*` directly).
|
|
3
|
+
|
|
4
|
+
export * from './useBridgeApiAllowance.js'; // query
|
|
5
|
+
export * from './useBridgeApiApprove.js'; // mutation
|
|
6
|
+
export * from './useBridgeApiBridgeableAmount.js'; // query (deposit capacity / withdrawal liquidity)
|
|
7
|
+
export * from './useBridgeApiCreateBridgeIntent.js'; // mutation
|
|
8
|
+
export * from './useBridgeApiFee.js'; // query (partner fee — per-request override or configured default)
|
|
9
|
+
export * from './useBridgeApiIsBridgeable.js'; // query (pair bridgeable?)
|
|
10
|
+
export * from './useBridgeApiSubmitTx.js'; // mutation
|
|
11
|
+
export * from './useBridgeApiSubmitTxStatus.js'; // query (poll)
|
|
12
|
+
export * from './useBridgeApiTokens.js'; // query (backend-served token list)
|
|
13
|
+
export * from './useBridgeApiTokensByChain.js'; // query (backend-served token list for one chain)
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { BridgeAllowanceCheckResponseV2, CreateBridgeIntentParamsV2, RequestOverrideConfig } from '@sodax/sdk';
|
|
3
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
4
|
+
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
5
|
+
import type { ReadHookParams } from '../shared/types.js';
|
|
6
|
+
|
|
7
|
+
export type UseBridgeApiAllowanceParams = ReadHookParams<
|
|
8
|
+
BridgeAllowanceCheckResponseV2 | undefined,
|
|
9
|
+
{
|
|
10
|
+
body: CreateBridgeIntentParamsV2 | undefined;
|
|
11
|
+
apiConfig?: RequestOverrideConfig;
|
|
12
|
+
}
|
|
13
|
+
>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* React hook to check whether the source-token allowance is already sufficient for a bridge via
|
|
17
|
+
* the bridge API — `sodax.api.bridge.checkAllowance`. Returns `{ valid }`.
|
|
18
|
+
*
|
|
19
|
+
* The `body` uses the wire DTO field names (`inputToken`/`inputAmount`).
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const { data: allowance } = useBridgeApiAllowance({ params: { body: createBridgeIntentParams } });
|
|
23
|
+
*/
|
|
24
|
+
export const useBridgeApiAllowance = ({
|
|
25
|
+
params,
|
|
26
|
+
queryOptions,
|
|
27
|
+
}: UseBridgeApiAllowanceParams = {}): UseQueryResult<BridgeAllowanceCheckResponseV2 | undefined, Error> => {
|
|
28
|
+
const { sodax } = useSodaxContext();
|
|
29
|
+
const body = params?.body;
|
|
30
|
+
const apiConfig = params?.apiConfig;
|
|
31
|
+
|
|
32
|
+
return useQuery({
|
|
33
|
+
queryKey: ['bridgeApi', 'allowance', body?.srcChainKey, body?.inputToken, body?.inputAmount, body?.srcAddress],
|
|
34
|
+
queryFn: async (): Promise<BridgeAllowanceCheckResponseV2 | undefined> => {
|
|
35
|
+
if (!body) return undefined;
|
|
36
|
+
return unwrapResult(await sodax.api.bridge.checkAllowance(body, apiConfig));
|
|
37
|
+
},
|
|
38
|
+
enabled: !!body,
|
|
39
|
+
retry: 3,
|
|
40
|
+
...queryOptions,
|
|
41
|
+
});
|
|
42
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { BridgeApproveResponseV2, CreateBridgeIntentParamsV2, RequestOverrideConfig } from '@sodax/sdk';
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
3
|
+
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
4
|
+
import type { MutationHookParams } from '../shared/types.js';
|
|
5
|
+
import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Mutation variables for {@link useBridgeApiApprove}. The per-request `apiConfig` override belongs
|
|
9
|
+
* here rather than at the hook level — different calls in the same component can target different
|
|
10
|
+
* endpoints without re-rendering.
|
|
11
|
+
*/
|
|
12
|
+
export type UseBridgeApiApproveVars = {
|
|
13
|
+
body: CreateBridgeIntentParamsV2;
|
|
14
|
+
apiConfig?: RequestOverrideConfig;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* React hook to build an unsigned token-approval transaction for the source token via the bridge
|
|
19
|
+
* API — `sodax.api.bridge.approve`. Returns `{ tx }` (chain-specific unsigned tx) to sign and
|
|
20
|
+
* broadcast yourself; it does not change state, so no queries are invalidated.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* const { mutateAsync: approve } = useBridgeApiApprove();
|
|
24
|
+
* const { tx } = await approve({ body: createBridgeIntentParams });
|
|
25
|
+
*/
|
|
26
|
+
export const useBridgeApiApprove = ({
|
|
27
|
+
mutationOptions,
|
|
28
|
+
}: MutationHookParams<BridgeApproveResponseV2, UseBridgeApiApproveVars> = {}): SafeUseMutationResult<
|
|
29
|
+
BridgeApproveResponseV2,
|
|
30
|
+
Error,
|
|
31
|
+
UseBridgeApiApproveVars
|
|
32
|
+
> => {
|
|
33
|
+
const { sodax } = useSodaxContext();
|
|
34
|
+
|
|
35
|
+
return useSafeMutation<BridgeApproveResponseV2, Error, UseBridgeApiApproveVars>({
|
|
36
|
+
mutationKey: ['bridgeApi', 'approve'],
|
|
37
|
+
retry: 3,
|
|
38
|
+
...mutationOptions,
|
|
39
|
+
mutationFn: async ({ body, apiConfig }): Promise<BridgeApproveResponseV2> =>
|
|
40
|
+
unwrapResult(await sodax.api.bridge.approve(body, apiConfig)),
|
|
41
|
+
});
|
|
42
|
+
};
|