@sodax/dapp-kit 0.0.1-rc.30 → 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/dist/index.mjs CHANGED
@@ -1,7 +1,8 @@
1
- import React, { createContext, useContext, useMemo } from 'react';
2
- import { SpokeService, deriveUserWalletAddress, STELLAR_MAINNET_CHAIN_ID, StellarSpokeProvider, StellarSpokeService, spokeChainConfig, SONIC_MAINNET_CHAIN_ID, SonicSpokeProvider, EvmSpokeProvider, SuiSpokeProvider, IconSpokeProvider, InjectiveSpokeProvider, SolanaSpokeProvider, Sodax, hubAssets } from '@sodax/sdk';
1
+ import React, { createContext, useContext, useState, useCallback, useMemo, useRef, useEffect } from 'react';
2
+ import { SpokeService, deriveUserWalletAddress, STELLAR_MAINNET_CHAIN_ID, StellarSpokeProvider, StellarSpokeService, spokeChainConfig, SONIC_MAINNET_CHAIN_ID, SonicSpokeProvider, EvmSpokeProvider, SuiSpokeProvider, IconSpokeProvider, InjectiveSpokeProvider, SolanaSpokeProvider, isLegacybnUSDToken, Sodax, hubAssets } from '@sodax/sdk';
3
3
  import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
4
4
  import { parseUnits } from 'viem';
5
+ import { ICON_MAINNET_CHAIN_ID } from '@sodax/types';
5
6
 
6
7
  // src/contexts/index.ts
7
8
  var SodaxContext = createContext(null);
@@ -64,21 +65,46 @@ function useStellarTrustlineCheck(token, amount, spokeProvider, chainId) {
64
65
  }
