tiwiflix-wallet-connector 1.6.2 → 1.6.4

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.esm.js CHANGED
@@ -7517,6 +7517,31 @@ const TWCBalanceModal = ({ isOpen, onClose, twcBalance, usdValue, colors, isDark
7517
7517
  }, children: "Learn More" })] })] }) }));
7518
7518
  };
7519
7519
 
7520
+ // Track previous connection status to detect new connections
7521
+ const prevStatusRef = useRef(null);
7522
+ // Listen for connector status changes and fetch TWC balance after WalletConnect connects
7523
+ useEffect(() => {
7524
+ const status = connector?.getState?.().status;
7525
+ const account = connector?.getState?.().account;
7526
+ const chainId = connector?.getState?.().chainId;
7527
+ // Only trigger on transition to CONNECTED
7528
+ if (status === 'connected' && prevStatusRef.current !== 'connected' && account && account.chainType === 'evm' && chainId === 56) {
7529
+ // Fetch TWC balance immediately after connection
7530
+ const cacheKey = `tiwiflix_twc_balance_cache_${account.address}`;
7531
+ fetchTWCBalanceWithRetry(2, 200).then((result) => {
7532
+ if (result && result.balance !== '0' && result.balance !== '0.00') {
7533
+ setTwcBalance(result.balance);
7534
+ setUsdValueStable(result.usdValue);
7535
+ if (account.address) {
7536
+ localStorage.setItem(cacheKey, JSON.stringify({ balance: result.balance, usdValue: result.usdValue, timestamp: Date.now() }));
7537
+ }
7538
+ setIsInitializing(false);
7539
+ console.log('[ConnectButton] TWC balance fetched after WalletConnect connect:', result);
7540
+ }
7541
+ });
7542
+ }
7543
+ prevStatusRef.current = status;
7544
+ }, [connector, fetchTWCBalanceWithRetry]);
7520
7545
  const tonApiCache = new Map();
7521
7546
  const CACHE_DURATION = 30000; // 30 seconds
7522
7547
  const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className, style, showBalance = false, modalPosition = 'center', theme = 'auto', buttonText = 'Connect Wallet', getExplorerUrl, }) => {
@@ -8404,6 +8429,8 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8404
8429
  }
