@sodax/dapp-kit 2.3.0 → 2.4.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.
Files changed (43) hide show
  1. package/README.md +10 -2
  2. package/dist/index.d.ts +182 -31
  3. package/dist/index.mjs +297 -25
  4. package/package.json +10 -5
  5. package/src/hooks/_mutationContract.test.ts +1 -0
  6. package/src/hooks/bridge/getBridgeDetailedStatusRefetchInterval.test.ts +28 -3
  7. package/src/hooks/bridge/getBridgeDetailedStatusRefetchInterval.ts +17 -8
  8. package/src/hooks/bridge/useBridge.ts +6 -0
  9. package/src/hooks/bridge/useBridgeDetailedStatus.test.ts +56 -1
  10. package/src/hooks/bridge/useBridgeDetailedStatus.ts +26 -9
  11. package/src/hooks/bridge/useBridgeWithApproval.test.ts +24 -1
  12. package/src/hooks/bridge/useBridgeWithApproval.ts +6 -0
  13. package/src/hooks/dex/useDexDeposit.ts +6 -0
  14. package/src/hooks/dex/useDexWithdraw.ts +12 -5
  15. package/src/hooks/mm/useBorrow.ts +15 -4
  16. package/src/hooks/mm/useRepay.ts +6 -0
  17. package/src/hooks/mm/useSupply.ts +6 -0
  18. package/src/hooks/mm/useWithdraw.ts +12 -5
  19. package/src/hooks/recovery/useWithdrawHubAsset.ts +12 -5
  20. package/src/hooks/shared/index.ts +4 -0
  21. package/src/hooks/shared/notFoundStreak.ts +5 -0
  22. package/src/hooks/shared/retryBeforeSubmission.ts +26 -0
  23. package/src/hooks/shared/useBalances.test.ts +10 -0
  24. package/src/hooks/shared/useBalances.ts +17 -4
  25. package/src/hooks/shared/useEstablishXrpTrustline.test.ts +113 -0
  26. package/src/hooks/shared/useEstablishXrpTrustline.ts +65 -0
  27. package/src/hooks/shared/useSafeMutation.retry.test.ts +47 -0
  28. package/src/hooks/shared/useSafeMutation.ts +8 -1
  29. package/src/hooks/shared/useXrpDestinationCheck.ts +75 -0
  30. package/src/hooks/shared/useXrpGate.test.ts +118 -0
  31. package/src/hooks/shared/useXrpGate.ts +81 -0
  32. package/src/hooks/shared/useXrpTrustlineReserveCheck.ts +45 -0
  33. package/src/hooks/swap/useCreateLimitOrder.ts +8 -0
  34. package/src/hooks/swap/useSwap.ts +6 -0
  35. package/src/hooks/swap/useSwapLifecycle.test.ts +73 -1
  36. package/src/hooks/swap/useSwapLifecycle.ts +29 -5
  37. package/src/hooks/swap/useSwapWithApproval.test.ts +24 -1
  38. package/src/hooks/swap/useSwapWithApproval.ts +6 -0
  39. package/src/utils/index.ts +1 -0
  40. package/src/utils/swapLifecycle.test.ts +35 -0
  41. package/src/utils/swapLifecycle.ts +29 -5
  42. package/src/utils/xrpGate.test.ts +112 -0
  43. package/src/utils/xrpGate.ts +72 -0
