@sodax/dapp-kit 0.0.1-rc.30 → 0.0.1-rc.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +109 -20
- package/dist/index.d.ts +109 -20
- package/dist/index.js +225 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +223 -13
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/hooks/index.ts +1 -0
- package/src/hooks/migrate/index.ts +4 -0
- package/src/hooks/migrate/types.ts +15 -0
- package/src/hooks/migrate/useMigrate.tsx +110 -0
- package/src/hooks/migrate/useMigrationAllowance.tsx +79 -0
- package/src/hooks/migrate/useMigrationApprove.tsx +129 -0
- package/src/hooks/mm/useMMAllowance.ts +2 -1
- package/src/hooks/mm/useMMApprove.ts +2 -1
- package/src/hooks/shared/useRequestTrustline.ts +55 -34
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sodax/dapp-kit",
|
|
3
3
|
"license": "MIT",
|
|
4
|
-
"version": "0.0.1-rc.
|
|
4
|
+
"version": "0.0.1-rc.32",
|
|
5
5
|
"description": "dapp-kit of New World",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"viem": "2.29.2",
|
|
19
|
-
"@sodax/sdk": "0.0.1-rc.
|
|
20
|
-
"@sodax/types": "0.0.1-rc.
|
|
19
|
+
"@sodax/sdk": "0.0.1-rc.46",
|
|
20
|
+
"@sodax/types": "0.0.1-rc.20"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@types/react": "18.3.1",
|
package/src/hooks/index.ts
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { XToken } from '@sodax/types';
|
|
2
|
+
|
|
3
|
+
export const MIGRATION_MODE_ICX_SODA = 'icxsoda';
|
|
4
|
+
export const MIGRATION_MODE_BNUSD = 'bnusd';
|
|
5
|
+
|
|
6
|
+
export type MigrationMode = typeof MIGRATION_MODE_ICX_SODA | typeof MIGRATION_MODE_BNUSD;
|
|
7
|
+
|
|
8
|
+
export interface MigrationIntentParams {
|
|
9
|
+
token: XToken | undefined;
|
|
10
|
+
amount: string | undefined;
|
|
11
|
+
sourceAddress: string | undefined;
|
|
12
|
+
migrationMode?: MigrationMode;
|
|
13
|
+
toToken?: XToken;
|
|
14
|
+
destinationAddress?: string;
|
|
15
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { parseUnits } from 'viem';
|
|
2
|
+
import {
|
|
3
|
+
spokeChainConfig,
|
|
4
|
+
type UnifiedBnUSDMigrateParams,
|
|
5
|
+
type IconSpokeProvider,
|
|
6
|
+
type SonicSpokeProvider,
|
|
7
|
+
isLegacybnUSDToken,
|
|
8
|
+
type SpokeProvider,
|
|
9
|
+
} from '@sodax/sdk';
|
|
10
|
+
import { type ChainId, ICON_MAINNET_CHAIN_ID } from '@sodax/types';
|
|
11
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
12
|
+
import { useMutation, type UseMutationResult } from '@tanstack/react-query';
|
|
13
|
+
import { MIGRATION_MODE_BNUSD, MIGRATION_MODE_ICX_SODA, type MigrationIntentParams } from './types';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Hook for executing migration operations between chains.
|
|
17
|
+
*
|
|
18
|
+
* This hook handles ICX/SODA and bnUSD migrations by accepting a spoke provider
|
|
19
|
+
* and returning a mutation function that accepts migration parameters.
|
|
20
|
+
*
|
|
21
|
+
* @param {SpokeProvider} spokeProvider - The spoke provider to use for the migration
|
|
22
|
+
* @returns {UseMutationResult} Mutation result object containing migration function and state
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const { mutateAsync: migrate, isPending } = useMigrate(spokeProvider);
|
|
27
|
+
*
|
|
28
|
+
* const result = await migrate({
|
|
29
|
+
* token: { address: "0x...", decimals: 18 },
|
|
30
|
+
* amount: "100",
|
|
31
|
+
* migrationMode: MIGRATION_MODE_ICX_SODA,
|
|
32
|
+
* toToken: { address: "0x...", decimals: 18 },
|
|
33
|
+
* destinationAddress: "0x..."
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export function useMigrate(
|
|
38
|
+
spokeProvider: SpokeProvider | undefined,
|
|
39
|
+
): UseMutationResult<{ spokeTxHash: string; hubTxHash: `0x${string}` }, Error, MigrationIntentParams> {
|
|
40
|
+
const { sodax } = useSodaxContext();
|
|
41
|
+
|
|
42
|
+
return useMutation({
|
|
43
|
+
mutationFn: async (params: MigrationIntentParams) => {
|
|
44
|
+
const { token, amount, migrationMode = MIGRATION_MODE_ICX_SODA, toToken, destinationAddress } = params;
|
|
45
|
+
const amountToMigrate = parseUnits(amount ?? '0', token?.decimals ?? 0);
|
|
46
|
+
|
|
47
|
+
if (!spokeProvider) {
|
|
48
|
+
throw new Error('Spoke provider not found');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (migrationMode === MIGRATION_MODE_ICX_SODA) {
|
|
52
|
+
// ICX->SODA migration logic
|
|
53
|
+
if (token?.xChainId === ICON_MAINNET_CHAIN_ID) {
|
|
54
|
+
const params = {
|
|
55
|
+
address: spokeChainConfig[ICON_MAINNET_CHAIN_ID].nativeToken,
|
|
56
|
+
amount: amountToMigrate,
|
|
57
|
+
to: destinationAddress as `0x${string}`,
|
|
58
|
+
};
|
|
59
|
+
const result = await sodax.migration.migrateIcxToSoda(params, spokeProvider as IconSpokeProvider, 30000);
|
|
60
|
+
if (result.ok) {
|
|
61
|
+
const [spokeTxHash, hubTxHash] = result.value;
|
|
62
|
+
return { spokeTxHash, hubTxHash };
|
|
63
|
+
}
|
|
64
|
+
throw new Error('ICX to SODA migration failed. Please try again.');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// SODA->ICX migration
|
|
68
|
+
const revertParams = {
|
|
69
|
+
amount: amountToMigrate,
|
|
70
|
+
to: destinationAddress as `hx${string}`,
|
|
71
|
+
};
|
|
72
|
+
const result = await sodax.migration.revertMigrateSodaToIcx(
|
|
73
|
+
revertParams,
|
|
74
|
+
spokeProvider as SonicSpokeProvider,
|
|
75
|
+
30000,
|
|
76
|
+
);
|
|
77
|
+
if (result.ok) {
|
|
78
|
+
const [hubTxHash, spokeTxHash] = result.value;
|
|
79
|
+
return { spokeTxHash, hubTxHash };
|
|
80
|
+
}
|
|
81
|
+
throw new Error('SODA to ICX migration failed. Please try again.');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (migrationMode === MIGRATION_MODE_BNUSD) {
|
|
85
|
+
// bnUSD migration logic - handle dynamic source/destination chains
|
|
86
|
+
const params = {
|
|
87
|
+
srcChainId: token?.xChainId as ChainId,
|
|
88
|
+
dstChainId: toToken?.xChainId as ChainId,
|
|
89
|
+
srcbnUSD: token?.address as string,
|
|
90
|
+
dstbnUSD: toToken?.address as string,
|
|
91
|
+
amount: amountToMigrate,
|
|
92
|
+
to: destinationAddress as `hx${string}` | `0x${string}`,
|
|
93
|
+
} satisfies UnifiedBnUSDMigrateParams;
|
|
94
|
+
|
|
95
|
+
const result = await sodax.migration.migratebnUSD(params, spokeProvider, 30000);
|
|
96
|
+
if (result.ok) {
|
|
97
|
+
const [spokeTxHash, hubTxHash] = result.value;
|
|
98
|
+
return { spokeTxHash, hubTxHash };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const errorMessage = isLegacybnUSDToken(token?.address as string)
|
|
102
|
+
? 'bnUSD migration failed. Please try again.'
|
|
103
|
+
: 'bnUSD reverse migration failed. Please try again.';
|
|
104
|
+
throw new Error(errorMessage);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
throw new Error('Invalid migration mode');
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { ChainId } from '@sodax/types';
|
|
3
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
4
|
+
import { parseUnits } from 'viem';
|
|
5
|
+
import type { IcxCreateRevertMigrationParams, UnifiedBnUSDMigrateParams, SpokeProvider } from '@sodax/sdk';
|
|
6
|
+
import { ICON_MAINNET_CHAIN_ID } from '@sodax/types';
|
|
7
|
+
import { MIGRATION_MODE_ICX_SODA, type MigrationIntentParams } from './types';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Hook for checking token allowance for migration operations.
|
|
11
|
+
*
|
|
12
|
+
* This hook verifies if the user has approved enough tokens for migration operations.
|
|
13
|
+
* It handles both ICX/SODA and bnUSD migration allowance checks.
|
|
14
|
+
*
|
|
15
|
+
* @param {MigrationIntentParams} params - The parameters for the migration allowance check
|
|
16
|
+
* @param {SpokeProvider} spokeProvider - The spoke provider to use for allowance checks
|
|
17
|
+
*
|
|
18
|
+
* @returns {UseQueryResult<boolean, Error>} A React Query result containing:
|
|
19
|
+
* - data: Boolean indicating if allowance is sufficient
|
|
20
|
+
* - isLoading: Loading state indicator
|
|
21
|
+
* - error: Any error that occurred during the check
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const { data: hasAllowed, isLoading } = useMigrationAllowance(params, spokeProvider);
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export function useMigrationAllowance(
|
|
29
|
+
params: MigrationIntentParams | undefined,
|
|
30
|
+
spokeProvider: SpokeProvider | undefined,
|
|
31
|
+
): UseQueryResult<boolean, Error> {
|
|
32
|
+
const { sodax } = useSodaxContext();
|
|
33
|
+
|
|
34
|
+
return useQuery({
|
|
35
|
+
queryKey: ['migration-allowance', params],
|
|
36
|
+
queryFn: async () => {
|
|
37
|
+
if (!spokeProvider || !params) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const { token, amount, migrationMode = MIGRATION_MODE_ICX_SODA, toToken, destinationAddress } = params;
|
|
42
|
+
|
|
43
|
+
// For ICON chain, no allowance is required (forward migrations)
|
|
44
|
+
if (token?.xChainId === ICON_MAINNET_CHAIN_ID) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (!spokeProvider) throw new Error('Spoke provider is required');
|
|
49
|
+
const amountToMigrate = parseUnits(amount ?? '0', token?.decimals ?? 0);
|
|
50
|
+
|
|
51
|
+
let migrationParams: IcxCreateRevertMigrationParams | UnifiedBnUSDMigrateParams;
|
|
52
|
+
if (migrationMode === MIGRATION_MODE_ICX_SODA) {
|
|
53
|
+
migrationParams = {
|
|
54
|
+
amount: amountToMigrate,
|
|
55
|
+
to: destinationAddress as `hx${string}`,
|
|
56
|
+
} satisfies IcxCreateRevertMigrationParams;
|
|
57
|
+
} else {
|
|
58
|
+
if (!toToken) throw new Error('Destination token is required for bnUSD migration');
|
|
59
|
+
|
|
60
|
+
migrationParams = {
|
|
61
|
+
srcChainId: token?.xChainId as ChainId,
|
|
62
|
+
dstChainId: toToken?.xChainId as ChainId,
|
|
63
|
+
srcbnUSD: token?.address as string,
|
|
64
|
+
dstbnUSD: toToken?.address as string,
|
|
65
|
+
amount: amountToMigrate,
|
|
66
|
+
to: destinationAddress as `hx${string}` | `0x${string}`,
|
|
67
|
+
} satisfies UnifiedBnUSDMigrateParams;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const allowance = await sodax.migration.isAllowanceValid(migrationParams, 'revert', spokeProvider);
|
|
71
|
+
if (allowance.ok) {
|
|
72
|
+
return allowance.value;
|
|
73
|
+
}
|
|
74
|
+
return false;
|
|
75
|
+
},
|
|
76
|
+
enabled: !!spokeProvider && !!params,
|
|
77
|
+
refetchInterval: 2000,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { parseUnits } from 'viem';
|
|
2
|
+
import { useCallback, useState, useRef, useEffect } from 'react';
|
|
3
|
+
import type {
|
|
4
|
+
IcxCreateRevertMigrationParams,
|
|
5
|
+
UnifiedBnUSDMigrateParams,
|
|
6
|
+
SpokeProvider,
|
|
7
|
+
Result,
|
|
8
|
+
ChainId,
|
|
9
|
+
} from '@sodax/sdk';
|
|
10
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
11
|
+
import { MIGRATION_MODE_BNUSD, MIGRATION_MODE_ICX_SODA, type MigrationIntentParams } from './types';
|
|
12
|
+
import { useQueryClient } from '@tanstack/react-query';
|
|
13
|
+
|
|
14
|
+
interface UseApproveReturn {
|
|
15
|
+
approve: ({ params }: { params: MigrationIntentParams }) => Promise<boolean>;
|
|
16
|
+
isLoading: boolean;
|
|
17
|
+
error: Error | null;
|
|
18
|
+
resetError: () => void;
|
|
19
|
+
isApproved: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Hook for approving token spending for migration actions
|
|
24
|
+
* @param params The parameters for the migration approval
|
|
25
|
+
* @param spokeProvider The spoke provider instance for the chain
|
|
26
|
+
* @returns Object containing approve function, loading state, error state and reset function
|
|
27
|
+
* @example
|
|
28
|
+
* ```tsx
|
|
29
|
+
* const { approve, isLoading, error } = useMigrationApprove(params, spokeProvider);
|
|
30
|
+
*
|
|
31
|
+
* // Approve tokens for migration
|
|
32
|
+
* await approve({ params });
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
export function useMigrationApprove(
|
|
37
|
+
params: MigrationIntentParams | undefined,
|
|
38
|
+
spokeProvider: SpokeProvider | undefined,
|
|
39
|
+
): UseApproveReturn {
|
|
40
|
+
const { sodax } = useSodaxContext();
|
|
41
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
42
|
+
const [error, setError] = useState<Error | null>(null);
|
|
43
|
+
const [isApproved, setIsApproved] = useState(false);
|
|
44
|
+
const queryClient = useQueryClient();
|
|
45
|
+
|
|
46
|
+
// Track previous values to reset approval state when needed
|
|
47
|
+
const prevTokenAddress = useRef<string | undefined>(undefined);
|
|
48
|
+
const prevAmount = useRef<string | undefined>(undefined);
|
|
49
|
+
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
if (prevTokenAddress.current !== params?.token?.address || prevAmount.current !== params?.amount) {
|
|
52
|
+
setIsApproved(false);
|
|
53
|
+
prevTokenAddress.current = params?.token?.address;
|
|
54
|
+
prevAmount.current = params?.amount;
|
|
55
|
+
}
|
|
56
|
+
}, [params?.token?.address, params?.amount]);
|
|
57
|
+
|
|
58
|
+
const approve = useCallback(
|
|
59
|
+
async ({ params: approveParams }: { params: MigrationIntentParams }) => {
|
|
60
|
+
try {
|
|
61
|
+
setIsLoading(true);
|
|
62
|
+
setError(null);
|
|
63
|
+
|
|
64
|
+
if (!spokeProvider) {
|
|
65
|
+
throw new Error('Spoke provider not found');
|
|
66
|
+
}
|
|
67
|
+
if (!approveParams) {
|
|
68
|
+
throw new Error('Migration intent not found');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const { token, amount, migrationMode = MIGRATION_MODE_ICX_SODA, toToken, destinationAddress } = approveParams;
|
|
72
|
+
const amountToMigrate = parseUnits(amount ?? '0', token?.decimals ?? 0);
|
|
73
|
+
|
|
74
|
+
let result: Result<string, unknown>;
|
|
75
|
+
if (migrationMode === MIGRATION_MODE_ICX_SODA) {
|
|
76
|
+
// ICX/SODA migration approval
|
|
77
|
+
const revertParams = {
|
|
78
|
+
amount: amountToMigrate,
|
|
79
|
+
to: destinationAddress as `hx${string}`,
|
|
80
|
+
} satisfies IcxCreateRevertMigrationParams;
|
|
81
|
+
|
|
82
|
+
result = await sodax.migration.approve(revertParams, 'revert', spokeProvider, false);
|
|
83
|
+
} else if (migrationMode === MIGRATION_MODE_BNUSD) {
|
|
84
|
+
// bnUSD migration approval
|
|
85
|
+
if (!toToken) throw new Error('Destination token is required for bnUSD migration');
|
|
86
|
+
|
|
87
|
+
const migrationParams = {
|
|
88
|
+
srcChainId: token?.xChainId as ChainId,
|
|
89
|
+
dstChainId: toToken?.xChainId as ChainId,
|
|
90
|
+
srcbnUSD: token?.address as string,
|
|
91
|
+
dstbnUSD: toToken?.address as string,
|
|
92
|
+
amount: amountToMigrate,
|
|
93
|
+
to: destinationAddress as `hx${string}` | `0x${string}`,
|
|
94
|
+
} satisfies UnifiedBnUSDMigrateParams;
|
|
95
|
+
|
|
96
|
+
result = await sodax.migration.approve(migrationParams, 'revert', spokeProvider, false);
|
|
97
|
+
} else {
|
|
98
|
+
throw new Error('Invalid migration mode');
|
|
99
|
+
}
|
|
100
|
+
if (!result.ok) {
|
|
101
|
+
throw new Error('Failed to approve tokens');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
setIsApproved(true);
|
|
105
|
+
queryClient.invalidateQueries({ queryKey: ['migration-allowance', params] });
|
|
106
|
+
return result.ok;
|
|
107
|
+
} catch (err) {
|
|
108
|
+
const error = err instanceof Error ? err : new Error('An unknown error occurred');
|
|
109
|
+
setError(error);
|
|
110
|
+
throw error;
|
|
111
|
+
} finally {
|
|
112
|
+
setIsLoading(false);
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
[spokeProvider, sodax, queryClient, params],
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
const resetError = useCallback(() => {
|
|
119
|
+
setError(null);
|
|
120
|
+
}, []);
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
approve,
|
|
124
|
+
isLoading,
|
|
125
|
+
error,
|
|
126
|
+
resetError,
|
|
127
|
+
isApproved,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
@@ -37,10 +37,11 @@ export function useMMAllowance(
|
|
|
37
37
|
queryKey: ['allowance', token.address, amount, action],
|
|
38
38
|
queryFn: async () => {
|
|
39
39
|
if (!spokeProvider) throw new Error('Spoke provider is required');
|
|
40
|
+
const actionBasedDecimals = action === 'withdraw' || action === 'borrow' ? 18 : token.decimals; // withdraw and borrow actions are in aToken decimals
|
|
40
41
|
const allowance = await sodax.moneyMarket.isAllowanceValid(
|
|
41
42
|
{
|
|
42
43
|
token: token.address,
|
|
43
|
-
amount: parseUnits(amount,
|
|
44
|
+
amount: parseUnits(amount, actionBasedDecimals),
|
|
44
45
|
action,
|
|
45
46
|
},
|
|
46
47
|
spokeProvider,
|
|
@@ -39,10 +39,11 @@ export function useMMApprove(token: XToken, spokeProvider: SpokeProvider | undef
|
|
|
39
39
|
if (!spokeProvider) {
|
|
40
40
|
throw new Error('Spoke provider not found');
|
|
41
41
|
}
|
|
42
|
+
const actionBasedDecimals = action === 'withdraw' || action === 'borrow' ? 18 : token.decimals; // withdraw and borrow actions are in aToken decimals
|
|
42
43
|
const allowance = await sodax.moneyMarket.approve(
|
|
43
44
|
{
|
|
44
45
|
token: token.address,
|
|
45
|
-
amount: parseUnits(amount,
|
|
46
|
+
amount: parseUnits(amount, actionBasedDecimals),
|
|
46
47
|
action,
|
|
47
48
|
},
|
|
48
49
|
spokeProvider,
|
|
@@ -1,32 +1,32 @@
|
|
|
1
1
|
import { type SpokeProvider, StellarSpokeProvider, StellarSpokeService, type TxReturnType } from '@sodax/sdk';
|
|
2
|
-
import {
|
|
2
|
+
import { useQueryClient } from '@tanstack/react-query';
|
|
3
|
+
import { useCallback, useState } from 'react';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* React hook to request a Stellar trustline for a given token and amount.
|
|
6
7
|
*
|
|
7
|
-
* This hook provides a
|
|
8
|
+
* This hook provides a callback function for requesting a trustline on the Stellar network
|
|
8
9
|
* using the provided SpokeProvider. It is intended for use with StellarSpokeProvider
|
|
9
10
|
* and will throw if used with a non-Stellar provider. Upon success, it invalidates
|
|
10
11
|
* the trustline check query to ensure UI reflects the updated trustline state.
|
|
11
12
|
*
|
|
12
13
|
* @template T - The type of SpokeProvider, defaults to SpokeProvider.
|
|
13
14
|
* @param {string | undefined} token - The Stellar asset code or token address for which to request a trustline.
|
|
14
|
-
* @returns {
|
|
15
|
-
*
|
|
16
|
-
* - `
|
|
17
|
-
* - `
|
|
15
|
+
* @returns {Object} An object containing:
|
|
16
|
+
* - `requestTrustline`: Function to trigger the trustline request.
|
|
17
|
+
* - `isLoading`: Whether the request is in progress.
|
|
18
|
+
* - `isRequested`: Whether a trustline has been successfully requested.
|
|
18
19
|
* - `error`: Any error encountered during the request.
|
|
19
|
-
* - `
|
|
20
|
-
* - Other React Query mutation state.
|
|
20
|
+
* - `data`: The transaction result if successful.
|
|
21
21
|
*
|
|
22
22
|
* @example
|
|
23
23
|
* ```tsx
|
|
24
24
|
* import { useRequestTrustline } from '@sodax/dapp-kit';
|
|
25
25
|
*
|
|
26
|
-
* const {
|
|
26
|
+
* const { requestTrustline, isLoading, isRequested, error, data } = useRequestTrustline('USDC-G...TOKEN');
|
|
27
27
|
*
|
|
28
28
|
* // To request a trustline:
|
|
29
|
-
* requestTrustline({
|
|
29
|
+
* await requestTrustline({
|
|
30
30
|
* token: 'USDC-G...TOKEN',
|
|
31
31
|
* amount: 10000000n,
|
|
32
32
|
* spokeProvider: stellarProvider,
|
|
@@ -34,33 +34,31 @@ import { useMutation, type UseMutationResult, useQueryClient } from '@tanstack/r
|
|
|
34
34
|
*
|
|
35
35
|
* if (isLoading) return <span>Requesting trustline...</span>;
|
|
36
36
|
* if (error) return <span>Error: {error.message}</span>;
|
|
37
|
-
* if (
|
|
37
|
+
* if (isRequested) return <span>Trustline requested! Tx: {data?.txHash}</span>;
|
|
38
38
|
* ```
|
|
39
39
|
*/
|
|
40
40
|
|
|
41
41
|
export function useRequestTrustline<T extends SpokeProvider = SpokeProvider>(
|
|
42
42
|
token: string | undefined,
|
|
43
|
-
):
|
|
44
|
-
|
|
45
|
-
Error,
|
|
46
|
-
{
|
|
43
|
+
): {
|
|
44
|
+
requestTrustline: (params: {
|
|
47
45
|
token: string;
|
|
48
46
|
amount: bigint;
|
|
49
47
|
spokeProvider: T;
|
|
50
|
-
}
|
|
51
|
-
|
|
48
|
+
}) => Promise<TxReturnType<StellarSpokeProvider, false>>;
|
|
49
|
+
isLoading: boolean;
|
|
50
|
+
isRequested: boolean;
|
|
51
|
+
error: Error | null;
|
|
52
|
+
data: TxReturnType<StellarSpokeProvider, false> | null;
|
|
53
|
+
} {
|
|
52
54
|
const queryClient = useQueryClient();
|
|
55
|
+
const [isLoading, setIsLoading] = useState<boolean>(false);
|
|
56
|
+
const [isRequested, setIsRequested] = useState<boolean>(false);
|
|
57
|
+
const [error, setError] = useState<Error | null>(null);
|
|
58
|
+
const [data, setData] = useState<TxReturnType<StellarSpokeProvider, false> | null>(null);
|
|
53
59
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
Error,
|
|
57
|
-
{
|
|
58
|
-
token: string;
|
|
59
|
-
amount: bigint;
|
|
60
|
-
spokeProvider: T;
|
|
61
|
-
}
|
|
62
|
-
>({
|
|
63
|
-
mutationFn: async ({
|
|
60
|
+
const requestTrustline = useCallback(
|
|
61
|
+
async ({
|
|
64
62
|
token,
|
|
65
63
|
amount,
|
|
66
64
|
spokeProvider,
|
|
@@ -68,15 +66,38 @@ export function useRequestTrustline<T extends SpokeProvider = SpokeProvider>(
|
|
|
68
66
|
token: string;
|
|
69
67
|
amount: bigint;
|
|
70
68
|
spokeProvider: T;
|
|
71
|
-
}) => {
|
|
69
|
+
}): Promise<TxReturnType<StellarSpokeProvider, false>> => {
|
|
72
70
|
if (!spokeProvider || !token || !amount || !(spokeProvider instanceof StellarSpokeProvider)) {
|
|
73
|
-
|
|
71
|
+
const error = new Error('Spoke provider, token or amount not found');
|
|
72
|
+
setError(error);
|
|
73
|
+
throw error;
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
76
|
+
setIsLoading(true);
|
|
77
|
+
setError(null);
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
const result = await StellarSpokeService.requestTrustline(token, amount, spokeProvider);
|
|
81
|
+
setData(result);
|
|
82
|
+
setIsRequested(true);
|
|
83
|
+
queryClient.invalidateQueries({ queryKey: ['stellar-trustline-check', token] });
|
|
84
|
+
return result;
|
|
85
|
+
} catch (err) {
|
|
86
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
87
|
+
setError(error);
|
|
88
|
+
throw error;
|
|
89
|
+
} finally {
|
|
90
|
+
setIsLoading(false);
|
|
91
|
+
}
|
|
80
92
|
},
|
|
81
|
-
|
|
93
|
+
[queryClient],
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
requestTrustline,
|
|
98
|
+
isLoading,
|
|
99
|
+
isRequested,
|
|
100
|
+
error,
|
|
101
|
+
data,
|
|
102
|
+
};
|
|
82
103
|
}
|