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