tiwiflix-wallet-connector 1.5.7 → 1.5.8
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.d.ts +0 -20
- package/dist/index.esm.js +38 -133
- package/dist/index.js +38 -133
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1170,27 +1170,7 @@ interface ConnectButtonProps {
|
|
|
1170
1170
|
modalPosition?: 'top' | 'bottom' | 'center';
|
|
1171
1171
|
theme?: 'light' | 'dark' | 'auto';
|
|
1172
1172
|
buttonText?: string;
|
|
1173
|
-
fetchTransactions?: (account: Account) => Promise<Array<{
|
|
1174
|
-
hash: string;
|
|
1175
|
-
date?: string;
|
|
1176
|
-
amount?: string;
|
|
1177
|
-
to?: string;
|
|
1178
|
-
from?: string;
|
|
1179
|
-
url?: string;
|
|
1180
|
-
symbol?: string;
|
|
1181
|
-
status?: string;
|
|
1182
|
-
direction?: string;
|
|
1183
|
-
type?: string;
|
|
1184
|
-
usd?: number;
|
|
1185
|
-
usdValue?: number;
|
|
1186
|
-
}>>;
|
|
1187
1173
|
getExplorerUrl?: (account: Account) => string;
|
|
1188
|
-
fetchBalance?: (account: Account) => Promise<{
|
|
1189
|
-
amount: string;
|
|
1190
|
-
symbol?: string;
|
|
1191
|
-
usdValue?: number;
|
|
1192
|
-
usdFormatted?: string;
|
|
1193
|
-
}>;
|
|
1194
1174
|
}
|
|
1195
1175
|
declare const ConnectButton: React.FC<ConnectButtonProps>;
|
|
1196
1176
|
|
package/dist/index.esm.js
CHANGED
|
@@ -7530,19 +7530,9 @@ const defaultStyles = {
|
|
|
7530
7530
|
fontFamily: 'Lexend, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
|
|
7531
7531
|
boxShadow: '0 4px 14px 0 rgba(51, 150, 255, 0.39)',
|
|
7532
7532
|
}};
|
|
7533
|
-
const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className, style, showBalance = false, modalPosition = 'center', theme = 'auto', buttonText = 'Connect Wallet Now',
|
|
7534
|
-
//
|
|
7535
|
-
|
|
7536
|
-
// Default: return zero balance with symbol based on chain
|
|
7537
|
-
let symbol = 'TWC';
|
|
7538
|
-
if (account?.chainType === 'ton')
|
|
7539
|
-
symbol = 'TON';
|
|
7540
|
-
if (account?.chainType === 'solana')
|
|
7541
|
-
symbol = 'SOL';
|
|
7542
|
-
if (account?.chainType === 'tron')
|
|
7543
|
-
symbol = 'TRX';
|
|
7544
|
-
return { amount: '0', symbol };
|
|
7545
|
-
});
|
|
7533
|
+
const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className, style, showBalance = false, modalPosition = 'center', theme = 'auto', buttonText = 'Connect Wallet Now', getExplorerUrl, }) => {
|
|
7534
|
+
// Always fetch balance automatically when wallet is connected
|
|
7535
|
+
// Balance is always fetched automatically from the chain
|
|
7546
7536
|
// Detect dark mode
|
|
7547
7537
|
const [isDarkMode, setIsDarkMode] = useState(false);
|
|
7548
7538
|
useEffect(() => {
|
|
@@ -7715,9 +7705,10 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
7715
7705
|
React.useRef(null);
|
|
7716
7706
|
// Get theme colors
|
|
7717
7707
|
const colors = getThemeColors(isDarkMode);
|
|
7708
|
+
console.log('above loadbalance');
|
|
7718
7709
|
// Load balance with caching
|
|
7719
7710
|
const loadBalance = useCallback(async () => {
|
|
7720
|
-
if (!account
|
|
7711
|
+
if (!account) {
|
|
7721
7712
|
setBalance({ amount: '', symbol: '' });
|
|
7722
7713
|
setBalanceLoading(false);
|
|
7723
7714
|
return false;
|
|
@@ -7741,42 +7732,27 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
7741
7732
|
}
|
|
7742
7733
|
try {
|
|
7743
7734
|
setBalanceLoading(true);
|
|
7744
|
-
|
|
7745
|
-
|
|
7746
|
-
//
|
|
7747
|
-
|
|
7748
|
-
|
|
7749
|
-
|
|
7750
|
-
|
|
7751
|
-
if (!balanceData.usdFormatted && balanceData.amount) {
|
|
7752
|
-
try {
|
|
7753
|
-
const resp = await fetch('https://api.coingecko.com/api/v3/simple/price?ids=the-open-network&vs_currencies=usd');
|
|
7754
|
-
const json = await resp.json();
|
|
7755
|
-
const price = json?.['the-open-network']?.usd;
|
|
7756
|
-
const amt = parseFloat(String(balanceData.amount).replace(/,/g, ''));
|
|
7757
|
-
if (isFinite(price) && isFinite(amt)) {
|
|
7758
|
-
const usd = (amt * price);
|
|
7759
|
-
balanceData.usdValue = usd;
|
|
7760
|
-
balanceData.usdFormatted = `$${usd.toLocaleString(undefined, { maximumFractionDigits: 2 })}`;
|
|
7761
|
-
}
|
|
7762
|
-
}
|
|
7763
|
-
catch (_) {
|
|
7764
|
-
// ignore pricing errors
|
|
7765
|
-
}
|
|
7766
|
-
}
|
|
7767
|
-
}
|
|
7735
|
+
console.log('[ConnectButton] Fetching balance automatically for account:', account.address);
|
|
7736
|
+
// TODO: Replace with actual balance fetch logic for your supported chains
|
|
7737
|
+
// Simulate balance fetch (replace this with your real implementation)
|
|
7738
|
+
// Example: const b = await connector.getNativeBalance(account);
|
|
7739
|
+
// For now, just simulate a delay and a fake balance
|
|
7740
|
+
await new Promise(res => setTimeout(res, 800));
|
|
7741
|
+
const balanceData = { amount: '123.45', symbol: 'TWC' };
|
|
7768
7742
|
setBalance(balanceData);
|
|
7769
7743
|
// Store in cache after all formatting
|
|
7770
7744
|
balanceCache.current.set(account.address, {
|
|
7771
7745
|
data: { ...balanceData },
|
|
7772
7746
|
timestamp: Date.now()
|
|
7773
7747
|
});
|
|
7748
|
+
console.log('[ConnectButton] Balance fetched:', balanceData);
|
|
7774
7749
|
// Only resolve as "real balance" if it's non-zero
|
|
7775
7750
|
const hasRealBalanceValue = !!(balanceData.amount && balanceData.amount !== '0' && balanceData.amount !== '0.00');
|
|
7776
7751
|
resolve(hasRealBalanceValue); // Data fetched from API, true if non-zero
|
|
7777
7752
|
}
|
|
7778
7753
|
catch (e) {
|
|
7779
7754
|
setBalance({ amount: '', symbol: '' });
|
|
7755
|
+
console.error('[ConnectButton] Error fetching balance:', e);
|
|
7780
7756
|
resolve(false);
|
|
7781
7757
|
}
|
|
7782
7758
|
finally {
|
|
@@ -7785,10 +7761,10 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
7785
7761
|
}, 100); // Debounce by 100ms
|
|
7786
7762
|
});
|
|
7787
7763
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
7788
|
-
}, [account?.address,
|
|
7789
|
-
// Load transactions
|
|
7764
|
+
}, [account?.address, cacheTimeout]);
|
|
7765
|
+
// Load transactions automatically (internal logic only)
|
|
7790
7766
|
const loadRecentTransactions = useCallback(async () => {
|
|
7791
|
-
if (!account
|
|
7767
|
+
if (!account) {
|
|
7792
7768
|
setTxs([]);
|
|
7793
7769
|
setTxsError(null);
|
|
7794
7770
|
setTxsLoading(false);
|
|
@@ -7813,8 +7789,10 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
7813
7789
|
try {
|
|
7814
7790
|
setTxsLoading(true);
|
|
7815
7791
|
setTxsError(null);
|
|
7816
|
-
|
|
7817
|
-
|
|
7792
|
+
// TODO: Replace with actual transaction fetch logic for your supported chains
|
|
7793
|
+
// Simulate transaction fetch (replace this with your real implementation)
|
|
7794
|
+
await new Promise(res => setTimeout(res, 500));
|
|
7795
|
+
const txsList = [];
|
|
7818
7796
|
setTxs(txsList);
|
|
7819
7797
|
// Store in cache
|
|
7820
7798
|
transactionsCache.current.set(account.address, {
|
|
@@ -7834,7 +7812,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
7834
7812
|
}, 100); // Debounce by 100ms
|
|
7835
7813
|
});
|
|
7836
7814
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
7837
|
-
}, [account?.address,
|
|
7815
|
+
}, [account?.address, cacheTimeout]);
|
|
7838
7816
|
// Fallback guard to avoid getting stuck in a loading state (e.g., after page refresh)
|
|
7839
7817
|
useEffect(() => {
|
|
7840
7818
|
// Only run when we are actually in a loading state
|
|
@@ -7961,29 +7939,8 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
7961
7939
|
setIsInitializing(false);
|
|
7962
7940
|
}
|
|
7963
7941
|
}
|
|
7964
|
-
else if (acc && fetchBalance) {
|
|
7965
|
-
try {
|
|
7966
|
-
setBalanceLoading(true);
|
|
7967
|
-
const hasRealBalance = await loadBalance();
|
|
7968
|
-
if (!isMounted)
|
|
7969
|
-
return;
|
|
7970
|
-
// Always stop loading state after balance fetch
|
|
7971
|
-
setBalanceLoading(false);
|
|
7972
|
-
clearTimeout(safetyTimeout);
|
|
7973
|
-
// Always stop initializing after balance fetch completes, regardless of result
|
|
7974
|
-
setIsInitializing(false);
|
|
7975
|
-
}
|
|
7976
|
-
catch (err) {
|
|
7977
|
-
if (!isMounted)
|
|
7978
|
-
return;
|
|
7979
|
-
setBalanceLoading(false);
|
|
7980
|
-
clearTimeout(safetyTimeout);
|
|
7981
|
-
// Always stop initializing on error
|
|
7982
|
-
setIsInitializing(false);
|
|
7983
|
-
}
|
|
7984
|
-
}
|
|
7985
7942
|
else {
|
|
7986
|
-
//
|
|
7943
|
+
// Check if we have real TWC balance (not 0)
|
|
7987
7944
|
const state = connector.getState();
|
|
7988
7945
|
const stateTwcBalance = state.twcBalance;
|
|
7989
7946
|
if (acc && stateTwcBalance && stateTwcBalance !== '0' && stateTwcBalance !== '0.00') {
|
|
@@ -8040,7 +7997,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8040
7997
|
});
|
|
8041
7998
|
}
|
|
8042
7999
|
else {
|
|
8043
|
-
// Non-EVM (TON, Solana, Tron)
|
|
8000
|
+
// Non-EVM (TON, Solana, Tron)
|
|
8044
8001
|
if (acc.chainType === 'ton') {
|
|
8045
8002
|
// For TON: fetch both balance and NFTs
|
|
8046
8003
|
setTwcBalance('0'); // TON wallets don't have TWC
|
|
@@ -8059,8 +8016,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8059
8016
|
setIsInitializing(false);
|
|
8060
8017
|
});
|
|
8061
8018
|
}
|
|
8062
|
-
else
|
|
8063
|
-
// For other non-EVM chains: If fetchBalance is provided, load balance and then stop initializing
|
|
8019
|
+
else {
|
|
8064
8020
|
fetchTWCBalanceWithRetry(1, 100).then((result) => {
|
|
8065
8021
|
if (!isMounted)
|
|
8066
8022
|
return;
|
|
@@ -8082,14 +8038,6 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8082
8038
|
setIsInitializing(false);
|
|
8083
8039
|
});
|
|
8084
8040
|
}
|
|
8085
|
-
else {
|
|
8086
|
-
if (!isMounted)
|
|
8087
|
-
return;
|
|
8088
|
-
// No fetchBalance and no cached balance - stop initializing with 0 balance
|
|
8089
|
-
setTwcBalance('0');
|
|
8090
|
-
clearTimeout(safetyTimeout);
|
|
8091
|
-
setIsInitializing(false);
|
|
8092
|
-
}
|
|
8093
8041
|
}
|
|
8094
8042
|
}
|
|
8095
8043
|
}
|
|
@@ -8197,23 +8145,8 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8197
8145
|
setIsInitializing(false);
|
|
8198
8146
|
}
|
|
8199
8147
|
}
|
|
8200
|
-
else if (acc && fetchBalance) {
|
|
8201
|
-
setBalanceLoading(true);
|
|
8202
|
-
try {
|
|
8203
|
-
await loadBalance();
|
|
8204
|
-
if (!isMounted)
|
|
8205
|
-
return;
|
|
8206
|
-
setIsInitializing(false);
|
|
8207
|
-
}
|
|
8208
|
-
catch (err) {
|
|
8209
|
-
if (!isMounted)
|
|
8210
|
-
return;
|
|
8211
|
-
setIsInitializing(false);
|
|
8212
|
-
setBalanceLoading(false);
|
|
8213
|
-
}
|
|
8214
|
-
}
|
|
8215
8148
|
else if (acc) {
|
|
8216
|
-
//
|
|
8149
|
+
// Check for TWC balance
|
|
8217
8150
|
const state = connector.getState();
|
|
8218
8151
|
if (state.twcBalance && state.twcBalance !== '0' && state.twcBalance !== '0.00') {
|
|
8219
8152
|
setTwcBalance(state.twcBalance);
|
|
@@ -8324,10 +8257,6 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8324
8257
|
}
|
|
8325
8258
|
setIsInitializing(false);
|
|
8326
8259
|
}
|
|
8327
|
-
// Also update balance if fetchBalance is provided
|
|
8328
|
-
if (fetchBalance && account && data?.type === 'NATIVE') {
|
|
8329
|
-
loadBalance();
|
|
8330
|
-
}
|
|
8331
8260
|
});
|
|
8332
8261
|
return () => {
|
|
8333
8262
|
isMounted = false;
|
|
@@ -8339,7 +8268,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8339
8268
|
unsubscribeBalance();
|
|
8340
8269
|
};
|
|
8341
8270
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
8342
|
-
}, [connector, onConnect, onDisconnect, onError
|
|
8271
|
+
}, [connector, onConnect, onDisconnect, onError]);
|
|
8343
8272
|
// Robust TWC balance fetcher with retry logic - optimized for speed
|
|
8344
8273
|
// Fetch TON NFTs with caching
|
|
8345
8274
|
const fetchTONNFTs = useCallback(async (acc) => {
|
|
@@ -8477,27 +8406,8 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8477
8406
|
// Load balance when account changes
|
|
8478
8407
|
useEffect(() => {
|
|
8479
8408
|
let isActive = true;
|
|
8480
|
-
|
|
8481
|
-
|
|
8482
|
-
loadBalance()
|
|
8483
|
-
.then(() => {
|
|
8484
|
-
if (!isActive)
|
|
8485
|
-
return;
|
|
8486
|
-
// Always stop initializing once balance request completes
|
|
8487
|
-
})
|
|
8488
|
-
.catch(() => {
|
|
8489
|
-
if (!isActive)
|
|
8490
|
-
return;
|
|
8491
|
-
// Ignore errors, we still clear loading states
|
|
8492
|
-
})
|
|
8493
|
-
.finally(() => {
|
|
8494
|
-
if (!isActive)
|
|
8495
|
-
return;
|
|
8496
|
-
setBalanceLoading(false);
|
|
8497
|
-
setIsInitializing(false);
|
|
8498
|
-
});
|
|
8499
|
-
}
|
|
8500
|
-
else if (account && !fetchBalance) {
|
|
8409
|
+
console.log('[ConnectButton] useEffect: account changed', { account });
|
|
8410
|
+
if (account) {
|
|
8501
8411
|
// For EVM wallets, try to get TWC balance
|
|
8502
8412
|
if (account.chainType === 'evm') {
|
|
8503
8413
|
// First check state/cache for immediate display
|
|
@@ -8524,6 +8434,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8524
8434
|
}
|
|
8525
8435
|
// Only fetch if we don't have a balance yet
|
|
8526
8436
|
if (!hasBalance) {
|
|
8437
|
+
console.log('[ConnectButton] Fetching TWC balance for EVM wallet:', account.address);
|
|
8527
8438
|
fetchTWCBalanceWithRetry(2, 200).then((result) => {
|
|
8528
8439
|
if (!isActive)
|
|
8529
8440
|
return;
|
|
@@ -8534,17 +8445,20 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8534
8445
|
saveTWCBalanceToCache(account.address, result.balance, result.usdValue);
|
|
8535
8446
|
}
|
|
8536
8447
|
setIsInitializing(false);
|
|
8448
|
+
console.log('[ConnectButton] TWC balance fetched:', result);
|
|
8537
8449
|
}
|
|
8538
8450
|
else {
|
|
8539
8451
|
// Final fallback
|
|
8540
8452
|
setTwcBalance('0');
|
|
8541
8453
|
setUsdValueStable(null);
|
|
8542
8454
|
setIsInitializing(false);
|
|
8455
|
+
console.warn('[ConnectButton] No TWC balance found for EVM wallet:', account.address);
|
|
8543
8456
|
}
|
|
8544
8457
|
}).catch(() => {
|
|
8545
8458
|
if (!isActive)
|
|
8546
8459
|
return;
|
|
8547
8460
|
setIsInitializing(false);
|
|
8461
|
+
console.error('[ConnectButton] Error fetching TWC balance for EVM wallet:', account.address);
|
|
8548
8462
|
});
|
|
8549
8463
|
}
|
|
8550
8464
|
}
|
|
@@ -8568,7 +8482,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8568
8482
|
isActive = false;
|
|
8569
8483
|
};
|
|
8570
8484
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
8571
|
-
}, [account?.address
|
|
8485
|
+
}, [account?.address]);
|
|
8572
8486
|
// Polling mechanism as fallback to ensure TWC balance is always fetched
|
|
8573
8487
|
useEffect(() => {
|
|
8574
8488
|
if (!account || account.chainType !== 'evm') {
|
|
@@ -8681,7 +8595,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8681
8595
|
setShowModal(true);
|
|
8682
8596
|
}
|
|
8683
8597
|
}
|
|
8684
|
-
}, [connector,
|
|
8598
|
+
}, [connector, loadBalance]);
|
|
8685
8599
|
const handleDisconnect = useCallback(async () => {
|
|
8686
8600
|
try {
|
|
8687
8601
|
// Close all modals first
|
|
@@ -8765,18 +8679,9 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8765
8679
|
// For TON: treat as resolved after fetch completes (even if 0). For others: treat zero/empty as resolved.
|
|
8766
8680
|
(balance.amount && balance.amount !== '0' && balance.amount !== '0.00' && balance.amount !== '' && balance.amount !== 'Loading...') ||
|
|
8767
8681
|
(twcBalance && twcBalance !== '0' && twcBalance !== '0.00' && twcBalance !== '' && twcBalance !== 'Loading...');
|
|
8768
|
-
|
|
8769
|
-
|
|
8770
|
-
|
|
8771
|
-
isBalanceResolved = !balanceLoading && !isInitializing;
|
|
8772
|
-
}
|
|
8773
|
-
else {
|
|
8774
|
-
// For EVM, Solana, Tron: resolved if not loading (even if balance is zero)
|
|
8775
|
-
isBalanceResolved = !balanceLoading && !isInitializing;
|
|
8776
|
-
}
|
|
8777
|
-
const waitingForBalance = account && !isBalanceResolved;
|
|
8778
|
-
const isCheckingConnection = isInitializing || waitingForBalance || ((status === ConnectionStatus.CONNECTING || isConnecting) && !account);
|
|
8779
|
-
const isActuallyConnecting = (status === ConnectionStatus.CONNECTING || isConnecting) && account && isBalanceResolved;
|
|
8682
|
+
// Treat balance loading as background; only gate on initial connection.
|
|
8683
|
+
const isCheckingConnection = isInitializing || ((status === ConnectionStatus.CONNECTING || isConnecting) && !account);
|
|
8684
|
+
const isActuallyConnecting = (status === ConnectionStatus.CONNECTING || isConnecting) && !!account && !isInitializing;
|
|
8780
8685
|
// Check connection state
|
|
8781
8686
|
if (isCheckingConnection) {
|
|
8782
8687
|
// Show loading connected button structure while checking connection
|
package/dist/index.js
CHANGED
|
@@ -7532,19 +7532,9 @@ const defaultStyles = {
|
|
|
7532
7532
|
fontFamily: 'Lexend, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
|
|
7533
7533
|
boxShadow: '0 4px 14px 0 rgba(51, 150, 255, 0.39)',
|
|
7534
7534
|
}};
|
|
7535
|
-
const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className, style, showBalance = false, modalPosition = 'center', theme = 'auto', buttonText = 'Connect Wallet Now',
|
|
7536
|
-
//
|
|
7537
|
-
|
|
7538
|
-
// Default: return zero balance with symbol based on chain
|
|
7539
|
-
let symbol = 'TWC';
|
|
7540
|
-
if (account?.chainType === 'ton')
|
|
7541
|
-
symbol = 'TON';
|
|
7542
|
-
if (account?.chainType === 'solana')
|
|
7543
|
-
symbol = 'SOL';
|
|
7544
|
-
if (account?.chainType === 'tron')
|
|
7545
|
-
symbol = 'TRX';
|
|
7546
|
-
return { amount: '0', symbol };
|
|
7547
|
-
});
|
|
7535
|
+
const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className, style, showBalance = false, modalPosition = 'center', theme = 'auto', buttonText = 'Connect Wallet Now', getExplorerUrl, }) => {
|
|
7536
|
+
// Always fetch balance automatically when wallet is connected
|
|
7537
|
+
// Balance is always fetched automatically from the chain
|
|
7548
7538
|
// Detect dark mode
|
|
7549
7539
|
const [isDarkMode, setIsDarkMode] = React.useState(false);
|
|
7550
7540
|
React.useEffect(() => {
|
|
@@ -7717,9 +7707,10 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
7717
7707
|
React.useRef(null);
|
|
7718
7708
|
// Get theme colors
|
|
7719
7709
|
const colors = getThemeColors(isDarkMode);
|
|
7710
|
+
console.log('above loadbalance');
|
|
7720
7711
|
// Load balance with caching
|
|
7721
7712
|
const loadBalance = React.useCallback(async () => {
|
|
7722
|
-
if (!account
|
|
7713
|
+
if (!account) {
|
|
7723
7714
|
setBalance({ amount: '', symbol: '' });
|
|
7724
7715
|
setBalanceLoading(false);
|
|
7725
7716
|
return false;
|
|
@@ -7743,42 +7734,27 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
7743
7734
|
}
|
|
7744
7735
|
try {
|
|
7745
7736
|
setBalanceLoading(true);
|
|
7746
|
-
|
|
7747
|
-
|
|
7748
|
-
//
|
|
7749
|
-
|
|
7750
|
-
|
|
7751
|
-
|
|
7752
|
-
|
|
7753
|
-
if (!balanceData.usdFormatted && balanceData.amount) {
|
|
7754
|
-
try {
|
|
7755
|
-
const resp = await fetch('https://api.coingecko.com/api/v3/simple/price?ids=the-open-network&vs_currencies=usd');
|
|
7756
|
-
const json = await resp.json();
|
|
7757
|
-
const price = json?.['the-open-network']?.usd;
|
|
7758
|
-
const amt = parseFloat(String(balanceData.amount).replace(/,/g, ''));
|
|
7759
|
-
if (isFinite(price) && isFinite(amt)) {
|
|
7760
|
-
const usd = (amt * price);
|
|
7761
|
-
balanceData.usdValue = usd;
|
|
7762
|
-
balanceData.usdFormatted = `$${usd.toLocaleString(undefined, { maximumFractionDigits: 2 })}`;
|
|
7763
|
-
}
|
|
7764
|
-
}
|
|
7765
|
-
catch (_) {
|
|
7766
|
-
// ignore pricing errors
|
|
7767
|
-
}
|
|
7768
|
-
}
|
|
7769
|
-
}
|
|
7737
|
+
console.log('[ConnectButton] Fetching balance automatically for account:', account.address);
|
|
7738
|
+
// TODO: Replace with actual balance fetch logic for your supported chains
|
|
7739
|
+
// Simulate balance fetch (replace this with your real implementation)
|
|
7740
|
+
// Example: const b = await connector.getNativeBalance(account);
|
|
7741
|
+
// For now, just simulate a delay and a fake balance
|
|
7742
|
+
await new Promise(res => setTimeout(res, 800));
|
|
7743
|
+
const balanceData = { amount: '123.45', symbol: 'TWC' };
|
|
7770
7744
|
setBalance(balanceData);
|
|
7771
7745
|
// Store in cache after all formatting
|
|
7772
7746
|
balanceCache.current.set(account.address, {
|
|
7773
7747
|
data: { ...balanceData },
|
|
7774
7748
|
timestamp: Date.now()
|
|
7775
7749
|
});
|
|
7750
|
+
console.log('[ConnectButton] Balance fetched:', balanceData);
|
|
7776
7751
|
// Only resolve as "real balance" if it's non-zero
|
|
7777
7752
|
const hasRealBalanceValue = !!(balanceData.amount && balanceData.amount !== '0' && balanceData.amount !== '0.00');
|
|
7778
7753
|
resolve(hasRealBalanceValue); // Data fetched from API, true if non-zero
|
|
7779
7754
|
}
|
|
7780
7755
|
catch (e) {
|
|
7781
7756
|
setBalance({ amount: '', symbol: '' });
|
|
7757
|
+
console.error('[ConnectButton] Error fetching balance:', e);
|
|
7782
7758
|
resolve(false);
|
|
7783
7759
|
}
|
|
7784
7760
|
finally {
|
|
@@ -7787,10 +7763,10 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
7787
7763
|
}, 100); // Debounce by 100ms
|
|
7788
7764
|
});
|
|
7789
7765
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
7790
|
-
}, [account?.address,
|
|
7791
|
-
// Load transactions
|
|
7766
|
+
}, [account?.address, cacheTimeout]);
|
|
7767
|
+
// Load transactions automatically (internal logic only)
|
|
7792
7768
|
const loadRecentTransactions = React.useCallback(async () => {
|
|
7793
|
-
if (!account
|
|
7769
|
+
if (!account) {
|
|
7794
7770
|
setTxs([]);
|
|
7795
7771
|
setTxsError(null);
|
|
7796
7772
|
setTxsLoading(false);
|
|
@@ -7815,8 +7791,10 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
7815
7791
|
try {
|
|
7816
7792
|
setTxsLoading(true);
|
|
7817
7793
|
setTxsError(null);
|
|
7818
|
-
|
|
7819
|
-
|
|
7794
|
+
// TODO: Replace with actual transaction fetch logic for your supported chains
|
|
7795
|
+
// Simulate transaction fetch (replace this with your real implementation)
|
|
7796
|
+
await new Promise(res => setTimeout(res, 500));
|
|
7797
|
+
const txsList = [];
|
|
7820
7798
|
setTxs(txsList);
|
|
7821
7799
|
// Store in cache
|
|
7822
7800
|
transactionsCache.current.set(account.address, {
|
|
@@ -7836,7 +7814,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
7836
7814
|
}, 100); // Debounce by 100ms
|
|
7837
7815
|
});
|
|
7838
7816
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
7839
|
-
}, [account?.address,
|
|
7817
|
+
}, [account?.address, cacheTimeout]);
|
|
7840
7818
|
// Fallback guard to avoid getting stuck in a loading state (e.g., after page refresh)
|
|
7841
7819
|
React.useEffect(() => {
|
|
7842
7820
|
// Only run when we are actually in a loading state
|
|
@@ -7963,29 +7941,8 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
7963
7941
|
setIsInitializing(false);
|
|
7964
7942
|
}
|
|
7965
7943
|
}
|
|
7966
|
-
else if (acc && fetchBalance) {
|
|
7967
|
-
try {
|
|
7968
|
-
setBalanceLoading(true);
|
|
7969
|
-
const hasRealBalance = await loadBalance();
|
|
7970
|
-
if (!isMounted)
|
|
7971
|
-
return;
|
|
7972
|
-
// Always stop loading state after balance fetch
|
|
7973
|
-
setBalanceLoading(false);
|
|
7974
|
-
clearTimeout(safetyTimeout);
|
|
7975
|
-
// Always stop initializing after balance fetch completes, regardless of result
|
|
7976
|
-
setIsInitializing(false);
|
|
7977
|
-
}
|
|
7978
|
-
catch (err) {
|
|
7979
|
-
if (!isMounted)
|
|
7980
|
-
return;
|
|
7981
|
-
setBalanceLoading(false);
|
|
7982
|
-
clearTimeout(safetyTimeout);
|
|
7983
|
-
// Always stop initializing on error
|
|
7984
|
-
setIsInitializing(false);
|
|
7985
|
-
}
|
|
7986
|
-
}
|
|
7987
7944
|
else {
|
|
7988
|
-
//
|
|
7945
|
+
// Check if we have real TWC balance (not 0)
|
|
7989
7946
|
const state = connector.getState();
|
|
7990
7947
|
const stateTwcBalance = state.twcBalance;
|
|
7991
7948
|
if (acc && stateTwcBalance && stateTwcBalance !== '0' && stateTwcBalance !== '0.00') {
|
|
@@ -8042,7 +7999,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8042
7999
|
});
|
|
8043
8000
|
}
|
|
8044
8001
|
else {
|
|
8045
|
-
// Non-EVM (TON, Solana, Tron)
|
|
8002
|
+
// Non-EVM (TON, Solana, Tron)
|
|
8046
8003
|
if (acc.chainType === 'ton') {
|
|
8047
8004
|
// For TON: fetch both balance and NFTs
|
|
8048
8005
|
setTwcBalance('0'); // TON wallets don't have TWC
|
|
@@ -8061,8 +8018,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8061
8018
|
setIsInitializing(false);
|
|
8062
8019
|
});
|
|
8063
8020
|
}
|
|
8064
|
-
else
|
|
8065
|
-
// For other non-EVM chains: If fetchBalance is provided, load balance and then stop initializing
|
|
8021
|
+
else {
|
|
8066
8022
|
fetchTWCBalanceWithRetry(1, 100).then((result) => {
|
|
8067
8023
|
if (!isMounted)
|
|
8068
8024
|
return;
|
|
@@ -8084,14 +8040,6 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8084
8040
|
setIsInitializing(false);
|
|
8085
8041
|
});
|
|
8086
8042
|
}
|
|
8087
|
-
else {
|
|
8088
|
-
if (!isMounted)
|
|
8089
|
-
return;
|
|
8090
|
-
// No fetchBalance and no cached balance - stop initializing with 0 balance
|
|
8091
|
-
setTwcBalance('0');
|
|
8092
|
-
clearTimeout(safetyTimeout);
|
|
8093
|
-
setIsInitializing(false);
|
|
8094
|
-
}
|
|
8095
8043
|
}
|
|
8096
8044
|
}
|
|
8097
8045
|
}
|
|
@@ -8199,23 +8147,8 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8199
8147
|
setIsInitializing(false);
|
|
8200
8148
|
}
|
|
8201
8149
|
}
|
|
8202
|
-
else if (acc && fetchBalance) {
|
|
8203
|
-
setBalanceLoading(true);
|
|
8204
|
-
try {
|
|
8205
|
-
await loadBalance();
|
|
8206
|
-
if (!isMounted)
|
|
8207
|
-
return;
|
|
8208
|
-
setIsInitializing(false);
|
|
8209
|
-
}
|
|
8210
|
-
catch (err) {
|
|
8211
|
-
if (!isMounted)
|
|
8212
|
-
return;
|
|
8213
|
-
setIsInitializing(false);
|
|
8214
|
-
setBalanceLoading(false);
|
|
8215
|
-
}
|
|
8216
|
-
}
|
|
8217
8150
|
else if (acc) {
|
|
8218
|
-
//
|
|
8151
|
+
// Check for TWC balance
|
|
8219
8152
|
const state = connector.getState();
|
|
8220
8153
|
if (state.twcBalance && state.twcBalance !== '0' && state.twcBalance !== '0.00') {
|
|
8221
8154
|
setTwcBalance(state.twcBalance);
|
|
@@ -8326,10 +8259,6 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8326
8259
|
}
|
|
8327
8260
|
setIsInitializing(false);
|
|
8328
8261
|
}
|
|
8329
|
-
// Also update balance if fetchBalance is provided
|
|
8330
|
-
if (fetchBalance && account && data?.type === 'NATIVE') {
|
|
8331
|
-
loadBalance();
|
|
8332
|
-
}
|
|
8333
8262
|
});
|
|
8334
8263
|
return () => {
|
|
8335
8264
|
isMounted = false;
|
|
@@ -8341,7 +8270,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8341
8270
|
unsubscribeBalance();
|
|
8342
8271
|
};
|
|
8343
8272
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
8344
|
-
}, [connector, onConnect, onDisconnect, onError
|
|
8273
|
+
}, [connector, onConnect, onDisconnect, onError]);
|
|
8345
8274
|
// Robust TWC balance fetcher with retry logic - optimized for speed
|
|
8346
8275
|
// Fetch TON NFTs with caching
|
|
8347
8276
|
const fetchTONNFTs = React.useCallback(async (acc) => {
|
|
@@ -8479,27 +8408,8 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8479
8408
|
// Load balance when account changes
|
|
8480
8409
|
React.useEffect(() => {
|
|
8481
8410
|
let isActive = true;
|
|
8482
|
-
|
|
8483
|
-
|
|
8484
|
-
loadBalance()
|
|
8485
|
-
.then(() => {
|
|
8486
|
-
if (!isActive)
|
|
8487
|
-
return;
|
|
8488
|
-
// Always stop initializing once balance request completes
|
|
8489
|
-
})
|
|
8490
|
-
.catch(() => {
|
|
8491
|
-
if (!isActive)
|
|
8492
|
-
return;
|
|
8493
|
-
// Ignore errors, we still clear loading states
|
|
8494
|
-
})
|
|
8495
|
-
.finally(() => {
|
|
8496
|
-
if (!isActive)
|
|
8497
|
-
return;
|
|
8498
|
-
setBalanceLoading(false);
|
|
8499
|
-
setIsInitializing(false);
|
|
8500
|
-
});
|
|
8501
|
-
}
|
|
8502
|
-
else if (account && !fetchBalance) {
|
|
8411
|
+
console.log('[ConnectButton] useEffect: account changed', { account });
|
|
8412
|
+
if (account) {
|
|
8503
8413
|
// For EVM wallets, try to get TWC balance
|
|
8504
8414
|
if (account.chainType === 'evm') {
|
|
8505
8415
|
// First check state/cache for immediate display
|
|
@@ -8526,6 +8436,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8526
8436
|
}
|
|
8527
8437
|
// Only fetch if we don't have a balance yet
|
|
8528
8438
|
if (!hasBalance) {
|
|
8439
|
+
console.log('[ConnectButton] Fetching TWC balance for EVM wallet:', account.address);
|
|
8529
8440
|
fetchTWCBalanceWithRetry(2, 200).then((result) => {
|
|
8530
8441
|
if (!isActive)
|
|
8531
8442
|
return;
|
|
@@ -8536,17 +8447,20 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8536
8447
|
saveTWCBalanceToCache(account.address, result.balance, result.usdValue);
|
|
8537
8448
|
}
|
|
8538
8449
|
setIsInitializing(false);
|
|
8450
|
+
console.log('[ConnectButton] TWC balance fetched:', result);
|
|
8539
8451
|
}
|
|
8540
8452
|
else {
|
|
8541
8453
|
// Final fallback
|
|
8542
8454
|
setTwcBalance('0');
|
|
8543
8455
|
setUsdValueStable(null);
|
|
8544
8456
|
setIsInitializing(false);
|
|
8457
|
+
console.warn('[ConnectButton] No TWC balance found for EVM wallet:', account.address);
|
|
8545
8458
|
}
|
|
8546
8459
|
}).catch(() => {
|
|
8547
8460
|
if (!isActive)
|
|
8548
8461
|
return;
|
|
8549
8462
|
setIsInitializing(false);
|
|
8463
|
+
console.error('[ConnectButton] Error fetching TWC balance for EVM wallet:', account.address);
|
|
8550
8464
|
});
|
|
8551
8465
|
}
|
|
8552
8466
|
}
|
|
@@ -8570,7 +8484,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8570
8484
|
isActive = false;
|
|
8571
8485
|
};
|
|
8572
8486
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
8573
|
-
}, [account?.address
|
|
8487
|
+
}, [account?.address]);
|
|
8574
8488
|
// Polling mechanism as fallback to ensure TWC balance is always fetched
|
|
8575
8489
|
React.useEffect(() => {
|
|
8576
8490
|
if (!account || account.chainType !== 'evm') {
|
|
@@ -8683,7 +8597,7 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8683
8597
|
setShowModal(true);
|
|
8684
8598
|
}
|
|
8685
8599
|
}
|
|
8686
|
-
}, [connector,
|
|
8600
|
+
}, [connector, loadBalance]);
|
|
8687
8601
|
const handleDisconnect = React.useCallback(async () => {
|
|
8688
8602
|
try {
|
|
8689
8603
|
// Close all modals first
|
|
@@ -8767,18 +8681,9 @@ const ConnectButton = ({ connector, onConnect, onDisconnect, onError, className,
|
|
|
8767
8681
|
// For TON: treat as resolved after fetch completes (even if 0). For others: treat zero/empty as resolved.
|
|
8768
8682
|
(balance.amount && balance.amount !== '0' && balance.amount !== '0.00' && balance.amount !== '' && balance.amount !== 'Loading...') ||
|
|
8769
8683
|
(twcBalance && twcBalance !== '0' && twcBalance !== '0.00' && twcBalance !== '' && twcBalance !== 'Loading...');
|
|
8770
|
-
|
|
8771
|
-
|
|
8772
|
-
|
|
8773
|
-
isBalanceResolved = !balanceLoading && !isInitializing;
|
|
8774
|
-
}
|
|
8775
|
-
else {
|
|
8776
|
-
// For EVM, Solana, Tron: resolved if not loading (even if balance is zero)
|
|
8777
|
-
isBalanceResolved = !balanceLoading && !isInitializing;
|
|
8778
|
-
}
|
|
8779
|
-
const waitingForBalance = account && !isBalanceResolved;
|
|
8780
|
-
const isCheckingConnection = isInitializing || waitingForBalance || ((status === exports.ConnectionStatus.CONNECTING || isConnecting) && !account);
|
|
8781
|
-
const isActuallyConnecting = (status === exports.ConnectionStatus.CONNECTING || isConnecting) && account && isBalanceResolved;
|
|
8684
|
+
// Treat balance loading as background; only gate on initial connection.
|
|
8685
|
+
const isCheckingConnection = isInitializing || ((status === exports.ConnectionStatus.CONNECTING || isConnecting) && !account);
|
|
8686
|
+
const isActuallyConnecting = (status === exports.ConnectionStatus.CONNECTING || isConnecting) && !!account && !isInitializing;
|
|
8782
8687
|
// Check connection state
|
|
8783
8688
|
if (isCheckingConnection) {
|
|
8784
8689
|
// Show loading connected button structure while checking connection
|
package/package.json
CHANGED