@xswap-link/sdk 0.13.0 → 0.13.1

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
@@ -20933,6 +20933,7 @@ var SwapProvider = ({
20933
20933
  const [route, setRoute] = (0, import_react11.useState)(init2.route);
20934
20934
  const routeAbortController = (0, import_react11.useRef)(null);
20935
20935
  const refreshSelectedTokenBalanceNonce = (0, import_react11.useRef)(crypto.randomUUID());
20936
+ const prevWalletAddresses = (0, import_react11.useRef)(void 0);
20936
20937
  const srcTokenLocked = (0, import_react11.useMemo)(
20937
20938
  () => !!integrationConfig?.srcTokenLocked,
20938
20939
  [integrationConfig?.srcTokenLocked]
@@ -21189,66 +21190,81 @@ var SwapProvider = ({
21189
21190
  srcToken,
21190
21191
  srcValueWei
21191
21192
  ]);
21192
- const refreshBalance = (0, import_react11.useCallback)(async () => {
21193
- if (!evmAddress && !solanaAddress || !srcChain?.publicRpcUrls[0] || !srcToken || !srcToken?.address) {
21194
- return;
21195
- }
21196
- const nonce = crypto.randomUUID();
21197
- refreshSelectedTokenBalanceNonce.current = nonce;
21198
- setIsFetchingBalance(true);
21199
- let balance = "0";
21200
- if (srcChain?.ecosystem === "evm" /* EVM */ && evmAddress) {
21201
- const balanceFromBackend = tokenBalances[srcChain?.chainId]?.[srcToken.address];
21202
- if (balanceFromBackend === void 0) {
21203
- const balanceUI = await getBalanceOf(
21204
- evmAddress,
21205
- srcToken.address,
21206
- srcChain?.publicRpcUrls[0]
21207
- );
21208
- balance = humanReadableToWei({
21209
- amount: balanceUI,
21210
- decimals: srcToken.decimals
21211
- });
21212
- } else {
21213
- balance = balanceFromBackend;
21214
- }
21215
- } else if (srcChain?.ecosystem === "solana" /* SOLANA */ && solanaAddress) {
21216
- const solanaPublicKey = new import_web35.PublicKey(solanaAddress);
21217
- const connection = new import_web35.Connection(srcChain.publicRpcUrls[0]);
21218
- if (srcToken.address === SOLANA_NATIVE_TOKEN_ADDRESS.toBase58()) {
21219
- balance = new import_bignumber22.default(
21220
- await connection.getBalance(solanaPublicKey)
21221
- ).toString();
21222
- } else {
21223
- const tokenAccounts = await connection.getParsedTokenAccountsByOwner(
21224
- solanaPublicKey,
21225
- {
21226
- programId: SOLANA_TOKEN_PROGRAM_ID
21193
+ const refreshBalance = (0, import_react11.useCallback)(
21194
+ async (options) => {
21195
+ if (!evmAddress && !solanaAddress || !srcChain?.publicRpcUrls[0] || !srcToken || !srcToken?.address) {
21196
+ return;
21197
+ }
21198
+ const nonce = crypto.randomUUID();
21199
+ refreshSelectedTokenBalanceNonce.current = nonce;
21200
+ setIsFetchingBalance(true);
21201
+ let balance = "0";
21202
+ if (srcChain?.ecosystem === "evm" /* EVM */ && evmAddress) {
21203
+ const balanceFromBackend = tokenBalances[srcChain?.chainId]?.[srcToken.address];
21204
+ if (options?.forceRpc || balanceFromBackend === void 0) {
21205
+ const balanceUI = await getBalanceOf(
21206
+ evmAddress,
21207
+ srcToken.address,
21208
+ srcChain?.publicRpcUrls[0]
21209
+ );
21210
+ balance = humanReadableToWei({
21211
+ amount: balanceUI,
21212
+ decimals: srcToken.decimals
21213
+ });
21214
+ if (options?.forceRpc && balanceFromBackend !== void 0) {
21215
+ const chainId = srcChain.chainId;
21216
+ const tokenAddress = srcToken.address;
21217
+ const freshBalance = balance;
21218
+ setTokenBalances((prev) => ({
21219
+ ...prev,
21220
+ [chainId]: {
21221
+ ...prev[chainId],
21222
+ [tokenAddress]: freshBalance
21223
+ }
21224
+ }));
21227
21225
  }
21228
- );
21229
- const token2022Accounts = await connection.getParsedTokenAccountsByOwner(solanaPublicKey, {
21230
- programId: SOLANA_TOKEN_2022_PROGRAM_ID
21231
- });
21232
- const allTokenAccount = [
21233
- ...tokenAccounts.value,
21234
- ...token2022Accounts.value
21235
- ];
21236
- const tokenAccount = allTokenAccount.find(
21237
- (account) => account.account.data.parsed.info.mint === srcToken.address
21238
- );
21239
- if (!tokenAccount) {
21240
- balance = "0";
21241
21226
  } else {
21242
- balance = tokenAccount.account.data.parsed.info.tokenAmount.amount;
21227
+ balance = balanceFromBackend;
21228
+ }
21229
+ } else if (srcChain?.ecosystem === "solana" /* SOLANA */ && solanaAddress) {
21230
+ const solanaPublicKey = new import_web35.PublicKey(solanaAddress);
21231
+ const connection = new import_web35.Connection(srcChain.publicRpcUrls[0]);
21232
+ if (srcToken.address === SOLANA_NATIVE_TOKEN_ADDRESS.toBase58()) {
21233
+ balance = new import_bignumber22.default(
21234
+ await connection.getBalance(solanaPublicKey)
21235
+ ).toString();
21236
+ } else {
21237
+ const tokenAccounts = await connection.getParsedTokenAccountsByOwner(
21238
+ solanaPublicKey,
21239
+ {
21240
+ programId: SOLANA_TOKEN_PROGRAM_ID
21241
+ }
21242
+ );
21243
+ const token2022Accounts = await connection.getParsedTokenAccountsByOwner(solanaPublicKey, {
21244
+ programId: SOLANA_TOKEN_2022_PROGRAM_ID
21245
+ });
21246
+ const allTokenAccount = [
21247
+ ...tokenAccounts.value,
21248
+ ...token2022Accounts.value
21249
+ ];
21250
+ const tokenAccount = allTokenAccount.find(
21251
+ (account) => account.account.data.parsed.info.mint === srcToken.address
21252
+ );
21253
+ if (!tokenAccount) {
21254
+ balance = "0";
21255
+ } else {
21256
+ balance = tokenAccount.account.data.parsed.info.tokenAmount.amount;
21257
+ }
21243
21258
  }
21244
21259
  }
21245
- }
21246
- if (nonce !== refreshSelectedTokenBalanceNonce.current) {
21247
- return;
21248
- }
21249
- setSrcTokenBalanceWei(balance);
21250
- setIsFetchingBalance(false);
21251
- }, [evmAddress, solanaAddress, srcChain, srcToken, tokenBalances]);
21260
+ if (nonce !== refreshSelectedTokenBalanceNonce.current) {
21261
+ return;
21262
+ }
21263
+ setSrcTokenBalanceWei(balance);
21264
+ setIsFetchingBalance(false);
21265
+ },
21266
+ [evmAddress, solanaAddress, srcChain, srcToken, tokenBalances]
21267
+ );
21252
21268
  const findTokenDataByAddressAndChain = (0, import_react11.useCallback)(
21253
21269
  (tokenAddress, chainId) => {
21254
21270
  return supportedTokens[chainId]?.[tokenAddress];
@@ -21293,8 +21309,11 @@ var SwapProvider = ({
21293
21309
  quoteRoute();
21294
21310
  }, [quoteRoute]);
21295
21311
  (0, import_react11.useEffect)(() => {
21296
- refreshBalance();
21297
- }, [refreshBalance]);
21312
+ const walletAddresses = `${evmAddress ?? ""}:${solanaAddress ?? ""}`;
21313
+ const walletChanged = prevWalletAddresses.current !== void 0 && prevWalletAddresses.current !== walletAddresses;
21314
+ prevWalletAddresses.current = walletAddresses;
21315
+ refreshBalance(walletChanged ? { forceRpc: true } : void 0);
21316
+ }, [refreshBalance, evmAddress, solanaAddress]);
21298
21317
  (0, import_react11.useEffect)(() => {
21299
21318
  let isCancelled = false;
21300
21319
  if ((evmAddress || solanaAddress) && supportedChains.length > 0) {
@@ -24618,7 +24637,7 @@ var TxOverview = ({ onCloseClick }) => {
24618
24637
  handlers: {
24619
24638
  onSuccess: (data) => {
24620
24639
  setTxStatus(5 /* COMPLETED */);
24621
- refreshBalance();
24640
+ refreshBalance({ forceRpc: true });
24622
24641
  trackTransaction(data);
24623
24642
  if (srcChain?.chainId && dstChain?.chainId && srcChain?.chainId !== dstChain?.chainId) {
24624
24643
  renderTxStatus(
@@ -24762,7 +24781,7 @@ var TxOverview = ({ onCloseClick }) => {
24762
24781
  handlers: {
24763
24782
  onSuccess: () => {
24764
24783
  setTxStatus(5 /* COMPLETED */);
24765
- refreshBalance();
24784
+ refreshBalance({ forceRpc: true });
24766
24785
  }
24767
24786
  },
24768
24787
  network: "solana" /* SOLANA */