@sodax/dapp-kit 0.0.1-rc.3 → 0.0.1-rc.31
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/LICENSE +21 -0
- package/README.md +152 -53
- package/dist/index.d.mts +1514 -0
- package/dist/index.d.ts +1514 -4
- package/dist/index.js +1141 -189
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1096 -187
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -8
- package/src/contexts/index.ts +2 -0
- package/src/core/index.ts +5 -33
- package/src/hooks/backend/README.md +135 -0
- package/src/hooks/backend/index.ts +23 -0
- package/src/hooks/backend/useBackendAllMoneyMarketAssets.ts +49 -0
- package/src/hooks/backend/useBackendAllMoneyMarketBorrowers.ts +61 -0
- package/src/hooks/backend/useBackendIntentByHash.ts +53 -0
- package/src/hooks/backend/useBackendIntentByTxHash.ts +52 -0
- package/src/hooks/backend/useBackendMoneyMarketAsset.ts +57 -0
- package/src/hooks/backend/useBackendMoneyMarketAssetBorrowers.ts +67 -0
- package/src/hooks/backend/useBackendMoneyMarketAssetSuppliers.ts +67 -0
- package/src/hooks/backend/useBackendMoneyMarketPosition.ts +56 -0
- package/src/hooks/backend/useBackendOrderbook.ts +63 -0
- package/src/hooks/bridge/index.ts +5 -0
- package/src/hooks/bridge/useBridge.ts +57 -0
- package/src/hooks/bridge/useBridgeAllowance.ts +49 -0
- package/src/hooks/bridge/useBridgeApprove.ts +68 -0
- package/src/hooks/bridge/useGetBridgeableAmount.ts +50 -0
- package/src/hooks/bridge/useGetBridgeableTokens.ts +62 -0
- package/src/hooks/index.ts +4 -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/index.ts +3 -1
- package/src/hooks/mm/useBorrow.ts +20 -10
- package/src/hooks/mm/useMMAllowance.ts +56 -0
- package/src/hooks/mm/useMMApprove.ts +68 -0
- package/src/hooks/mm/useRepay.ts +20 -10
- package/src/hooks/mm/useReservesData.ts +30 -0
- package/src/hooks/mm/useReservesHumanized.ts +30 -0
- package/src/hooks/mm/useReservesList.ts +29 -0
- package/src/hooks/mm/useReservesUsdFormat.ts +38 -0
- package/src/hooks/mm/useSupply.ts +9 -6
- package/src/hooks/mm/useUserFormattedSummary.ts +54 -0
- package/src/hooks/mm/useUserReservesData.ts +30 -48
- package/src/hooks/mm/useWithdraw.ts +17 -11
- package/src/hooks/provider/useHubProvider.ts +3 -21
- package/src/hooks/provider/useSpokeProvider.ts +97 -6
- package/src/hooks/shared/index.ts +4 -2
- package/src/hooks/shared/useDeriveUserWalletAddress.ts +44 -0
- package/src/hooks/shared/useEstimateGas.ts +18 -0
- package/src/hooks/shared/useRequestTrustline.ts +103 -0
- package/src/hooks/shared/useStellarTrustlineCheck.ts +71 -0
- package/src/hooks/staking/index.ts +19 -0
- package/src/hooks/staking/useCancelUnstake.ts +52 -0
- package/src/hooks/staking/useClaim.ts +46 -0
- package/src/hooks/staking/useConvertedAssets.ts +47 -0
- package/src/hooks/staking/useInstantUnstake.ts +50 -0
- package/src/hooks/staking/useInstantUnstakeAllowance.ts +59 -0
- package/src/hooks/staking/useInstantUnstakeApprove.ts +52 -0
- package/src/hooks/staking/useInstantUnstakeRatio.ts +54 -0
- package/src/hooks/staking/useStake.ts +47 -0
- package/src/hooks/staking/useStakeAllowance.ts +57 -0
- package/src/hooks/staking/useStakeApprove.ts +50 -0
- package/src/hooks/staking/useStakeRatio.ts +53 -0
- package/src/hooks/staking/useStakingConfig.ts +40 -0
- package/src/hooks/staking/useStakingInfo.ts +50 -0
- package/src/hooks/staking/useUnstake.ts +54 -0
- package/src/hooks/staking/useUnstakeAllowance.ts +58 -0
- package/src/hooks/staking/useUnstakeApprove.ts +52 -0
- package/src/hooks/staking/useUnstakingInfo.ts +53 -0
- package/src/hooks/staking/useUnstakingInfoWithPenalty.ts +59 -0
- package/src/hooks/swap/index.ts +4 -1
- package/src/hooks/swap/useCancelSwap.ts +44 -0
- package/src/hooks/swap/useQuote.ts +20 -6
- package/src/hooks/swap/useStatus.ts +3 -3
- package/src/hooks/swap/{useCreateIntentOrder.ts → useSwap.ts} +22 -19
- package/src/hooks/swap/useSwapAllowance.ts +48 -0
- package/src/hooks/swap/useSwapApprove.ts +68 -0
- package/src/providers/SodaxProvider.tsx +7 -4
- package/dist/contexts/index.d.ts +0 -7
- package/dist/contexts/index.d.ts.map +0 -1
- package/dist/core/index.d.ts +0 -4
- package/dist/core/index.d.ts.map +0 -1
- package/dist/hooks/index.d.ts +0 -5
- package/dist/hooks/index.d.ts.map +0 -1
- package/dist/hooks/mm/index.d.ts +0 -7
- package/dist/hooks/mm/index.d.ts.map +0 -1
- package/dist/hooks/mm/useBorrow.d.ts +0 -26
- package/dist/hooks/mm/useBorrow.d.ts.map +0 -1
- package/dist/hooks/mm/useHubWalletAddress.d.ts +0 -24
- package/dist/hooks/mm/useHubWalletAddress.d.ts.map +0 -1
- package/dist/hooks/mm/useRepay.d.ts +0 -26
- package/dist/hooks/mm/useRepay.d.ts.map +0 -1
- package/dist/hooks/mm/useSupply.d.ts +0 -32
- package/dist/hooks/mm/useSupply.d.ts.map +0 -1
- package/dist/hooks/mm/useUserReservesData.d.ts +0 -9
- package/dist/hooks/mm/useUserReservesData.d.ts.map +0 -1
- package/dist/hooks/mm/useWithdraw.d.ts +0 -26
- package/dist/hooks/mm/useWithdraw.d.ts.map +0 -1
- package/dist/hooks/provider/index.d.ts +0 -3
- package/dist/hooks/provider/index.d.ts.map +0 -1
- package/dist/hooks/provider/useHubProvider.d.ts +0 -3
- package/dist/hooks/provider/useHubProvider.d.ts.map +0 -1
- package/dist/hooks/provider/useSpokeProvider.d.ts +0 -4
- package/dist/hooks/provider/useSpokeProvider.d.ts.map +0 -1
- package/dist/hooks/shared/index.d.ts +0 -4
- package/dist/hooks/shared/index.d.ts.map +0 -1
- package/dist/hooks/shared/useAllowance.d.ts +0 -3
- package/dist/hooks/shared/useAllowance.d.ts.map +0 -1
- package/dist/hooks/shared/useApprove.d.ts +0 -10
- package/dist/hooks/shared/useApprove.d.ts.map +0 -1
- package/dist/hooks/shared/useSodaxContext.d.ts +0 -8
- package/dist/hooks/shared/useSodaxContext.d.ts.map +0 -1
- package/dist/hooks/swap/index.d.ts +0 -4
- package/dist/hooks/swap/index.d.ts.map +0 -1
- package/dist/hooks/swap/useCreateIntentOrder.d.ts +0 -33
- package/dist/hooks/swap/useCreateIntentOrder.d.ts.map +0 -1
- package/dist/hooks/swap/useQuote.d.ts +0 -39
- package/dist/hooks/swap/useQuote.d.ts.map +0 -1
- package/dist/hooks/swap/useStatus.d.ts +0 -31
- package/dist/hooks/swap/useStatus.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/providers/SodaxProvider.d.ts +0 -10
- package/dist/providers/SodaxProvider.d.ts.map +0 -1
- package/dist/providers/index.d.ts +0 -2
- package/dist/providers/index.d.ts.map +0 -1
- package/src/hooks/mm/useHubWalletAddress.ts +0 -49
- package/src/hooks/shared/useAllowance.ts +0 -31
- package/src/hooks/shared/useApprove.ts +0 -53
|
@@ -1,58 +1,40 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import type { HubChainId, SpokeChainId } from '@sodax/types';
|
|
4
|
-
import { useXAccount, useWalletProvider } from '@sodax/wallet-sdk';
|
|
5
|
-
import type { ChainId } from '@sodax/types';
|
|
6
|
-
import { useQuery } from '@tanstack/react-query';
|
|
7
|
-
import { useHubProvider } from '../provider/useHubProvider';
|
|
8
|
-
import { useHubWalletAddress } from './useHubWalletAddress';
|
|
1
|
+
import type { SpokeProvider, UserReserveData } from '@sodax/sdk';
|
|
2
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
9
3
|
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
10
4
|
|
|
11
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Hook for fetching user reserves data from the Sodax money market.
|
|
7
|
+
*
|
|
8
|
+
* This hook provides access to the current state of user reserves in the money market protocol.
|
|
9
|
+
* The data is automatically fetched and cached using React Query.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const { data: userReservesData, isLoading, error } = useUserReservesData(spokeProvider, address);
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @returns A React Query result object containing:
|
|
17
|
+
* - data: The user reserves data when available
|
|
18
|
+
* - isLoading: Loading state indicator
|
|
19
|
+
* - error: Any error that occurred during data fetching
|
|
20
|
+
*/
|
|
21
|
+
export function useUserReservesData(
|
|
22
|
+
spokeProvider: SpokeProvider | undefined,
|
|
23
|
+
address: string | undefined,
|
|
24
|
+
refetchInterval = 5000,
|
|
25
|
+
): UseQueryResult<readonly [readonly UserReserveData[], number], Error> {
|
|
12
26
|
const { sodax } = useSodaxContext();
|
|
13
|
-
const hubChainId = (sodax.config?.hubProviderConfig?.chainConfig.chain.id ?? 'sonic') as HubChainId;
|
|
14
|
-
const hubWalletProvider = useWalletProvider(hubChainId);
|
|
15
|
-
const hubProvider = useHubProvider();
|
|
16
|
-
const { address } = useXAccount(spokeChainId);
|
|
17
|
-
const { data: hubWalletAddress } = useHubWalletAddress(
|
|
18
|
-
spokeChainId as SpokeChainId,
|
|
19
|
-
address,
|
|
20
|
-
hubProvider as EvmHubProvider,
|
|
21
|
-
);
|
|
22
27
|
|
|
23
|
-
|
|
24
|
-
queryKey: ['userReserves',
|
|
28
|
+
return useQuery({
|
|
29
|
+
queryKey: ['userReserves', spokeProvider?.chainConfig.chain.id, address],
|
|
25
30
|
queryFn: async () => {
|
|
26
|
-
if (!
|
|
27
|
-
|
|
31
|
+
if (!spokeProvider) {
|
|
32
|
+
throw new Error('Spoke provider or address is not defined');
|
|
28
33
|
}
|
|
29
34
|
|
|
30
|
-
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const moneyMarketConfig = getMoneyMarketConfig(hubChainId);
|
|
35
|
-
try {
|
|
36
|
-
const [res] = await sodax.moneyMarket.getUserReservesData(
|
|
37
|
-
hubWalletAddress as `0x${string}`,
|
|
38
|
-
moneyMarketConfig.uiPoolDataProvider,
|
|
39
|
-
moneyMarketConfig.poolAddressesProvider,
|
|
40
|
-
);
|
|
41
|
-
|
|
42
|
-
return res?.map(r => {
|
|
43
|
-
return {
|
|
44
|
-
...r,
|
|
45
|
-
token: allXTokens.find(t => t.address === r.underlyingAsset),
|
|
46
|
-
};
|
|
47
|
-
});
|
|
48
|
-
} catch (error) {
|
|
49
|
-
console.log('error', error);
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
35
|
+
return await sodax.moneyMarket.data.getUserReservesData(spokeProvider);
|
|
52
36
|
},
|
|
53
|
-
enabled: !!
|
|
54
|
-
refetchInterval
|
|
37
|
+
enabled: !!spokeProvider && !!address,
|
|
38
|
+
refetchInterval,
|
|
55
39
|
});
|
|
56
|
-
|
|
57
|
-
return userReserves;
|
|
58
40
|
}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { SpokeProvider } from '@sodax/sdk';
|
|
2
|
+
import type { XToken } from '@sodax/types';
|
|
3
3
|
import { useMutation, type UseMutationResult } from '@tanstack/react-query';
|
|
4
4
|
import { parseUnits } from 'viem';
|
|
5
|
-
import { useSpokeProvider } from '../provider/useSpokeProvider';
|
|
6
5
|
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
7
6
|
|
|
8
7
|
interface WithdrawResponse {
|
|
9
8
|
ok: true;
|
|
10
|
-
value: [
|
|
9
|
+
value: [string, string];
|
|
11
10
|
}
|
|
12
11
|
|
|
13
12
|
/**
|
|
@@ -17,9 +16,15 @@ interface WithdrawResponse {
|
|
|
17
16
|
* handling the entire withdrawal process including transaction creation, submission,
|
|
18
17
|
* and cross-chain communication.
|
|
19
18
|
*
|
|
19
|
+
* @param {XToken} spokeToken - The token to withdraw from the spoke chain. Must be an XToken with valid address and chain information.
|
|
20
|
+
* @param {SpokeProvider} spokeProvider - The spoke provider to use for the withdraw transaction. Must be a valid SpokeProvider instance.
|
|
21
|
+
*
|
|
22
|
+
* @returns {UseMutationResult<WithdrawResponse, Error, string>} A mutation result object with the following properties:
|
|
23
|
+
* - mutateAsync: Function to execute the withdraw transaction
|
|
24
|
+
* - isPending: Boolean indicating if a transaction is in progress
|
|
20
25
|
* @example
|
|
21
26
|
* ```typescript
|
|
22
|
-
* const { mutateAsync: withdraw, isPending, error } = useWithdraw(
|
|
27
|
+
* const { mutateAsync: withdraw, isPending, error } = useWithdraw(spokeToken);
|
|
23
28
|
* await withdraw('100');
|
|
24
29
|
* ```
|
|
25
30
|
*
|
|
@@ -28,11 +33,10 @@ interface WithdrawResponse {
|
|
|
28
33
|
* - Transaction execution fails
|
|
29
34
|
*/
|
|
30
35
|
export function useWithdraw(
|
|
31
|
-
|
|
32
|
-
|
|
36
|
+
spokeToken: XToken,
|
|
37
|
+
spokeProvider: SpokeProvider | undefined,
|
|
33
38
|
): UseMutationResult<WithdrawResponse, Error, string> {
|
|
34
39
|
const { sodax } = useSodaxContext();
|
|
35
|
-
const spokeProvider = useSpokeProvider(spokeChainId as SpokeChainId);
|
|
36
40
|
|
|
37
41
|
return useMutation<WithdrawResponse, Error, string>({
|
|
38
42
|
mutationFn: async (amount: string) => {
|
|
@@ -40,10 +44,12 @@ export function useWithdraw(
|
|
|
40
44
|
throw new Error('spokeProvider is not found');
|
|
41
45
|
}
|
|
42
46
|
|
|
43
|
-
const response = await sodax.moneyMarket.
|
|
47
|
+
const response = await sodax.moneyMarket.withdraw(
|
|
44
48
|
{
|
|
45
|
-
token:
|
|
46
|
-
|
|
49
|
+
token: spokeToken.address,
|
|
50
|
+
// vault token on hub chain decimals is 18
|
|
51
|
+
amount: parseUnits(amount, 18),
|
|
52
|
+
action: 'withdraw',
|
|
47
53
|
},
|
|
48
54
|
spokeProvider,
|
|
49
55
|
);
|
|
@@ -1,26 +1,8 @@
|
|
|
1
|
-
import { EvmHubProvider
|
|
2
|
-
import { getXChainType } from '@sodax/wallet-sdk';
|
|
3
|
-
import { useMemo } from 'react';
|
|
1
|
+
import type { EvmHubProvider } from '@sodax/sdk';
|
|
4
2
|
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
5
3
|
|
|
6
|
-
export function useHubProvider(): EvmHubProvider
|
|
4
|
+
export function useHubProvider(): EvmHubProvider {
|
|
7
5
|
const { sodax } = useSodaxContext();
|
|
8
|
-
const hubChainId = sodax.config?.hubProviderConfig?.chainConfig.chain.id;
|
|
9
|
-
const hubRpcUrl = sodax.config?.hubProviderConfig?.hubRpcUrl;
|
|
10
|
-
const xChainType = getXChainType(hubChainId);
|
|
11
|
-
const hubProvider = useMemo(() => {
|
|
12
|
-
if (xChainType === 'EVM' && hubChainId && hubRpcUrl) {
|
|
13
|
-
const hubChainCfg = getHubChainConfig(hubChainId);
|
|
14
6
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return new EvmHubProvider({
|
|
18
|
-
hubRpcUrl: hubRpcUrl,
|
|
19
|
-
chainConfig: hubChainCfg,
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
return undefined;
|
|
23
|
-
}, [xChainType, hubChainId, hubRpcUrl]);
|
|
24
|
-
|
|
25
|
-
return hubProvider;
|
|
7
|
+
return sodax.hubProvider;
|
|
26
8
|
}
|
|
@@ -1,33 +1,124 @@
|
|
|
1
|
+
import { useSodaxContext } from '@/index';
|
|
1
2
|
import {
|
|
2
3
|
EvmSpokeProvider,
|
|
3
4
|
spokeChainConfig,
|
|
4
5
|
type SuiSpokeChainConfig,
|
|
5
6
|
SuiSpokeProvider,
|
|
6
7
|
type EvmSpokeChainConfig,
|
|
8
|
+
IconSpokeProvider,
|
|
9
|
+
type IconSpokeChainConfig,
|
|
10
|
+
InjectiveSpokeProvider,
|
|
11
|
+
type InjectiveSpokeChainConfig,
|
|
12
|
+
StellarSpokeProvider,
|
|
13
|
+
type StellarSpokeChainConfig,
|
|
14
|
+
type SpokeProvider,
|
|
15
|
+
type IWalletProvider,
|
|
16
|
+
SolanaSpokeProvider,
|
|
17
|
+
type SolanaChainConfig,
|
|
18
|
+
SONIC_MAINNET_CHAIN_ID,
|
|
19
|
+
SonicSpokeProvider,
|
|
20
|
+
type SonicSpokeChainConfig,
|
|
7
21
|
} from '@sodax/sdk';
|
|
8
|
-
import type {
|
|
9
|
-
|
|
22
|
+
import type {
|
|
23
|
+
IEvmWalletProvider,
|
|
24
|
+
IIconWalletProvider,
|
|
25
|
+
ISuiWalletProvider,
|
|
26
|
+
SpokeChainId,
|
|
27
|
+
IInjectiveWalletProvider,
|
|
28
|
+
IStellarWalletProvider,
|
|
29
|
+
ISolanaWalletProvider,
|
|
30
|
+
} from '@sodax/types';
|
|
10
31
|
import { useMemo } from 'react';
|
|
11
32
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Hook to get the appropriate spoke provider based on the chain type.
|
|
35
|
+
* Supports EVM, SUI, ICON and INJECTIVE chains.
|
|
36
|
+
*
|
|
37
|
+
* @param {SpokeChainId | undefined} spokeChainId - The spoke chain ID to get the provider for
|
|
38
|
+
* @param {IWalletProvider | undefined} walletProvider - The wallet provider to use
|
|
39
|
+
* @returns {SpokeProvider | undefined} The appropriate spoke provider instance for the given chain ID, or undefined if invalid/unsupported
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```tsx
|
|
43
|
+
* // Using a specific SpokeChainId and wallet provider
|
|
44
|
+
* const spokeProvider = useSpokeProvider(spokeChainId, walletProvider);
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export function useSpokeProvider(
|
|
48
|
+
spokeChainId: SpokeChainId | undefined,
|
|
49
|
+
walletProvider?: IWalletProvider | undefined,
|
|
50
|
+
): SpokeProvider | undefined {
|
|
51
|
+
const { rpcConfig } = useSodaxContext();
|
|
52
|
+
const xChainType = spokeChainId ? spokeChainConfig[spokeChainId]?.chain.type : undefined;
|
|
53
|
+
|
|
15
54
|
const spokeProvider = useMemo(() => {
|
|
16
55
|
if (!walletProvider) return undefined;
|
|
56
|
+
if (!spokeChainId) return undefined;
|
|
57
|
+
if (!xChainType) return undefined;
|
|
58
|
+
if (!rpcConfig) return undefined;
|
|
59
|
+
|
|
17
60
|
if (xChainType === 'EVM') {
|
|
61
|
+
if (spokeChainId === SONIC_MAINNET_CHAIN_ID) {
|
|
62
|
+
return new SonicSpokeProvider(
|
|
63
|
+
walletProvider as IEvmWalletProvider,
|
|
64
|
+
spokeChainConfig[spokeChainId] as SonicSpokeChainConfig,
|
|
65
|
+
);
|
|
66
|
+
}
|
|
18
67
|
return new EvmSpokeProvider(
|
|
19
68
|
walletProvider as IEvmWalletProvider,
|
|
20
69
|
spokeChainConfig[spokeChainId] as EvmSpokeChainConfig,
|
|
21
70
|
);
|
|
22
71
|
}
|
|
72
|
+
|
|
23
73
|
if (xChainType === 'SUI') {
|
|
24
74
|
return new SuiSpokeProvider(
|
|
25
75
|
spokeChainConfig[spokeChainId] as SuiSpokeChainConfig,
|
|
26
76
|
walletProvider as ISuiWalletProvider,
|
|
27
77
|
);
|
|
28
78
|
}
|
|
79
|
+
|
|
80
|
+
if (xChainType === 'ICON') {
|
|
81
|
+
return new IconSpokeProvider(
|
|
82
|
+
walletProvider as IIconWalletProvider,
|
|
83
|
+
spokeChainConfig[spokeChainId] as IconSpokeChainConfig,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (xChainType === 'INJECTIVE') {
|
|
88
|
+
return new InjectiveSpokeProvider(
|
|
89
|
+
spokeChainConfig[spokeChainId] as InjectiveSpokeChainConfig,
|
|
90
|
+
walletProvider as IInjectiveWalletProvider,
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (xChainType === 'STELLAR') {
|
|
95
|
+
const stellarConfig = spokeChainConfig[spokeChainId] as StellarSpokeChainConfig;
|
|
96
|
+
return new StellarSpokeProvider(
|
|
97
|
+
walletProvider as IStellarWalletProvider,
|
|
98
|
+
stellarConfig,
|
|
99
|
+
rpcConfig.stellar
|
|
100
|
+
? rpcConfig.stellar
|
|
101
|
+
: {
|
|
102
|
+
horizonRpcUrl: stellarConfig.horizonRpcUrl,
|
|
103
|
+
sorobanRpcUrl: stellarConfig.sorobanRpcUrl,
|
|
104
|
+
},
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (xChainType === 'SOLANA') {
|
|
109
|
+
return new SolanaSpokeProvider(
|
|
110
|
+
walletProvider as ISolanaWalletProvider,
|
|
111
|
+
rpcConfig.solana
|
|
112
|
+
? ({
|
|
113
|
+
...spokeChainConfig[spokeChainId],
|
|
114
|
+
rpcUrl: rpcConfig.solana,
|
|
115
|
+
} as SolanaChainConfig)
|
|
116
|
+
: (spokeChainConfig[spokeChainId] as SolanaChainConfig),
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
29
120
|
return undefined;
|
|
30
|
-
}, [
|
|
121
|
+
}, [spokeChainId, xChainType, walletProvider, rpcConfig]);
|
|
31
122
|
|
|
32
123
|
return spokeProvider;
|
|
33
124
|
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export * from './useSodaxContext';
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
2
|
+
export * from './useEstimateGas';
|
|
3
|
+
export * from './useDeriveUserWalletAddress';
|
|
4
|
+
export * from './useStellarTrustlineCheck';
|
|
5
|
+
export * from './useRequestTrustline';
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// packages/dapp-kit/src/hooks/shared/useDeriveUserWalletAddress.ts
|
|
2
|
+
import { deriveUserWalletAddress, type SpokeProvider } from '@sodax/sdk';
|
|
3
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
4
|
+
import { useSodaxContext } from './useSodaxContext';
|
|
5
|
+
import type { Address } from 'viem';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Hook for deriving user wallet address for hub abstraction.
|
|
9
|
+
*
|
|
10
|
+
* This hook derives the user's abstracted wallet address for the hub chain.
|
|
11
|
+
* If the spoke chain is the same as the hub chain, it returns the original wallet address.
|
|
12
|
+
* Otherwise, it returns the abstracted wallet address for cross-chain operations.
|
|
13
|
+
*
|
|
14
|
+
* @param spokeProvider - The spoke provider instance for the origin chain
|
|
15
|
+
* @param walletAddress - Optional user wallet address on spoke chain. If not provided, will fetch from spokeProvider
|
|
16
|
+
* @returns A React Query result object containing:
|
|
17
|
+
* - data: The derived user wallet address when available
|
|
18
|
+
* - isLoading: Loading state indicator
|
|
19
|
+
* - error: Any error that occurred during derivation
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const { data: derivedAddress, isLoading, error } = useDeriveUserWalletAddress(spokeProvider, userAddress);
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function useDeriveUserWalletAddress(
|
|
27
|
+
spokeProvider: SpokeProvider | undefined,
|
|
28
|
+
walletAddress?: string | undefined,
|
|
29
|
+
): UseQueryResult<Address, Error> {
|
|
30
|
+
const { sodax } = useSodaxContext();
|
|
31
|
+
|
|
32
|
+
return useQuery({
|
|
33
|
+
queryKey: ['deriveUserWalletAddress', spokeProvider?.chainConfig.chain.id, walletAddress],
|
|
34
|
+
queryFn: async (): Promise<Address> => {
|
|
35
|
+
if (!spokeProvider) {
|
|
36
|
+
throw new Error('Spoke provider is required');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return await deriveUserWalletAddress(spokeProvider, sodax.hubProvider, walletAddress);
|
|
40
|
+
},
|
|
41
|
+
enabled: !!spokeProvider,
|
|
42
|
+
refetchInterval: false, // This is a deterministic operation, no need to refetch
|
|
43
|
+
});
|
|
44
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type GetEstimateGasReturnType, type SpokeProvider, SpokeService, type TxReturnType } from '@sodax/sdk';
|
|
2
|
+
import { useMutation, type UseMutationResult } from '@tanstack/react-query';
|
|
3
|
+
|
|
4
|
+
export function useEstimateGas<T extends SpokeProvider = SpokeProvider>(
|
|
5
|
+
spokeProvider: T | undefined,
|
|
6
|
+
): UseMutationResult<GetEstimateGasReturnType<T>, Error, TxReturnType<T, true>> {
|
|
7
|
+
return useMutation<GetEstimateGasReturnType<T>, Error, TxReturnType<T, true>>({
|
|
8
|
+
mutationFn: async (rawTx: TxReturnType<T, true>) => {
|
|
9
|
+
if (!spokeProvider) {
|
|
10
|
+
throw new Error('spokeProvider is not found');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const response = await SpokeService.estimateGas(rawTx, spokeProvider);
|
|
14
|
+
|
|
15
|
+
return response;
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { type SpokeProvider, StellarSpokeProvider, StellarSpokeService, type TxReturnType } from '@sodax/sdk';
|
|
2
|
+
import { useQueryClient } from '@tanstack/react-query';
|
|
3
|
+
import { useCallback, useState } from 'react';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* React hook to request a Stellar trustline for a given token and amount.
|
|
7
|
+
*
|
|
8
|
+
* This hook provides a callback function for requesting a trustline on the Stellar network
|
|
9
|
+
* using the provided SpokeProvider. It is intended for use with StellarSpokeProvider
|
|
10
|
+
* and will throw if used with a non-Stellar provider. Upon success, it invalidates
|
|
11
|
+
* the trustline check query to ensure UI reflects the updated trustline state.
|
|
12
|
+
*
|
|
13
|
+
* @template T - The type of SpokeProvider, defaults to SpokeProvider.
|
|
14
|
+
* @param {string | undefined} token - The Stellar asset code or token address for which to request a trustline.
|
|
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.
|
|
19
|
+
* - `error`: Any error encountered during the request.
|
|
20
|
+
* - `data`: The transaction result if successful.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```tsx
|
|
24
|
+
* import { useRequestTrustline } from '@sodax/dapp-kit';
|
|
25
|
+
*
|
|
26
|
+
* const { requestTrustline, isLoading, isRequested, error, data } = useRequestTrustline('USDC-G...TOKEN');
|
|
27
|
+
*
|
|
28
|
+
* // To request a trustline:
|
|
29
|
+
* await requestTrustline({
|
|
30
|
+
* token: 'USDC-G...TOKEN',
|
|
31
|
+
* amount: 10000000n,
|
|
32
|
+
* spokeProvider: stellarProvider,
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* if (isLoading) return <span>Requesting trustline...</span>;
|
|
36
|
+
* if (error) return <span>Error: {error.message}</span>;
|
|
37
|
+
* if (isRequested) return <span>Trustline requested! Tx: {data?.txHash}</span>;
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
export function useRequestTrustline<T extends SpokeProvider = SpokeProvider>(
|
|
42
|
+
token: string | undefined,
|
|
43
|
+
): {
|
|
44
|
+
requestTrustline: (params: {
|
|
45
|
+
token: string;
|
|
46
|
+
amount: bigint;
|
|
47
|
+
spokeProvider: T;
|
|
48
|
+
}) => Promise<TxReturnType<StellarSpokeProvider, false>>;
|
|
49
|
+
isLoading: boolean;
|
|
50
|
+
isRequested: boolean;
|
|
51
|
+
error: Error | null;
|
|
52
|
+
data: TxReturnType<StellarSpokeProvider, false> | null;
|
|
53
|
+
} {
|
|
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);
|
|
59
|
+
|
|
60
|
+
const requestTrustline = useCallback(
|
|
61
|
+
async ({
|
|
62
|
+
token,
|
|
63
|
+
amount,
|
|
64
|
+
spokeProvider,
|
|
65
|
+
}: {
|
|
66
|
+
token: string;
|
|
67
|
+
amount: bigint;
|
|
68
|
+
spokeProvider: T;
|
|
69
|
+
}): Promise<TxReturnType<StellarSpokeProvider, false>> => {
|
|
70
|
+
if (!spokeProvider || !token || !amount || !(spokeProvider instanceof StellarSpokeProvider)) {
|
|
71
|
+
const error = new Error('Spoke provider, token or amount not found');
|
|
72
|
+
setError(error);
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
|
|
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
|
+
}
|
|
92
|
+
},
|
|
93
|
+
[queryClient],
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
requestTrustline,
|
|
98
|
+
isLoading,
|
|
99
|
+
isRequested,
|
|
100
|
+
error,
|
|
101
|
+
data,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { STELLAR_MAINNET_CHAIN_ID, StellarSpokeProvider, StellarSpokeService } from '@sodax/sdk';
|
|
2
|
+
import type { SpokeChainId, SpokeProvider } from '@sodax/sdk';
|
|
3
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* React hook to check if a Stellar account has a sufficient trustline for a given token and amount.
|
|
7
|
+
*
|
|
8
|
+
* This hook queries the Stellar network (via the provided SpokeProvider) to determine
|
|
9
|
+
* whether the account has established a trustline for the specified token and if the trustline
|
|
10
|
+
* is sufficient for the intended amount. It is useful for gating UI actions that require
|
|
11
|
+
* a trustline to be present and funded.
|
|
12
|
+
*
|
|
13
|
+
* @template T - The type of SpokeProvider, defaults to SpokeProvider.
|
|
14
|
+
* @param {string | undefined} token - The Stellar asset code or token address to check the trustline for.
|
|
15
|
+
* @param {bigint | undefined} amount - The minimum amount required for the trustline.
|
|
16
|
+
* @param {T | undefined} spokeProvider - The provider instance for interacting with the Stellar network.
|
|
17
|
+
* @param {SpokeChainId | undefined} chainId - The chain ID to determine if the check should be performed (only on Stellar mainnet).
|
|
18
|
+
* @returns {UseQueryResult<boolean, Error>} A React Query result object containing:
|
|
19
|
+
* - `data`: `true` if the trustline exists and is sufficient, `false` otherwise.
|
|
20
|
+
* - `error`: Any error encountered during the check.
|
|
21
|
+
* - `isLoading`: Whether the query is in progress.
|
|
22
|
+
* - Other React Query state.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```tsx
|
|
26
|
+
* import { useStellarTrustlineCheck } from '@sodax/dapp-kit';
|
|
27
|
+
*
|
|
28
|
+
* const { data: hasTrustline, isLoading, error } = useStellarTrustlineCheck(
|
|
29
|
+
* 'USDC-G...TOKEN',
|
|
30
|
+
* 10000000n,
|
|
31
|
+
* stellarProvider,
|
|
32
|
+
* 'stellar'
|
|
33
|
+
* );
|
|
34
|
+
*
|
|
35
|
+
* if (isLoading) return <span>Checking trustline...</span>;
|
|
36
|
+
* if (error) return <span>Error: {error.message}</span>;
|
|
37
|
+
* if (!hasTrustline) return <span>Trustline not established or insufficient.</span>;
|
|
38
|
+
* return <span>Trustline is sufficient!</span>;
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
export function useStellarTrustlineCheck<T extends SpokeProvider = SpokeProvider>(
|
|
43
|
+
token: string | undefined,
|
|
44
|
+
amount: bigint | undefined,
|
|
45
|
+
spokeProvider: T | undefined,
|
|
46
|
+
chainId: SpokeChainId | undefined,
|
|
47
|
+
): UseQueryResult<boolean, Error> {
|
|
48
|
+
return useQuery({
|
|
49
|
+
queryKey: ['stellar-trustline-check', token],
|
|
50
|
+
queryFn: async () => {
|
|
51
|
+
if (chainId !== STELLAR_MAINNET_CHAIN_ID) {
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
if (!spokeProvider || !token || !amount || !(spokeProvider instanceof StellarSpokeProvider)) {
|
|
55
|
+
console.error(
|
|
56
|
+
'Spoke provider, token or amount not found. Details: spokeProvider:',
|
|
57
|
+
spokeProvider,
|
|
58
|
+
'token:',
|
|
59
|
+
token,
|
|
60
|
+
'amount:',
|
|
61
|
+
amount,
|
|
62
|
+
);
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
const response = await StellarSpokeService.hasSufficientTrustline(token, amount, spokeProvider);
|
|
66
|
+
|
|
67
|
+
return response;
|
|
68
|
+
},
|
|
69
|
+
enabled: !!spokeProvider && !!token && !!amount,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// packages/dapp-kit/src/hooks/staking/index.ts
|
|
2
|
+
export { useStake } from './useStake';
|
|
3
|
+
export { useStakeApprove } from './useStakeApprove';
|
|
4
|
+
export { useStakeAllowance } from './useStakeAllowance';
|
|
5
|
+
export { useUnstake } from './useUnstake';
|
|
6
|
+
export { useClaim } from './useClaim';
|
|
7
|
+
export { useCancelUnstake } from './useCancelUnstake';
|
|
8
|
+
export { useStakingInfo } from './useStakingInfo';
|
|
9
|
+
export { useUnstakingInfoWithPenalty } from './useUnstakingInfoWithPenalty';
|
|
10
|
+
export { useStakingConfig } from './useStakingConfig';
|
|
11
|
+
export { useStakeRatio } from './useStakeRatio';
|
|
12
|
+
export { useInstantUnstakeRatio } from './useInstantUnstakeRatio';
|
|
13
|
+
export { useConvertedAssets } from './useConvertedAssets';
|
|
14
|
+
export { useInstantUnstake } from './useInstantUnstake';
|
|
15
|
+
export { useUnstakeAllowance } from './useUnstakeAllowance';
|
|
16
|
+
export { useUnstakeApprove } from './useUnstakeApprove';
|
|
17
|
+
export { useUnstakingInfo } from './useUnstakingInfo';
|
|
18
|
+
export { useInstantUnstakeApprove } from './useInstantUnstakeApprove';
|
|
19
|
+
export { useInstantUnstakeAllowance } from './useInstantUnstakeAllowance';
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// packages/dapp-kit/src/hooks/staking/useCancelUnstake.ts
|
|
2
|
+
import { useMutation, useQueryClient, type UseMutationResult } from '@tanstack/react-query';
|
|
3
|
+
import { useSodaxContext } from '../shared/useSodaxContext';
|
|
4
|
+
import type { CancelUnstakeParams, SpokeProvider, SpokeTxHash, HubTxHash } from '@sodax/sdk';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Hook for executing cancel unstake transactions to cancel pending unstake requests.
|
|
8
|
+
* Uses React Query's useMutation for better state management and caching.
|
|
9
|
+
*
|
|
10
|
+
* @param {SpokeProvider | undefined} spokeProvider - The spoke provider to use for the cancel unstake
|
|
11
|
+
* @returns {UseMutationResult<[SpokeTxHash, HubTxHash], Error, Omit<CancelUnstakeParams, 'action'>>} Mutation result object containing mutation function and state
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* const { mutateAsync: cancelUnstake, isPending } = useCancelUnstake(spokeProvider);
|
|
16
|
+
*
|
|
17
|
+
* const handleCancelUnstake = async () => {
|
|
18
|
+
* const result = await cancelUnstake({
|
|
19
|
+
* requestId: 1n
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* console.log('Cancel unstake successful:', result);
|
|
23
|
+
* };
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function useCancelUnstake(
|
|
27
|
+
spokeProvider: SpokeProvider | undefined,
|
|
28
|
+
): UseMutationResult<[SpokeTxHash, HubTxHash], Error, Omit<CancelUnstakeParams, 'action'>> {
|
|
29
|
+
const { sodax } = useSodaxContext();
|
|
30
|
+
const queryClient = useQueryClient();
|
|
31
|
+
|
|
32
|
+
return useMutation({
|
|
33
|
+
mutationFn: async (params: Omit<CancelUnstakeParams, 'action'>) => {
|
|
34
|
+
if (!spokeProvider) {
|
|
35
|
+
throw new Error('Spoke provider not available');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const result = await sodax.staking.cancelUnstake({ ...params, action: 'cancelUnstake' }, spokeProvider);
|
|
39
|
+
if (!result.ok) {
|
|
40
|
+
throw new Error(`Cancel unstake failed: ${result.error.code}`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return result.value;
|
|
44
|
+
},
|
|
45
|
+
onSuccess: () => {
|
|
46
|
+
// Invalidate relevant queries to refresh data
|
|
47
|
+
queryClient.invalidateQueries({ queryKey: ['stakingInfo'] });
|
|
48
|
+
queryClient.invalidateQueries({ queryKey: ['unstakingInfo'] });
|
|
49
|
+
queryClient.invalidateQueries({ queryKey: ['unstakingInfoWithPenalty'] });
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
}
|