tiwiflix-wallet-connector 1.6.3 → 1.6.5

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
@@ -8419,6 +8419,30 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8419
8419
  }
8420
8420
  return null;
8421
8421
  }, [connector]);
8422
+ const prevStatusRef = useRef(null);
8423
+ // Listen for connector status changes and fetch TWC balance after WalletConnect connects
8424
+ useEffect(() => {
8425
+ const status = connector?.getState?.().status;
8426
+ const account = connector?.getState?.().account;
8427
+ const chainId = connector?.getState?.().chainId;
8428
+ // Only trigger on transition to CONNECTED
8429
+ if (status === 'connected' && prevStatusRef.current !== 'connected' && account && account.chainType === 'evm' && chainId === 56) {
8430
+ // Fetch TWC balance immediately after connection
8431
+ const cacheKey = `tiwiflix_twc_balance_cache_${account.address}`;
8432
+ fetchTWCBalanceWithRetry(2, 200).then((result) => {
8433
+ if (result && result.balance !== '0' && result.balance !== '0.00') {
8434
+ setTwcBalance(result.balance);
8435
+ setUsdValueStable(result.usdValue);
8436
+ if (account.address) {
8437
+ localStorage.setItem(cacheKey, JSON.stringify({ balance: result.balance, usdValue: result.usdValue, timestamp: Date.now() }));
8438
+ }
8439
+ setIsInitializing(false);
8440
+ console.log('[ConnectButton] TWC balance fetched after WalletConnect connect:', result);
8441
+ }
8442
+ });
8443
+ }
8444
+ prevStatusRef.current = status;
8445
+ }, [connector, fetchTWCBalanceWithRetry]);
8422
8446
  // Load balance when account or network (chain) changes