8405
8430
  catch (error) {
8406
8431
  const err = error instanceof Error ? error : new Error(String(error));
8432
+ // Log error for debugging WalletConnect issues
8433
+ console.error('[ConnectButton] Error fetching TWC balance:', err);
8407
8434
  // Don't retry on certain errors
8408
8435
  if (err.message.includes('No wallet connected') ||
8409
8436
  err.message.includes('No EVM adapter')) {
@@ -8425,45 +8452,59 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8425
8452
  console.log('[ConnectButton] useEffect: account or chain changed', { account, currentChainId });
8426
8453
  // For EVM wallets, only fetch TWC balance if on BSC
8427
8454
  if (account.chainType === 'evm' && currentChainId === 56) {
8428
- // First check state/cache for immediate display
8429
- const state = connector.getState();
8430
- let hasBalance = false;
8431
- if (state.twcBalance && state.twcBalance !== '0' && state.twcBalance !== '0.00') {
8432
- setTwcBalance(state.twcBalance);
8433
- setUsdValueStable(state.usdValue ?? null);
8434
- if (account.address) {
8435
- saveTWCBalanceToCache(account.address, state.twcBalance, state.usdValue ?? null);
8455
+ // Always check localStorage for cache (30s TTL)
8456
+ const cacheKey = `tiwiflix_twc_balance_cache_${account.address}`;
8457
+ let cache = null;
8458
+ try {
8459
+ const raw = localStorage.getItem(cacheKey);
8460
+ if (raw) {
8461
+ const parsed = JSON.parse(raw);
8462
+ if (parsed && parsed.balance && parsed.timestamp && (Date.now() - parsed.timestamp < 30000)) {
8463
+ cache = parsed;
8464
+ }
8436
8465
  }
8437
- setIsInitializing(false);
8438
- hasBalance = true;
8439
8466
  }
8440
- else if (account.address) {
8441
- // Try cache first
8442
- const cached = loadTWCBalanceFromCache(account.address);
8443
- if (cached?.balance && cached.balance !== '0' && cached.balance !== '0.00') {
8444
- setTwcBalance(cached.balance);
8445
- setUsdValueStable(cached.usdValue);
8446
- setIsInitializing(false);
8447
- hasBalance = true;
8448
- }
8467
+ catch { }
8468
+ if (cache && cache.balance !== '0' && cache.balance !== '0.00') {
8469
+ setTwcBalance(cache.balance);
8470
+ setUsdValueStable(cache.usdValue);
8471
+ setIsInitializing(false);
8472
+ console.log('[ConnectButton] Using localStorage cached TWC balance:', cache);
8473
+ // Fetch in background to update cache, but don't reset UI while fetching
8474
+ fetchTWCBalanceWithRetry(2, 200).then((result) => {
8475
+ if (!isActive)
8476
+ return;
8477
+ if (result && result.balance !== '0' && result.balance !== '0.00' && result.balance !== cache?.balance) {
8478
+ setTwcBalance(result.balance);
8479
+ setUsdValueStable(result.usdValue);
8480
+ if (account.address) {
8481
+ // Save to localStorage with timestamp
8482
+ localStorage.setItem(cacheKey, JSON.stringify({ balance: result.balance, usdValue: result.usdValue, timestamp: Date.now() }));
8483
+ }
8484
+ setIsInitializing(false);
8485
+ console.log('[ConnectButton] TWC balance updated after background fetch:', result);
8486
+ }
8487
+ }).catch(() => {
8488
+ // Ignore errors in background fetch
8489
+ });
8449
8490
  }
8450
- // Only fetch if we don't have a balance yet, or if the chain/network changed
8451
- if (!hasBalance || currentChainId !== null) {
8452
- console.log('[ConnectButton] Fetching TWC balance for BSC wallet:', account.address, currentChainId);
8491
+ else {
8492
+ // No valid cache, show loading state until fetch completes
8493
+ setIsInitializing(true);
8453
8494
  fetchTWCBalanceWithRetry(2, 200).then((result) => {
8454
8495
  if (!isActive)
8455
8496
  return;
8456
- if (result) {
8497
+ if (result && result.balance !== '0' && result.balance !== '0.00') {
8457
8498
  setTwcBalance(result.balance);
8458
8499
  setUsdValueStable(result.usdValue);
8459
8500
  if (account.address) {
8460
- saveTWCBalanceToCache(account.address, result.balance, result.usdValue);
8501
+ // Save to localStorage with timestamp
8502
+ localStorage.setItem(cacheKey, JSON.stringify({ balance: result.balance, usdValue: result.usdValue, timestamp: Date.now() }));
8461
8503
  }
8462
8504
  setIsInitializing(false);
8463
- console.log('[ConnectButton] TWC balance fetched:', result);
8505
+ console.log('[ConnectButton] TWC balance fetched and cached:', result);
8464
8506
  }
8465
8507
  else {
8466
- // Final fallback
8467
8508
  setTwcBalance('0');
8468
8509
  setUsdValueStable(null);
8469
8510
  setIsInitializing(false);
@@ -8491,7 +8532,8 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8491
8532
  setTwcBalance(state.twcBalance);
8492
8533
  setUsdValueStable(state.usdValue ?? null);
8493
8534
  if (account.address) {
8494
- saveTWCBalanceToCache(account.address, state.twcBalance, state.usdValue ?? null);
8535
+ // Save to localStorage for consistency
8536
+ localStorage.setItem(`tiwiflix_twc_balance_cache_${account.address}`, JSON.stringify({ balance: state.twcBalance, usdValue: state.usdValue ?? null, timestamp: Date.now() }));
8495
8537
  }
8496
8538
  setIsInitializing(false);
8497
8539
  }
package/dist/index.js CHANGED
@@ -7519,6 +7519,31 @@ const TWCBalanceModal = ({ isOpen, onClose, twcBalance, usdValue, colors, isDark
7519
7519
  }, children: "Learn More" })] })] }) }));
7520
7520
  };
