@sodax/dapp-kit 0.0.1-rc.1 → 0.0.1-rc.10
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/README.md +14 -13
- package/dist/contexts/index.d.ts +2 -1
- package/dist/contexts/index.d.ts.map +1 -1
- package/dist/core/index.d.ts +2 -3
- package/dist/core/index.d.ts.map +1 -1
- package/dist/hooks/mm/index.d.ts +3 -1
- package/dist/hooks/mm/index.d.ts.map +1 -1
- package/dist/hooks/mm/useBorrow.d.ts +13 -4
- package/dist/hooks/mm/useBorrow.d.ts.map +1 -1
- package/dist/hooks/mm/useMMAllowance.d.ts +26 -0
- package/dist/hooks/mm/useMMAllowance.d.ts.map +1 -0
- package/dist/hooks/mm/useMMApprove.d.ts +27 -0
- package/dist/hooks/mm/useMMApprove.d.ts.map +1 -0
- package/dist/hooks/mm/useRepay.d.ts +13 -4
- package/dist/hooks/mm/useRepay.d.ts.map +1 -1
- package/dist/hooks/mm/useReservesData.d.ts +19 -0
- package/dist/hooks/mm/useReservesData.d.ts.map +1 -0
- package/dist/hooks/mm/useSupply.d.ts +4 -2
- package/dist/hooks/mm/useSupply.d.ts.map +1 -1
- package/dist/hooks/mm/useUserReservesData.d.ts +1 -7
- package/dist/hooks/mm/useUserReservesData.d.ts.map +1 -1
- package/dist/hooks/mm/useWithdraw.d.ts +11 -4
- package/dist/hooks/mm/useWithdraw.d.ts.map +1 -1
- package/dist/hooks/provider/useHubProvider.d.ts +1 -1
- package/dist/hooks/provider/useHubProvider.d.ts.map +1 -1
- package/dist/hooks/provider/useSpokeProvider.d.ts +16 -2
- package/dist/hooks/provider/useSpokeProvider.d.ts.map +1 -1
- package/dist/hooks/shared/index.d.ts +0 -2
- package/dist/hooks/shared/index.d.ts.map +1 -1
- package/dist/hooks/swap/index.d.ts +2 -0
- package/dist/hooks/swap/index.d.ts.map +1 -1
- package/dist/hooks/swap/useCreateIntentOrder.d.ts +6 -6
- package/dist/hooks/swap/useCreateIntentOrder.d.ts.map +1 -1
- package/dist/hooks/swap/useQuote.d.ts +4 -4
- package/dist/hooks/swap/useQuote.d.ts.map +1 -1
- package/dist/hooks/swap/useStatus.d.ts +3 -3
- package/dist/hooks/swap/useStatus.d.ts.map +1 -1
- package/dist/hooks/swap/useSwapAllowance.d.ts +23 -0
- package/dist/hooks/swap/useSwapAllowance.d.ts.map +1 -0
- package/dist/hooks/swap/useSwapApprove.d.ts +26 -0
- package/dist/hooks/swap/useSwapApprove.d.ts.map +1 -0
- package/dist/index.js +230 -189
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +228 -188
- package/dist/index.mjs.map +1 -1
- package/dist/providers/SodaxProvider.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/contexts/index.ts +2 -1
- package/src/core/index.ts +5 -35
- package/src/hooks/mm/index.ts +3 -1
- package/src/hooks/mm/useBorrow.ts +20 -10
- package/src/hooks/mm/useMMAllowance.ts +55 -0
- package/src/hooks/mm/useMMApprove.ts +67 -0
- package/src/hooks/mm/useRepay.ts +20 -10
- package/src/hooks/mm/useReservesData.ts +37 -0
- package/src/hooks/mm/useSupply.ts +9 -6
- package/src/hooks/mm/useUserReservesData.ts +17 -34
- package/src/hooks/mm/useWithdraw.ts +17 -11
- package/src/hooks/provider/useHubProvider.ts +2 -21
- package/src/hooks/provider/useSpokeProvider.ts +87 -10
- package/src/hooks/shared/index.ts +0 -2
- package/src/hooks/swap/index.ts +2 -0
- package/src/hooks/swap/useCreateIntentOrder.ts +9 -11
- package/src/hooks/swap/useQuote.ts +5 -5
- package/src/hooks/swap/useStatus.ts +3 -3
- package/src/hooks/swap/useSwapAllowance.ts +44 -0
- package/src/hooks/swap/useSwapApprove.ts +68 -0
- package/src/providers/SodaxProvider.tsx +18 -3
- package/dist/hooks/mm/useHubWalletAddress.d.ts +0 -24
- package/dist/hooks/mm/useHubWalletAddress.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/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.mjs
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import React, { createContext, useContext, useMemo } from 'react';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { EvmSpokeProvider, spokeChainConfig, SuiSpokeProvider, IconSpokeProvider, InjectiveSpokeProvider, StellarSpokeProvider, SolanaSpokeProvider, encodeAddress, EvmWalletAbstraction, getMoneyMarketConfig, Sodax, getHubChainConfig, EvmHubProvider, hubAssets } from '@sodax/sdk';
|
|
3
|
+
import { getXChainType, useWalletProvider } from '@sodax/wallet-sdk';
|
|
4
|
+
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
5
5
|
import { parseUnits } from 'viem';
|
|
6
|
-
import { CHAIN_IDS } from '@sodax/types';
|
|
7
6
|
|
|
8
7
|
// src/contexts/index.ts
|
|
9
8
|
var SodaxContext = createContext(null);
|
|
@@ -14,111 +13,72 @@ var useSodaxContext = () => {
|
|
|
14
13
|
}
|
|
15
14
|
return context;
|
|
16
15
|
};
|
|
17
|
-
|
|
16
|
+
|
|
17
|
+
// src/hooks/provider/useHubProvider.ts
|
|
18
|
+
function useHubProvider() {
|
|
19
|
+
const { hubProvider } = useSodaxContext();
|
|
20
|
+
return hubProvider;
|
|
21
|
+
}
|
|
22
|
+
function useSpokeProvider(spokeChainId, walletProvider) {
|
|
18
23
|
const xChainType = getXChainType(spokeChainId);
|
|
19
|
-
const
|
|
24
|
+
const walletProvider_ = useWalletProvider(spokeChainId);
|
|
25
|
+
const _walletProvider = walletProvider ?? walletProvider_;
|
|
20
26
|
const spokeProvider = useMemo(() => {
|
|
21
|
-
if (!
|
|
27
|
+
if (!_walletProvider) return void 0;
|
|
28
|
+
if (!spokeChainId) return void 0;
|
|
22
29
|
if (xChainType === "EVM") {
|
|
23
|
-
return new EvmSpokeProvider(
|
|
30
|
+
return new EvmSpokeProvider(
|
|
31
|
+
_walletProvider,
|
|
32
|
+
spokeChainConfig[spokeChainId]
|
|
33
|
+
);
|
|
24
34
|
}
|
|
25
35
|
if (xChainType === "SUI") {
|
|
26
|
-
return new SuiSpokeProvider(
|
|
36
|
+
return new SuiSpokeProvider(
|
|
37
|
+
spokeChainConfig[spokeChainId],
|
|
38
|
+
_walletProvider
|
|
39
|
+
);
|
|
27
40
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
function useAllowance(token, amount) {
|
|
33
|
-
const { sodax } = useSodaxContext();
|
|
34
|
-
const spokeProvider = useSpokeProvider(token.xChainId);
|
|
35
|
-
return useQuery({
|
|
36
|
-
queryKey: ["allowance", token.address, amount],
|
|
37
|
-
queryFn: async () => {
|
|
38
|
-
if (!spokeProvider) {
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
const allowance = await sodax.moneyMarket.isAllowanceValid(
|
|
42
|
-
{
|
|
43
|
-
token: token.address,
|
|
44
|
-
amount: parseUnits(amount, token.decimals)
|
|
45
|
-
},
|
|
46
|
-
spokeProvider
|
|
41
|
+
if (xChainType === "ICON") {
|
|
42
|
+
return new IconSpokeProvider(
|
|
43
|
+
_walletProvider,
|
|
44
|
+
spokeChainConfig[spokeChainId]
|
|
47
45
|
);
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
enabled: !!spokeProvider
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
function useApprove(token) {
|
|
57
|
-
const { sodax } = useSodaxContext();
|
|
58
|
-
const spokeProvider = useSpokeProvider(token.xChainId);
|
|
59
|
-
const queryClient = useQueryClient();
|
|
60
|
-
const {
|
|
61
|
-
mutateAsync: approve,
|
|
62
|
-
isPending,
|
|
63
|
-
error,
|
|
64
|
-
reset: resetError
|
|
65
|
-
} = useMutation({
|
|
66
|
-
mutationFn: async (amount) => {
|
|
67
|
-
if (!spokeProvider) {
|
|
68
|
-
throw new Error("Spoke provider not found");
|
|
69
|
-
}
|
|
70
|
-
const allowance = await sodax.moneyMarket.approve(
|
|
71
|
-
token.address,
|
|
72
|
-
parseUnits(amount, token.decimals),
|
|
73
|
-
spokeProvider.chainConfig.addresses.assetManager,
|
|
74
|
-
spokeProvider
|
|
46
|
+
}
|
|
47
|
+
if (xChainType === "INJECTIVE") {
|
|
48
|
+
return new InjectiveSpokeProvider(
|
|
49
|
+
spokeChainConfig[spokeChainId],
|
|
50
|
+
_walletProvider
|
|
75
51
|
);
|
|
76
|
-
if (!allowance.ok) {
|
|
77
|
-
throw new Error("Failed to approve tokens");
|
|
78
|
-
}
|
|
79
|
-
return allowance.ok;
|
|
80
|
-
},
|
|
81
|
-
onSuccess: () => {
|
|
82
|
-
queryClient.invalidateQueries({ queryKey: ["allowance", token.address] });
|
|
83
52
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
resetError
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
function useHubProvider() {
|
|
93
|
-
const { sodax } = useSodaxContext();
|
|
94
|
-
const hubChainId = sodax.config?.hubProviderConfig?.chainConfig.chain.id;
|
|
95
|
-
const hubRpcUrl = sodax.config?.hubProviderConfig?.hubRpcUrl;
|
|
96
|
-
const xChainType = getXChainType(hubChainId);
|
|
97
|
-
const hubProvider = useMemo(() => {
|
|
98
|
-
if (xChainType === "EVM" && hubChainId && hubRpcUrl) {
|
|
99
|
-
const hubChainCfg = getHubChainConfig(hubChainId);
|
|
100
|
-
if (!hubChainCfg) return void 0;
|
|
101
|
-
return new EvmHubProvider({
|
|
102
|
-
hubRpcUrl,
|
|
103
|
-
chainConfig: hubChainCfg
|
|
53
|
+
if (xChainType === "STELLAR") {
|
|
54
|
+
const stellarConfig = spokeChainConfig[spokeChainId];
|
|
55
|
+
return new StellarSpokeProvider(_walletProvider, stellarConfig, {
|
|
56
|
+
horizonRpcUrl: stellarConfig.horizonRpcUrl,
|
|
57
|
+
sorobanRpcUrl: stellarConfig.sorobanRpcUrl
|
|
104
58
|
});
|
|
105
59
|
}
|
|
60
|
+
if (xChainType === "SOLANA") {
|
|
61
|
+
return new SolanaSpokeProvider(
|
|
62
|
+
_walletProvider,
|
|
63
|
+
spokeChainConfig[spokeChainId]
|
|
64
|
+
);
|
|
65
|
+
}
|
|
106
66
|
return void 0;
|
|
107
|
-
}, [
|
|
108
|
-
return
|
|
67
|
+
}, [spokeChainId, xChainType, _walletProvider]);
|
|
68
|
+
return spokeProvider;
|
|
109
69
|
}
|
|
110
|
-
function useBorrow(
|
|
70
|
+
function useBorrow(spokeToken, spokeProvider) {
|
|
111
71
|
const { sodax } = useSodaxContext();
|
|
112
|
-
const spokeProvider = useSpokeProvider(spokeChainId);
|
|
113
72
|
return useMutation({
|
|
114
73
|
mutationFn: async (amount) => {
|
|
115
74
|
if (!spokeProvider) {
|
|
116
75
|
throw new Error("spokeProvider is not found");
|
|
117
76
|
}
|
|
118
|
-
const response = await sodax.moneyMarket.
|
|
77
|
+
const response = await sodax.moneyMarket.borrow(
|
|
119
78
|
{
|
|
120
|
-
token:
|
|
121
|
-
amount: parseUnits(amount,
|
|
79
|
+
token: spokeToken.address,
|
|
80
|
+
amount: parseUnits(amount, 18),
|
|
81
|
+
action: "borrow"
|
|
122
82
|
},
|
|
123
83
|
spokeProvider
|
|
124
84
|
);
|
|
@@ -130,18 +90,18 @@ function useBorrow(hubToken, spokeChainId) {
|
|
|
130
90
|
}
|
|
131
91
|
});
|
|
132
92
|
}
|
|
133
|
-
function useRepay(
|
|
93
|
+
function useRepay(spokeToken, spokeProvider) {
|
|
134
94
|
const { sodax } = useSodaxContext();
|
|
135
|
-
const spokeProvider = useSpokeProvider(spokeChainId);
|
|
136
95
|
return useMutation({
|
|
137
96
|
mutationFn: async (amount) => {
|
|
138
97
|
if (!spokeProvider) {
|
|
139
98
|
throw new Error("spokeProvider is not found");
|
|
140
99
|
}
|
|
141
|
-
const response = await sodax.moneyMarket.
|
|
100
|
+
const response = await sodax.moneyMarket.repay(
|
|
142
101
|
{
|
|
143
|
-
token:
|
|
144
|
-
amount: parseUnits(amount,
|
|
102
|
+
token: spokeToken.address,
|
|
103
|
+
amount: parseUnits(amount, spokeToken.decimals),
|
|
104
|
+
action: "repay"
|
|
145
105
|
},
|
|
146
106
|
spokeProvider
|
|
147
107
|
);
|
|
@@ -153,18 +113,18 @@ function useRepay(hubToken, spokeChainId) {
|
|
|
153
113
|
}
|
|
154
114
|
});
|
|
155
115
|
}
|
|
156
|
-
function useSupply(spokeToken) {
|
|
116
|
+
function useSupply(spokeToken, spokeProvider) {
|
|
157
117
|
const { sodax } = useSodaxContext();
|
|
158
|
-
const spokeProvider = useSpokeProvider(spokeToken.xChainId);
|
|
159
118
|
return useMutation({
|
|
160
119
|
mutationFn: async (amount) => {
|
|
161
120
|
if (!spokeProvider) {
|
|
162
121
|
throw new Error("spokeProvider is not found");
|
|
163
122
|
}
|
|
164
|
-
const response = await sodax.moneyMarket.
|
|
123
|
+
const response = await sodax.moneyMarket.supply(
|
|
165
124
|
{
|
|
166
125
|
token: spokeToken.address,
|
|
167
|
-
amount: parseUnits(amount, spokeToken.decimals)
|
|
126
|
+
amount: parseUnits(amount, spokeToken.decimals),
|
|
127
|
+
action: "supply"
|
|
168
128
|
},
|
|
169
129
|
spokeProvider
|
|
170
130
|
);
|
|
@@ -176,18 +136,19 @@ function useSupply(spokeToken) {
|
|
|
176
136
|
}
|
|
177
137
|
});
|
|
178
138
|
}
|
|
179
|
-
function useWithdraw(
|
|
139
|
+
function useWithdraw(spokeToken, spokeProvider) {
|
|
180
140
|
const { sodax } = useSodaxContext();
|
|
181
|
-
const spokeProvider = useSpokeProvider(spokeChainId);
|
|
182
141
|
return useMutation({
|
|
183
142
|
mutationFn: async (amount) => {
|
|
184
143
|
if (!spokeProvider) {
|
|
185
144
|
throw new Error("spokeProvider is not found");
|
|
186
145
|
}
|
|
187
|
-
const response = await sodax.moneyMarket.
|
|
146
|
+
const response = await sodax.moneyMarket.withdraw(
|
|
188
147
|
{
|
|
189
|
-
token:
|
|
190
|
-
|
|
148
|
+
token: spokeToken.address,
|
|
149
|
+
// vault token on hub chain decimals is 18
|
|
150
|
+
amount: parseUnits(amount, 18),
|
|
151
|
+
action: "withdraw"
|
|
191
152
|
},
|
|
192
153
|
spokeProvider
|
|
193
154
|
);
|
|
@@ -199,100 +160,107 @@ function useWithdraw(hubToken, spokeChainId) {
|
|
|
199
160
|
}
|
|
200
161
|
});
|
|
201
162
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
xChainId,
|
|
209
|
-
symbol: token.symbol,
|
|
210
|
-
name: token.name,
|
|
211
|
-
decimals: token.decimal,
|
|
212
|
-
address: tokenAddress
|
|
213
|
-
});
|
|
214
|
-
allXTokens.push({
|
|
215
|
-
// @ts-ignore
|
|
216
|
-
xChainId: CHAIN_IDS.includes(xChainId) ? "sonic" : "sonic-blaze",
|
|
217
|
-
symbol: token.symbol,
|
|
218
|
-
name: token.name,
|
|
219
|
-
decimals: token.decimal,
|
|
220
|
-
address: token.vault
|
|
221
|
-
});
|
|
222
|
-
});
|
|
223
|
-
});
|
|
224
|
-
var getSpokeTokenAddressByVault = (spokeChainId, vault) => {
|
|
225
|
-
const tokens = hubAssets[spokeChainId];
|
|
226
|
-
const token = Object.keys(tokens).find((tokenAddress) => tokens[tokenAddress].vault === vault);
|
|
227
|
-
if (!token) {
|
|
228
|
-
throw new Error("Token not found");
|
|
229
|
-
}
|
|
230
|
-
return token;
|
|
231
|
-
};
|
|
232
|
-
function useHubWalletAddress(spokeChainId, address, hubProvider) {
|
|
233
|
-
return useQuery({
|
|
234
|
-
queryKey: ["hubWallet", spokeChainId, address],
|
|
163
|
+
function useUserReservesData(spokeChainId, address) {
|
|
164
|
+
const { sodax } = useSodaxContext();
|
|
165
|
+
const hubChainId = sodax.config?.hubProviderConfig?.chainConfig.chain.id ?? "sonic";
|
|
166
|
+
const hubProvider = useHubProvider();
|
|
167
|
+
const { data: userReserves } = useQuery({
|
|
168
|
+
queryKey: ["userReserves", spokeChainId, address],
|
|
235
169
|
queryFn: async () => {
|
|
236
|
-
if (!address)
|
|
237
|
-
|
|
238
|
-
const hubWalletAddress = await EvmWalletAbstraction.getUserHubWalletAddress(
|
|
239
|
-
spokeChainId,
|
|
240
|
-
address,
|
|
241
|
-
hubProvider
|
|
242
|
-
);
|
|
243
|
-
return hubWalletAddress;
|
|
244
|
-
} catch (error) {
|
|
245
|
-
console.log("error", error);
|
|
246
|
-
return null;
|
|
170
|
+
if (!hubProvider || !address) {
|
|
171
|
+
return;
|
|
247
172
|
}
|
|
173
|
+
const addressBytes = encodeAddress(spokeChainId, address);
|
|
174
|
+
const hubWalletAddress = await EvmWalletAbstraction.getUserHubWalletAddress(
|
|
175
|
+
spokeChainId,
|
|
176
|
+
addressBytes,
|
|
177
|
+
hubProvider
|
|
178
|
+
);
|
|
179
|
+
const moneyMarketConfig = getMoneyMarketConfig(hubChainId);
|
|
180
|
+
const [res] = await sodax.moneyMarket.getUserReservesData(
|
|
181
|
+
hubWalletAddress,
|
|
182
|
+
moneyMarketConfig.uiPoolDataProvider,
|
|
183
|
+
moneyMarketConfig.poolAddressesProvider
|
|
184
|
+
);
|
|
185
|
+
return res;
|
|
248
186
|
},
|
|
249
|
-
enabled: !!
|
|
187
|
+
enabled: !!spokeChainId && !!hubProvider && !!address,
|
|
188
|
+
refetchInterval: 5e3
|
|
250
189
|
});
|
|
190
|
+
return userReserves;
|
|
251
191
|
}
|
|
252
|
-
|
|
253
|
-
// src/hooks/mm/useUserReservesData.ts
|
|
254
|
-
function useUserReservesData(spokeChainId) {
|
|
192
|
+
function useReservesData() {
|
|
255
193
|
const { sodax } = useSodaxContext();
|
|
256
194
|
const hubChainId = sodax.config?.hubProviderConfig?.chainConfig.chain.id ?? "sonic";
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
const { address } = useXAccount(spokeChainId);
|
|
260
|
-
const { data: hubWalletAddress } = useHubWalletAddress(
|
|
261
|
-
spokeChainId,
|
|
262
|
-
address,
|
|
263
|
-
hubProvider
|
|
264
|
-
);
|
|
265
|
-
const { data: userReserves } = useQuery({
|
|
266
|
-
queryKey: ["userReserves", hubWalletAddress],
|
|
195
|
+
return useQuery({
|
|
196
|
+
queryKey: ["reservesData"],
|
|
267
197
|
queryFn: async () => {
|
|
268
|
-
|
|
269
|
-
|
|
198
|
+
const moneyMarketConfig = getMoneyMarketConfig(hubChainId);
|
|
199
|
+
return await sodax.moneyMarket.getReservesData(
|
|
200
|
+
moneyMarketConfig.uiPoolDataProvider,
|
|
201
|
+
moneyMarketConfig.poolAddressesProvider
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
function useMMAllowance(token, amount, action, spokeProvider) {
|
|
207
|
+
const { sodax } = useSodaxContext();
|
|
208
|
+
return useQuery({
|
|
209
|
+
queryKey: ["allowance", token.address, amount, action],
|
|
210
|
+
queryFn: async () => {
|
|
211
|
+
if (!spokeProvider) throw new Error("Spoke provider is required");
|
|
212
|
+
const allowance = await sodax.moneyMarket.isAllowanceValid(
|
|
213
|
+
{
|
|
214
|
+
token: token.address,
|
|
215
|
+
amount: parseUnits(amount, token.decimals),
|
|
216
|
+
action
|
|
217
|
+
},
|
|
218
|
+
spokeProvider
|
|
219
|
+
);
|
|
220
|
+
if (allowance.ok) {
|
|
221
|
+
return allowance.value;
|
|
270
222
|
}
|
|
271
|
-
|
|
272
|
-
|
|
223
|
+
return false;
|
|
224
|
+
},
|
|
225
|
+
enabled: !!spokeProvider
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
function useMMApprove(token, spokeProvider) {
|
|
229
|
+
const { sodax } = useSodaxContext();
|
|
230
|
+
const queryClient = useQueryClient();
|
|
231
|
+
const {
|
|
232
|
+
mutateAsync: approve,
|
|
233
|
+
isPending,
|
|
234
|
+
error,
|
|
235
|
+
reset: resetError
|
|
236
|
+
} = useMutation({
|
|
237
|
+
mutationFn: async ({ amount, action }) => {
|
|
238
|
+
if (!spokeProvider) {
|
|
239
|
+
throw new Error("Spoke provider not found");
|
|
273
240
|
}
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
token: allXTokens.find((t) => t.address === r.underlyingAsset)
|
|
285
|
-
};
|
|
286
|
-
});
|
|
287
|
-
} catch (error) {
|
|
288
|
-
console.log("error", error);
|
|
289
|
-
return;
|
|
241
|
+
const allowance = await sodax.moneyMarket.approve(
|
|
242
|
+
{
|
|
243
|
+
token: token.address,
|
|
244
|
+
amount: parseUnits(amount, token.decimals),
|
|
245
|
+
action
|
|
246
|
+
},
|
|
247
|
+
spokeProvider
|
|
248
|
+
);
|
|
249
|
+
if (!allowance.ok) {
|
|
250
|
+
throw new Error("Failed to approve tokens");
|
|
290
251
|
}
|
|
252
|
+
return allowance.ok;
|
|
291
253
|
},
|
|
292
|
-
|
|
293
|
-
|
|
254
|
+
onSuccess: () => {
|
|
255
|
+
queryClient.invalidateQueries({ queryKey: ["allowance", token.address] });
|
|
256
|
+
}
|
|
294
257
|
});
|
|
295
|
-
return
|
|
258
|
+
return {
|
|
259
|
+
approve,
|
|
260
|
+
isLoading: isPending,
|
|
261
|
+
error,
|
|
262
|
+
resetError
|
|
263
|
+
};
|
|
296
264
|
}
|
|
297
265
|
var useQuote = (payload) => {
|
|
298
266
|
const { sodax } = useSodaxContext();
|
|
@@ -308,9 +276,8 @@ var useQuote = (payload) => {
|
|
|
308
276
|
refetchInterval: 3e3
|
|
309
277
|
});
|
|
310
278
|
};
|
|
311
|
-
function useCreateIntentOrder(
|
|
279
|
+
function useCreateIntentOrder(spokeProvider) {
|
|
312
280
|
const { sodax } = useSodaxContext();
|
|
313
|
-
const spokeProvider = useSpokeProvider(chainId);
|
|
314
281
|
return useMutation({
|
|
315
282
|
mutationFn: async (params) => {
|
|
316
283
|
if (!spokeProvider) {
|
|
@@ -331,11 +298,84 @@ var useStatus = (intent_tx_hash) => {
|
|
|
331
298
|
// 3s
|
|
332
299
|
});
|
|
333
300
|
};
|
|
301
|
+
function useSwapAllowance(params, spokeProvider) {
|
|
302
|
+
const { sodax } = useSodaxContext();
|
|
303
|
+
return useQuery({
|
|
304
|
+
queryKey: ["allowance", params],
|
|
305
|
+
queryFn: async () => {
|
|
306
|
+
if (!spokeProvider || !params) {
|
|
307
|
+
return false;
|
|
308
|
+
}
|
|
309
|
+
const allowance = await sodax.solver.isAllowanceValid(params, spokeProvider);
|
|
310
|
+
if (allowance.ok) {
|
|
311
|
+
return allowance.value;
|
|
312
|
+
}
|
|
313
|
+
return false;
|
|
314
|
+
},
|
|
315
|
+
enabled: !!spokeProvider && !!params
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
function useSwapApprove(token, spokeProvider) {
|
|
319
|
+
const { sodax } = useSodaxContext();
|
|
320
|
+
const queryClient = useQueryClient();
|
|
321
|
+
const {
|
|
322
|
+
mutateAsync: approve,
|
|
323
|
+
isPending,
|
|
324
|
+
error,
|
|
325
|
+
reset: resetError
|
|
326
|
+
} = useMutation({
|
|
327
|
+
mutationFn: async ({ amount }) => {
|
|
328
|
+
if (!spokeProvider) {
|
|
329
|
+
throw new Error("Spoke provider not found");
|
|
330
|
+
}
|
|
331
|
+
if (!token) {
|
|
332
|
+
throw new Error("Token not found");
|
|
333
|
+
}
|
|
334
|
+
const allowance = await sodax.solver.approve(
|
|
335
|
+
token.address,
|
|
336
|
+
parseUnits(amount, token.decimals),
|
|
337
|
+
spokeProvider
|
|
338
|
+
);
|
|
339
|
+
if (!allowance.ok) {
|
|
340
|
+
throw new Error("Failed to approve tokens");
|
|
341
|
+
}
|
|
342
|
+
return allowance.ok;
|
|
343
|
+
},
|
|
344
|
+
onSuccess: () => {
|
|
345
|
+
queryClient.invalidateQueries({ queryKey: ["allowance", token?.address] });
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
return {
|
|
349
|
+
approve,
|
|
350
|
+
isLoading: isPending,
|
|
351
|
+
error,
|
|
352
|
+
resetError
|
|
353
|
+
};
|
|
354
|
+
}
|
|
334
355
|
var SodaxProvider = ({ children, testnet = false, config }) => {
|
|
335
356
|
const sodax = new Sodax(config);
|
|
336
|
-
|
|
357
|
+
const hubChainId = config?.hubProviderConfig?.chainConfig.chain.id;
|
|
358
|
+
const hubRpcUrl = config?.hubProviderConfig?.hubRpcUrl;
|
|
359
|
+
const hubProvider = useMemo(() => {
|
|
360
|
+
if (hubChainId && hubRpcUrl) {
|
|
361
|
+
const hubChainCfg = getHubChainConfig(hubChainId);
|
|
362
|
+
return new EvmHubProvider({
|
|
363
|
+
hubRpcUrl,
|
|
364
|
+
chainConfig: hubChainCfg
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
return void 0;
|
|
368
|
+
}, [hubChainId, hubRpcUrl]);
|
|
369
|
+
return /* @__PURE__ */ React.createElement(SodaxContext.Provider, { value: { sodax, testnet, hubProvider } }, children);
|
|
370
|
+
};
|
|
371
|
+
var getSpokeTokenAddressByVault = (spokeChainId, vault) => {
|
|
372
|
+
const tokens = hubAssets[spokeChainId];
|
|
373
|
+
const address = Object.keys(tokens).find(
|
|
374
|
+
(tokenAddress) => tokens[tokenAddress].vault.toLowerCase() === vault.toLowerCase()
|
|
375
|
+
);
|
|
376
|
+
return address;
|
|
337
377
|
};
|
|
338
378
|
|
|
339
|
-
export { SodaxProvider,
|
|
379
|
+
export { SodaxProvider, getSpokeTokenAddressByVault, useBorrow, useCreateIntentOrder, useHubProvider, useMMAllowance, useMMApprove, useQuote, useRepay, useReservesData, useSodaxContext, useSpokeProvider, useStatus, useSupply, useSwapAllowance, useSwapApprove, useUserReservesData, useWithdraw };
|
|
340
380
|
//# sourceMappingURL=index.mjs.map
|
|
341
381
|
//# sourceMappingURL=index.mjs.map
|