@sodax/dapp-kit 0.0.1-rc.8 → 1.0.0-beta
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 +157 -60
- package/dist/index.d.mts +1558 -0
- package/dist/index.d.ts +1558 -4
- package/dist/index.js +1030 -135
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +988 -134
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -8
- package/src/contexts/index.ts +3 -2
- 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 +57 -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 +2 -1
- package/src/hooks/mm/useAToken.ts +47 -0
- package/src/hooks/mm/useBorrow.ts +2 -2
- package/src/hooks/mm/useMMAllowance.ts +2 -1
- package/src/hooks/mm/useMMApprove.ts +2 -1
- package/src/hooks/mm/useRepay.ts +2 -2
- package/src/hooks/mm/useReservesData.ts +1 -8
- 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 +2 -2
- package/src/hooks/mm/useUserFormattedSummary.ts +54 -0
- package/src/hooks/mm/useUserReservesData.ts +30 -37
- package/src/hooks/mm/useWithdraw.ts +2 -2
- package/src/hooks/provider/useHubProvider.ts +3 -3
- package/src/hooks/provider/useSpokeProvider.ts +50 -18
- package/src/hooks/shared/index.ts +4 -0
- 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 +2 -1
- package/src/hooks/swap/useCancelSwap.ts +44 -0
- package/src/hooks/swap/useQuote.ts +21 -7
- package/src/hooks/swap/useStatus.ts +4 -4
- package/src/hooks/swap/{useCreateIntentOrder.ts → useSwap.ts} +19 -14
- package/src/hooks/swap/useSwapAllowance.ts +5 -1
- package/src/hooks/swap/useSwapApprove.ts +14 -14
- package/src/index.ts +0 -1
- package/src/providers/SodaxProvider.tsx +8 -20
- package/dist/contexts/index.d.ts +0 -8
- 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 -10
- package/dist/hooks/mm/index.d.ts.map +0 -1
- package/dist/hooks/mm/useBorrow.d.ts +0 -35
- 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/useMMAllowance.d.ts +0 -26
- package/dist/hooks/mm/useMMAllowance.d.ts.map +0 -1
- package/dist/hooks/mm/useMMApprove.d.ts +0 -27
- package/dist/hooks/mm/useMMApprove.d.ts.map +0 -1
- package/dist/hooks/mm/useRepay.d.ts +0 -35
- package/dist/hooks/mm/useRepay.d.ts.map +0 -1
- package/dist/hooks/mm/useReservesData.d.ts +0 -19
- package/dist/hooks/mm/useReservesData.d.ts.map +0 -1
- package/dist/hooks/mm/useSupply.d.ts +0 -34
- 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 -33
- 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 -18
- package/dist/hooks/provider/useSpokeProvider.d.ts.map +0 -1
- package/dist/hooks/shared/index.d.ts +0 -2
- package/dist/hooks/shared/index.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 -6
- 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/hooks/swap/useSwapAllowance.d.ts +0 -23
- package/dist/hooks/swap/useSwapAllowance.d.ts.map +0 -1
- package/dist/hooks/swap/useSwapApprove.d.ts +0 -26
- package/dist/hooks/swap/useSwapApprove.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/core/index.ts +0 -35
- package/src/hooks/mm/useHubWalletAddress.ts +0 -49
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import React, { createContext, useContext, useMemo } from 'react';
|
|
2
|
-
import {
|
|
3
|
-
import { getXChainType, useWalletProvider } from '@sodax/wallet-sdk';
|
|
1
|
+
import React, { createContext, useContext, useState, useCallback, useMemo, useRef, useEffect } from 'react';
|
|
2
|
+
import { SpokeService, deriveUserWalletAddress, STELLAR_MAINNET_CHAIN_ID, StellarSpokeProvider, StellarSpokeService, spokeChainConfig, SONIC_MAINNET_CHAIN_ID, SonicSpokeProvider, EvmSpokeProvider, SuiSpokeProvider, IconSpokeProvider, InjectiveSpokeProvider, SolanaSpokeProvider, isLegacybnUSDToken, Sodax } from '@sodax/sdk';
|
|
4
3
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
5
4
|
import { parseUnits } from 'viem';
|
|
5
|
+
import { ICON_MAINNET_CHAIN_ID } from '@sodax/types';
|
|
6
6
|
|
|
7
7
|
// src/contexts/index.ts
|
|
8
8
|
var SodaxContext = createContext(null);
|
|
@@ -13,54 +13,165 @@ var useSodaxContext = () => {
|
|
|
13
13
|
}
|
|
14
14
|
return context;
|
|
15
15
|
};
|
|
16
|
+
function useEstimateGas(spokeProvider) {
|
|
17
|
+
return useMutation({
|
|
18
|
+
mutationFn: async (rawTx) => {
|
|
19
|
+
if (!spokeProvider) {
|
|
20
|
+
throw new Error("spokeProvider is not found");
|
|
21
|
+
}
|
|
22
|
+
const response = await SpokeService.estimateGas(rawTx, spokeProvider);
|
|
23
|
+
return response;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
function useDeriveUserWalletAddress(spokeProvider, walletAddress) {
|
|
28
|
+
const { sodax } = useSodaxContext();
|
|
29
|
+
return useQuery({
|
|
30
|
+
queryKey: ["deriveUserWalletAddress", spokeProvider?.chainConfig.chain.id, walletAddress],
|
|
31
|
+
queryFn: async () => {
|
|
32
|
+
if (!spokeProvider) {
|
|
33
|
+
throw new Error("Spoke provider is required");
|
|
34
|
+
}
|
|
35
|
+
return await deriveUserWalletAddress(spokeProvider, sodax.hubProvider, walletAddress);
|
|
36
|
+
},
|
|
37
|
+
enabled: !!spokeProvider,
|
|
38
|
+
refetchInterval: false
|
|
39
|
+
// This is a deterministic operation, no need to refetch
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function useStellarTrustlineCheck(token, amount, spokeProvider, chainId) {
|
|
43
|
+
return useQuery({
|
|
44
|
+
queryKey: ["stellar-trustline-check", token],
|
|
45
|
+
queryFn: async () => {
|
|
46
|
+
if (chainId !== STELLAR_MAINNET_CHAIN_ID) {
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
if (!spokeProvider || !token || !amount || !(spokeProvider instanceof StellarSpokeProvider)) {
|
|
50
|
+
console.error(
|
|
51
|
+
"Spoke provider, token or amount not found. Details: spokeProvider:",
|
|
52
|
+
spokeProvider,
|
|
53
|
+
"token:",
|
|
54
|
+
token,
|
|
55
|
+
"amount:",
|
|
56
|
+
amount
|
|
57
|
+
);
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
const response = await StellarSpokeService.hasSufficientTrustline(token, amount, spokeProvider);
|
|
61
|
+
return response;
|
|
62
|
+
},
|
|
63
|
+
enabled: !!spokeProvider && !!token && !!amount
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
function useRequestTrustline(token) {
|
|
67
|
+
const queryClient = useQueryClient();
|
|
68
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
69
|
+
const [isRequested, setIsRequested] = useState(false);
|
|
70
|
+
const [error, setError] = useState(null);
|
|
71
|
+
const [data, setData] = useState(null);
|
|
72
|
+
const requestTrustline = useCallback(
|
|
73
|
+
async ({
|
|
74
|
+
token: token2,
|
|
75
|
+
amount,
|
|
76
|
+
spokeProvider
|
|
77
|
+
}) => {
|
|
78
|
+
if (!spokeProvider || !token2 || !amount || !(spokeProvider instanceof StellarSpokeProvider)) {
|
|
79
|
+
const error2 = new Error("Spoke provider, token or amount not found");
|
|
80
|
+
setError(error2);
|
|
81
|
+
throw error2;
|
|
82
|
+
}
|
|
83
|
+
setIsLoading(true);
|
|
84
|
+
setError(null);
|
|
85
|
+
try {
|
|
86
|
+
const result = await StellarSpokeService.requestTrustline(token2, amount, spokeProvider);
|
|
87
|
+
setData(result);
|
|
88
|
+
setIsRequested(true);
|
|
89
|
+
queryClient.invalidateQueries({ queryKey: ["stellar-trustline-check", token2] });
|
|
90
|
+
return result;
|
|
91
|
+
} catch (err) {
|
|
92
|
+
const error2 = err instanceof Error ? err : new Error("Unknown error occurred");
|
|
93
|
+
setError(error2);
|
|
94
|
+
throw error2;
|
|
95
|
+
} finally {
|
|
96
|
+
setIsLoading(false);
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
[queryClient]
|
|
100
|
+
);
|
|
101
|
+
return {
|
|
102
|
+
requestTrustline,
|
|
103
|
+
isLoading,
|
|
104
|
+
isRequested,
|
|
105
|
+
error,
|
|
106
|
+
data
|
|
107
|
+
};
|
|
108
|
+
}
|
|
16
109
|
|
|
17
110
|
// src/hooks/provider/useHubProvider.ts
|
|
18
111
|
function useHubProvider() {
|
|
19
|
-
const {
|
|
20
|
-
return hubProvider;
|
|
112
|
+
const { sodax } = useSodaxContext();
|
|
113
|
+
return sodax.hubProvider;
|
|
21
114
|
}
|
|
22
115
|
function useSpokeProvider(spokeChainId, walletProvider) {
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
const _walletProvider = walletProvider ?? walletProvider_;
|
|
116
|
+
const { rpcConfig } = useSodaxContext();
|
|
117
|
+
const xChainType = spokeChainId ? spokeChainConfig[spokeChainId]?.chain.type : void 0;
|
|
26
118
|
const spokeProvider = useMemo(() => {
|
|
27
|
-
if (!
|
|
119
|
+
if (!walletProvider) return void 0;
|
|
28
120
|
if (!spokeChainId) return void 0;
|
|
121
|
+
if (!xChainType) return void 0;
|
|
122
|
+
if (!rpcConfig) return void 0;
|
|
29
123
|
if (xChainType === "EVM") {
|
|
124
|
+
if (spokeChainId === SONIC_MAINNET_CHAIN_ID) {
|
|
125
|
+
return new SonicSpokeProvider(
|
|
126
|
+
walletProvider,
|
|
127
|
+
spokeChainConfig[spokeChainId]
|
|
128
|
+
);
|
|
129
|
+
}
|
|
30
130
|
return new EvmSpokeProvider(
|
|
31
|
-
|
|
131
|
+
walletProvider,
|
|
32
132
|
spokeChainConfig[spokeChainId]
|
|
33
133
|
);
|
|
34
134
|
}
|
|
35
135
|
if (xChainType === "SUI") {
|
|
36
136
|
return new SuiSpokeProvider(
|
|
37
137
|
spokeChainConfig[spokeChainId],
|
|
38
|
-
|
|
138
|
+
walletProvider
|
|
39
139
|
);
|
|
40
140
|
}
|
|
41
141
|
if (xChainType === "ICON") {
|
|
42
142
|
return new IconSpokeProvider(
|
|
43
|
-
|
|
143
|
+
walletProvider,
|
|
44
144
|
spokeChainConfig[spokeChainId]
|
|
45
145
|
);
|
|
46
146
|
}
|
|
47
147
|
if (xChainType === "INJECTIVE") {
|
|
48
|
-
return new
|
|
148
|
+
return new InjectiveSpokeProvider(
|
|
49
149
|
spokeChainConfig[spokeChainId],
|
|
50
|
-
|
|
150
|
+
walletProvider
|
|
51
151
|
);
|
|
52
152
|
}
|
|
53
153
|
if (xChainType === "STELLAR") {
|
|
54
154
|
const stellarConfig = spokeChainConfig[spokeChainId];
|
|
55
155
|
return new StellarSpokeProvider(
|
|
56
|
-
|
|
57
|
-
stellarConfig.addresses.assetManager,
|
|
156
|
+
walletProvider,
|
|
58
157
|
stellarConfig,
|
|
59
|
-
|
|
158
|
+
rpcConfig.stellar ? rpcConfig.stellar : {
|
|
159
|
+
horizonRpcUrl: stellarConfig.horizonRpcUrl,
|
|
160
|
+
sorobanRpcUrl: stellarConfig.sorobanRpcUrl
|
|
161
|
+
}
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
if (xChainType === "SOLANA") {
|
|
165
|
+
return new SolanaSpokeProvider(
|
|
166
|
+
walletProvider,
|
|
167
|
+
rpcConfig.solana ? {
|
|
168
|
+
...spokeChainConfig[spokeChainId],
|
|
169
|
+
rpcUrl: rpcConfig.solana
|
|
170
|
+
} : spokeChainConfig[spokeChainId]
|
|
60
171
|
);
|
|
61
172
|
}
|
|
62
173
|
return void 0;
|
|
63
|
-
}, [spokeChainId, xChainType,
|
|
174
|
+
}, [spokeChainId, xChainType, walletProvider, rpcConfig]);
|
|
64
175
|
return spokeProvider;
|
|
65
176
|
}
|
|
66
177
|
function useBorrow(spokeToken, spokeProvider) {
|
|
@@ -70,7 +181,7 @@ function useBorrow(spokeToken, spokeProvider) {
|
|
|
70
181
|
if (!spokeProvider) {
|
|
71
182
|
throw new Error("spokeProvider is not found");
|
|
72
183
|
}
|
|
73
|
-
const response = await sodax.moneyMarket.
|
|
184
|
+
const response = await sodax.moneyMarket.borrow(
|
|
74
185
|
{
|
|
75
186
|
token: spokeToken.address,
|
|
76
187
|
amount: parseUnits(amount, 18),
|
|
@@ -93,7 +204,7 @@ function useRepay(spokeToken, spokeProvider) {
|
|
|
93
204
|
if (!spokeProvider) {
|
|
94
205
|
throw new Error("spokeProvider is not found");
|
|
95
206
|
}
|
|
96
|
-
const response = await sodax.moneyMarket.
|
|
207
|
+
const response = await sodax.moneyMarket.repay(
|
|
97
208
|
{
|
|
98
209
|
token: spokeToken.address,
|
|
99
210
|
amount: parseUnits(amount, spokeToken.decimals),
|
|
@@ -116,7 +227,7 @@ function useSupply(spokeToken, spokeProvider) {
|
|
|
116
227
|
if (!spokeProvider) {
|
|
117
228
|
throw new Error("spokeProvider is not found");
|
|
118
229
|
}
|
|
119
|
-
const response = await sodax.moneyMarket.
|
|
230
|
+
const response = await sodax.moneyMarket.supply(
|
|
120
231
|
{
|
|
121
232
|
token: spokeToken.address,
|
|
122
233
|
amount: parseUnits(amount, spokeToken.decimals),
|
|
@@ -139,7 +250,7 @@ function useWithdraw(spokeToken, spokeProvider) {
|
|
|
139
250
|
if (!spokeProvider) {
|
|
140
251
|
throw new Error("spokeProvider is not found");
|
|
141
252
|
}
|
|
142
|
-
const response = await sodax.moneyMarket.
|
|
253
|
+
const response = await sodax.moneyMarket.withdraw(
|
|
143
254
|
{
|
|
144
255
|
token: spokeToken.address,
|
|
145
256
|
// vault token on hub chain decimals is 18
|
|
@@ -156,97 +267,26 @@ function useWithdraw(spokeToken, spokeProvider) {
|
|
|
156
267
|
}
|
|
157
268
|
});
|
|
158
269
|
}
|
|
159
|
-
|
|
160
|
-
Object.keys(hubAssets).forEach((xChainId) => {
|
|
161
|
-
const tokens = hubAssets[xChainId];
|
|
162
|
-
Object.keys(tokens).forEach((tokenAddress) => {
|
|
163
|
-
const token = tokens[tokenAddress];
|
|
164
|
-
allXTokens.push({
|
|
165
|
-
xChainId,
|
|
166
|
-
symbol: token.symbol,
|
|
167
|
-
name: token.name,
|
|
168
|
-
decimals: token.decimal,
|
|
169
|
-
address: tokenAddress
|
|
170
|
-
});
|
|
171
|
-
allXTokens.push({
|
|
172
|
-
xChainId: "sonic",
|
|
173
|
-
symbol: token.symbol,
|
|
174
|
-
name: token.name,
|
|
175
|
-
decimals: token.decimal,
|
|
176
|
-
address: token.vault
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
});
|
|
180
|
-
var getSpokeTokenAddressByVault = (spokeChainId, vault) => {
|
|
181
|
-
const tokens = hubAssets[spokeChainId];
|
|
182
|
-
const address = Object.keys(tokens).find((tokenAddress) => tokens[tokenAddress].vault === vault);
|
|
183
|
-
return address;
|
|
184
|
-
};
|
|
185
|
-
function useUserReservesData(spokeChainId, address) {
|
|
270
|
+
function useUserReservesData(spokeProvider, address, refetchInterval = 5e3) {
|
|
186
271
|
const { sodax } = useSodaxContext();
|
|
187
|
-
const hubChainId = sodax.config?.hubProviderConfig?.chainConfig.chain.id ?? "sonic";
|
|
188
|
-
const hubProvider = useHubProvider();
|
|
189
|
-
const { data: userReserves } = useQuery({
|
|
190
|
-
queryKey: ["userReserves", spokeChainId, address],
|
|
191
|
-
queryFn: async () => {
|
|
192
|
-
if (!hubProvider || !address) {
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
const addressBytes = encodeAddress(spokeChainId, address);
|
|
196
|
-
const hubWalletAddress = await EvmWalletAbstraction.getUserHubWalletAddress(
|
|
197
|
-
spokeChainId,
|
|
198
|
-
addressBytes,
|
|
199
|
-
hubProvider
|
|
200
|
-
);
|
|
201
|
-
const moneyMarketConfig = getMoneyMarketConfig(hubChainId);
|
|
202
|
-
const [res] = await sodax.moneyMarket.getUserReservesData(
|
|
203
|
-
hubWalletAddress,
|
|
204
|
-
moneyMarketConfig.uiPoolDataProvider,
|
|
205
|
-
moneyMarketConfig.poolAddressesProvider
|
|
206
|
-
);
|
|
207
|
-
return res?.map((r) => {
|
|
208
|
-
return {
|
|
209
|
-
...r,
|
|
210
|
-
token: allXTokens.find((t) => t.address === r.underlyingAsset)
|
|
211
|
-
};
|
|
212
|
-
});
|
|
213
|
-
},
|
|
214
|
-
enabled: !!spokeChainId && !!hubProvider && !!address,
|
|
215
|
-
refetchInterval: 5e3
|
|
216
|
-
});
|
|
217
|
-
return userReserves;
|
|
218
|
-
}
|
|
219
|
-
function useHubWalletAddress(spokeChainId, address, hubProvider) {
|
|
220
272
|
return useQuery({
|
|
221
|
-
queryKey: ["
|
|
273
|
+
queryKey: ["userReserves", spokeProvider?.chainConfig.chain.id, address],
|
|
222
274
|
queryFn: async () => {
|
|
223
|
-
if (!
|
|
224
|
-
|
|
225
|
-
const hubWalletAddress = await EvmWalletAbstraction.getUserHubWalletAddress(
|
|
226
|
-
spokeChainId,
|
|
227
|
-
address,
|
|
228
|
-
hubProvider
|
|
229
|
-
);
|
|
230
|
-
return hubWalletAddress;
|
|
231
|
-
} catch (error) {
|
|
232
|
-
console.log("error", error);
|
|
233
|
-
return null;
|
|
275
|
+
if (!spokeProvider) {
|
|
276
|
+
throw new Error("Spoke provider or address is not defined");
|
|
234
277
|
}
|
|
278
|
+
return await sodax.moneyMarket.data.getUserReservesData(spokeProvider);
|
|
235
279
|
},
|
|
236
|
-
enabled: !!
|
|
280
|
+
enabled: !!spokeProvider && !!address,
|
|
281
|
+
refetchInterval
|
|
237
282
|
});
|
|
238
283
|
}
|
|
239
284
|
function useReservesData() {
|
|
240
285
|
const { sodax } = useSodaxContext();
|
|
241
|
-
const hubChainId = sodax.config?.hubProviderConfig?.chainConfig.chain.id ?? "sonic";
|
|
242
286
|
return useQuery({
|
|
243
287
|
queryKey: ["reservesData"],
|
|
244
288
|
queryFn: async () => {
|
|
245
|
-
|
|
246
|
-
return await sodax.moneyMarket.getReservesData(
|
|
247
|
-
moneyMarketConfig.uiPoolDataProvider,
|
|
248
|
-
moneyMarketConfig.poolAddressesProvider
|
|
249
|
-
);
|
|
289
|
+
return await sodax.moneyMarket.data.getReservesData();
|
|
250
290
|
}
|
|
251
291
|
});
|
|
252
292
|
}
|
|
@@ -256,10 +296,11 @@ function useMMAllowance(token, amount, action, spokeProvider) {
|
|
|
256
296
|
queryKey: ["allowance", token.address, amount, action],
|
|
257
297
|
queryFn: async () => {
|
|
258
298
|
if (!spokeProvider) throw new Error("Spoke provider is required");
|
|
299
|
+
const actionBasedDecimals = action === "withdraw" || action === "borrow" ? 18 : token.decimals;
|
|
259
300
|
const allowance = await sodax.moneyMarket.isAllowanceValid(
|
|
260
301
|
{
|
|
261
302
|
token: token.address,
|
|
262
|
-
amount: parseUnits(amount,
|
|
303
|
+
amount: parseUnits(amount, actionBasedDecimals),
|
|
263
304
|
action
|
|
264
305
|
},
|
|
265
306
|
spokeProvider
|
|
@@ -285,10 +326,11 @@ function useMMApprove(token, spokeProvider) {
|
|
|
285
326
|
if (!spokeProvider) {
|
|
286
327
|
throw new Error("Spoke provider not found");
|
|
287
328
|
}
|
|
329
|
+
const actionBasedDecimals = action === "withdraw" || action === "borrow" ? 18 : token.decimals;
|
|
288
330
|
const allowance = await sodax.moneyMarket.approve(
|
|
289
331
|
{
|
|
290
332
|
token: token.address,
|
|
291
|
-
amount: parseUnits(amount,
|
|
333
|
+
amount: parseUnits(amount, actionBasedDecimals),
|
|
292
334
|
action
|
|
293
335
|
},
|
|
294
336
|
spokeProvider
|
|
@@ -309,28 +351,72 @@ function useMMApprove(token, spokeProvider) {
|
|
|
309
351
|
resetError
|
|
310
352
|
};
|
|
311
353
|
}
|
|
354
|
+
function useAToken(aToken) {
|
|
355
|
+
const { sodax } = useSodaxContext();
|
|
356
|
+
return useQuery({
|
|
357
|
+
queryKey: ["aToken", sodax.hubProvider.chainConfig.chain.id, aToken],
|
|
358
|
+
queryFn: async () => {
|
|
359
|
+
if (!aToken) {
|
|
360
|
+
throw new Error("aToken address or hub provider is not defined");
|
|
361
|
+
}
|
|
362
|
+
const aTokenData = await sodax.moneyMarket.data.getATokenData(aToken);
|
|
363
|
+
return {
|
|
364
|
+
...aTokenData,
|
|
365
|
+
xChainId: sodax.hubProvider.chainConfig.chain.id
|
|
366
|
+
};
|
|
367
|
+
},
|
|
368
|
+
enabled: !!aToken
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
function useReservesUsdFormat() {
|
|
372
|
+
const { sodax } = useSodaxContext();
|
|
373
|
+
return useQuery({
|
|
374
|
+
queryKey: ["reservesUsdFormat"],
|
|
375
|
+
queryFn: async () => {
|
|
376
|
+
const reserves = await sodax.moneyMarket.data.getReservesHumanized();
|
|
377
|
+
return sodax.moneyMarket.data.formatReservesUSD(sodax.moneyMarket.data.buildReserveDataWithPrice(reserves));
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
}
|
|
312
381
|
var useQuote = (payload) => {
|
|
313
382
|
const { sodax } = useSodaxContext();
|
|
383
|
+
const queryKey = useMemo(() => {
|
|
384
|
+
if (!payload) return ["quote", void 0];
|
|
385
|
+
return [
|
|
386
|
+
"quote",
|
|
387
|
+
{
|
|
388
|
+
...payload,
|
|
389
|
+
amount: payload.amount.toString()
|
|
390
|
+
}
|
|
391
|
+
];
|
|
392
|
+
}, [payload]);
|
|
314
393
|
return useQuery({
|
|
315
|
-
queryKey
|
|
394
|
+
queryKey,
|
|
316
395
|
queryFn: async () => {
|
|
317
396
|
if (!payload) {
|
|
318
397
|
return void 0;
|
|
319
398
|
}
|
|
320
|
-
return sodax.
|
|
399
|
+
return sodax.swaps.getQuote(payload);
|
|
321
400
|
},
|
|
322
401
|
enabled: !!payload,
|
|
323
402
|
refetchInterval: 3e3
|
|
324
403
|
});
|
|
325
404
|
};
|
|
326
|
-
function
|
|
405
|
+
function useSwap(spokeProvider) {
|
|
327
406
|
const { sodax } = useSodaxContext();
|
|
407
|
+
const queryClient = useQueryClient();
|
|
328
408
|
return useMutation({
|
|
329
409
|
mutationFn: async (params) => {
|
|
330
410
|
if (!spokeProvider) {
|
|
331
411
|
throw new Error("Spoke provider not found");
|
|
332
412
|
}
|
|
333
|
-
return sodax.
|
|
413
|
+
return sodax.swaps.swap({
|
|
414
|
+
intentParams: params,
|
|
415
|
+
spokeProvider
|
|
416
|
+
});
|
|
417
|
+
},
|
|
418
|
+
onSuccess: () => {
|
|
419
|
+
queryClient.invalidateQueries({ queryKey: ["xBalances"] });
|
|
334
420
|
}
|
|
335
421
|
});
|
|
336
422
|
}
|
|
@@ -339,7 +425,7 @@ var useStatus = (intent_tx_hash) => {
|
|
|
339
425
|
return useQuery({
|
|
340
426
|
queryKey: [intent_tx_hash],
|
|
341
427
|
queryFn: async () => {
|
|
342
|
-
return sodax.
|
|
428
|
+
return sodax.swaps.getStatus({ intent_tx_hash });
|
|
343
429
|
},
|
|
344
430
|
refetchInterval: 3e3
|
|
345
431
|
// 3s
|
|
@@ -353,16 +439,20 @@ function useSwapAllowance(params, spokeProvider) {
|
|
|
353
439
|
if (!spokeProvider || !params) {
|
|
354
440
|
return false;
|
|
355
441
|
}
|
|
356
|
-
const allowance = await sodax.
|
|
442
|
+
const allowance = await sodax.swaps.isAllowanceValid({
|
|
443
|
+
intentParams: params,
|
|
444
|
+
spokeProvider
|
|
445
|
+
});
|
|
357
446
|
if (allowance.ok) {
|
|
358
447
|
return allowance.value;
|
|
359
448
|
}
|
|
360
449
|
return false;
|
|
361
450
|
},
|
|
362
|
-
enabled: !!spokeProvider && !!params
|
|
451
|
+
enabled: !!spokeProvider && !!params,
|
|
452
|
+
refetchInterval: 2e3
|
|
363
453
|
});
|
|
364
454
|
}
|
|
365
|
-
function useSwapApprove(
|
|
455
|
+
function useSwapApprove(params, spokeProvider) {
|
|
366
456
|
const { sodax } = useSodaxContext();
|
|
367
457
|
const queryClient = useQueryClient();
|
|
368
458
|
const {
|
|
@@ -371,25 +461,24 @@ function useSwapApprove(token, spokeProvider) {
|
|
|
371
461
|
error,
|
|
372
462
|
reset: resetError
|
|
373
463
|
} = useMutation({
|
|
374
|
-
mutationFn: async ({
|
|
464
|
+
mutationFn: async ({ params: params2 }) => {
|
|
375
465
|
if (!spokeProvider) {
|
|
376
466
|
throw new Error("Spoke provider not found");
|
|
377
467
|
}
|
|
378
|
-
if (!
|
|
379
|
-
throw new Error("
|
|
468
|
+
if (!params2) {
|
|
469
|
+
throw new Error("Swap Params not found");
|
|
380
470
|
}
|
|
381
|
-
const allowance = await sodax.
|
|
382
|
-
|
|
383
|
-
parseUnits(amount, token.decimals),
|
|
471
|
+
const allowance = await sodax.swaps.approve({
|
|
472
|
+
intentParams: params2,
|
|
384
473
|
spokeProvider
|
|
385
|
-
);
|
|
474
|
+
});
|
|
386
475
|
if (!allowance.ok) {
|
|
387
|
-
throw new Error("Failed to approve
|
|
476
|
+
throw new Error("Failed to approve input token");
|
|
388
477
|
}
|
|
389
478
|
return allowance.ok;
|
|
390
479
|
},
|
|
391
480
|
onSuccess: () => {
|
|
392
|
-
queryClient.invalidateQueries({ queryKey: ["allowance",
|
|
481
|
+
queryClient.invalidateQueries({ queryKey: ["allowance", params] });
|
|
393
482
|
}
|
|
394
483
|
});
|
|
395
484
|
return {
|
|
@@ -399,23 +488,788 @@ function useSwapApprove(token, spokeProvider) {
|
|
|
399
488
|
resetError
|
|
400
489
|
};
|
|
401
490
|
}
|
|
402
|
-
|
|
403
|
-
const sodax =
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
return
|
|
410
|
-
|
|
411
|
-
|
|
491
|
+
function useCancelSwap(spokeProvider) {
|
|
492
|
+
const { sodax } = useSodaxContext();
|
|
493
|
+
return useMutation({
|
|
494
|
+
mutationFn: async ({ intent, raw = false }) => {
|
|
495
|
+
if (!spokeProvider) {
|
|
496
|
+
throw new Error("Spoke provider not found");
|
|
497
|
+
}
|
|
498
|
+
return sodax.swaps.cancelIntent(intent, spokeProvider, raw);
|
|
499
|
+
}
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
var useBackendIntentByTxHash = (txHash, refetchInterval = 1e3) => {
|
|
503
|
+
const { sodax } = useSodaxContext();
|
|
504
|
+
return useQuery({
|
|
505
|
+
queryKey: ["backend", "intent", "txHash", txHash],
|
|
506
|
+
queryFn: async () => {
|
|
507
|
+
if (!txHash) {
|
|
508
|
+
return void 0;
|
|
509
|
+
}
|
|
510
|
+
return sodax.backendApi.getIntentByTxHash(txHash);
|
|
511
|
+
},
|
|
512
|
+
refetchInterval,
|
|
513
|
+
enabled: !!txHash && txHash.length > 0,
|
|
514
|
+
retry: 3
|
|
515
|
+
});
|
|
516
|
+
};
|
|
517
|
+
var useBackendIntentByHash = (intentHash) => {
|
|
518
|
+
const { sodax } = useSodaxContext();
|
|
519
|
+
return useQuery({
|
|
520
|
+
queryKey: ["backend", "intent", "hash", intentHash],
|
|
521
|
+
queryFn: async () => {
|
|
522
|
+
if (!intentHash) {
|
|
523
|
+
return void 0;
|
|
524
|
+
}
|
|
525
|
+
return sodax.backendApi.getIntentByHash(intentHash);
|
|
526
|
+
},
|
|
527
|
+
enabled: !!intentHash && intentHash.length > 0,
|
|
528
|
+
retry: 3
|
|
529
|
+
});
|
|
530
|
+
};
|
|
531
|
+
var useBackendOrderbook = (params) => {
|
|
532
|
+
const { sodax } = useSodaxContext();
|
|
533
|
+
return useQuery({
|
|
534
|
+
queryKey: ["backend", "solver", "orderbook", params],
|
|
535
|
+
queryFn: async () => {
|
|
536
|
+
if (!params || !params.offset || !params.limit) {
|
|
537
|
+
return void 0;
|
|
538
|
+
}
|
|
539
|
+
return sodax.backendApi.getOrderbook(params);
|
|
540
|
+
},
|
|
541
|
+
enabled: !!params && !!params.offset && !!params.limit,
|
|
542
|
+
staleTime: 30 * 1e3,
|
|
543
|
+
// 30 seconds for real-time data
|
|
544
|
+
retry: 3
|
|
545
|
+
});
|
|
546
|
+
};
|
|
547
|
+
var useBackendMoneyMarketPosition = (userAddress) => {
|
|
548
|
+
const { sodax } = useSodaxContext();
|
|
549
|
+
return useQuery({
|
|
550
|
+
queryKey: ["backend", "moneymarket", "position", userAddress],
|
|
551
|
+
queryFn: async () => {
|
|
552
|
+
if (!userAddress) {
|
|
553
|
+
return void 0;
|
|
554
|
+
}
|
|
555
|
+
return sodax.backendApi.getMoneyMarketPosition(userAddress);
|
|
556
|
+
},
|
|
557
|
+
enabled: !!userAddress && userAddress.length > 0,
|
|
558
|
+
retry: 3
|
|
559
|
+
});
|
|
560
|
+
};
|
|
561
|
+
var useBackendAllMoneyMarketAssets = () => {
|
|
562
|
+
const { sodax } = useSodaxContext();
|
|
563
|
+
return useQuery({
|
|
564
|
+
queryKey: ["backend", "moneymarket", "assets", "all"],
|
|
565
|
+
queryFn: async () => {
|
|
566
|
+
return sodax.backendApi.getAllMoneyMarketAssets();
|
|
567
|
+
},
|
|
568
|
+
retry: 3
|
|
569
|
+
});
|
|
570
|
+
};
|
|
571
|
+
var useBackendMoneyMarketAsset = (reserveAddress) => {
|
|
572
|
+
const { sodax } = useSodaxContext();
|
|
573
|
+
return useQuery({
|
|
574
|
+
queryKey: ["backend", "moneymarket", "asset", reserveAddress],
|
|
575
|
+
queryFn: async () => {
|
|
576
|
+
if (!reserveAddress) {
|
|
577
|
+
return void 0;
|
|
578
|
+
}
|
|
579
|
+
return sodax.backendApi.getMoneyMarketAsset(reserveAddress);
|
|
580
|
+
},
|
|
581
|
+
enabled: !!reserveAddress && reserveAddress.length > 0,
|
|
582
|
+
retry: 3
|
|
583
|
+
});
|
|
584
|
+
};
|
|
585
|
+
var useBackendMoneyMarketAssetBorrowers = (params) => {
|
|
586
|
+
const { sodax } = useSodaxContext();
|
|
587
|
+
return useQuery({
|
|
588
|
+
queryKey: ["backend", "moneymarket", "asset", "borrowers", params],
|
|
589
|
+
queryFn: async () => {
|
|
590
|
+
if (!params.reserveAddress || !params.offset || !params.limit) {
|
|
591
|
+
return void 0;
|
|
592
|
+
}
|
|
593
|
+
return sodax.backendApi.getMoneyMarketAssetBorrowers(params.reserveAddress, {
|
|
594
|
+
offset: params.offset,
|
|
595
|
+
limit: params.limit
|
|
596
|
+
});
|
|
597
|
+
},
|
|
598
|
+
enabled: !!params.reserveAddress && !!params.offset && !!params.limit,
|
|
599
|
+
retry: 3
|
|
600
|
+
});
|
|
601
|
+
};
|
|
602
|
+
var useBackendMoneyMarketAssetSuppliers = (params) => {
|
|
603
|
+
const { sodax } = useSodaxContext();
|
|
604
|
+
return useQuery({
|
|
605
|
+
queryKey: ["backend", "moneymarket", "asset", "suppliers", params],
|
|
606
|
+
queryFn: async () => {
|
|
607
|
+
if (!params.reserveAddress || !params.offset || !params.limit) {
|
|
608
|
+
return void 0;
|
|
609
|
+
}
|
|
610
|
+
return sodax.backendApi.getMoneyMarketAssetSuppliers(params.reserveAddress, {
|
|
611
|
+
offset: params.offset,
|
|
612
|
+
limit: params.limit
|
|
613
|
+
});
|
|
614
|
+
},
|
|
615
|
+
enabled: !!params.reserveAddress && !!params.offset && !!params.limit,
|
|
616
|
+
retry: 3
|
|
617
|
+
});
|
|
618
|
+
};
|
|
619
|
+
var useBackendAllMoneyMarketBorrowers = (params) => {
|
|
620
|
+
const { sodax } = useSodaxContext();
|
|
621
|
+
return useQuery({
|
|
622
|
+
queryKey: ["backend", "moneymarket", "borrowers", "all", params],
|
|
623
|
+
queryFn: async () => {
|
|
624
|
+
if (!params || !params.offset || !params.limit) {
|
|
625
|
+
return void 0;
|
|
626
|
+
}
|
|
627
|
+
return sodax.backendApi.getAllMoneyMarketBorrowers(params);
|
|
628
|
+
},
|
|
629
|
+
enabled: !!params && !!params.offset && !!params.limit,
|
|
630
|
+
retry: 3
|
|
631
|
+
});
|
|
632
|
+
};
|
|
633
|
+
function useBridgeAllowance(params, spokeProvider) {
|
|
634
|
+
const { sodax } = useSodaxContext();
|
|
635
|
+
return useQuery({
|
|
636
|
+
queryKey: ["bridge-allowance", params],
|
|
637
|
+
queryFn: async () => {
|
|
638
|
+
if (!spokeProvider || !params) {
|
|
639
|
+
return false;
|
|
640
|
+
}
|
|
641
|
+
const allowance = await sodax.bridge.isAllowanceValid({
|
|
642
|
+
params,
|
|
643
|
+
spokeProvider
|
|
644
|
+
});
|
|
645
|
+
if (allowance.ok) {
|
|
646
|
+
return allowance.value;
|
|
647
|
+
}
|
|
648
|
+
return false;
|
|
649
|
+
},
|
|
650
|
+
enabled: !!spokeProvider && !!params
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
function useBridgeApprove(spokeProvider) {
|
|
654
|
+
const { sodax } = useSodaxContext();
|
|
655
|
+
const queryClient = useQueryClient();
|
|
656
|
+
const {
|
|
657
|
+
mutateAsync: approve,
|
|
658
|
+
isPending,
|
|
659
|
+
error,
|
|
660
|
+
reset: resetError
|
|
661
|
+
} = useMutation({
|
|
662
|
+
mutationFn: async (params) => {
|
|
663
|
+
if (!spokeProvider) {
|
|
664
|
+
throw new Error("Spoke provider not found");
|
|
665
|
+
}
|
|
666
|
+
const allowance = await sodax.bridge.approve({
|
|
667
|
+
params,
|
|
668
|
+
spokeProvider
|
|
412
669
|
});
|
|
670
|
+
if (!allowance.ok) {
|
|
671
|
+
throw new Error("Failed to approve tokens for bridge");
|
|
672
|
+
}
|
|
673
|
+
return true;
|
|
674
|
+
},
|
|
675
|
+
onSuccess: (_, params) => {
|
|
676
|
+
queryClient.invalidateQueries({ queryKey: ["bridge-allowance", params] });
|
|
413
677
|
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
678
|
+
});
|
|
679
|
+
return {
|
|
680
|
+
approve,
|
|
681
|
+
isLoading: isPending,
|
|
682
|
+
error,
|
|
683
|
+
resetError
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
function useBridge(spokeProvider) {
|
|
687
|
+
const { sodax } = useSodaxContext();
|
|
688
|
+
return useMutation({
|
|
689
|
+
mutationFn: async (params) => {
|
|
690
|
+
if (!spokeProvider) {
|
|
691
|
+
throw new Error("Spoke provider not found");
|
|
692
|
+
}
|
|
693
|
+
const result = await sodax.bridge.bridge({
|
|
694
|
+
params,
|
|
695
|
+
spokeProvider
|
|
696
|
+
});
|
|
697
|
+
if (!result.ok) {
|
|
698
|
+
throw new Error(`Bridge failed: ${result.error.code}`);
|
|
699
|
+
}
|
|
700
|
+
return result;
|
|
701
|
+
}
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
function useGetBridgeableAmount(from, to) {
|
|
705
|
+
const { sodax } = useSodaxContext();
|
|
706
|
+
return useQuery({
|
|
707
|
+
queryKey: ["spoke-asset-manager-token-balance", from, to],
|
|
708
|
+
queryFn: async () => {
|
|
709
|
+
if (!from || !to) {
|
|
710
|
+
return 0n;
|
|
711
|
+
}
|
|
712
|
+
const result = await sodax.bridge.getBridgeableAmount(from, to);
|
|
713
|
+
if (result.ok) {
|
|
714
|
+
return result.value;
|
|
715
|
+
}
|
|
716
|
+
console.error("Error getting bridgeable amount:", result.error);
|
|
717
|
+
return 0n;
|
|
718
|
+
},
|
|
719
|
+
enabled: !!from && !!to
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
function useGetBridgeableTokens(from, to, token) {
|
|
723
|
+
const { sodax } = useSodaxContext();
|
|
724
|
+
return useQuery({
|
|
725
|
+
queryKey: ["bridgeable-tokens", from, to, token],
|
|
726
|
+
queryFn: async () => {
|
|
727
|
+
if (!from || !to || !token) {
|
|
728
|
+
return [];
|
|
729
|
+
}
|
|
730
|
+
const result = sodax.bridge.getBridgeableTokens(from, to, token);
|
|
731
|
+
if (result.ok) {
|
|
732
|
+
return result.value;
|
|
733
|
+
}
|
|
734
|
+
console.error("Error getting bridgeable tokens:", result.error);
|
|
735
|
+
return [];
|
|
736
|
+
},
|
|
737
|
+
enabled: !!from && !!to && !!token
|
|
738
|
+
});
|
|
739
|
+
}
|
|
740
|
+
function useStake(spokeProvider) {
|
|
741
|
+
const { sodax } = useSodaxContext();
|
|
742
|
+
return useMutation({
|
|
743
|
+
mutationFn: async (params) => {
|
|
744
|
+
if (!spokeProvider) {
|
|
745
|
+
throw new Error("Spoke provider not found");
|
|
746
|
+
}
|
|
747
|
+
const result = await sodax.staking.stake(params, spokeProvider);
|
|
748
|
+
if (!result.ok) {
|
|
749
|
+
throw new Error(`Stake failed: ${result.error.code}`);
|
|
750
|
+
}
|
|
751
|
+
return result.value;
|
|
752
|
+
}
|
|
753
|
+
});
|
|
754
|
+
}
|
|
755
|
+
function useStakeApprove(spokeProvider) {
|
|
756
|
+
const { sodax } = useSodaxContext();
|
|
757
|
+
return useMutation({
|
|
758
|
+
mutationFn: async (params) => {
|
|
759
|
+
if (!spokeProvider) {
|
|
760
|
+
throw new Error("Spoke provider not found");
|
|
761
|
+
}
|
|
762
|
+
const result = await sodax.staking.approve({
|
|
763
|
+
params: { ...params, action: "stake" },
|
|
764
|
+
spokeProvider
|
|
765
|
+
});
|
|
766
|
+
if (!result.ok) {
|
|
767
|
+
throw new Error(`Stake approval failed: ${result.error.code}`);
|
|
768
|
+
}
|
|
769
|
+
return result.value;
|
|
770
|
+
}
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
function useStakeAllowance(params, spokeProvider) {
|
|
774
|
+
const { sodax } = useSodaxContext();
|
|
775
|
+
return useQuery({
|
|
776
|
+
queryKey: ["soda", "stakeAllowance", params, spokeProvider?.chainConfig.chain.id],
|
|
777
|
+
queryFn: async () => {
|
|
778
|
+
if (!params || !spokeProvider) {
|
|
779
|
+
return false;
|
|
780
|
+
}
|
|
781
|
+
const result = await sodax.staking.isAllowanceValid({
|
|
782
|
+
params: { ...params, action: "stake" },
|
|
783
|
+
spokeProvider
|
|
784
|
+
});
|
|
785
|
+
if (!result.ok) {
|
|
786
|
+
throw new Error(`Allowance check failed: ${result.error.code}`);
|
|
787
|
+
}
|
|
788
|
+
return result.value;
|
|
789
|
+
},
|
|
790
|
+
enabled: !!params && !!spokeProvider,
|
|
791
|
+
refetchInterval: 5e3
|
|
792
|
+
// Refetch every 5 seconds
|
|
793
|
+
});
|
|
794
|
+
}
|
|
795
|
+
function useUnstake(spokeProvider) {
|
|
796
|
+
const { sodax } = useSodaxContext();
|
|
797
|
+
const queryClient = useQueryClient();
|
|
798
|
+
return useMutation({
|
|
799
|
+
mutationFn: async (params) => {
|
|
800
|
+
if (!spokeProvider) {
|
|
801
|
+
throw new Error("Spoke provider not found");
|
|
802
|
+
}
|
|
803
|
+
const result = await sodax.staking.unstake({ ...params, action: "unstake" }, spokeProvider);
|
|
804
|
+
if (!result.ok) {
|
|
805
|
+
throw new Error(`Unstake failed: ${result.error.code}`);
|
|
806
|
+
}
|
|
807
|
+
return result.value;
|
|
808
|
+
},
|
|
809
|
+
onSuccess: () => {
|
|
810
|
+
queryClient.invalidateQueries({ queryKey: ["stakingInfo"] });
|
|
811
|
+
queryClient.invalidateQueries({ queryKey: ["unstakingInfo"] });
|
|
812
|
+
queryClient.invalidateQueries({ queryKey: ["unstakingInfoWithPenalty"] });
|
|
813
|
+
}
|
|
814
|
+
});
|
|
815
|
+
}
|
|
816
|
+
function useClaim(spokeProvider) {
|
|
817
|
+
const { sodax } = useSodaxContext();
|
|
818
|
+
return useMutation({
|
|
819
|
+
mutationFn: async (params) => {
|
|
820
|
+
if (!spokeProvider) {
|
|
821
|
+
throw new Error("Spoke provider not found");
|
|
822
|
+
}
|
|
823
|
+
const result = await sodax.staking.claim({ ...params, action: "claim" }, spokeProvider);
|
|
824
|
+
if (!result.ok) {
|
|
825
|
+
throw new Error(`Claim failed: ${result.error.code}`);
|
|
826
|
+
}
|
|
827
|
+
return result.value;
|
|
828
|
+
}
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
function useCancelUnstake(spokeProvider) {
|
|
832
|
+
const { sodax } = useSodaxContext();
|
|
833
|
+
const queryClient = useQueryClient();
|
|
834
|
+
return useMutation({
|
|
835
|
+
mutationFn: async (params) => {
|
|
836
|
+
if (!spokeProvider) {
|
|
837
|
+
throw new Error("Spoke provider not available");
|
|
838
|
+
}
|
|
839
|
+
const result = await sodax.staking.cancelUnstake({ ...params, action: "cancelUnstake" }, spokeProvider);
|
|
840
|
+
if (!result.ok) {
|
|
841
|
+
throw new Error(`Cancel unstake failed: ${result.error.code}`);
|
|
842
|
+
}
|
|
843
|
+
return result.value;
|
|
844
|
+
},
|
|
845
|
+
onSuccess: () => {
|
|
846
|
+
queryClient.invalidateQueries({ queryKey: ["stakingInfo"] });
|
|
847
|
+
queryClient.invalidateQueries({ queryKey: ["unstakingInfo"] });
|
|
848
|
+
queryClient.invalidateQueries({ queryKey: ["unstakingInfoWithPenalty"] });
|
|
849
|
+
}
|
|
850
|
+
});
|
|
851
|
+
}
|
|
852
|
+
function useStakingInfo(spokeProvider, refetchInterval = 5e3) {
|
|
853
|
+
const { sodax } = useSodaxContext();
|
|
854
|
+
return useQuery({
|
|
855
|
+
queryKey: ["soda", "stakingInfo", spokeProvider?.chainConfig.chain.id],
|
|
856
|
+
queryFn: async () => {
|
|
857
|
+
if (!spokeProvider) {
|
|
858
|
+
throw new Error("Spoke provider not found");
|
|
859
|
+
}
|
|
860
|
+
const result = await sodax.staking.getStakingInfoFromSpoke(spokeProvider);
|
|
861
|
+
if (!result.ok) {
|
|
862
|
+
throw new Error(`Failed to fetch staking info: ${result.error.code}`);
|
|
863
|
+
}
|
|
864
|
+
return result.value;
|
|
865
|
+
},
|
|
866
|
+
enabled: !!spokeProvider,
|
|
867
|
+
refetchInterval
|
|
868
|
+
});
|
|
869
|
+
}
|
|
870
|
+
function useUnstakingInfoWithPenalty(userAddress, spokeProvider, refetchInterval = 5e3) {
|
|
871
|
+
const { sodax } = useSodaxContext();
|
|
872
|
+
return useQuery({
|
|
873
|
+
queryKey: ["soda", "unstakingInfoWithPenalty", spokeProvider?.chainConfig.chain.id, userAddress],
|
|
874
|
+
queryFn: async () => {
|
|
875
|
+
if (!spokeProvider) {
|
|
876
|
+
throw new Error("Spoke provider not found");
|
|
877
|
+
}
|
|
878
|
+
const penaltyResult = await sodax.staking.getUnstakingInfoWithPenalty(spokeProvider);
|
|
879
|
+
if (!penaltyResult.ok) {
|
|
880
|
+
throw new Error(`Failed to fetch unstaking info with penalty: ${penaltyResult.error.code}`);
|
|
881
|
+
}
|
|
882
|
+
return penaltyResult.value;
|
|
883
|
+
},
|
|
884
|
+
enabled: !!spokeProvider && !!userAddress,
|
|
885
|
+
refetchInterval
|
|
886
|
+
});
|
|
887
|
+
}
|
|
888
|
+
function useStakingConfig(refetchInterval = 3e4) {
|
|
889
|
+
const { sodax } = useSodaxContext();
|
|
890
|
+
return useQuery({
|
|
891
|
+
queryKey: ["soda", "stakingConfig"],
|
|
892
|
+
queryFn: async () => {
|
|
893
|
+
const result = await sodax.staking.getStakingConfig();
|
|
894
|
+
if (!result.ok) {
|
|
895
|
+
throw new Error(`Failed to fetch staking config: ${result.error.code}`);
|
|
896
|
+
}
|
|
897
|
+
return result.value;
|
|
898
|
+
},
|
|
899
|
+
refetchInterval
|
|
900
|
+
});
|
|
901
|
+
}
|
|
902
|
+
function useStakeRatio(amount, refetchInterval = 1e4) {
|
|
903
|
+
const { sodax } = useSodaxContext();
|
|
904
|
+
return useQuery({
|
|
905
|
+
queryKey: ["soda", "stakeRatio", amount?.toString()],
|
|
906
|
+
queryFn: async () => {
|
|
907
|
+
if (!amount || amount <= 0n) {
|
|
908
|
+
throw new Error("Amount must be greater than 0");
|
|
909
|
+
}
|
|
910
|
+
if (!sodax?.staking) {
|
|
911
|
+
throw new Error("Staking service not available");
|
|
912
|
+
}
|
|
913
|
+
const result = await sodax.staking.getStakeRatio(amount);
|
|
914
|
+
if (!result.ok) {
|
|
915
|
+
throw new Error(`Failed to fetch stake ratio: ${result.error.code}`);
|
|
916
|
+
}
|
|
917
|
+
return result.value;
|
|
918
|
+
},
|
|
919
|
+
enabled: !!amount && amount > 0n && !!sodax?.staking,
|
|
920
|
+
refetchInterval
|
|
921
|
+
});
|
|
922
|
+
}
|
|
923
|
+
function useInstantUnstakeRatio(amount, refetchInterval = 1e4) {
|
|
924
|
+
const { sodax } = useSodaxContext();
|
|
925
|
+
console.log("useInstantUnstakeRatio hook called with:", { amount: amount?.toString(), sodax: !!sodax });
|
|
926
|
+
return useQuery({
|
|
927
|
+
queryKey: ["soda", "instantUnstakeRatio", amount?.toString()],
|
|
928
|
+
queryFn: async () => {
|
|
929
|
+
console.log("useInstantUnstakeRatio queryFn called with amount:", amount?.toString());
|
|
930
|
+
if (!amount || amount <= 0n) {
|
|
931
|
+
throw new Error("Amount must be greater than 0");
|
|
932
|
+
}
|
|
933
|
+
if (!sodax?.staking) {
|
|
934
|
+
throw new Error("Staking service not available");
|
|
935
|
+
}
|
|
936
|
+
const result = await sodax.staking.getInstantUnstakeRatio(amount);
|
|
937
|
+
if (!result.ok) {
|
|
938
|
+
throw new Error(`Failed to fetch instant unstake ratio: ${result.error.code}`);
|
|
939
|
+
}
|
|
940
|
+
return result.value;
|
|
941
|
+
},
|
|
942
|
+
enabled: !!amount && amount > 0n && !!sodax?.staking,
|
|
943
|
+
refetchInterval
|
|
944
|
+
});
|
|
945
|
+
}
|
|
946
|
+
function useConvertedAssets(amount, refetchInterval = 1e4) {
|
|
947
|
+
const { sodax } = useSodaxContext();
|
|
948
|
+
console.log("useConvertedAssets hook called with:", { amount: amount?.toString(), sodax: !!sodax });
|
|
949
|
+
return useQuery({
|
|
950
|
+
queryKey: ["soda", "convertedAssets", amount?.toString()],
|
|
951
|
+
queryFn: async () => {
|
|
952
|
+
console.log("useConvertedAssets queryFn called with amount:", amount?.toString());
|
|
953
|
+
if (!amount || amount <= 0n) {
|
|
954
|
+
throw new Error("Amount must be greater than 0");
|
|
955
|
+
}
|
|
956
|
+
const result = await sodax.staking.getConvertedAssets(amount);
|
|
957
|
+
if (!result.ok) {
|
|
958
|
+
throw new Error(`Failed to fetch converted assets: ${result.error.code}`);
|
|
959
|
+
}
|
|
960
|
+
return result.value;
|
|
961
|
+
},
|
|
962
|
+
enabled: !!amount && amount > 0n && !!sodax?.staking,
|
|
963
|
+
refetchInterval
|
|
964
|
+
});
|
|
965
|
+
}
|
|
966
|
+
function useInstantUnstake(spokeProvider) {
|
|
967
|
+
const { sodax } = useSodaxContext();
|
|
968
|
+
return useMutation({
|
|
969
|
+
mutationFn: async (params) => {
|
|
970
|
+
if (!spokeProvider) {
|
|
971
|
+
throw new Error("spokeProvider is not found");
|
|
972
|
+
}
|
|
973
|
+
const result = await sodax.staking.instantUnstake({ ...params, action: "instantUnstake" }, spokeProvider);
|
|
974
|
+
if (!result.ok) {
|
|
975
|
+
throw new Error(`Instant unstake failed: ${result.error.code}`);
|
|
976
|
+
}
|
|
977
|
+
return result.value;
|
|
978
|
+
},
|
|
979
|
+
onError: (error) => {
|
|
980
|
+
console.error("Instant unstake error:", error);
|
|
981
|
+
}
|
|
982
|
+
});
|
|
983
|
+
}
|
|
984
|
+
function useUnstakeAllowance(params, spokeProvider) {
|
|
985
|
+
const { sodax } = useSodaxContext();
|
|
986
|
+
return useQuery({
|
|
987
|
+
queryKey: ["soda", "unstakeAllowance", params, spokeProvider?.chainConfig.chain.id],
|
|
988
|
+
queryFn: async () => {
|
|
989
|
+
if (!params || !spokeProvider) {
|
|
990
|
+
return false;
|
|
991
|
+
}
|
|
992
|
+
const result = await sodax.staking.isAllowanceValid({
|
|
993
|
+
params: { ...params, action: "unstake" },
|
|
994
|
+
spokeProvider
|
|
995
|
+
});
|
|
996
|
+
if (!result.ok) {
|
|
997
|
+
console.error(`Unstake allowance check failed: ${result.error.code}, error: ${result.error.error}`);
|
|
998
|
+
throw new Error(`Unstake allowance check failed: ${result.error.code}`);
|
|
999
|
+
}
|
|
1000
|
+
return result.value;
|
|
1001
|
+
},
|
|
1002
|
+
enabled: !!params && !!spokeProvider,
|
|
1003
|
+
refetchInterval: 5e3
|
|
1004
|
+
// Refetch every 5 seconds
|
|
1005
|
+
});
|
|
1006
|
+
}
|
|
1007
|
+
function useUnstakeApprove(spokeProvider) {
|
|
1008
|
+
const { sodax } = useSodaxContext();
|
|
1009
|
+
return useMutation({
|
|
1010
|
+
mutationFn: async (params) => {
|
|
1011
|
+
console.log("useUnstakeApprove called with params:", params);
|
|
1012
|
+
if (!spokeProvider) {
|
|
1013
|
+
throw new Error("Spoke provider not found");
|
|
1014
|
+
}
|
|
1015
|
+
const result = await sodax.staking.approve({
|
|
1016
|
+
params: { ...params, action: "unstake" },
|
|
1017
|
+
spokeProvider
|
|
1018
|
+
});
|
|
1019
|
+
if (!result.ok) {
|
|
1020
|
+
throw new Error(`Unstake approval failed: ${result.error.code}`);
|
|
1021
|
+
}
|
|
1022
|
+
return result.value;
|
|
1023
|
+
}
|
|
1024
|
+
});
|
|
1025
|
+
}
|
|
1026
|
+
function useUnstakingInfo(userAddress, spokeProvider, refetchInterval = 5e3) {
|
|
1027
|
+
const { sodax } = useSodaxContext();
|
|
1028
|
+
return useQuery({
|
|
1029
|
+
queryKey: ["soda", "unstakingInfoWithPenalty", spokeProvider?.chainConfig.chain.id, userAddress],
|
|
1030
|
+
queryFn: async () => {
|
|
1031
|
+
if (!spokeProvider || !userAddress) {
|
|
1032
|
+
throw new Error("Spoke provider or user address not found");
|
|
1033
|
+
}
|
|
1034
|
+
const result = await sodax.staking.getUnstakingInfo(spokeProvider);
|
|
1035
|
+
if (!result.ok) {
|
|
1036
|
+
throw new Error(`Failed to fetch unstaking info: ${result.error.code}`);
|
|
1037
|
+
}
|
|
1038
|
+
return result.value;
|
|
1039
|
+
},
|
|
1040
|
+
enabled: !!spokeProvider && !!userAddress,
|
|
1041
|
+
refetchInterval
|
|
1042
|
+
});
|
|
1043
|
+
}
|
|
1044
|
+
function useInstantUnstakeApprove(spokeProvider) {
|
|
1045
|
+
const { sodax } = useSodaxContext();
|
|
1046
|
+
return useMutation({
|
|
1047
|
+
mutationFn: async (params) => {
|
|
1048
|
+
console.log("useInstantUnstakeApprove called with params:", params);
|
|
1049
|
+
if (!spokeProvider) {
|
|
1050
|
+
throw new Error("Spoke provider not found");
|
|
1051
|
+
}
|
|
1052
|
+
const result = await sodax.staking.approve({
|
|
1053
|
+
params: { ...params, action: "instantUnstake" },
|
|
1054
|
+
spokeProvider
|
|
1055
|
+
});
|
|
1056
|
+
if (!result.ok) {
|
|
1057
|
+
throw new Error(`Instant unstake approval failed: ${result.error.code}`);
|
|
1058
|
+
}
|
|
1059
|
+
return result.value;
|
|
1060
|
+
}
|
|
1061
|
+
});
|
|
1062
|
+
}
|
|
1063
|
+
function useInstantUnstakeAllowance(params, spokeProvider) {
|
|
1064
|
+
const { sodax } = useSodaxContext();
|
|
1065
|
+
return useQuery({
|
|
1066
|
+
queryKey: ["soda", "instantUnstakeAllowance", params, spokeProvider?.chainConfig.chain.id],
|
|
1067
|
+
queryFn: async () => {
|
|
1068
|
+
if (!params || !spokeProvider) {
|
|
1069
|
+
return false;
|
|
1070
|
+
}
|
|
1071
|
+
const result = await sodax.staking.isAllowanceValid({
|
|
1072
|
+
params: { ...params, action: "instantUnstake" },
|
|
1073
|
+
spokeProvider
|
|
1074
|
+
});
|
|
1075
|
+
if (!result.ok) {
|
|
1076
|
+
console.error(`Unstake allowance check failed: ${result.error.code}, error: ${result.error.error}`);
|
|
1077
|
+
throw new Error(`Unstake allowance check failed: ${result.error.code}`);
|
|
1078
|
+
}
|
|
1079
|
+
return result.value;
|
|
1080
|
+
},
|
|
1081
|
+
enabled: !!params && !!spokeProvider,
|
|
1082
|
+
refetchInterval: 5e3
|
|
1083
|
+
// Refetch every 5 seconds
|
|
1084
|
+
});
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
// src/hooks/migrate/types.ts
|
|
1088
|
+
var MIGRATION_MODE_ICX_SODA = "icxsoda";
|
|
1089
|
+
var MIGRATION_MODE_BNUSD = "bnusd";
|
|
1090
|
+
|
|
1091
|
+
// src/hooks/migrate/useMigrate.tsx
|
|
1092
|
+
function useMigrate(spokeProvider) {
|
|
1093
|
+
const { sodax } = useSodaxContext();
|
|
1094
|
+
return useMutation({
|
|
1095
|
+
mutationFn: async (params) => {
|
|
1096
|
+
const { token, amount, migrationMode = MIGRATION_MODE_ICX_SODA, toToken, destinationAddress } = params;
|
|
1097
|
+
const amountToMigrate = parseUnits(amount ?? "0", token?.decimals ?? 0);
|
|
1098
|
+
if (!spokeProvider) {
|
|
1099
|
+
throw new Error("Spoke provider not found");
|
|
1100
|
+
}
|
|
1101
|
+
if (migrationMode === MIGRATION_MODE_ICX_SODA) {
|
|
1102
|
+
if (token?.xChainId === ICON_MAINNET_CHAIN_ID) {
|
|
1103
|
+
const params2 = {
|
|
1104
|
+
address: spokeChainConfig[ICON_MAINNET_CHAIN_ID].nativeToken,
|
|
1105
|
+
amount: amountToMigrate,
|
|
1106
|
+
to: destinationAddress
|
|
1107
|
+
};
|
|
1108
|
+
const result2 = await sodax.migration.migrateIcxToSoda(params2, spokeProvider, 3e4);
|
|
1109
|
+
if (result2.ok) {
|
|
1110
|
+
const [spokeTxHash, hubTxHash] = result2.value;
|
|
1111
|
+
return { spokeTxHash, hubTxHash };
|
|
1112
|
+
}
|
|
1113
|
+
throw new Error("ICX to SODA migration failed. Please try again.");
|
|
1114
|
+
}
|
|
1115
|
+
const revertParams = {
|
|
1116
|
+
amount: amountToMigrate,
|
|
1117
|
+
to: destinationAddress
|
|
1118
|
+
};
|
|
1119
|
+
const result = await sodax.migration.revertMigrateSodaToIcx(
|
|
1120
|
+
revertParams,
|
|
1121
|
+
spokeProvider,
|
|
1122
|
+
3e4
|
|
1123
|
+
);
|
|
1124
|
+
if (result.ok) {
|
|
1125
|
+
const [hubTxHash, spokeTxHash] = result.value;
|
|
1126
|
+
return { spokeTxHash, hubTxHash };
|
|
1127
|
+
}
|
|
1128
|
+
throw new Error("SODA to ICX migration failed. Please try again.");
|
|
1129
|
+
}
|
|
1130
|
+
if (migrationMode === MIGRATION_MODE_BNUSD) {
|
|
1131
|
+
const params2 = {
|
|
1132
|
+
srcChainId: token?.xChainId,
|
|
1133
|
+
dstChainId: toToken?.xChainId,
|
|
1134
|
+
srcbnUSD: token?.address,
|
|
1135
|
+
dstbnUSD: toToken?.address,
|
|
1136
|
+
amount: amountToMigrate,
|
|
1137
|
+
to: destinationAddress
|
|
1138
|
+
};
|
|
1139
|
+
const result = await sodax.migration.migratebnUSD(params2, spokeProvider, 3e4);
|
|
1140
|
+
if (result.ok) {
|
|
1141
|
+
const [spokeTxHash, hubTxHash] = result.value;
|
|
1142
|
+
return { spokeTxHash, hubTxHash };
|
|
1143
|
+
}
|
|
1144
|
+
const errorMessage = isLegacybnUSDToken(token?.address) ? "bnUSD migration failed. Please try again." : "bnUSD reverse migration failed. Please try again.";
|
|
1145
|
+
throw new Error(errorMessage);
|
|
1146
|
+
}
|
|
1147
|
+
throw new Error("Invalid migration mode");
|
|
1148
|
+
}
|
|
1149
|
+
});
|
|
1150
|
+
}
|
|
1151
|
+
function useMigrationAllowance(params, spokeProvider) {
|
|
1152
|
+
const { sodax } = useSodaxContext();
|
|
1153
|
+
return useQuery({
|
|
1154
|
+
queryKey: ["migration-allowance", params],
|
|
1155
|
+
queryFn: async () => {
|
|
1156
|
+
if (!spokeProvider || !params) {
|
|
1157
|
+
return false;
|
|
1158
|
+
}
|
|
1159
|
+
const { token, amount, migrationMode = MIGRATION_MODE_ICX_SODA, toToken, destinationAddress } = params;
|
|
1160
|
+
if (token?.xChainId === ICON_MAINNET_CHAIN_ID) {
|
|
1161
|
+
return true;
|
|
1162
|
+
}
|
|
1163
|
+
if (!spokeProvider) throw new Error("Spoke provider is required");
|
|
1164
|
+
const amountToMigrate = parseUnits(amount ?? "0", token?.decimals ?? 0);
|
|
1165
|
+
let migrationParams;
|
|
1166
|
+
if (migrationMode === MIGRATION_MODE_ICX_SODA) {
|
|
1167
|
+
migrationParams = {
|
|
1168
|
+
amount: amountToMigrate,
|
|
1169
|
+
to: destinationAddress
|
|
1170
|
+
};
|
|
1171
|
+
} else {
|
|
1172
|
+
if (!toToken) throw new Error("Destination token is required for bnUSD migration");
|
|
1173
|
+
migrationParams = {
|
|
1174
|
+
srcChainId: token?.xChainId,
|
|
1175
|
+
dstChainId: toToken?.xChainId,
|
|
1176
|
+
srcbnUSD: token?.address,
|
|
1177
|
+
dstbnUSD: toToken?.address,
|
|
1178
|
+
amount: amountToMigrate,
|
|
1179
|
+
to: destinationAddress
|
|
1180
|
+
};
|
|
1181
|
+
}
|
|
1182
|
+
const allowance = await sodax.migration.isAllowanceValid(migrationParams, "revert", spokeProvider);
|
|
1183
|
+
if (allowance.ok) {
|
|
1184
|
+
return allowance.value;
|
|
1185
|
+
}
|
|
1186
|
+
return false;
|
|
1187
|
+
},
|
|
1188
|
+
enabled: !!spokeProvider && !!params,
|
|
1189
|
+
refetchInterval: 2e3
|
|
1190
|
+
});
|
|
1191
|
+
}
|
|
1192
|
+
function useMigrationApprove(params, spokeProvider) {
|
|
1193
|
+
const { sodax } = useSodaxContext();
|
|
1194
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
1195
|
+
const [error, setError] = useState(null);
|
|
1196
|
+
const [isApproved, setIsApproved] = useState(false);
|
|
1197
|
+
const queryClient = useQueryClient();
|
|
1198
|
+
const prevTokenAddress = useRef(void 0);
|
|
1199
|
+
const prevAmount = useRef(void 0);
|
|
1200
|
+
useEffect(() => {
|
|
1201
|
+
if (prevTokenAddress.current !== params?.token?.address || prevAmount.current !== params?.amount) {
|
|
1202
|
+
setIsApproved(false);
|
|
1203
|
+
prevTokenAddress.current = params?.token?.address;
|
|
1204
|
+
prevAmount.current = params?.amount;
|
|
1205
|
+
}
|
|
1206
|
+
}, [params?.token?.address, params?.amount]);
|
|
1207
|
+
const approve = useCallback(
|
|
1208
|
+
async ({ params: approveParams }) => {
|
|
1209
|
+
try {
|
|
1210
|
+
setIsLoading(true);
|
|
1211
|
+
setError(null);
|
|
1212
|
+
if (!spokeProvider) {
|
|
1213
|
+
throw new Error("Spoke provider not found");
|
|
1214
|
+
}
|
|
1215
|
+
if (!approveParams) {
|
|
1216
|
+
throw new Error("Migration intent not found");
|
|
1217
|
+
}
|
|
1218
|
+
const { token, amount, migrationMode = MIGRATION_MODE_ICX_SODA, toToken, destinationAddress } = approveParams;
|
|
1219
|
+
const amountToMigrate = parseUnits(amount ?? "0", token?.decimals ?? 0);
|
|
1220
|
+
let result;
|
|
1221
|
+
if (migrationMode === MIGRATION_MODE_ICX_SODA) {
|
|
1222
|
+
const revertParams = {
|
|
1223
|
+
amount: amountToMigrate,
|
|
1224
|
+
to: destinationAddress
|
|
1225
|
+
};
|
|
1226
|
+
result = await sodax.migration.approve(revertParams, "revert", spokeProvider, false);
|
|
1227
|
+
} else if (migrationMode === MIGRATION_MODE_BNUSD) {
|
|
1228
|
+
if (!toToken) throw new Error("Destination token is required for bnUSD migration");
|
|
1229
|
+
const migrationParams = {
|
|
1230
|
+
srcChainId: token?.xChainId,
|
|
1231
|
+
dstChainId: toToken?.xChainId,
|
|
1232
|
+
srcbnUSD: token?.address,
|
|
1233
|
+
dstbnUSD: toToken?.address,
|
|
1234
|
+
amount: amountToMigrate,
|
|
1235
|
+
to: destinationAddress
|
|
1236
|
+
};
|
|
1237
|
+
result = await sodax.migration.approve(migrationParams, "revert", spokeProvider, false);
|
|
1238
|
+
} else {
|
|
1239
|
+
throw new Error("Invalid migration mode");
|
|
1240
|
+
}
|
|
1241
|
+
if (!result.ok) {
|
|
1242
|
+
throw new Error("Failed to approve tokens");
|
|
1243
|
+
}
|
|
1244
|
+
setIsApproved(true);
|
|
1245
|
+
queryClient.invalidateQueries({ queryKey: ["migration-allowance", params] });
|
|
1246
|
+
return result.ok;
|
|
1247
|
+
} catch (err) {
|
|
1248
|
+
const error2 = err instanceof Error ? err : new Error("An unknown error occurred");
|
|
1249
|
+
setError(error2);
|
|
1250
|
+
throw error2;
|
|
1251
|
+
} finally {
|
|
1252
|
+
setIsLoading(false);
|
|
1253
|
+
}
|
|
1254
|
+
},
|
|
1255
|
+
[spokeProvider, sodax, queryClient, params]
|
|
1256
|
+
);
|
|
1257
|
+
const resetError = useCallback(() => {
|
|
1258
|
+
setError(null);
|
|
1259
|
+
}, []);
|
|
1260
|
+
return {
|
|
1261
|
+
approve,
|
|
1262
|
+
isLoading,
|
|
1263
|
+
error,
|
|
1264
|
+
resetError,
|
|
1265
|
+
isApproved
|
|
1266
|
+
};
|
|
1267
|
+
}
|
|
1268
|
+
var SodaxProvider = ({ children, testnet = false, config, rpcConfig }) => {
|
|
1269
|
+
const sodax = new Sodax(config);
|
|
1270
|
+
return /* @__PURE__ */ React.createElement(SodaxContext.Provider, { value: { sodax, testnet, rpcConfig } }, children);
|
|
417
1271
|
};
|
|
418
1272
|
|
|
419
|
-
export { SodaxProvider,
|
|
1273
|
+
export { MIGRATION_MODE_BNUSD, MIGRATION_MODE_ICX_SODA, SodaxProvider, useAToken, useBackendAllMoneyMarketAssets, useBackendAllMoneyMarketBorrowers, useBackendIntentByHash, useBackendIntentByTxHash, useBackendMoneyMarketAsset, useBackendMoneyMarketAssetBorrowers, useBackendMoneyMarketAssetSuppliers, useBackendMoneyMarketPosition, useBackendOrderbook, useBorrow, useBridge, useBridgeAllowance, useBridgeApprove, useCancelSwap, useCancelUnstake, useClaim, useConvertedAssets, useDeriveUserWalletAddress, useEstimateGas, useGetBridgeableAmount, useGetBridgeableTokens, useHubProvider, useInstantUnstake, useInstantUnstakeAllowance, useInstantUnstakeApprove, useInstantUnstakeRatio, useMMAllowance, useMMApprove, useMigrate, useMigrationAllowance, useMigrationApprove, useQuote, useRepay, useRequestTrustline, useReservesData, useReservesUsdFormat, useSodaxContext, useSpokeProvider, useStake, useStakeAllowance, useStakeApprove, useStakeRatio, useStakingConfig, useStakingInfo, useStatus, useStellarTrustlineCheck, useSupply, useSwap, useSwapAllowance, useSwapApprove, useUnstake, useUnstakeAllowance, useUnstakeApprove, useUnstakingInfo, useUnstakingInfoWithPenalty, useUserReservesData, useWithdraw };
|
|
420
1274
|
//# sourceMappingURL=index.mjs.map
|
|
421
1275
|
//# sourceMappingURL=index.mjs.map
|