65
66
  function useRequestTrustline(token) {
66
67
  const queryClient = useQueryClient();
67
- return useMutation({
68
- mutationFn: async ({
68
+ const [isLoading, setIsLoading] = useState(false);
69
+ const [isRequested, setIsRequested] = useState(false);
70
+ const [error, setError] = useState(null);
71
+ const [data, setData] = useState(null);
72
+ const requestTrustline = useCallback(
73
+ async ({
69
74
  token: token2,
70
75
  amount,
71
76
  spokeProvider
72
77
  }) => {
73
78
  if (!spokeProvider || !token2 || !amount || !(spokeProvider instanceof StellarSpokeProvider)) {
74
- throw new Error("Spoke provider, token or amount not found");
79
+ const error2 = new Error("Spoke provider, token or amount not found");
80
+ setError(error2);
81
+ throw error2;
82
+ }
83
+ setIsLoading(true);
84
+ setError(null);
85
+ try {
86
+ const result = await StellarSpokeService.requestTrustline(token2, amount, spokeProvider);
87
+ setData(result);
88
+ setIsRequested(true);
89
+ queryClient.invalidateQueries({ queryKey: ["stellar-trustline-check", token2] });
90
+ return result;
91
+ } catch (err) {
92
+ const error2 = err instanceof Error ? err : new Error("Unknown error occurred");
93
+ setError(error2);
94
+ throw error2;
95
+ } finally {
96
+ setIsLoading(false);
75
97
  }
76
- return StellarSpokeService.requestTrustline(token2, amount, spokeProvider);
77
98
  },
78
- onSuccess: () => {
79
- queryClient.invalidateQueries({ queryKey: ["stellar-trustline-check", token] });
80
- }
81
- });
99
+ [queryClient]
100
+ );
101
+ return {
102
+ requestTrustline,
103
+ isLoading,
104
+ isRequested,
105
+ error,
106
+ data
107
+ };
82
108
  }
83
109
 
84
110
  // src/hooks/provider/useHubProvider.ts
@@ -270,10 +296,11 @@ function useMMAllowance(token, amount, action, spokeProvider) {
270
296
  queryKey: ["allowance", token.address, amount, action],
271
297
  queryFn: async () => {
272
298
  if (!spokeProvider) throw new Error("Spoke provider is required");
299
+ const actionBasedDecimals = action === "withdraw" || action === "borrow" ? 18 : token.decimals;
273
300
  const allowance = await sodax.moneyMarket.isAllowanceValid(
274
301
  {
275
302
  token: token.address,
276
- amount: parseUnits(amount, token.decimals),
303
+ amount: parseUnits(amount, actionBasedDecimals),
277
304
  action
278
305
  },
279
306
  spokeProvider
@@ -299,10 +326,11 @@ function useMMApprove(token, spokeProvider) {
299
326
  if (!spokeProvider) {
300
327
  throw new Error("Spoke provider not found");
301
328
  }
329
+ const actionBasedDecimals = action === "withdraw" || action === "borrow" ? 18 : token.decimals;
302
330
  const allowance = await sodax.moneyMarket.approve(
303
331
  {
304
332
  token: token.address,
305
- amount: parseUnits(amount, token.decimals),
333
+ amount: parseUnits(amount, actionBasedDecimals),
306
334
  action
307
335
  },
308
336
  spokeProvider
@@ -1027,6 +1055,188 @@ function useInstantUnstakeAllowance(params, spokeProvider) {
1027
1055
  // Refetch every 5 seconds
1028
1056
  });
1029
1057
  }
1058
+
1059
+ // src/hooks/migrate/types.ts
1060
+ var MIGRATION_MODE_ICX_SODA = "icxsoda";
1061
+ var MIGRATION_MODE_BNUSD = "bnusd";
1062
+
1063
+ // src/hooks/migrate/useMigrate.tsx
1064
+ function useMigrate(spokeProvider) {
1065
+ const { sodax } = useSodaxContext();
1066
+ return useMutation({
1067
+ mutationFn: async (params) => {
1068
+ const { token, amount, migrationMode = MIGRATION_MODE_ICX_SODA, toToken, destinationAddress } = params;
1069
+ const amountToMigrate = parseUnits(amount ?? "0", token?.decimals ?? 0);
1070
+ if (!spokeProvider) {
1071
+ throw new Error("Spoke provider not found");
1072
+ }
1073
+ if (migrationMode === MIGRATION_MODE_ICX_SODA) {
1074
+ if (token?.xChainId === ICON_MAINNET_CHAIN_ID) {
1075
+ const params2 = {
1076
+ address: spokeChainConfig[ICON_MAINNET_CHAIN_ID].nativeToken,
1077
+ amount: amountToMigrate,
1078
+ to: destinationAddress
1079
+ };
1080
+ const result2 = await sodax.migration.migrateIcxToSoda(params2, spokeProvider, 3e4);
1081
+ if (result2.ok) {
1082
+ const [spokeTxHash, hubTxHash] = result2.value;
1083
+ return { spokeTxHash, hubTxHash };
1084
+ }
1085
+ throw new Error("ICX to SODA migration failed. Please try again.");
1086
+ }
1087
+ const revertParams = {
1088
+ amount: amountToMigrate,
1089
+ to: destinationAddress
1090
+ };
1091
+ const result = await sodax.migration.revertMigrateSodaToIcx(
1092
+ revertParams,
1093
+ spokeProvider,
1094
+ 3e4
1095
+ );
1096
+ if (result.ok) {
1097
+ const [hubTxHash, spokeTxHash] = result.value;
1098
+ return { spokeTxHash, hubTxHash };
1099
+ }
1100
+ throw new Error("SODA to ICX migration failed. Please try again.");
1101
+ }
1102
+ if (migrationMode === MIGRATION_MODE_BNUSD) {
1103
+ const params2 = {
1104
+ srcChainId: token?.xChainId,
1105
+ dstChainId: toToken?.xChainId,
1106
+ srcbnUSD: token?.address,
1107
+ dstbnUSD: toToken?.address,
1108
+ amount: amountToMigrate,
1109
+ to: destinationAddress
1110
+ };
1111
+ const result = await sodax.migration.migratebnUSD(params2, spokeProvider, 3e4);
1112
+ if (result.ok) {
1113
+ const [spokeTxHash, hubTxHash] = result.value;
1114
+ return { spokeTxHash, hubTxHash };
1115
+ }
1116
+ const errorMessage = isLegacybnUSDToken(token?.address) ? "bnUSD migration failed. Please try again." : "bnUSD reverse migration failed. Please try again.";
1117
+ throw new Error(errorMessage);
1118
+ }
1119
+ throw new Error("Invalid migration mode");
1120
+ }
1121
+ });
1122
+ }
1123
+ function useMigrationAllowance(params, spokeProvider) {
1124
+ const { sodax } = useSodaxContext();
1125
+ return useQuery({
1126
+ queryKey: ["migration-allowance", params],
1127
+ queryFn: async () => {
1128
+ if (!spokeProvider || !params) {
1129
+ return false;
1130
+ }
1131
+ const { token, amount, migrationMode = MIGRATION_MODE_ICX_SODA, toToken, destinationAddress } = params;
1132
+ if (token?.xChainId === ICON_MAINNET_CHAIN_ID) {
1133
+ return true;
1134
+ }
1135
+ if (!spokeProvider) throw new Error("Spoke provider is required");
1136
+ const amountToMigrate = parseUnits(amount ?? "0", token?.decimals ?? 0);
1137
+ let migrationParams;
1138
+ if (migrationMode === MIGRATION_MODE_ICX_SODA) {
1139
+ migrationParams = {
1140
+ amount: amountToMigrate,
1141
+ to: destinationAddress
1142
+ };
1143
+ } else {
1144
+ if (!toToken) throw new Error("Destination token is required for bnUSD migration");
1145
+ migrationParams = {
1146
+ srcChainId: token?.xChainId,
1147
+ dstChainId: toToken?.xChainId,
1148
+ srcbnUSD: token?.address,
1149
+ dstbnUSD: toToken?.address,
1150
+ amount: amountToMigrate,
1151
+ to: destinationAddress
1152
+ };
1153
+ }
1154
+ const allowance = await sodax.migration.isAllowanceValid(migrationParams, "revert", spokeProvider);
1155
+ if (allowance.ok) {
1156
+ return allowance.value;
1157
+ }
1158
+ return false;
1159
+ },
1160
+ enabled: !!spokeProvider && !!params,
1161
+ refetchInterval: 2e3
1162
+ });
1163
+ }
1164
+ function useMigrationApprove(params, spokeProvider) {
1165
+ const { sodax } = useSodaxContext();
1166
+ const [isLoading, setIsLoading] = useState(false);
1167
+ const [error, setError] = useState(null);
1168
+ const [isApproved, setIsApproved] = useState(false);
1169
+ const queryClient = useQueryClient();
1170
+ const prevTokenAddress = useRef(void 0);
1171
+ const prevAmount = useRef(void 0);
1172
+ useEffect(() => {
1173
+ if (prevTokenAddress.current !== params?.token?.address || prevAmount.current !== params?.amount) {
1174
+ setIsApproved(false);
1175
+ prevTokenAddress.current = params?.token?.address;
1176
+ prevAmount.current = params?.amount;
1177
+ }
1178
+ }, [params?.token?.address, params?.amount]);
1179
+ const approve = useCallback(
1180
+ async ({ params: approveParams }) => {
1181
+ try {
1182
+ setIsLoading(true);
1183
+ setError(null);
1184
+ if (!spokeProvider) {
1185
+ throw new Error("Spoke provider not found");
1186
+ }
1187
+ if (!approveParams) {
1188
+ throw new Error("Migration intent not found");
1189
+ }
1190
+ const { token, amount, migrationMode = MIGRATION_MODE_ICX_SODA, toToken, destinationAddress } = approveParams;
1191
+ const amountToMigrate = parseUnits(amount ?? "0", token?.decimals ?? 0);
1192
+ let result;
1193
+ if (migrationMode === MIGRATION_MODE_ICX_SODA) {
1194
+ const revertParams = {
1195
+ amount: amountToMigrate,
1196
+ to: destinationAddress
1197
+ };
1198
+ result = await sodax.migration.approve(revertParams, "revert", spokeProvider, false);
1199
+ } else if (migrationMode === MIGRATION_MODE_BNUSD) {
1200
+ if (!toToken) throw new Error("Destination token is required for bnUSD migration");
1201
+ const migrationParams = {
1202
+ srcChainId: token?.xChainId,
1203
+ dstChainId: toToken?.xChainId,
1204
+ srcbnUSD: token?.address,
1205
+ dstbnUSD: toToken?.address,
1206
+ amount: amountToMigrate,
1207
+ to: destinationAddress
1208
+ };
1209
+ result = await sodax.migration.approve(migrationParams, "revert", spokeProvider, false);
1210
+ } else {
1211
+ throw new Error("Invalid migration mode");
1212
+ }
1213
+ if (!result.ok) {
1214
+ throw new Error("Failed to approve tokens");
1215
+ }
1216
+ setIsApproved(true);
1217
+ queryClient.invalidateQueries({ queryKey: ["migration-allowance", params] });
1218
+ return result.ok;
1219
+ } catch (err) {
1220
+ const error2 = err instanceof Error ? err : new Error("An unknown error occurred");
1221
+ setError(error2);
1222
+ throw error2;
1223
+ } finally {
1224
+ setIsLoading(false);
1225
+ }
1226
+ },
1227
+ [spokeProvider, sodax, queryClient, params]
1228
+ );
1229
+ const resetError = useCallback(() => {
1230
+ setError(null);
1231
+ }, []);
1232
+ return {
1233
+ approve,
1234
+ isLoading,
1235
+ error,
1236
+ resetError,
1237
+ isApproved
1238
+ };
1239
+ }
1030
1240
  var SodaxProvider = ({ children, testnet = false, config, rpcConfig }) => {
1031
1241
  const sodax = new Sodax(config);
1032
1242
  return /* @__PURE__ */ React.createElement(SodaxContext.Provider, { value: { sodax, testnet, rpcConfig } }, children);
@@ -1039,6 +1249,6 @@ var getSpokeTokenAddressByVault = (spokeChainId, vault) => {
1039
1249
  return address;
1040
1250
  };
1041
1251
 
1042
- export { SodaxProvider, getSpokeTokenAddressByVault, useBackendAllMoneyMarketAssets, useBackendAllMoneyMarketBorrowers, useBackendIntentByHash, useBackendIntentByTxHash, useBackendMoneyMarketAsset, useBackendMoneyMarketAssetBorrowers, useBackendMoneyMarketAssetSuppliers, useBackendMoneyMarketPosition, useBackendOrderbook, useBorrow, useBridge, useBridgeAllowance, useBridgeApprove, useCancelSwap, useCancelUnstake, useClaim, useConvertedAssets, useDeriveUserWalletAddress, useEstimateGas, useGetBridgeableAmount, useGetBridgeableTokens, useHubProvider, useInstantUnstake, useInstantUnstakeAllowance, useInstantUnstakeApprove, useInstantUnstakeRatio, useMMAllowance, useMMApprove, useQuote, useRepay, useRequestTrustline, useReservesData, useSodaxContext, useSpokeProvider, useStake, useStakeAllowance, useStakeApprove, useStakeRatio, useStakingConfig, useStakingInfo, useStatus, useStellarTrustlineCheck, useSupply, useSwap, useSwapAllowance, useSwapApprove, useUnstake, useUnstakeAllowance, useUnstakeApprove, useUnstakingInfo, useUnstakingInfoWithPenalty, useUserReservesData, useWithdraw };
1252
+ export { MIGRATION_MODE_BNUSD, MIGRATION_MODE_ICX_SODA, SodaxProvider, getSpokeTokenAddressByVault, useBackendAllMoneyMarketAssets, useBackendAllMoneyMarketBorrowers, useBackendIntentByHash, useBackendIntentByTxHash, useBackendMoneyMarketAsset, useBackendMoneyMarketAssetBorrowers, useBackendMoneyMarketAssetSuppliers, useBackendMoneyMarketPosition, useBackendOrderbook, useBorrow, useBridge, useBridgeAllowance, useBridgeApprove, useCancelSwap, useCancelUnstake, useClaim, useConvertedAssets, useDeriveUserWalletAddress, useEstimateGas, useGetBridgeableAmount, useGetBridgeableTokens, useHubProvider, useInstantUnstake, useInstantUnstakeAllowance, useInstantUnstakeApprove, useInstantUnstakeRatio, useMMAllowance, useMMApprove, useMigrate, useMigrationAllowance, useMigrationApprove, useQuote, useRepay, useRequestTrustline, useReservesData, useSodaxContext, useSpokeProvider, useStake, useStakeAllowance, useStakeApprove, useStakeRatio, useStakingConfig, useStakingInfo, useStatus, useStellarTrustlineCheck, useSupply, useSwap, useSwapAllowance, useSwapApprove, useUnstake, useUnstakeAllowance, useUnstakeApprove, useUnstakingInfo, useUnstakingInfoWithPenalty, useUserReservesData, useWithdraw };
1043
1253
  //# sourceMappingURL=index.mjs.map
1044
1254
  //# sourceMappingURL=index.mjs.map