8423
8447
  useEffect(() => {
8424
8448
  let isActive = true;
@@ -8427,42 +8451,59 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8427
8451
  console.log('[ConnectButton] useEffect: account or chain changed', { account, currentChainId });
8428
8452
  // For EVM wallets, only fetch TWC balance if on BSC
8429
8453
  if (account.chainType === 'evm' && currentChainId === 56) {
8430
- // Check cache first (30s TTL)
8431
- const cache = loadTWCBalanceFromCache(account.address);
8432
- const cacheValid = cache && typeof cache.balance === 'string' && cache.balance !== '0' && cache.balance !== '0.00' && cache.usdValue !== undefined && (() => {
8433
- const cacheKey = `tiwiflix_twc_balance_cache_${account.address}`;
8454
+ // Always check localStorage for cache (30s TTL)
8455
+ const cacheKey = `tiwiflix_twc_balance_cache_${account.address}`;
8456
+ let cache = null;
8457
+ try {
8434
8458
  const raw = localStorage.getItem(cacheKey);
8435
- if (!raw)
8436
- return false;
8437
- try {
8459
+ if (raw) {
8438
8460
  const parsed = JSON.parse(raw);
8439
- return parsed.timestamp && (Date.now() - parsed.timestamp < 30000);
8440
- }
8441
- catch {
8442
- return false;
8461
+ if (parsed && parsed.balance && parsed.timestamp && (Date.now() - parsed.timestamp < 30000)) {
8462
+ cache = parsed;
8463
+ }
8443
8464
  }
8444
- })();
8445
- if (cacheValid) {
8465
+ }
8466
+ catch { }
8467
+ if (cache && cache.balance !== '0' && cache.balance !== '0.00') {
8446
8468
  setTwcBalance(cache.balance);
8447
8469
  setUsdValueStable(cache.usdValue);
8448
8470
  setIsInitializing(false);
8449
- console.log('[ConnectButton] Using cached TWC balance:', cache);
8471
+ console.log('[ConnectButton] Using localStorage cached TWC balance:', cache);
8472
+ // Fetch in background to update cache, but don't reset UI while fetching
8473
+ fetchTWCBalanceWithRetry(2, 200).then((result) => {
8474
+ if (!isActive)
8475
+ return;
8476
+ if (result && result.balance !== '0' && result.balance !== '0.00' && result.balance !== cache?.balance) {
8477
+ setTwcBalance(result.balance);
8478
+ setUsdValueStable(result.usdValue);
8479
+ if (account.address) {
8480
+ // Save to localStorage with timestamp
8481
+ localStorage.setItem(cacheKey, JSON.stringify({ balance: result.balance, usdValue: result.usdValue, timestamp: Date.now() }));
8482
+ }
8483
+ setIsInitializing(false);
8484
+ console.log('[ConnectButton] TWC balance updated after background fetch:', result);
8485
+ }
8486
+ }).catch(() => {
8487
+ // Ignore errors in background fetch
8488
+ });
8450
8489
  }
8451
8490
  else {
8491
+ // No valid cache, show loading state until fetch completes
8492
+ setIsInitializing(true);
8452
8493
  fetchTWCBalanceWithRetry(2, 200).then((result) => {
8453
8494
  if (!isActive)
8454
8495
  return;
8455
- if (result) {
8496
+ if (result && result.balance !== '0' && result.balance !== '0.00') {
8456
8497
  setTwcBalance(result.balance);
8457
8498
  setUsdValueStable(result.usdValue);
8458
8499
  if (account.address) {
8459
- saveTWCBalanceToCache(account.address, result.balance, result.usdValue);
8500
+ // Save to localStorage with timestamp
8501
+ localStorage.setItem(cacheKey, JSON.stringify({ balance: result.balance, usdValue: result.usdValue, timestamp: Date.now() }));
8460
8502
  }
8461
8503
  setIsInitializing(false);
8462
- console.log('[ConnectButton] TWC balance fetched:', result);
8504
+ console.log('[ConnectButton] TWC balance fetched and cached:', result);
8463
8505
  }
8464
8506
  else {
8465
- // Final fallback
8466
8507
  setTwcBalance('0');
8467
8508
  setUsdValueStable(null);
8468
8509
  setIsInitializing(false);
@@ -8490,7 +8531,8 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8490
8531
  setTwcBalance(state.twcBalance);
8491
8532
  setUsdValueStable(state.usdValue ?? null);
8492
8533
  if (account.address) {
8493
- saveTWCBalanceToCache(account.address, state.twcBalance, state.usdValue ?? null);
8534
+ // Save to localStorage for consistency
8535
+ localStorage.setItem(`tiwiflix_twc_balance_cache_${account.address}`, JSON.stringify({ balance: state.twcBalance, usdValue: state.usdValue ?? null, timestamp: Date.now() }));
8494
8536
  }
8495
8537
  setIsInitializing(false);
8496
8538
  }
package/dist/index.js CHANGED
@@ -8421,6 +8421,30 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8421
8421
  }
8422
8422
  return null;
8423
8423
  }, [connector]);
8424
+ const prevStatusRef = React.useRef(null);
8425
+ // Listen for connector status changes and fetch TWC balance after WalletConnect connects
8426
+ React.useEffect(() => {
8427
+ const status = connector?.getState?.().status;
8428
+ const account = connector?.getState?.().account;
8429
+ const chainId = connector?.getState?.().chainId;
8430
+ // Only trigger on transition to CONNECTED
8431
+ if (status === 'connected' && prevStatusRef.current !== 'connected' && account && account.chainType === 'evm' && chainId === 56) {
8432
+ // Fetch TWC balance immediately after connection
8433
+ const cacheKey = `tiwiflix_twc_balance_cache_${account.address}`;
8434
+ fetchTWCBalanceWithRetry(2, 200).then((result) => {
8435
+ if (result && result.balance !== '0' && result.balance !== '0.00') {
8436
+ setTwcBalance(result.balance);
8437
+ setUsdValueStable(result.usdValue);
8438
+ if (account.address) {
8439
+ localStorage.setItem(cacheKey, JSON.stringify({ balance: result.balance, usdValue: result.usdValue, timestamp: Date.now() }));
8440
+ }
8441
+ setIsInitializing(false);
8442
+ console.log('[ConnectButton] TWC balance fetched after WalletConnect connect:', result);
8443
+ }
8444
+ });
8445
+ }
8446
+ prevStatusRef.current = status;
8447
+ }, [connector, fetchTWCBalanceWithRetry]);
8424
8448
  // Load balance when account or network (chain) changes
8425
8449
  React.useEffect(() => {
8426
8450
  let isActive = true;
@@ -8429,42 +8453,59 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8429
8453
  console.log('[ConnectButton] useEffect: account or chain changed', { account, currentChainId });
8430
8454
  // For EVM wallets, only fetch TWC balance if on BSC
8431
8455
  if (account.chainType === 'evm' && currentChainId === 56) {
8432
- // Check cache first (30s TTL)
8433
- const cache = loadTWCBalanceFromCache(account.address);
8434
- const cacheValid = cache && typeof cache.balance === 'string' && cache.balance !== '0' && cache.balance !== '0.00' && cache.usdValue !== undefined && (() => {
8435
- const cacheKey = `tiwiflix_twc_balance_cache_${account.address}`;
8456
+ // Always check localStorage for cache (30s TTL)
8457
+ const cacheKey = `tiwiflix_twc_balance_cache_${account.address}`;
8458
+ let cache = null;
8459
+ try {
8436
8460
  const raw = localStorage.getItem(cacheKey);
8437
- if (!raw)
8438
- return false;
8439
- try {
8461
+ if (raw) {
8440
8462
  const parsed = JSON.parse(raw);
8441
- return parsed.timestamp && (Date.now() - parsed.timestamp < 30000);
8442
- }
8443
- catch {
8444
- return false;
8463
+ if (parsed && parsed.balance && parsed.timestamp && (Date.now() - parsed.timestamp < 30000)) {
8464
+ cache = parsed;
8465
+ }
8445
8466
  }
8446
- })();
8447
- if (cacheValid) {
8467
+ }
8468
+ catch { }
8469
+ if (cache && cache.balance !== '0' && cache.balance !== '0.00') {
8448
8470
  setTwcBalance(cache.balance);
8449
8471
  setUsdValueStable(cache.usdValue);
8450
8472
  setIsInitializing(false);
8451
- console.log('[ConnectButton] Using cached TWC balance:', cache);
8473
+ console.log('[ConnectButton] Using localStorage cached TWC balance:', cache);
8474
+ // Fetch in background to update cache, but don't reset UI while fetching
8475
+ fetchTWCBalanceWithRetry(2, 200).then((result) => {
8476
+ if (!isActive)
8477
+ return;
8478
+ if (result && result.balance !== '0' && result.balance !== '0.00' && result.balance !== cache?.balance) {
8479
+ setTwcBalance(result.balance);
8480
+ setUsdValueStable(result.usdValue);
8481
+ if (account.address) {
8482
+ // Save to localStorage with timestamp
8483
+ localStorage.setItem(cacheKey, JSON.stringify({ balance: result.balance, usdValue: result.usdValue, timestamp: Date.now() }));
8484
+ }
8485
+ setIsInitializing(false);
8486
+ console.log('[ConnectButton] TWC balance updated after background fetch:', result);
8487
+ }
8488
+ }).catch(() => {
8489
+ // Ignore errors in background fetch
8490
+ });
8452
8491
  }
8453
8492
  else {
8493
+ // No valid cache, show loading state until fetch completes
8494
+ setIsInitializing(true);
8454
8495
  fetchTWCBalanceWithRetry(2, 200).then((result) => {
8455
8496
  if (!isActive)
8456
8497
  return;
8457
- if (result) {
8498
+ if (result && result.balance !== '0' && result.balance !== '0.00') {
8458
8499
  setTwcBalance(result.balance);
8459
8500
  setUsdValueStable(result.usdValue);
8460
8501
  if (account.address) {
8461
- saveTWCBalanceToCache(account.address, result.balance, result.usdValue);
8502
+ // Save to localStorage with timestamp
8503
+ localStorage.setItem(cacheKey, JSON.stringify({ balance: result.balance, usdValue: result.usdValue, timestamp: Date.now() }));
8462
8504
  }
8463
8505
  setIsInitializing(false);
8464
- console.log('[ConnectButton] TWC balance fetched:', result);
8506
+ console.log('[ConnectButton] TWC balance fetched and cached:', result);
8465
8507
  }
8466
8508
  else {
8467
- // Final fallback
8468
8509
  setTwcBalance('0');
8469
8510
  setUsdValueStable(null);
8470
8511
  setIsInitializing(false);
@@ -8492,7 +8533,8 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
8492
8533
  setTwcBalance(state.twcBalance);
8493
8534
  setUsdValueStable(state.usdValue ?? null);
8494
8535
  if (account.address) {
8495
- saveTWCBalanceToCache(account.address, state.twcBalance, state.usdValue ?? null);
8536
+ // Save to localStorage for consistency
8537
+ localStorage.setItem(`tiwiflix_twc_balance_cache_${account.address}`, JSON.stringify({ balance: state.twcBalance, usdValue: state.usdValue ?? null, timestamp: Date.now() }));
8496
8538
  }
8497
8539
  setIsInitializing(false);
8498
8540
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiwiflix-wallet-connector",
3
- "version": "1.6.3",
3
+ "version": "1.6.5",
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",