@sodax/dapp-kit 2.0.0-rc.18 → 2.0.0-rc.19
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 +12 -3
- package/dist/index.d.ts +371 -46
- package/dist/index.mjs +370 -46
- package/package.json +2 -2
- package/src/hooks/_mutationContract.test.ts +6 -1
- package/src/hooks/backend/index.ts +1 -5
- package/src/hooks/bitcoin/index.ts +1 -0
- package/src/hooks/bitcoin/useBitcoinTradingSetup.ts +45 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/swapsApi/index.ts +41 -0
- package/src/hooks/swapsApi/isTerminalSwapIntentStatus.test.ts +24 -0
- package/src/hooks/swapsApi/isTerminalSwapIntentStatus.ts +11 -0
- package/src/hooks/swapsApi/useSwapsApiAllowance.ts +40 -0
- package/src/hooks/swapsApi/useSwapsApiApprove.ts +42 -0
- package/src/hooks/swapsApi/useSwapsApiCancelIntent.ts +41 -0
- package/src/hooks/swapsApi/useSwapsApiCreateIntent.ts +41 -0
- package/src/hooks/swapsApi/useSwapsApiCreateLimitOrder.ts +41 -0
- package/src/hooks/swapsApi/useSwapsApiDeadline.ts +36 -0
- package/src/hooks/swapsApi/useSwapsApiEstimateGas.ts +40 -0
- package/src/hooks/swapsApi/useSwapsApiFilledIntent.ts +41 -0
- package/src/hooks/swapsApi/useSwapsApiIntent.ts +40 -0
- package/src/hooks/swapsApi/useSwapsApiIntentExtraData.ts +41 -0
- package/src/hooks/swapsApi/useSwapsApiIntentHash.ts +40 -0
- package/src/hooks/swapsApi/useSwapsApiIntentPacket.ts +43 -0
- package/src/hooks/swapsApi/useSwapsApiPartnerFee.ts +40 -0
- package/src/hooks/swapsApi/useSwapsApiQuote.ts +55 -0
- package/src/hooks/swapsApi/useSwapsApiSolverFee.ts +40 -0
- package/src/hooks/swapsApi/useSwapsApiStatus.ts +45 -0
- package/src/hooks/swapsApi/useSwapsApiSubmitIntent.ts +43 -0
- package/src/hooks/{backend/useBackendSubmitSwapTx.ts → swapsApi/useSwapsApiSubmitTx.ts} +16 -17
- package/src/hooks/swapsApi/useSwapsApiSubmitTxStatus.ts +55 -0
- package/src/hooks/swapsApi/useSwapsApiTokens.ts +34 -0
- package/src/hooks/swapsApi/useSwapsApiTokensByChain.ts +40 -0
- package/src/hooks/backend/useBackendSubmitSwapTxStatus.ts +0 -59
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { AllowanceCheckResponseV2, CreateIntentParamsV2, 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 UseSwapsApiAllowanceParams = ReadHookParams<
|
|
8
|
+
AllowanceCheckResponseV2 | undefined,
|
|
9
|
+
{
|
|
10
|
+
body: CreateIntentParamsV2 | undefined;
|
|
11
|
+
apiConfig?: RequestOverrideConfig;
|
|
12
|
+
}
|
|
13
|
+
>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* React hook to check whether the source-token allowance is already sufficient for an intent via
|
|
17
|
+
* the swaps API — `sodax.api.swaps.checkAllowance`. Returns `{ valid }`.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const { data: allowance } = useSwapsApiAllowance({ params: { body: createIntentParams } });
|
|
21
|
+
*/
|
|
22
|
+
export const useSwapsApiAllowance = ({
|
|
23
|
+
params,
|
|
24
|
+
queryOptions,
|
|
25
|
+
}: UseSwapsApiAllowanceParams = {}): UseQueryResult<AllowanceCheckResponseV2 | undefined, Error> => {
|
|
26
|
+
const { sodax } = useSodaxContext();
|
|
27
|
+
const body = params?.body;
|
|
28
|
+
const apiConfig = params?.apiConfig;
|
|
29
|
+
|
|
30
|
+
return useQuery({
|
|
31
|
+
queryKey: ['swapsApi', 'allowance', body?.srcChainKey, body?.inputToken, body?.inputAmount, body?.srcAddress],
|
|
32
|
+
queryFn: async (): Promise<AllowanceCheckResponseV2 | undefined> => {
|
|
33
|
+
if (!body) return undefined;
|
|
34
|
+
return unwrapResult(await sodax.api.swaps.checkAllowance(body, apiConfig));
|
|
35
|
+
},
|
|
36
|
+
enabled: !!body,
|
|
37
|
+
retry: 3,
|
|
38
|
+
...queryOptions,
|
|
39
|
+
});
|
|
40
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ApproveResponseV2, CreateIntentParamsV2, 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 useSwapsApiApprove}. 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 UseSwapsApiApproveVars = {
|
|
13
|
+
body: CreateIntentParamsV2;
|
|
14
|
+
apiConfig?: RequestOverrideConfig;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* React hook to build an unsigned token-approval transaction for the source token via the swaps
|
|
19
|
+
* API — `sodax.api.swaps.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 } = useSwapsApiApprove();
|
|
24
|
+
* const { tx } = await approve({ body: createIntentParams });
|
|
25
|
+
*/
|
|
26
|
+
export const useSwapsApiApprove = ({
|
|
27
|
+
mutationOptions,
|
|
28
|
+
}: MutationHookParams<ApproveResponseV2, UseSwapsApiApproveVars> = {}): SafeUseMutationResult<
|
|
29
|
+
ApproveResponseV2,
|
|
30
|
+
Error,
|
|
31
|
+
UseSwapsApiApproveVars
|
|
32
|
+
> => {
|
|
33
|
+
const { sodax } = useSodaxContext();
|
|
34
|
+
|
|
35
|
+
return useSafeMutation<ApproveResponseV2, Error, UseSwapsApiApproveVars>({
|
|
36
|
+
mutationKey: ['swapsApi', 'approve'],
|
|
37
|
+
retry: 3,
|
|
38
|
+
...mutationOptions,
|
|
39
|
+
mutationFn: async ({ body, apiConfig }): Promise<ApproveResponseV2> =>
|
|
40
|
+
unwrapResult(await sodax.api.swaps.approve(body, apiConfig)),
|
|
41
|
+
});
|
|
42
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { CancelIntentRequestV2, CancelIntentResponseV2, 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 useSwapsApiCancelIntent}. The per-request `apiConfig` override
|
|
9
|
+
* belongs here rather than at the hook level.
|
|
10
|
+
*/
|
|
11
|
+
export type UseSwapsApiCancelIntentVars = {
|
|
12
|
+
body: CancelIntentRequestV2;
|
|
13
|
+
apiConfig?: RequestOverrideConfig;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* React hook to build an unsigned cancel-intent transaction via the swaps API —
|
|
18
|
+
* `sodax.api.swaps.cancelIntent`. The `intent` carries `bigint` numerics. Returns `{ tx }` to sign
|
|
19
|
+
* and broadcast yourself; it does not change state, so no queries are invalidated.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const { mutateAsync: cancelIntent } = useSwapsApiCancelIntent();
|
|
23
|
+
* const { tx } = await cancelIntent({ body: { srcChainKey: 'sonic', intent } });
|
|
24
|
+
*/
|
|
25
|
+
export const useSwapsApiCancelIntent = ({
|
|
26
|
+
mutationOptions,
|
|
27
|
+
}: MutationHookParams<CancelIntentResponseV2, UseSwapsApiCancelIntentVars> = {}): SafeUseMutationResult<
|
|
28
|
+
CancelIntentResponseV2,
|
|
29
|
+
Error,
|
|
30
|
+
UseSwapsApiCancelIntentVars
|
|
31
|
+
> => {
|
|
32
|
+
const { sodax } = useSodaxContext();
|
|
33
|
+
|
|
34
|
+
return useSafeMutation<CancelIntentResponseV2, Error, UseSwapsApiCancelIntentVars>({
|
|
35
|
+
mutationKey: ['swapsApi', 'cancelIntent'],
|
|
36
|
+
retry: 3,
|
|
37
|
+
...mutationOptions,
|
|
38
|
+
mutationFn: async ({ body, apiConfig }): Promise<CancelIntentResponseV2> =>
|
|
39
|
+
unwrapResult(await sodax.api.swaps.cancelIntent(body, apiConfig)),
|
|
40
|
+
});
|
|
41
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { CreateIntentParamsV2, CreateIntentResponseV2, 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 useSwapsApiCreateIntent}. The per-request `apiConfig` override
|
|
9
|
+
* belongs here rather than at the hook level.
|
|
10
|
+
*/
|
|
11
|
+
export type UseSwapsApiCreateIntentVars = {
|
|
12
|
+
body: CreateIntentParamsV2;
|
|
13
|
+
apiConfig?: RequestOverrideConfig;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* React hook to build an unsigned create-intent transaction via the swaps API —
|
|
18
|
+
* `sodax.api.swaps.createIntent`. Returns `{ tx, intent, relayData }` to sign and broadcast
|
|
19
|
+
* yourself; it does not change state, so no queries are invalidated.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const { mutateAsync: createIntent } = useSwapsApiCreateIntent();
|
|
23
|
+
* const { tx, intent, relayData } = await createIntent({ body: createIntentParams });
|
|
24
|
+
*/
|
|
25
|
+
export const useSwapsApiCreateIntent = ({
|
|
26
|
+
mutationOptions,
|
|
27
|
+
}: MutationHookParams<CreateIntentResponseV2, UseSwapsApiCreateIntentVars> = {}): SafeUseMutationResult<
|
|
28
|
+
CreateIntentResponseV2,
|
|
29
|
+
Error,
|
|
30
|
+
UseSwapsApiCreateIntentVars
|
|
31
|
+
> => {
|
|
32
|
+
const { sodax } = useSodaxContext();
|
|
33
|
+
|
|
34
|
+
return useSafeMutation<CreateIntentResponseV2, Error, UseSwapsApiCreateIntentVars>({
|
|
35
|
+
mutationKey: ['swapsApi', 'createIntent'],
|
|
36
|
+
retry: 3,
|
|
37
|
+
...mutationOptions,
|
|
38
|
+
mutationFn: async ({ body, apiConfig }): Promise<CreateIntentResponseV2> =>
|
|
39
|
+
unwrapResult(await sodax.api.swaps.createIntent(body, apiConfig)),
|
|
40
|
+
});
|
|
41
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { CreateLimitOrderParamsV2, CreateLimitOrderResponseV2, 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 useSwapsApiCreateLimitOrder}. The per-request `apiConfig` override
|
|
9
|
+
* belongs here rather than at the hook level.
|
|
10
|
+
*/
|
|
11
|
+
export type UseSwapsApiCreateLimitOrderVars = {
|
|
12
|
+
body: CreateLimitOrderParamsV2;
|
|
13
|
+
apiConfig?: RequestOverrideConfig;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* React hook to build an unsigned create-limit-order-intent transaction via the swaps API —
|
|
18
|
+
* `sodax.api.swaps.createLimitOrderIntent` (same as create-intent but `deadline` is optional).
|
|
19
|
+
* Returns `{ tx, intent, relayData }`; it does not change state, so no queries are invalidated.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const { mutateAsync: createLimitOrder } = useSwapsApiCreateLimitOrder();
|
|
23
|
+
* const { tx, intent, relayData } = await createLimitOrder({ body: createLimitOrderParams });
|
|
24
|
+
*/
|
|
25
|
+
export const useSwapsApiCreateLimitOrder = ({
|
|
26
|
+
mutationOptions,
|
|
27
|
+
}: MutationHookParams<CreateLimitOrderResponseV2, UseSwapsApiCreateLimitOrderVars> = {}): SafeUseMutationResult<
|
|
28
|
+
CreateLimitOrderResponseV2,
|
|
29
|
+
Error,
|
|
30
|
+
UseSwapsApiCreateLimitOrderVars
|
|
31
|
+
> => {
|
|
32
|
+
const { sodax } = useSodaxContext();
|
|
33
|
+
|
|
34
|
+
return useSafeMutation<CreateLimitOrderResponseV2, Error, UseSwapsApiCreateLimitOrderVars>({
|
|
35
|
+
mutationKey: ['swapsApi', 'createLimitOrder'],
|
|
36
|
+
retry: 3,
|
|
37
|
+
...mutationOptions,
|
|
38
|
+
mutationFn: async ({ body, apiConfig }): Promise<CreateLimitOrderResponseV2> =>
|
|
39
|
+
unwrapResult(await sodax.api.swaps.createLimitOrderIntent(body, apiConfig)),
|
|
40
|
+
});
|
|
41
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { DeadlineQueryV2, DeadlineResponseV2, 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 UseSwapsApiDeadlineParams = ReadHookParams<
|
|
8
|
+
DeadlineResponseV2,
|
|
9
|
+
{
|
|
10
|
+
query?: DeadlineQueryV2;
|
|
11
|
+
apiConfig?: RequestOverrideConfig;
|
|
12
|
+
}
|
|
13
|
+
>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* React hook to compute a swap deadline (hub timestamp + `offsetSeconds`, default 300s) via the
|
|
17
|
+
* swaps API — `sodax.api.swaps.getDeadline`.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const { data: deadline } = useSwapsApiDeadline({ params: { query: { offsetSeconds: 600 } } });
|
|
21
|
+
*/
|
|
22
|
+
export const useSwapsApiDeadline = ({
|
|
23
|
+
params,
|
|
24
|
+
queryOptions,
|
|
25
|
+
}: UseSwapsApiDeadlineParams = {}): UseQueryResult<DeadlineResponseV2, Error> => {
|
|
26
|
+
const { sodax } = useSodaxContext();
|
|
27
|
+
const query = params?.query;
|
|
28
|
+
const apiConfig = params?.apiConfig;
|
|
29
|
+
|
|
30
|
+
return useQuery<DeadlineResponseV2, Error>({
|
|
31
|
+
queryKey: ['swapsApi', 'deadline', query?.offsetSeconds ?? null],
|
|
32
|
+
queryFn: async (): Promise<DeadlineResponseV2> => unwrapResult(await sodax.api.swaps.getDeadline(query, apiConfig)),
|
|
33
|
+
retry: 3,
|
|
34
|
+
...queryOptions,
|
|
35
|
+
});
|
|
36
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { GasEstimateRequestV2, GasEstimateResponseV2, 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 UseSwapsApiEstimateGasParams = ReadHookParams<
|
|
8
|
+
GasEstimateResponseV2 | undefined,
|
|
9
|
+
{
|
|
10
|
+
body: GasEstimateRequestV2 | undefined;
|
|
11
|
+
apiConfig?: RequestOverrideConfig;
|
|
12
|
+
}
|
|
13
|
+
>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* React hook to estimate gas for a raw transaction on a spoke chain via the swaps API —
|
|
17
|
+
* `sodax.api.swaps.estimateGas`. Returns `{ gas }` (chain-specific shape).
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const { data } = useSwapsApiEstimateGas({ params: { body: { chainKey: 'sonic', tx } } });
|
|
21
|
+
*/
|
|
22
|
+
export const useSwapsApiEstimateGas = ({
|
|
23
|
+
params,
|
|
24
|
+
queryOptions,
|
|
25
|
+
}: UseSwapsApiEstimateGasParams = {}): UseQueryResult<GasEstimateResponseV2 | undefined, Error> => {
|
|
26
|
+
const { sodax } = useSodaxContext();
|
|
27
|
+
const body = params?.body;
|
|
28
|
+
const apiConfig = params?.apiConfig;
|
|
29
|
+
|
|
30
|
+
return useQuery({
|
|
31
|
+
queryKey: ['swapsApi', 'estimateGas', body?.chainKey, body?.tx],
|
|
32
|
+
queryFn: async (): Promise<GasEstimateResponseV2 | undefined> => {
|
|
33
|
+
if (!body) return undefined;
|
|
34
|
+
return unwrapResult(await sodax.api.swaps.estimateGas(body, apiConfig));
|
|
35
|
+
},
|
|
36
|
+
enabled: !!body,
|
|
37
|
+
retry: 3,
|
|
38
|
+
...queryOptions,
|
|
39
|
+
});
|
|
40
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { IntentStateV2, 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 UseSwapsApiFilledIntentParams = ReadHookParams<
|
|
8
|
+
IntentStateV2 | undefined,
|
|
9
|
+
{
|
|
10
|
+
txHash: string | undefined;
|
|
11
|
+
apiConfig?: RequestOverrideConfig;
|
|
12
|
+
}
|
|
13
|
+
>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* React hook to get the on-chain fill state for an intent by its hub-chain tx hash via the swaps
|
|
17
|
+
* API — `sodax.api.swaps.getFilledIntent`. Returns
|
|
18
|
+
* `{ exists, remainingInput, receivedOutput, pendingPayment }`.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const { data: fill } = useSwapsApiFilledIntent({ params: { txHash: '0x123...' } });
|
|
22
|
+
*/
|
|
23
|
+
export const useSwapsApiFilledIntent = ({
|
|
24
|
+
params,
|
|
25
|
+
queryOptions,
|
|
26
|
+
}: UseSwapsApiFilledIntentParams = {}): UseQueryResult<IntentStateV2 | undefined, Error> => {
|
|
27
|
+
const { sodax } = useSodaxContext();
|
|
28
|
+
const txHash = params?.txHash;
|
|
29
|
+
const apiConfig = params?.apiConfig;
|
|
30
|
+
|
|
31
|
+
return useQuery({
|
|
32
|
+
queryKey: ['swapsApi', 'filledIntent', txHash],
|
|
33
|
+
queryFn: async (): Promise<IntentStateV2 | undefined> => {
|
|
34
|
+
if (!txHash) return undefined;
|
|
35
|
+
return unwrapResult(await sodax.api.swaps.getFilledIntent(txHash, apiConfig));
|
|
36
|
+
},
|
|
37
|
+
enabled: !!txHash && txHash.length > 0,
|
|
38
|
+
retry: 3,
|
|
39
|
+
...queryOptions,
|
|
40
|
+
});
|
|
41
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { GetIntentResponseV2, 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 UseSwapsApiIntentParams = ReadHookParams<
|
|
8
|
+
GetIntentResponseV2 | undefined,
|
|
9
|
+
{
|
|
10
|
+
txHash: string | undefined;
|
|
11
|
+
apiConfig?: RequestOverrideConfig;
|
|
12
|
+
}
|
|
13
|
+
>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* React hook to look up an Intent struct by its hub-chain creation tx hash via the swaps API —
|
|
17
|
+
* `sodax.api.swaps.getIntent`. The decoded intent's bigint fields are returned as decimal strings.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const { data: intent } = useSwapsApiIntent({ params: { txHash: '0x123...' } });
|
|
21
|
+
*/
|
|
22
|
+
export const useSwapsApiIntent = ({
|
|
23
|
+
params,
|
|
24
|
+
queryOptions,
|
|
25
|
+
}: UseSwapsApiIntentParams = {}): UseQueryResult<GetIntentResponseV2 | undefined, Error> => {
|
|
26
|
+
const { sodax } = useSodaxContext();
|
|
27
|
+
const txHash = params?.txHash;
|
|
28
|
+
const apiConfig = params?.apiConfig;
|
|
29
|
+
|
|
30
|
+
return useQuery({
|
|
31
|
+
queryKey: ['swapsApi', 'intent', txHash],
|
|
32
|
+
queryFn: async (): Promise<GetIntentResponseV2 | undefined> => {
|
|
33
|
+
if (!txHash) return undefined;
|
|
34
|
+
return unwrapResult(await sodax.api.swaps.getIntent(txHash, apiConfig));
|
|
35
|
+
},
|
|
36
|
+
enabled: !!txHash && txHash.length > 0,
|
|
37
|
+
retry: 3,
|
|
38
|
+
...queryOptions,
|
|
39
|
+
});
|
|
40
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { IntentExtraDataRequestV2, IntentExtraDataResponseV2, 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 UseSwapsApiIntentExtraDataParams = ReadHookParams<
|
|
8
|
+
IntentExtraDataResponseV2 | undefined,
|
|
9
|
+
{
|
|
10
|
+
body: IntentExtraDataRequestV2 | undefined;
|
|
11
|
+
apiConfig?: RequestOverrideConfig;
|
|
12
|
+
}
|
|
13
|
+
>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* React hook to recover the relay extra data needed by `/swaps/intents/submit` via the swaps API —
|
|
17
|
+
* `sodax.api.swaps.getIntentSubmitTxExtraData`. Provide EITHER `txHash` OR `intent` in the body
|
|
18
|
+
* (whose `bigint` numerics are serialized by the SDK). Returns `{ address, payload }`.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const { data } = useSwapsApiIntentExtraData({ params: { body: { txHash: '0x123...' } } });
|
|
22
|
+
*/
|
|
23
|
+
export const useSwapsApiIntentExtraData = ({
|
|
24
|
+
params,
|
|
25
|
+
queryOptions,
|
|
26
|
+
}: UseSwapsApiIntentExtraDataParams = {}): UseQueryResult<IntentExtraDataResponseV2 | undefined, Error> => {
|
|
27
|
+
const { sodax } = useSodaxContext();
|
|
28
|
+
const body = params?.body;
|
|
29
|
+
const apiConfig = params?.apiConfig;
|
|
30
|
+
|
|
31
|
+
return useQuery({
|
|
32
|
+
queryKey: ['swapsApi', 'intentExtraData', body?.txHash ?? body?.intent?.intentId?.toString()],
|
|
33
|
+
queryFn: async (): Promise<IntentExtraDataResponseV2 | undefined> => {
|
|
34
|
+
if (!body || (!body.txHash && !body.intent)) return undefined;
|
|
35
|
+
return unwrapResult(await sodax.api.swaps.getIntentSubmitTxExtraData(body, apiConfig));
|
|
36
|
+
},
|
|
37
|
+
enabled: !!body && (!!body.txHash || !!body.intent),
|
|
38
|
+
retry: 3,
|
|
39
|
+
...queryOptions,
|
|
40
|
+
});
|
|
41
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { IntentHashResponseV2, IntentRequestV2, 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 UseSwapsApiIntentHashParams = ReadHookParams<
|
|
8
|
+
IntentHashResponseV2 | undefined,
|
|
9
|
+
{
|
|
10
|
+
intent: IntentRequestV2 | undefined;
|
|
11
|
+
apiConfig?: RequestOverrideConfig;
|
|
12
|
+
}
|
|
13
|
+
>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* React hook to compute the keccak256 hash of an Intent struct via the swaps API —
|
|
17
|
+
* `sodax.api.swaps.getIntentHash`. The `intent` carries `bigint` numerics. Returns `{ hash }`.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const { data } = useSwapsApiIntentHash({ params: { intent } });
|
|
21
|
+
*/
|
|
22
|
+
export const useSwapsApiIntentHash = ({
|
|
23
|
+
params,
|
|
24
|
+
queryOptions,
|
|
25
|
+
}: UseSwapsApiIntentHashParams = {}): UseQueryResult<IntentHashResponseV2 | undefined, Error> => {
|
|
26
|
+
const { sodax } = useSodaxContext();
|
|
27
|
+
const intent = params?.intent;
|
|
28
|
+
const apiConfig = params?.apiConfig;
|
|
29
|
+
|
|
30
|
+
return useQuery({
|
|
31
|
+
queryKey: ['swapsApi', 'intentHash', intent?.intentId?.toString()],
|
|
32
|
+
queryFn: async (): Promise<IntentHashResponseV2 | undefined> => {
|
|
33
|
+
if (!intent) return undefined;
|
|
34
|
+
return unwrapResult(await sodax.api.swaps.getIntentHash({ intent }, apiConfig));
|
|
35
|
+
},
|
|
36
|
+
enabled: !!intent,
|
|
37
|
+
retry: 3,
|
|
38
|
+
...queryOptions,
|
|
39
|
+
});
|
|
40
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { IntentPacketRequestV2, IntentPacketResponseV2, 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 UseSwapsApiIntentPacketParams = ReadHookParams<
|
|
8
|
+
IntentPacketResponseV2 | undefined,
|
|
9
|
+
{
|
|
10
|
+
body: IntentPacketRequestV2 | undefined;
|
|
11
|
+
apiConfig?: RequestOverrideConfig;
|
|
12
|
+
}
|
|
13
|
+
>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* React hook to long-poll the relayer until the fill packet lands on the destination chain via the
|
|
17
|
+
* swaps API — `sodax.api.swaps.getSolvedIntentPacket`. The request is held open server-side until
|
|
18
|
+
* the packet arrives (or `body.timeout` ms elapses), so no client-side `refetchInterval` is set.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const { data: packet } = useSwapsApiIntentPacket({
|
|
22
|
+
* params: { body: { chainId: '146', fillTxHash: '0x123...' } },
|
|
23
|
+
* });
|
|
24
|
+
*/
|
|
25
|
+
export const useSwapsApiIntentPacket = ({
|
|
26
|
+
params,
|
|
27
|
+
queryOptions,
|
|
28
|
+
}: UseSwapsApiIntentPacketParams = {}): UseQueryResult<IntentPacketResponseV2 | undefined, Error> => {
|
|
29
|
+
const { sodax } = useSodaxContext();
|
|
30
|
+
const body = params?.body;
|
|
31
|
+
const apiConfig = params?.apiConfig;
|
|
32
|
+
|
|
33
|
+
return useQuery({
|
|
34
|
+
queryKey: ['swapsApi', 'intentPacket', body?.chainId, body?.fillTxHash],
|
|
35
|
+
queryFn: async (): Promise<IntentPacketResponseV2 | undefined> => {
|
|
36
|
+
if (!body) return undefined;
|
|
37
|
+
return unwrapResult(await sodax.api.swaps.getSolvedIntentPacket(body, apiConfig));
|
|
38
|
+
},
|
|
39
|
+
enabled: !!body,
|
|
40
|
+
retry: 3,
|
|
41
|
+
...queryOptions,
|
|
42
|
+
});
|
|
43
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { FeeResponseV2, 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 UseSwapsApiPartnerFeeParams = ReadHookParams<
|
|
8
|
+
FeeResponseV2 | undefined,
|
|
9
|
+
{
|
|
10
|
+
amount: string | undefined;
|
|
11
|
+
apiConfig?: RequestOverrideConfig;
|
|
12
|
+
}
|
|
13
|
+
>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* React hook to compute the partner fee for a given input amount via the swaps API —
|
|
17
|
+
* `sodax.api.swaps.getPartnerFee`. Returns `{ fee }` (decimal string).
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const { data } = useSwapsApiPartnerFee({ params: { amount: '1000000' } });
|
|
21
|
+
*/
|
|
22
|
+
export const useSwapsApiPartnerFee = ({
|
|
23
|
+
params,
|
|
24
|
+
queryOptions,
|
|
25
|
+
}: UseSwapsApiPartnerFeeParams = {}): UseQueryResult<FeeResponseV2 | undefined, Error> => {
|
|
26
|
+
const { sodax } = useSodaxContext();
|
|
27
|
+
const amount = params?.amount;
|
|
28
|
+
const apiConfig = params?.apiConfig;
|
|
29
|
+
|
|
30
|
+
return useQuery({
|
|
31
|
+
queryKey: ['swapsApi', 'partnerFee', amount],
|
|
32
|
+
queryFn: async (): Promise<FeeResponseV2 | undefined> => {
|
|
33
|
+
if (!amount) return undefined;
|
|
34
|
+
return unwrapResult(await sodax.api.swaps.getPartnerFee({ amount }, apiConfig));
|
|
35
|
+
},
|
|
36
|
+
enabled: !!amount && amount.length > 0,
|
|
37
|
+
retry: 3,
|
|
38
|
+
...queryOptions,
|
|
39
|
+
});
|
|
40
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { QuoteQueryV2, QuoteRequestV2, QuoteResponseV2, 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 UseSwapsApiQuoteParams = ReadHookParams<
|
|
8
|
+
QuoteResponseV2 | undefined,
|
|
9
|
+
{
|
|
10
|
+
body: QuoteRequestV2 | undefined;
|
|
11
|
+
query?: QuoteQueryV2;
|
|
12
|
+
apiConfig?: RequestOverrideConfig;
|
|
13
|
+
}
|
|
14
|
+
>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* React hook to get a solver quote for a cross-chain swap via the swaps API —
|
|
18
|
+
* `sodax.api.swaps.getQuote`. Pass `query.includeTxData = true` to also build an unsigned
|
|
19
|
+
* create-intent transaction (`txData`); `srcAddress`/`dstAddress` are then required in the body.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const { data: quote } = useSwapsApiQuote({
|
|
23
|
+
* params: {
|
|
24
|
+
* body: {
|
|
25
|
+
* tokenSrc: '0x...', tokenSrcChainKey: '0xa4b1.arbitrum',
|
|
26
|
+
* tokenDst: '0x...', tokenDstChainKey: 'sonic',
|
|
27
|
+
* amount: '1000000', quoteType: 'exact_input',
|
|
28
|
+
* },
|
|
29
|
+
* },
|
|
30
|
+
* });
|
|
31
|
+
*/
|
|
32
|
+
export const useSwapsApiQuote = ({
|
|
33
|
+
params,
|
|
34
|
+
queryOptions,
|
|
35
|
+
}: UseSwapsApiQuoteParams = {}): UseQueryResult<QuoteResponseV2 | undefined, Error> => {
|
|
36
|
+
const { sodax } = useSodaxContext();
|
|
37
|
+
const body = params?.body;
|
|
38
|
+
const query = params?.query;
|
|
39
|
+
const apiConfig = params?.apiConfig;
|
|
40
|
+
|
|
41
|
+
return useQuery({
|
|
42
|
+
// Hash the whole request body so every quote input — including partnerFee and the other
|
|
43
|
+
// SwapExtrasV2 fields — is a cache dimension; a partnerFee change alters the quote and must
|
|
44
|
+
// miss the cache. QuoteRequestV2 is bigint-free (compile-time guaranteed), so React Query's
|
|
45
|
+
// default key hashing handles the object directly.
|
|
46
|
+
queryKey: ['swapsApi', 'quote', body, query?.includeTxData ?? false],
|
|
47
|
+
queryFn: async (): Promise<QuoteResponseV2 | undefined> => {
|
|
48
|
+
if (!body) return undefined;
|
|
49
|
+
return unwrapResult(await sodax.api.swaps.getQuote(body, query, apiConfig));
|
|
50
|
+
},
|
|
51
|
+
enabled: !!body,
|
|
52
|
+
retry: 3,
|
|
53
|
+
...queryOptions,
|
|
54
|
+
});
|
|
55
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { FeeResponseV2, 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 UseSwapsApiSolverFeeParams = ReadHookParams<
|
|
8
|
+
FeeResponseV2 | undefined,
|
|
9
|
+
{
|
|
10
|
+
amount: string | undefined;
|
|
11
|
+
apiConfig?: RequestOverrideConfig;
|
|
12
|
+
}
|
|
13
|
+
>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* React hook to compute the protocol (solver) fee for a given input amount via the swaps API —
|
|
17
|
+
* `sodax.api.swaps.getSolverFee`. Returns `{ fee }` (decimal string).
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const { data } = useSwapsApiSolverFee({ params: { amount: '1000000' } });
|
|
21
|
+
*/
|
|
22
|
+
export const useSwapsApiSolverFee = ({
|
|
23
|
+
params,
|
|
24
|
+
queryOptions,
|
|
25
|
+
}: UseSwapsApiSolverFeeParams = {}): UseQueryResult<FeeResponseV2 | undefined, Error> => {
|
|
26
|
+
const { sodax } = useSodaxContext();
|
|
27
|
+
const amount = params?.amount;
|
|
28
|
+
const apiConfig = params?.apiConfig;
|
|
29
|
+
|
|
30
|
+
return useQuery({
|
|
31
|
+
queryKey: ['swapsApi', 'solverFee', amount],
|
|
32
|
+
queryFn: async (): Promise<FeeResponseV2 | undefined> => {
|
|
33
|
+
if (!amount) return undefined;
|
|
34
|
+
return unwrapResult(await sodax.api.swaps.getSolverFee({ amount }, apiConfig));
|
|
35
|
+
},
|
|
36
|
+
enabled: !!amount && amount.length > 0,
|
|
37
|
+
retry: 3,
|
|
38
|
+
...queryOptions,
|
|
39
|
+
});
|
|
40
|
+
};
|