@@ -0,0 +1,112 @@
1
+ import { ChainKeys, type XrpDestinationReadiness } from '@sodax/sdk';
2
+ import { describe, expect, it } from 'vitest';
3
+ import { resolveXrpGate, type XrpCheckResult } from './xrpGate.js';
4
+
5
+ const XRP = ChainKeys.XRP_MAINNET;
6
+
7
+ const resolved = <T>(data: T): XrpCheckResult<T> => ({ isLoading: false, isError: false, data });
8
+ const loading = <T>(): XrpCheckResult<T> => ({ isLoading: true, isError: false, data: undefined });
9
+ const errored = <T>(): XrpCheckResult<T> => ({ isLoading: false, isError: true, data: undefined });
10
+ const idle = <T>(): XrpCheckResult<T> => ({ isLoading: false, isError: false, data: undefined });
11
+
12
+ const refused = (reason: Extract<XrpDestinationReadiness, { ready: false }>['reason']): XrpDestinationReadiness => ({
13
+ ready: false,
14
+ reason,
15
+ });
16
+
17
+ const gate = (
18
+ chainKey: typeof XRP | undefined,
19
+ inputs: {
20
+ readinessCheck?: XrpCheckResult<XrpDestinationReadiness>;
21
+ reserveCheck?: XrpCheckResult<boolean>;
22
+ requiresTrustline?: boolean;
23
+ } = {},
24
+ ) =>
25
+ resolveXrpGate(chainKey, {
26
+ readinessCheck: inputs.readinessCheck ?? resolved({ ready: true }),
27
+ reserveCheck: inputs.reserveCheck ?? idle(),
28
+ requiresTrustline: inputs.requiresTrustline ?? true,
29
+ });
30
+
31
+ const OPEN = {
32
+ isXrpIssuedToken: true,
33
+ needsTrustline: false,
34
+ needsFunding: false,
35
+ blockedReason: undefined,
36
+ checkFailed: false,
37
+ blocksAction: false,
38
+ };
39
+
40
+ describe('resolveXrpGate — what it leaves alone', () => {
41
+ it.each([[ChainKeys.STELLAR_MAINNET], [undefined]])('is inert for %s', chainKey => {
42
+ expect(gate(chainKey as undefined, { readinessCheck: refused('no_trustline') as never })).toMatchObject({
43
+ isXrpIssuedToken: false,
44
+ blocksAction: false,
45
+ });
46
+ });
47
+
48
+ it('is inert for native XRP, which needs no trustline', () => {
49
+ expect(gate(XRP, { requiresTrustline: false, readinessCheck: loading() })).toMatchObject({
50
+ isXrpIssuedToken: false,
51
+ blocksAction: false,
52
+ });
53
+ });
54
+ });
55
+
56
+ describe('resolveXrpGate — an XRPL issued token', () => {
57
+ it('lets the swap through once the destination can receive', () => {
58
+ expect(gate(XRP)).toEqual(OPEN);
59
+ });
60
+
61
+ it('fails closed while the readiness read is loading, and reports a failed read', () => {
62
+ expect(gate(XRP, { readinessCheck: loading() })).toEqual({ ...OPEN, blocksAction: true });
63
+ expect(gate(XRP, { readinessCheck: errored() })).toEqual({ ...OPEN, checkFailed: true, blocksAction: true });
64
+ });
65
+
66
+ it.each([
67
+ 'no_trustline',
68
+ 'trustline_limit',
69
+ ] as const)('asks for a trustline on %s when the account can pay for it', reason => {
70
+ expect(gate(XRP, { readinessCheck: resolved(refused(reason)), reserveCheck: resolved(true) })).toEqual({
71
+ ...OPEN,
72
+ needsTrustline: true,
73
+ blocksAction: true,
74
+ });
75
+ });
76
+
77
+ it('asks for funding when the account cannot pay for the trustline', () => {
78
+ expect(gate(XRP, { readinessCheck: resolved(refused('no_trustline')), reserveCheck: resolved(false) })).toEqual({
79
+ ...OPEN,
80
+ needsFunding: true,
81
+ blocksAction: true,
82
+ });
83
+ });
84
+
85
+ it('fails closed while the reserve read is pending, and reports a failed one', () => {
86
+ const readinessCheck = resolved(refused('no_trustline'));
87
+ expect(gate(XRP, { readinessCheck, reserveCheck: idle() })).toEqual({ ...OPEN, blocksAction: true });
88
+ expect(gate(XRP, { readinessCheck, reserveCheck: loading() })).toEqual({ ...OPEN, blocksAction: true });
89
+ expect(gate(XRP, { readinessCheck, reserveCheck: errored() })).toEqual({
90
+ ...OPEN,
91
+ checkFailed: true,
92
+ blocksAction: true,
93
+ });
94
+ });
95
+
96
+ it.each([
97
+ 'account_not_found',
98
+ 'destination_tag_required',
99
+ 'deposit_auth',
100
+ 'issuer_frozen',
101
+ 'issuer_transfer_fee',
102
+ 'trustline_unauthorized',
103
+ 'trustline_frozen',
104
+ ] as const)('blocks with the reason on %s, which no trustline change fixes', reason => {
105
+ // The reserve is irrelevant here, so even an affordable account gets no trustline action.
106
+ expect(gate(XRP, { readinessCheck: resolved(refused(reason)), reserveCheck: resolved(true) })).toEqual({
107
+ ...OPEN,
108
+ blockedReason: reason,
109
+ blocksAction: true,
110
+ });
111
+ });
112
+ });
@@ -0,0 +1,72 @@
1
+ import { ChainKeys, type SpokeChainKey, type XrpDestinationReadiness } from '@sodax/sdk';
2
+ import type { UseQueryResult } from '@tanstack/react-query';
3
+
4
+ /** The subset of a query result the gate reads. A full `UseQueryResult<T>` satisfies it. */
5
+ export type XrpCheckResult<T> = Pick<UseQueryResult<T>, 'isLoading' | 'isError' | 'data'>;
6
+
7
+ /** A destination refusal the account's own trustline cannot fix. */
8
+ export type XrpBlockedReason = Exclude<
9
+ Extract<XrpDestinationReadiness, { ready: false }>['reason'],
10
+ 'no_trustline' | 'trustline_limit'
11
+ >;
12
+
13
+ export interface XrpGateState {
14
+ /** The destination is XRPL and the token is an issued currency, so a trustline is involved. */
15
+ isXrpIssuedToken: boolean;
16
+ /** The trustline is missing or too small, and the account can afford to set it. */
17
+ needsTrustline: boolean;
18
+ /** The trustline needs setting, but the account lacks the spendable XRP for its fee and reserve. */
19
+ needsFunding: boolean;
20
+ /** Why the destination cannot receive, when no trustline change by the user would fix it. */
21
+ blockedReason: XrpBlockedReason | undefined;
22
+ /** A query failed; disabled or unresolved queries do not set this. */
23
+ checkFailed: boolean;
24
+ /** Whether the downstream action must remain disabled. */
25
+ blocksAction: boolean;
26
+ }
27
+
28
+ export interface XrpGateInputs {
29
+ /** Whether the destination can receive the payout, from the same ledger reads the SDK refuses a swap on. */
30
+ readinessCheck: XrpCheckResult<XrpDestinationReadiness>;
31
+ /** Whether the account can pay for the TrustSet. Only read once the trustline is known to need setting. */
32
+ reserveCheck: XrpCheckResult<boolean>;
33
+ /** Resolve from chain config: false for native XRP. */
34
+ requiresTrustline: boolean;
35
+ }
36
+
37
+ const NOT_XRP_ISSUED_TOKEN: XrpGateState = {
38
+ isXrpIssuedToken: false,
39
+ needsTrustline: false,
40
+ needsFunding: false,
41
+ blockedReason: undefined,
42
+ checkFailed: false,
43
+ blocksAction: false,
44
+ };
45
+
46
+ function unresolved<T>(check: XrpCheckResult<T>): boolean {
47
+ return check.isLoading || check.data === undefined;
48
+ }
49
+
50
+ /**
51
+ * Resolve the XRPL destination prerequisites for an issued token. Native XRP and other chains pass through:
52
+ * the SDK still refuses an unreceivable payout before signing. Unknown states fail closed.
53
+ */
54
+ export function resolveXrpGate(chainKey: SpokeChainKey | undefined, inputs: XrpGateInputs): XrpGateState {
55
+ const { readinessCheck, reserveCheck, requiresTrustline } = inputs;
56
+ if (chainKey !== ChainKeys.XRP_MAINNET || !requiresTrustline) return NOT_XRP_ISSUED_TOKEN;
57
+ const base = { ...NOT_XRP_ISSUED_TOKEN, isXrpIssuedToken: true };
58
+
59
+ if (readinessCheck.isError) return { ...base, checkFailed: true, blocksAction: true };
60
+ const readiness = readinessCheck.data;
61
+ if (readinessCheck.isLoading || readiness === undefined) return { ...base, blocksAction: true };
62
+ if (readiness.ready) return base;
63
+
64
+ if (readiness.reason !== 'no_trustline' && readiness.reason !== 'trustline_limit') {
65
+ return { ...base, blockedReason: readiness.reason, blocksAction: true };
66
+ }
67
+
68
+ if (reserveCheck.isError) return { ...base, checkFailed: true, blocksAction: true };
69
+ if (unresolved(reserveCheck)) return { ...base, blocksAction: true };
70
+ if (reserveCheck.data !== true) return { ...base, needsFunding: true, blocksAction: true };
71
+ return { ...base, needsTrustline: true, blocksAction: true };
72
+ }