@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.js CHANGED
@@ -4,6 +4,7 @@ var React = require('react');
4
4
  var sdk = require('@sodax/sdk');
5
5
  var reactQuery = require('@tanstack/react-query');
6
6
  var viem = require('viem');
7
+ var types = require('@sodax/types');
7
8
 
8
9
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
9
10
 
@@ -70,21 +71,46 @@ function useStellarTrustlineCheck(token, amount, spokeProvider, chainId) {
70
71
  }
71
72
  function useRequestTrustline(token) {
72
73
  const queryClient = reactQuery.useQueryClient();
73
- return reactQuery.useMutation({
74
- mutationFn: async ({
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 ({
75
80
  token: token2,
76
81
  amount,
77
82
  spokeProvider
78
83
  }) => {
79
84
  if (!spokeProvider || !token2 || !amount || !(spokeProvider instanceof sdk.StellarSpokeProvider)) {
80
- throw new Error("Spoke provider, token or amount not found");
85
+ const error2 = new Error("Spoke provider, token or amount not found");
86
+ setError(error2);
87
+ throw error2;
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);
81
103
  }
82
- return sdk.StellarSpokeService.requestTrustline(token2, amount, spokeProvider);
83
104
  },
84
- onSuccess: () => {
85
- queryClient.invalidateQueries({ queryKey: ["stellar-trustline-check", token] });
86
- }
87
- });
105
+ [queryClient]
106
+ );
107
+ return {
108
+ requestTrustline,
109
+ isLoading,
110
+ isRequested,
111
+ error,
112
+ data
113
+ };
88
114
  }
89
115
 
90
116
  // src/hooks/provider/useHubProvider.ts
@@ -276,10 +302,11 @@ function useMMAllowance(token, amount, action, spokeProvider) {
276
302
  queryKey: ["allowance", token.address, amount, action],
277
303
  queryFn: async () => {
278
304
  if (!spokeProvider) throw new Error("Spoke provider is required");
305
+ const actionBasedDecimals = action === "withdraw" || action === "borrow" ? 18 : token.decimals;
279
306
  const allowance = await sodax.moneyMarket.isAllowanceValid(
280
307
  {
281
308
  token: token.address,
282
- amount: viem.parseUnits(amount, token.decimals),
309
+ amount: viem.parseUnits(amount, actionBasedDecimals),
283
310
  action
284
311
  },
285
312
  spokeProvider
@@ -305,10 +332,11 @@ function useMMApprove(token, spokeProvider) {
305
332
  if (!spokeProvider) {
306
333
  throw new Error("Spoke provider not found");
307
334
  }
335
+ const actionBasedDecimals = action === "withdraw" || action === "borrow" ? 18 : token.decimals;
308
336
  const allowance = await sodax.moneyMarket.approve(
309
337
  {
310
338
  token: token.address,
311
- amount: viem.parseUnits(amount, token.decimals),
339
+ amount: viem.parseUnits(amount, actionBasedDecimals),
312
340
  action
313
341
  },
314
342
  spokeProvider
@@ -1033,6 +1061,188 @@ function useInstantUnstakeAllowance(params, spokeProvider) {
1033
1061
  // Refetch every 5 seconds
1034
1062
  });
1035
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
+ }
1036
1246
  var SodaxProvider = ({ children, testnet = false, config, rpcConfig }) => {
1037
1247
  const sodax = new sdk.Sodax(config);
1038
1248
  return /* @__PURE__ */ React__default.default.createElement(SodaxContext.Provider, { value: { sodax, testnet, rpcConfig } }, children);
@@ -1045,6 +1255,8 @@ var getSpokeTokenAddressByVault = (spokeChainId, vault) => {
1045
1255
  return address;
1046
1256
  };
1047
1257
 
1258
+ exports.MIGRATION_MODE_BNUSD = MIGRATION_MODE_BNUSD;
1259
+ exports.MIGRATION_MODE_ICX_SODA = MIGRATION_MODE_ICX_SODA;
1048
1260
  exports.SodaxProvider = SodaxProvider;
1049
1261
  exports.getSpokeTokenAddressByVault = getSpokeTokenAddressByVault;
1050
1262
  exports.useBackendAllMoneyMarketAssets = useBackendAllMoneyMarketAssets;
@@ -1075,6 +1287,9 @@ exports.useInstantUnstakeApprove = useInstantUnstakeApprove;
1075
1287
  exports.useInstantUnstakeRatio = useInstantUnstakeRatio;
1076
1288
  exports.useMMAllowance = useMMAllowance;
1077
1289
  exports.useMMApprove = useMMApprove;
1290
+ exports.useMigrate = useMigrate;
1291
+ exports.useMigrationAllowance = useMigrationAllowance;
1292
+ exports.useMigrationApprove = useMigrationApprove;
1078
1293
  exports.useQuote = useQuote;
1079
1294
  exports.useRepay = useRepay;
1080
1295
  exports.useRequestTrustline = useRequestTrustline;