7521
7521
 
7522
+ // Track previous connection status to detect new connections
7523
+ const prevStatusRef = React.useRef(null);
7524
+ // Listen for connector status changes and fetch TWC balance after WalletConnect connects
7525
+ React.useEffect(() => {
7526
+ const status = connector?.getState?.().status;
7527
+ const account = connector?.getState?.().account;
7528
+ const chainId = connector?.getState?.().chainId;
7529
+ // Only trigger on transition to CONNECTED
7530
+ if (status === 'connected' && prevStatusRef.current !== 'connected' && account && account.chainType === 'evm' && chainId === 56) {
7531
+ // Fetch TWC balance immediately after connection
7532
+ const cacheKey = `tiwiflix_twc_balance_cache_${account.address}`;
7533
+ fetchTWCBalanceWithRetry(2, 200).then((result) => {
7534
+ if (result && result.balance !== '0' && result.balance !== '0.00') {
7535
+ setTwcBalance(result.balance);
7536
+ setUsdValueStable(result.usdValue);
7537
+ if (account.address) {
7538
+ localStorage.setItem(cacheKey, JSON.stringify({ balance: result.balance, usdValue: result.usdValue, timestamp: Date.now() }));
7539
+ }
7540
+ setIsInitializing(false);
7541
+ console.log('[ConnectButton] TWC balance fetched after WalletConnect connect:', result);
7542
+ }
7543
+ });
7544
+ }
7545
+ prevStatusRef.current = status;
7546
+ }, [connector, fetchTWCBalanceWithRetry]);
7522
7547
  const tonApiCache = new Map();
7523
7548
  const CACHE_DURATION = 30000; // 30 seconds
7524
7549
  const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className, style, showBalance = false, modalPosition = 'center', theme = 'auto', buttonText = 'Connect Wallet', getExplorerUrl, }) => {
@@ -8406,6 +8431,8 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8406
8431
  }
