CW-974: Add support for automatic node switching. (#2361)

* feat: Add support for automatic node switching.

This change:
- Adds isTrustedNodeForSwitching property to Node class.
- Adds UI Checkbox to mark a node as trusted for automatic switching in Node create or edit form.
- Implements checkNodeHealth method in wallet classes to verify node connectivity.
- Adds NodeSwitchingService for managing automatic node switching based on health checks.
- Adds setting option for user to enable/disable automated node switching

* fix(automatic-node-switch): Set node health check interval to 5 minutes in NodeSwitchingService

* refactor: Update handler method

* feat: Automatic Node Switching Improvements

This change:
- Moves enable automatic node switch to the Manage Nodes Page
- Adds check to verify node health before attempting to sync in EVM, Sol and Tron Wallets.
- Modifies trusted ndoes list for monero
- Adds trigger for Node Health Check when app resumes from background

* fix: Increase default request timeout for Tron and update health check interval for node switch

* feat: Enhance Node health check functionality.

This change adds additional checks for token balance in EVM, Solana and Tron Wallets in checkNodeHealth.

* fix: Update health check interval to 30 seconds in NodeSwitchingService

* Update bitcoin_electrum_server_list.yml

* fix: Electru wallet not connecting to new node after switching

* fix: Automated Node Switch not working on Decred Wallet

* fix: Add more nodes for AutoSwitching

This change:
- Enables more nodes across wallet types for auto switching
- Modifies CTA for checkbox
- Renames flags to fit

* fix: Add migration function for setting up default nodes for autoswitching

* fix: Review and testing fixes

* chore: Adjust balance and history fetch timer
This commit is contained in:
David Adegoke
2025-07-25 16:42:59 +03:00
committed by GitHub
parent 8f4eed4d9b
commit 4b2af63fa5
73 changed files with 639 additions and 68 deletions
+4 -1
View File
@@ -65,10 +65,13 @@ abstract class EVMChainClient {
// });
}
Future<EtherAmount> getBalance(EthereumAddress address) async {
Future<EtherAmount> getBalance(EthereumAddress address, {bool throwOnError = false}) async {
try {
return await _client!.getBalance(address);
} catch (_) {
if (throwOnError) {
rethrow;
}
return EtherAmount.zero();
}
}
+39 -3
View File
@@ -164,6 +164,36 @@ abstract class EVMChainWalletBase
EncryptionFileUtils encryptionFileUtils,
);
@override
Future<bool> checkNodeHealth() async {
try {
// Check native balance
await _client.getBalance(_evmChainPrivateKey.address, throwOnError: true);
// Check USDC token balance
String usdcContractAddress;
if (_client.chainId == 1) {
// Ethereum mainnet
usdcContractAddress = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48";
} else if (_client.chainId == 137) {
// Polygon mainnet
usdcContractAddress = "0x2791bca1f2de4661ed88a30c99a7a9449aa84174";
} else {
// If we are not on Ethereum or Polygon, we skip ERC20 token check
return true;
}
await _client.fetchERC20Balances(
_evmChainPrivateKey.address,
usdcContractAddress,
);
return true;
} catch (e) {
return false;
}
}
//! Common Methods across child classes
String idFor(String name, WalletType type) => '${walletTypeToString(type).toLowerCase()}_$name';
@@ -379,15 +409,21 @@ abstract class EVMChainWalletBase
Future<void> startSync() async {
try {
syncStatus = AttemptingSyncStatus();
// Verify node health before attempting to sync
final isHealthy = await checkNodeHealth();
if (!isHealthy) {
syncStatus = FailedSyncStatus();
return;
}
await _updateBalance();
await _updateTransactions();
await _updateEstimatedGasFeeParams();
_updateFeesTimer ??= Timer.periodic(const Duration(seconds: 30), (timer) async {
await _updateEstimatedGasFeeParams();
});
syncStatus = SyncedSyncStatus();
} catch (e) {
syncStatus = FailedSyncStatus();
@@ -825,7 +861,7 @@ abstract class EVMChainWalletBase
_transactionsUpdateTimer!.cancel();
}
_transactionsUpdateTimer = Timer.periodic(const Duration(seconds: 15), (_) {
_transactionsUpdateTimer = Timer.periodic(const Duration(seconds: 20), (_) {
_updateTransactions();
_updateBalance();
});