@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
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { BridgeableAmountResponseV2, BridgeQuoteRequestV2, 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 UseBridgeApiBridgeableAmountParams = ReadHookParams<
|
|
8
|
+
BridgeableAmountResponseV2 | undefined,
|
|
9
|
+
{
|
|
10
|
+
body: BridgeQuoteRequestV2 | undefined;
|
|
11
|
+
apiConfig?: RequestOverrideConfig;
|
|
12
|
+
}
|
|
13
|
+
>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* React hook to fetch the deposit capacity / withdrawal liquidity limit for a (from, to) token pair
|
|
17
|
+
* via the bridge API — `sodax.api.bridge.getBridgeableAmount`. Returns `{ limit }`.
|
|
18
|
+
*
|
|
19
|
+
* Computable client-side from config + vault reserves — prefer `sodax.bridge.getBridgeableAmount`
|
|
20
|
+
* for a no-round-trip read; this HTTP variant mirrors the `/bridge/bridgeable-amount` endpoint.
|
|
21
|
+
*
|
|
22
|
+
* The `body` uses the wire DTO field names (`srcChainKey`/`dstChainKey`/`inputToken`/`outputToken`).
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* const { data } = useBridgeApiBridgeableAmount({ params: { body: quote } });
|
|
26
|
+
*/
|
|
27
|
+
export const useBridgeApiBridgeableAmount = ({
|
|
28
|
+
params,
|
|
29
|
+
queryOptions,
|
|
30
|
+
}: UseBridgeApiBridgeableAmountParams = {}): UseQueryResult<BridgeableAmountResponseV2 | undefined, Error> => {
|
|
31
|
+
const { sodax } = useSodaxContext();
|
|
32
|
+
const body = params?.body;
|
|
33
|
+
const apiConfig = params?.apiConfig;
|
|
34
|
+
|
|
35
|
+
return useQuery({
|
|
36
|
+
queryKey: ['bridgeApi', 'bridgeableAmount', body?.srcChainKey, body?.dstChainKey, body?.inputToken, body?.outputToken],
|
|
37
|
+
queryFn: async (): Promise<BridgeableAmountResponseV2 | undefined> => {
|
|
38
|
+
if (!body) return undefined;
|
|
39
|
+
return unwrapResult(await sodax.api.bridge.getBridgeableAmount(body, apiConfig));
|
|
40
|
+
},
|
|
41
|
+
enabled: !!body,
|
|
42
|
+
retry: 3,
|
|
43
|
+
...queryOptions,
|
|
44
|
+
});
|
|
45
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { CreateBridgeIntentParamsV2, CreateBridgeIntentResponseV2, 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 useBridgeApiCreateBridgeIntent}. The per-request `apiConfig`
|
|
9
|
+
* override belongs here rather than at the hook level.
|
|
10
|
+
*/
|
|
11
|
+
export type UseBridgeApiCreateBridgeIntentVars = {
|
|
12
|
+
body: CreateBridgeIntentParamsV2;
|
|
13
|
+
apiConfig?: RequestOverrideConfig;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* React hook to build an unsigned spoke-deposit (create-bridge-intent) transaction via the bridge
|
|
18
|
+
* API — `sodax.api.bridge.createBridgeIntent`. Returns `{ tx, relayData }` (no `intent` struct —
|
|
19
|
+
* bridge is vault-backed) to sign and broadcast yourself; it does not change state, so no queries
|
|
20
|
+
* are invalidated.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* const { mutateAsync: createBridgeIntent } = useBridgeApiCreateBridgeIntent();
|
|
24
|
+
* const { tx, relayData } = await createBridgeIntent({ body: createBridgeIntentParams });
|
|
25
|
+
*/
|
|
26
|
+
export const useBridgeApiCreateBridgeIntent = ({
|
|
27
|
+
mutationOptions,
|
|
28
|
+
}: MutationHookParams<CreateBridgeIntentResponseV2, UseBridgeApiCreateBridgeIntentVars> = {}): SafeUseMutationResult<
|
|
29
|
+
CreateBridgeIntentResponseV2,
|
|
30
|
+
Error,
|
|
31
|
+
UseBridgeApiCreateBridgeIntentVars
|
|
32
|
+
> => {
|
|
33
|
+
const { sodax } = useSodaxContext();
|
|
34
|
+
|
|
35
|
+
return useSafeMutation<CreateBridgeIntentResponseV2, Error, UseBridgeApiCreateBridgeIntentVars>({
|
|
36
|
+
mutationKey: ['bridgeApi', 'createBridgeIntent'],
|
|
37
|
+
retry: 3,
|
|
38
|
+
...mutationOptions,
|
|
39
|
+
mutationFn: async ({ body, apiConfig }): Promise<CreateBridgeIntentResponseV2> =>
|
|
40
|
+
unwrapResult(await sodax.api.bridge.createBridgeIntent(body, apiConfig)),
|
|
41
|
+
});
|
|
42
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { BridgeFeeRequestV2, BridgeFeeResponseV2, 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 UseBridgeApiFeeParams = ReadHookParams<
|
|
8
|
+
BridgeFeeResponseV2 | undefined,
|
|
9
|
+
{
|
|
10
|
+
body: BridgeFeeRequestV2 | undefined;
|
|
11
|
+
apiConfig?: RequestOverrideConfig;
|
|
12
|
+
}
|
|
13
|
+
>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* React hook to fetch the bridge partner fee for an input amount via the bridge API (uses a per-request
|
|
17
|
+
* `body.partnerFee` override when supplied, else the backend-configured fee) —
|
|
18
|
+
* `sodax.api.bridge.getFee`. Returns `{ fee }` (smallest unit, decimal string).
|
|
19
|
+
*
|
|
20
|
+
* The fee is computable client-side (config-driven, token-independent) — prefer `sodax.bridge.getFee`
|
|
21
|
+
* for a no-round-trip read; this HTTP variant mirrors the `/bridge/fee` endpoint for parity.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* const { data: fee } = useBridgeApiFee({ params: { body: { inputAmount } } });
|
|
25
|
+
*/
|
|
26
|
+
export const useBridgeApiFee = ({
|
|
27
|
+
params,
|
|
28
|
+
queryOptions,
|
|
29
|
+
}: UseBridgeApiFeeParams = {}): UseQueryResult<BridgeFeeResponseV2 | undefined, Error> => {
|
|
30
|
+
const { sodax } = useSodaxContext();
|
|
31
|
+
const body = params?.body;
|
|
32
|
+
const apiConfig = params?.apiConfig;
|
|
33
|
+
|
|
34
|
+
return useQuery({
|
|
35
|
+
queryKey: ['bridgeApi', 'fee', body?.inputAmount, body?.partnerFee],
|
|
36
|
+
queryFn: async (): Promise<BridgeFeeResponseV2 | undefined> => {
|
|
37
|
+
if (!body) return undefined;
|
|
38
|
+
return unwrapResult(await sodax.api.bridge.getFee(body, apiConfig));
|
|
39
|
+
},
|
|
40
|
+
enabled: !!body,
|
|
41
|
+
retry: 3,
|
|
42
|
+
...queryOptions,
|
|
43
|
+
});
|
|
44
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { BridgeableCheckResponseV2, BridgeQuoteRequestV2, 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 UseBridgeApiIsBridgeableParams = ReadHookParams<
|
|
8
|
+
BridgeableCheckResponseV2 | undefined,
|
|
9
|
+
{
|
|
10
|
+
body: BridgeQuoteRequestV2 | undefined;
|
|
11
|
+
apiConfig?: RequestOverrideConfig;
|
|
12
|
+
}
|
|
13
|
+
>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* React hook to check whether a (from, to) token pair is bridgeable via the bridge API —
|
|
17
|
+
* `sodax.api.bridge.isBridgeable`. Returns `{ bridgeable }`.
|
|
18
|
+
*
|
|
19
|
+
* Computable client-side from config + vault reserves — prefer `sodax.bridge.isBridgeable`
|
|
20
|
+
* for a no-round-trip read; this HTTP variant mirrors the `/bridge/bridgeable/check` endpoint.
|
|
21
|
+
*
|
|
22
|
+
* The `body` uses the wire DTO field names (`srcChainKey`/`dstChainKey`/`inputToken`/`outputToken`).
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* const { data } = useBridgeApiIsBridgeable({ params: { body: quote } });
|
|
26
|
+
*/
|
|
27
|
+
export const useBridgeApiIsBridgeable = ({
|
|
28
|
+
params,
|
|
29
|
+
queryOptions,
|
|
30
|
+
}: UseBridgeApiIsBridgeableParams = {}): UseQueryResult<BridgeableCheckResponseV2 | undefined, Error> => {
|
|
31
|
+
const { sodax } = useSodaxContext();
|
|
32
|
+
const body = params?.body;
|
|
33
|
+
const apiConfig = params?.apiConfig;
|
|
34
|
+
|
|
35
|
+
return useQuery({
|
|
36
|
+
queryKey: ['bridgeApi', 'bridgeable', body?.srcChainKey, body?.dstChainKey, body?.inputToken, body?.outputToken],
|
|
37
|
+
queryFn: async (): Promise<BridgeableCheckResponseV2 | undefined> => {
|
|
38
|
+
if (!body) return undefined;
|
|
39
|
+
return unwrapResult(await sodax.api.bridge.isBridgeable(body, apiConfig));
|
|
40
|
+
},
|
|
41
|
+
enabled: !!body,
|
|
42
|
+
retry: 3,
|
|
43
|
+
...queryOptions,
|
|
44
|
+
});
|
|
45
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { BridgeSubmitTxRequestV2, BridgeSubmitTxResponseV2, 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 useBridgeApiSubmitTx}. The per-request `apiConfig` override
|
|
9
|
+
* (e.g. base URL) belongs here rather than at the hook level — different submissions in the same
|
|
10
|
+
* component can target different endpoints without re-rendering.
|
|
11
|
+
*/
|
|
12
|
+
export type UseBridgeApiSubmitTxVars = {
|
|
13
|
+
request: BridgeSubmitTxRequestV2;
|
|
14
|
+
apiConfig?: RequestOverrideConfig;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* React hook for submitting a broadcast bridge (spoke-deposit) transaction to be processed
|
|
19
|
+
* (relayed server-side) via the bridge API — `sodax.api.bridge.submitTx`. Called AFTER signing +
|
|
20
|
+
* broadcasting the spoke-deposit tx, handing it (with the FULL `relayData { address, payload }`
|
|
21
|
+
* envelope) to the backend.
|
|
22
|
+
*
|
|
23
|
+
* Pure mutation: pass `{ request, apiConfig? }` to `mutate({...})`. Default `retry: 3` is applied
|
|
24
|
+
* at the hook level — consumers can override via `mutationOptions.retry`.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* const { mutateAsync: submitBridgeTx, isPending, error } = useBridgeApiSubmitTx();
|
|
28
|
+
*
|
|
29
|
+
* const result = await submitBridgeTx({
|
|
30
|
+
* request: { txHash: '0x123...', srcChainKey: '0xa4b1.arbitrum', walletAddress: '0xabc...', relayData: { address: '0x...', payload: '0x...' } },
|
|
31
|
+
* apiConfig: { baseURL: 'https://...' },
|
|
32
|
+
* });
|
|
33
|
+
*/
|
|
34
|
+
export const useBridgeApiSubmitTx = ({
|
|
35
|
+
mutationOptions,
|
|
36
|
+
}: MutationHookParams<BridgeSubmitTxResponseV2, UseBridgeApiSubmitTxVars> = {}): SafeUseMutationResult<
|
|
37
|
+
BridgeSubmitTxResponseV2,
|
|
38
|
+
Error,
|
|
39
|
+
UseBridgeApiSubmitTxVars
|
|
40
|
+
> => {
|
|
41
|
+
const { sodax } = useSodaxContext();
|
|
42
|
+
|
|
43
|
+
return useSafeMutation<BridgeSubmitTxResponseV2, Error, UseBridgeApiSubmitTxVars>({
|
|
44
|
+
mutationKey: ['bridgeApi', 'submitTx'],
|
|
45
|
+
retry: 3,
|
|
46
|
+
...mutationOptions,
|
|
47
|
+
mutationFn: async ({ request, apiConfig }): Promise<BridgeSubmitTxResponseV2> =>
|
|
48
|
+
unwrapResult(await sodax.api.bridge.submitTx(request, apiConfig)),
|
|
49
|
+
});
|
|
50
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { BridgeSubmitTxStatusResponseV2, 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 UseBridgeApiSubmitTxStatusParams = ReadHookParams<
|
|
8
|
+
BridgeSubmitTxStatusResponseV2 | undefined,
|
|
9
|
+
{
|
|
10
|
+
txHash: string | undefined;
|
|
11
|
+
srcChainKey?: string;
|
|
12
|
+
apiConfig?: RequestOverrideConfig;
|
|
13
|
+
}
|
|
14
|
+
>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* React hook for polling the processing status of a submitted bridge transaction via the bridge
|
|
18
|
+
* API — `sodax.api.bridge.getSubmitTxStatus`. Both `txHash` and `srcChainKey` are required for the
|
|
19
|
+
* query to run.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const { data: status } = useBridgeApiSubmitTxStatus({
|
|
23
|
+
* params: { txHash: '0x123...', srcChainKey: '0xa4b1.arbitrum' },
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* @remarks
|
|
27
|
+
* - Default refetch interval is 1 second; stops on 'executed' or 'failed' status, or when the
|
|
28
|
+
* backend marks the submission abandoned (`abandonedAt`) while `status` stays non-terminal —
|
|
29
|
+
* mirroring the SDK's backend submit-tx poll (no solver `posting_execution` state — bridge has
|
|
30
|
+
* no post-execution).
|
|
31
|
+
*/
|
|
32
|
+
export const useBridgeApiSubmitTxStatus = ({
|
|
33
|
+
params,
|
|
34
|
+
queryOptions,
|
|
35
|
+
}: UseBridgeApiSubmitTxStatusParams = {}): UseQueryResult<BridgeSubmitTxStatusResponseV2 | undefined, Error> => {
|
|
36
|
+
const { sodax } = useSodaxContext();
|
|
37
|
+
const txHash = params?.txHash;
|
|
38
|
+
const srcChainKey = params?.srcChainKey;
|
|
39
|
+
const apiConfig = params?.apiConfig;
|
|
40
|
+
|
|
41
|
+
return useQuery({
|
|
42
|
+
queryKey: ['bridgeApi', 'submitTx', 'status', txHash, srcChainKey],
|
|
43
|
+
queryFn: async (): Promise<BridgeSubmitTxStatusResponseV2 | undefined> => {
|
|
44
|
+
// `srcChainKey` is required by the bridge API status query. The `enabled` gate below ensures
|
|
45
|
+
// both are present; this guard also narrows the types for the call.
|
|
46
|
+
if (!txHash || !srcChainKey) return undefined;
|
|
47
|
+
return unwrapResult(await sodax.api.bridge.getSubmitTxStatus({ txHash, srcChainKey }, apiConfig));
|
|
48
|
+
},
|
|
49
|
+
enabled: !!txHash && txHash.length > 0 && !!srcChainKey,
|
|
50
|
+
retry: 3,
|
|
51
|
+
refetchInterval: query => {
|
|
52
|
+
const data = query.state.data?.data;
|
|
53
|
+
// `abandonedAt` is terminal even when `status` is still non-terminal (e.g. 'relayed') —
|
|
54
|
+
// same rule as the SDK's pollBackendSubmitTx.
|
|
55
|
+
if (data?.status === 'executed' || data?.status === 'failed' || data?.abandonedAt) return false;
|
|
56
|
+
return 1000;
|
|
57
|
+
},
|
|
58
|
+
...queryOptions,
|
|
59
|
+
});
|
|
60
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { GetBridgeTokensResponseV2, 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 UseBridgeApiTokensParams = ReadHookParams<
|
|
8
|
+
GetBridgeTokensResponseV2,
|
|
9
|
+
{
|
|
10
|
+
apiConfig?: RequestOverrideConfig;
|
|
11
|
+
}
|
|
12
|
+
>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* React hook to fetch all supported bridge tokens grouped by SpokeChainKey via the bridge API —
|
|
16
|
+
* `sodax.api.bridge.getTokens`. The token list is backend-served; the bridgeable-amount quote is
|
|
17
|
+
* computable client-side via `useGetBridgeableAmount` (also mirrored by `useBridgeApiBridgeableAmount`).
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const { data: tokensByChain } = useBridgeApiTokens();
|
|
21
|
+
*/
|
|
22
|
+
export const useBridgeApiTokens = ({
|
|
23
|
+
params,
|
|
24
|
+
queryOptions,
|
|
25
|
+
}: UseBridgeApiTokensParams = {}): UseQueryResult<GetBridgeTokensResponseV2, Error> => {
|
|
26
|
+
const { sodax } = useSodaxContext();
|
|
27
|
+
const apiConfig = params?.apiConfig;
|
|
28
|
+
|
|
29
|
+
return useQuery<GetBridgeTokensResponseV2, Error>({
|
|
30
|
+
queryKey: ['bridgeApi', 'tokens'],
|
|
31
|
+
queryFn: async (): Promise<GetBridgeTokensResponseV2> => unwrapResult(await sodax.api.bridge.getTokens(apiConfig)),
|
|
32
|
+
retry: 3,
|
|
33
|
+
...queryOptions,
|
|
34
|
+
});
|
|
35
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { GetBridgeTokensByChainResponseV2, 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 UseBridgeApiTokensByChainParams = ReadHookParams<
|
|
8
|
+
GetBridgeTokensByChainResponseV2 | undefined,
|
|
9
|
+
{
|
|
10
|
+
chainKey: string | undefined;
|
|
11
|
+
apiConfig?: RequestOverrideConfig;
|
|
12
|
+
}
|
|
13
|
+
>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* React hook to fetch supported bridge tokens for a single SpokeChainKey via the bridge API —
|
|
17
|
+
* `sodax.api.bridge.getTokensByChain`. Use `useBridgeApiTokens` for every chain at once.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const { data: tokens } = useBridgeApiTokensByChain({ params: { chainKey: '0xa4b1.arbitrum' } });
|
|
21
|
+
*/
|
|
22
|
+
export const useBridgeApiTokensByChain = ({
|
|
23
|
+
params,
|
|
24
|
+
queryOptions,
|
|
25
|
+
}: UseBridgeApiTokensByChainParams = {}): UseQueryResult<GetBridgeTokensByChainResponseV2 | undefined, Error> => {
|
|
26
|
+
const { sodax } = useSodaxContext();
|
|
27
|
+
const chainKey = params?.chainKey;
|
|
28
|
+
const apiConfig = params?.apiConfig;
|
|
29
|
+
|
|
30
|
+
return useQuery({
|
|
31
|
+
queryKey: ['bridgeApi', 'tokens', chainKey],
|
|
32
|
+
queryFn: async (): Promise<GetBridgeTokensByChainResponseV2 | undefined> => {
|
|
33
|
+
if (!chainKey) return undefined;
|
|
34
|
+
return unwrapResult(await sodax.api.bridge.getTokensByChain(chainKey, apiConfig));
|
|
35
|
+
},
|
|
36
|
+
enabled: !!chainKey && chainKey.length > 0,
|
|
37
|
+
retry: 3,
|
|
38
|
+
...queryOptions,
|
|
39
|
+
});
|
|
40
|
+
};
|
package/src/hooks/index.ts
CHANGED
|
@@ -6,9 +6,11 @@ export * from './swap/index.js';
|
|
|
6
6
|
export * from './backend/index.js';
|
|
7
7
|
export * from './swapsApi/index.js';
|
|
8
8
|
export * from './bridge/index.js';
|
|
9
|
+
export * from './bridgeApi/index.js';
|
|
9
10
|
export * from './staking/index.js';
|
|
10
11
|
export * from './partner/index.js';
|
|
11
12
|
export * from './recovery/index.js';
|
|
12
13
|
export * from './migrate/index.js';
|
|
13
14
|
export * from './dex/index.js';
|
|
14
15
|
export * from './leverageYield/index.js';
|
|
16
|
+
export * from './sponsoring/index.js';
|
|
@@ -49,9 +49,7 @@ export function useMigrationApprove<K extends SpokeChainKey = SpokeChainKey>({
|
|
|
49
49
|
mutationFn: async ({ action, ...actionParams }) =>
|
|
50
50
|
unwrapResult(
|
|
51
51
|
await sodax.migration.approve(
|
|
52
|
-
{ ...actionParams, raw: false } as
|
|
53
|
-
| IcxRevertMigrationAction<false>
|
|
54
|
-
| UnifiedBnUSDMigrateAction<K, false>,
|
|
52
|
+
{ ...actionParams, raw: false } as IcxRevertMigrationAction<false> | UnifiedBnUSDMigrateAction<K, false>,
|
|
55
53
|
action,
|
|
56
54
|
),
|
|
57
55
|
),
|
|
@@ -24,7 +24,11 @@ export type UseWithdrawVars<K extends SpokeChainKey = SpokeChainKey> = Omit<
|
|
|
24
24
|
*/
|
|
25
25
|
export function useWithdraw<K extends SpokeChainKey = SpokeChainKey>({
|
|
26
26
|
mutationOptions,
|
|
27
|
-
}: MutationHookParams<TxHashPair, UseWithdrawVars<K>> = {}): SafeUseMutationResult<
|
|
27
|
+
}: MutationHookParams<TxHashPair, UseWithdrawVars<K>> = {}): SafeUseMutationResult<
|
|
28
|
+
TxHashPair,
|
|
29
|
+
Error,
|
|
30
|
+
UseWithdrawVars<K>
|
|
31
|
+
> {
|
|
28
32
|
const { sodax } = useSodaxContext();
|
|
29
33
|
const queryClient = useQueryClient();
|
|
30
34
|
|
|
@@ -29,8 +29,7 @@ export function useApproveToken({
|
|
|
29
29
|
return useSafeMutation<ApproveTokenData, Error, UseApproveTokenVars>({
|
|
30
30
|
mutationKey: ['partner', 'approveToken'],
|
|
31
31
|
...mutationOptions,
|
|
32
|
-
mutationFn: async vars =>
|
|
33
|
-
unwrapResult(await sodax.partners.feeClaim.approveToken<false>({ ...vars, raw: false })),
|
|
32
|
+
mutationFn: async vars => unwrapResult(await sodax.partners.feeClaim.approveToken<false>({ ...vars, raw: false })),
|
|
34
33
|
onSuccess: async (data, vars, ctx) => {
|
|
35
34
|
const { params } = vars;
|
|
36
35
|
queryClient.invalidateQueries({
|
|
@@ -6,9 +6,11 @@ export * from './useSodaxContext.js';
|
|
|
6
6
|
export * from './useEstimateGas.js';
|
|
7
7
|
export * from './useDeriveUserWalletAddress.js';
|
|
8
8
|
export * from './useStellarTrustlineCheck.js';
|
|
9
|
+
export * from './useEstablishTrustline.js';
|
|
9
10
|
export * from './useRequestTrustline.js';
|
|
10
11
|
export * from './useNearStorageCheck.js';
|
|
11
12
|
export * from './useRegisterNearStorage.js';
|
|
12
13
|
export * from './useNearStorageGate.js';
|
|
14
|
+
export * from './useStellarGate.js';
|
|
13
15
|
export * from './useGetUserHubWalletAddress.js';
|
|
14
16
|
export * from './useXBalances.js';
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { ChainKeys } from '@sodax/sdk';
|
|
2
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
+
|
|
4
|
+
const invalidateQueries = vi.fn();
|
|
5
|
+
const requestTrustline = vi.fn();
|
|
6
|
+
|
|
7
|
+
// The hook is a pure options builder around useSafeMutation, so stubbing the three
|
|
8
|
+
// React-side imports lets the real onSuccess run without a DOM.
|
|
9
|
+
vi.mock('@tanstack/react-query', () => ({ useQueryClient: () => ({ invalidateQueries }) }));
|
|
10
|
+
vi.mock('./useSodaxContext.js', () => ({
|
|
11
|
+
useSodaxContext: () => ({ sodax: { spoke: { stellar: { requestTrustline } } } }),
|
|
12
|
+
}));
|
|
13
|
+
vi.mock('./useSafeMutation.js', () => ({ useSafeMutation: (options: unknown) => options }));
|
|
14
|
+
|
|
15
|
+
const { useEstablishTrustline } = await import('./useEstablishTrustline.js');
|
|
16
|
+
|
|
17
|
+
const TOKEN = 'CBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB';
|
|
18
|
+
const ADDRESS = 'GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
|
|
19
|
+
|
|
20
|
+
type OnSuccess = (data: string, vars: unknown, ctx: unknown) => Promise<void>;
|
|
21
|
+
type MutationFn = (vars: unknown) => Promise<string>;
|
|
22
|
+
|
|
23
|
+
const vars = (getWalletAddress: () => Promise<string>) => ({
|
|
24
|
+
token: TOKEN,
|
|
25
|
+
amount: 1_000_000n,
|
|
26
|
+
srcChainKey: ChainKeys.STELLAR_MAINNET,
|
|
27
|
+
walletProvider: { getWalletAddress } as never,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const optionsOf = () => useEstablishTrustline() as unknown as { onSuccess: OnSuccess; mutationFn: MutationFn };
|
|
31
|
+
|
|
32
|
+
const invalidatedKeys = () => invalidateQueries.mock.calls.map(([arg]) => arg.queryKey);
|
|
33
|
+
|
|
34
|
+
beforeEach(() => {
|
|
35
|
+
invalidateQueries.mockReset();
|
|
36
|
+
requestTrustline.mockReset();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe('useEstablishTrustline — invalidation after a broadcast trustline', () => {
|
|
40
|
+
it('invalidates the account-scoped keys when the address is readable', async () => {
|
|
41
|
+
await optionsOf().onSuccess(
|
|
42
|
+
'hash',
|
|
43
|
+
vars(async () => ADDRESS),
|
|
44
|
+
undefined,
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
expect(invalidatedKeys()).toEqual([
|
|
48
|
+
['shared', 'stellarTrustlineCheck', ChainKeys.STELLAR_MAINNET, TOKEN, ADDRESS],
|
|
49
|
+
['sponsoring', 'stellarAccountStatus', ADDRESS],
|
|
50
|
+
['shared', 'xBalances', ChainKeys.STELLAR_MAINNET],
|
|
51
|
+
]);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('SURVIVES a wallet that fails right after signing, and still invalidates', async () => {
|
|
55
|
+
// The trustline is already on-chain here: throwing would report success as an
|
|
56
|
+
// error and leave the gate showing "needs a trustline" until a manual refetch.
|
|
57
|
+
const rejecting = vars(async () => {
|
|
58
|
+
throw new Error('wallet is locked');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
await expect(optionsOf().onSuccess('hash', rejecting, undefined)).resolves.toBeUndefined();
|
|
62
|
+
|
|
63
|
+
// Falls back to the whole prefix, because the account is no longer identifiable.
|
|
64
|
+
expect(invalidatedKeys()).toEqual([
|
|
65
|
+
['shared', 'stellarTrustlineCheck'],
|
|
66
|
+
['sponsoring', 'stellarAccountStatus'],
|
|
67
|
+
['shared', 'xBalances', ChainKeys.STELLAR_MAINNET],
|
|
68
|
+
]);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe('useEstablishTrustline — input guard carried over from useRequestTrustline', () => {
|
|
73
|
+
it.each([
|
|
74
|
+
['a zero amount', { amount: 0n }],
|
|
75
|
+
['an empty token', { token: '' }],
|
|
76
|
+
])('rejects %s without reaching the wallet', async (_label, override) => {
|
|
77
|
+
const invalid = { ...vars(async () => ADDRESS), ...override };
|
|
78
|
+
|
|
79
|
+
await expect(optionsOf().mutationFn(invalid)).rejects.toThrow('Token and amount are required');
|
|
80
|
+
expect(requestTrustline).not.toHaveBeenCalled();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('derives srcAddress from the wallet provider for a valid request', async () => {
|
|
84
|
+
requestTrustline.mockResolvedValue('hash');
|
|
85
|
+
|
|
86
|
+
await expect(optionsOf().mutationFn(vars(async () => ADDRESS))).resolves.toBe('hash');
|
|
87
|
+
expect(requestTrustline).toHaveBeenCalledWith(expect.objectContaining({ raw: false, srcAddress: ADDRESS }));
|
|
88
|
+
});
|
|
89
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { IStellarWalletProvider, StellarChainKey } from '@sodax/sdk';
|
|
2
|
+
import { ChainKeys } from '@sodax/sdk';
|
|
3
|
+
import { useQueryClient } from '@tanstack/react-query';
|
|
4
|
+
import type { MutationHookParams } from './types.js';
|
|
5
|
+
import { useSafeMutation, type SafeUseMutationResult } from './useSafeMutation.js';
|
|
6
|
+
import { useSodaxContext } from './useSodaxContext.js';
|
|
7
|
+
|
|
8
|
+
export type UseEstablishTrustlineVars = {
|
|
9
|
+
token: string;
|
|
10
|
+
amount: bigint;
|
|
11
|
+
srcChainKey: StellarChainKey;
|
|
12
|
+
walletProvider: IStellarWalletProvider;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Establish a Stellar trustline. The account pays its fee and reserve; use
|
|
17
|
+
* {@link useStellarGate} to verify it can afford them.
|
|
18
|
+
*
|
|
19
|
+
* Supersedes the deprecated `useRequestTrustline`, which keeps the hand-rolled
|
|
20
|
+
* shape released in 2.0.0.
|
|
21
|
+
*/
|
|
22
|
+
export function useEstablishTrustline({
|
|
23
|
+
mutationOptions,
|
|
24
|
+
}: MutationHookParams<string, UseEstablishTrustlineVars> = {}): SafeUseMutationResult<
|
|
25
|
+
string,
|
|
26
|
+
Error,
|
|
27
|
+
UseEstablishTrustlineVars
|
|
28
|
+
> {
|
|
29
|
+
const { sodax } = useSodaxContext();
|
|
30
|
+
const queryClient = useQueryClient();
|
|
31
|
+
|
|
32
|
+
return useSafeMutation<string, Error, UseEstablishTrustlineVars>({
|
|
33
|
+
mutationKey: ['shared', 'establishTrustline'],
|
|
34
|
+
...mutationOptions,
|
|
35
|
+
mutationFn: async ({ token, amount, srcChainKey, walletProvider }): Promise<string> => {
|
|
36
|
+
// Rejecting `0n` preserves the behaviour of the hook this replaced: an amount
|
|
37
|
+
// sizes the trustline check, so zero is a caller bug rather than a request.
|
|
38
|
+
if (!token || !amount) {
|
|
39
|
+
throw new Error('Token and amount are required');
|
|
40
|
+
}
|
|
41
|
+
const srcAddress = await walletProvider.getWalletAddress();
|
|
42
|
+
return sodax.spoke.stellar.requestTrustline<false>({
|
|
43
|
+
raw: false,
|
|
44
|
+
srcChainKey,
|
|
45
|
+
srcAddress,
|
|
46
|
+
token,
|
|
47
|
+
amount,
|
|
48
|
+
walletProvider,
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
onSuccess: async (data, vars, ctx) => {
|
|
52
|
+
// The trustline is already broadcast here, so a wallet that locks or switches
|
|
53
|
+
// accounts mid-flow must not turn success into an error — nor skip the
|
|
54
|
+
// invalidations below, which is what an unguarded throw would do.
|
|
55
|
+
const srcAddress = await vars.walletProvider.getWalletAddress().catch(() => undefined);
|
|
56
|
+
// Without the address, invalidate the whole prefix rather than nothing.
|
|
57
|
+
queryClient.invalidateQueries({
|
|
58
|
+
queryKey: srcAddress
|
|
59
|
+
? ['shared', 'stellarTrustlineCheck', vars.srcChainKey, vars.token, srcAddress]
|
|
60
|
+
: ['shared', 'stellarTrustlineCheck'],
|
|
61
|
+
});
|
|
62
|
+
// A new trustline changes spendable XLM for subsequent trustlines.
|
|
63
|
+
queryClient.invalidateQueries({
|
|
64
|
+
queryKey: srcAddress
|
|
65
|
+
? ['sponsoring', 'stellarAccountStatus', srcAddress]
|
|
66
|
+
: ['sponsoring', 'stellarAccountStatus'],
|
|
67
|
+
});
|
|
68
|
+
queryClient.invalidateQueries({ queryKey: ['shared', 'xBalances', ChainKeys.STELLAR_MAINNET] });
|
|
69
|
+
await mutationOptions?.onSuccess?.(data, vars, ctx);
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
}
|