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