8407
8432
  catch (error) {
8408
8433
  const err = error instanceof Error ? error : new Error(String(error));
8434
+ // Log error for debugging WalletConnect issues
8435
+ console.error('[ConnectButton] Error fetching TWC balance:', err);
8409
8436
  // Don't retry on certain errors
8410
8437
  if (err.message.includes('No wallet connected') ||
8411
8438
  err.message.includes('No EVM adapter')) {
@@ -8427,45 +8454,59 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8427
8454
  console.log('[ConnectButton] useEffect: account or chain changed', { account, currentChainId });
8428
8455
  // For EVM wallets, only fetch TWC balance if on BSC
8429
8456
  if (account.chainType === 'evm' && currentChainId === 56) {
8430
- // First check state/cache for immediate display
8431
- const state = connector.getState();
8432
- let hasBalance = false;
8433
- if (state.twcBalance && state.twcBalance !== '0' && state.twcBalance !== '0.00') {
8434
- setTwcBalance(state.twcBalance);
8435
- setUsdValueStable(state.usdValue ?? null);
8436
- if (account.address) {
8437
- saveTWCBalanceToCache(account.address, state.twcBalance, state.usdValue ?? null);
8457
+ // Always check localStorage for cache (30s TTL)
8458
+ const cacheKey = `tiwiflix_twc_balance_cache_${account.address}`;
8459
+ let cache = null;
8460
+ try {
8461
+ const raw = localStorage.getItem(cacheKey);
8462
+ if (raw) {
8463
+ const parsed = JSON.parse(raw);
8464
+ if (parsed && parsed.balance && parsed.timestamp && (Date.now() - parsed.timestamp < 30000)) {
8465
+ cache = parsed;
8466
+ }
8438
8467
  }
8439
- setIsInitializing(false);
8440
- hasBalance = true;
8441
8468
  }
8442
- else if (account.address) {
8443
- // Try cache first
8444
- const cached = loadTWCBalanceFromCache(account.address);
8445
- if (cached?.balance && cached.balance !== '0' && cached.balance !== '0.00') {
8446
- setTwcBalance(cached.balance);
8447
- setUsdValueStable(cached.usdValue);
8448
- setIsInitializing(false);
8449
- hasBalance = true;
8450
- }
8469
+ catch { }
8470
+ if (cache && cache.balance !== '0' && cache.balance !== '0.00') {
8471
+ setTwcBalance(cache.balance);
8472
+ setUsdValueStable(cache.usdValue);
8473
+ setIsInitializing(false);
8474
+ console.log('[ConnectButton] Using localStorage cached TWC balance:', cache);
8475
+ // Fetch in background to update cache, but don't reset UI while fetching
8476
+ fetchTWCBalanceWithRetry(2, 200).then((result) => {
8477
+ if (!isActive)
8478
+ return;
8479
+ if (result && result.balance !== '0' && result.balance !== '0.00' && result.balance !== cache?.balance) {
8480
+ setTwcBalance(result.balance);
8481
+ setUsdValueStable(result.usdValue);
8482
+ if (account.address) {
8483
+ // Save to localStorage with timestamp
8484
+ localStorage.setItem(cacheKey, JSON.stringify({ balance: result.balance, usdValue: result.usdValue, timestamp: Date.now() }));
8485
+ }
8486
+ setIsInitializing(false);
8487
+ console.log('[ConnectButton] TWC balance updated after background fetch:', result);
8488
+ }
8489
+ }).catch(() => {
8490
+ // Ignore errors in background fetch
8491
+ });
8451
8492
  }
8452
- // Only fetch if we don't have a balance yet, or if the chain/network changed
8453
- if (!hasBalance || currentChainId !== null) {
8454
- console.log('[ConnectButton] Fetching TWC balance for BSC wallet:', account.address, currentChainId);
8493
+ else {
8494
+ // No valid cache, show loading state until fetch completes
8495
+ setIsInitializing(true);
8455
8496
  fetchTWCBalanceWithRetry(2, 200).then((result) => {
8456
8497
  if (!isActive)
8457
8498
  return;
8458
- if (result) {
8499
+ if (result && result.balance !== '0' && result.balance !== '0.00') {
8459
8500
  setTwcBalance(result.balance);
8460
8501
  setUsdValueStable(result.usdValue);
8461
8502
  if (account.address) {
8462
- saveTWCBalanceToCache(account.address, result.balance, result.usdValue);
8503
+ // Save to localStorage with timestamp
8504
+ localStorage.setItem(cacheKey, JSON.stringify({ balance: result.balance, usdValue: result.usdValue, timestamp: Date.now() }));
8463
8505
  }
8464
8506
  setIsInitializing(false);
8465
- console.log('[ConnectButton] TWC balance fetched:', result);
8507
+ console.log('[ConnectButton] TWC balance fetched and cached:', result);
8466
8508
  }
8467
8509
  else {
8468
- // Final fallback
8469
8510
  setTwcBalance('0');
8470
8511
  setUsdValueStable(null);
8471
8512
  setIsInitializing(false);
@@ -8493,7 +8534,8 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8493
8534
  setTwcBalance(state.twcBalance);
8494
8535
  setUsdValueStable(state.usdValue ?? null);
8495
8536
  if (account.address) {
8496
- saveTWCBalanceToCache(account.address, state.twcBalance, state.usdValue ?? null);
8537
+ // Save to localStorage for consistency
8538
+ localStorage.setItem(`tiwiflix_twc_balance_cache_${account.address}`, JSON.stringify({ balance: state.twcBalance, usdValue: state.usdValue ?? null, timestamp: Date.now() }));
8497
8539
  }
8498
8540
  setIsInitializing(false);
8499
8541
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiwiflix-wallet-connector",
3
- "version": "1.6.2",
3
+ "version": "1.6.4",
4
4
  "description": "Multi-chain wallet connector for Tiwiflix supporting EVM, TON, Solana, and Tron wallets",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",