diff --git a/lib/core/src/providers/providers_use_cases.dart b/lib/core/src/providers/providers_use_cases.dart index f76449e8..52789cbe 100644 --- a/lib/core/src/providers/providers_use_cases.dart +++ b/lib/core/src/providers/providers_use_cases.dart @@ -1,6 +1,7 @@ import 'package:datadashwallet/common/common.dart'; import 'package:datadashwallet/features/common/account/log_out_use_case.dart'; import 'package:datadashwallet/features/common/common.dart'; +import 'package:datadashwallet/features/common/contract/chains_use_case.dart'; import 'package:datadashwallet/features/common/contract/nft_contract_use_case.dart'; import 'package:datadashwallet/features/common/contract/pricing_use_case.dart'; import 'package:datadashwallet/features/common/contract/tweets_use_case.dart'; @@ -146,3 +147,11 @@ final Provider appVersionUseCaseProvider = Provider( ref.watch(web3RepositoryProvider), ), ); + +final Provider chainsUseCaseProvider = Provider( + (ref) => ChainsUseCase( + ref.watch(web3RepositoryProvider), + ref.watch(chainConfigurationUseCaseProvider), + ref.watch(authUseCaseProvider), + ), +); diff --git a/lib/features/common/contract/chains_use_case.dart b/lib/features/common/contract/chains_use_case.dart new file mode 100644 index 00000000..20adca9f --- /dev/null +++ b/lib/features/common/contract/chains_use_case.dart @@ -0,0 +1,88 @@ +import 'dart:async'; + +import 'package:datadashwallet/core/core.dart'; +import 'package:datadashwallet/features/settings/subfeatures/chain_configuration/domain/chain_configuration_use_case.dart'; +import 'package:mxc_logic/mxc_logic.dart'; + +class ChainsUseCase extends ReactiveUseCase { + ChainsUseCase( + this._repository, this._chainConfigurationUseCase, this._authUseCase); + + final Web3Repository _repository; + final ChainConfigurationUseCase _chainConfigurationUseCase; + final AuthUseCase _authUseCase; + + Future getChainsRpcUrls() async { + return await _repository.chainsRepository.getChainsRpcUrls(); + } + + void updateChainsRPCUrls() async { + try { + final chainsRpcUrls = await getChainsRpcUrls(); + final networks = _chainConfigurationUseCase.networks.value; + + for (ChainRpcUrl chainRpcUrl in chainsRpcUrls.chainList ?? []) { + final foundIndex = networks + .indexWhere((element) => element.chainId == chainRpcUrl.chainId); + + if (foundIndex != -1) { + final network = networks.elementAt(foundIndex); + + // If any change is detected + if (network.web3RpcHttpUrl != chainRpcUrl.httpUrl || + network.web3RpcWebsocketUrl != chainRpcUrl.wssUrl) { + final updatedNetwork = network.copyWith( + web3RpcHttpUrl: chainRpcUrl.httpUrl, + web3RpcWebsocketUrl: chainRpcUrl.wssUrl); + // Update in DB + _chainConfigurationUseCase.updateItem(updatedNetwork, foundIndex); + + if (network.enabled) { + _chainConfigurationUseCase.updateSelectedNetwork(updatedNetwork); + _authUseCase.resetNetwork(updatedNetwork); + } + } + } + } + } catch (e) { + // This update necessary since, RPC change might be essential. + updateChainsRPCUrls(); + } + } + + // void checkChainsRpcUrls(void Function(Network) resetNetwork, Network selectedNetwork) async { + // try { + // final chainsRpcUrls = await getChainsRpcUrls(); + + // for (ChainRpcUrl chain in chainsRpcUrls.chainList ?? []) { + // await checkChainRpcUrls(chain); + // } + // } catch (e) { + // checkChainsRpcUrls(); + // } + // } + + // Future checkChainRpcUrls(ChainRpcUrl chain) async { + // for (String url in chain.rpcUrls ?? []) { + // try { + // if (url.contains('wss')) { + // await checkWebSocketRpcUrl(url); + // } else { + // await checkRpcUrl(url); + // } + // // select the rpc url + // } catch (e) { + // continue; + // } + // } + // } + + // // TODO: check websocket link + // Future checkWebSocketRpcUrl(String url) async { + // return _repository.chainsRepository.getBlockNumber(url); + // } + + // Future checkRpcUrl(String url) async { + // return _repository.chainsRepository.getBlockNumber(url); + // } +} diff --git a/lib/features/settings/subfeatures/chain_configuration/domain/chain_configuration_use_case.dart b/lib/features/settings/subfeatures/chain_configuration/domain/chain_configuration_use_case.dart index e497bbd2..891f4fa5 100644 --- a/lib/features/settings/subfeatures/chain_configuration/domain/chain_configuration_use_case.dart +++ b/lib/features/settings/subfeatures/chain_configuration/domain/chain_configuration_use_case.dart @@ -52,6 +52,7 @@ class ChainConfigurationUseCase extends ReactiveUseCase { } void updateFixedNetworks() { + final fixedList = Network.fixedNetworks(); for (int i = 0; i < _repository.items.length; i++) { final repoItem = _repository.items[i]; @@ -61,12 +62,26 @@ class ChainConfigurationUseCase extends ReactiveUseCase { if (index != -1) { // matches - final fixedItem = Network.fixedNetworks().elementAt(index); + final fixedItem = fixedList.elementAt(index); if (!repoItem.compareWithOther(fixedItem)) { _repository.updateItem(repoItem.copyWithOther(fixedItem), i); } + } else { + // Fixed network does't contain repo Item It means It's deleted + _repository.removeItem(repoItem); } } + + // Adding new networks If available + for (Network network in fixedList) { + final foundIndex = + _repository.items.indexWhere((e) => e.chainId == network.chainId); + + if (foundIndex == -1) { + _repository.addItem(network); + } + } + update(networks, _repository.items); } @@ -114,6 +129,10 @@ class ChainConfigurationUseCase extends ReactiveUseCase { update(selectedNetwork, selectedNetwork.value); } + void updateSelectedNetwork(Network updatedSelectedNetwork) { + update(selectedNetwork, updatedSelectedNetwork); + } + /// only for details of custom network delete network page void selectNetworkForDetails(Network network) { update(selectedNetworkForDetails, network); diff --git a/lib/main.dart b/lib/main.dart index 79883817..4987822d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -33,6 +33,9 @@ void main() { final appVersionUseCase = container.read(appVersionUseCaseProvider); await appVersionUseCase.checkLatestVersion(); + final initializationUseCase = container.read(chainsUseCaseProvider); + initializationUseCase.updateChainsRPCUrls(); + runApp( UncontrolledProviderScope( container: container, diff --git a/packages/shared b/packages/shared index ec30acf4..a3e46009 160000 --- a/packages/shared +++ b/packages/shared @@ -1 +1 @@ -Subproject commit ec30acf43d8e5369f6aae0cf642d8f4198fa4e2a +Subproject commit a3e460096a0242eff5fd7fa5c51d88a2ea6705d5