Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #101 from MXCzkEVM/dynamic_chains_props
Browse files Browse the repository at this point in the history
Dynamic chains props
  • Loading branch information
reasje authored Oct 23, 2023
2 parents 7f85d73 + 09ea830 commit b37dd05
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 37 deletions.
32 changes: 8 additions & 24 deletions lib/features/common/contract/chains_use_case.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,25 @@ class ChainsUseCase extends ReactiveUseCase {
final ChainConfigurationUseCase _chainConfigurationUseCase;
final AuthUseCase _authUseCase;

Future<ChainsRpcList> getChainsRpcUrls() async {
Future<ChainsList> getChainsRpcUrls() async {
return await _repository.chainsRepository.getChainsRpcUrls();
}

void updateChainsRPCUrls() async {
void updateChains() 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 (chainsRpcUrls.networks?.isNotEmpty ?? false) {
final selectedNetwork =
_chainConfigurationUseCase.updateNetworks(chainsRpcUrls.networks!);

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);
}
}
if (selectedNetwork != null) {
_authUseCase.resetNetwork(selectedNetwork);
}
}
} catch (e) {
// This update necessary since, RPC change might be essential.
updateChainsRPCUrls();
updateChains();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import 'chain_configuration_repository.dart';
class ChainConfigurationUseCase extends ReactiveUseCase {
ChainConfigurationUseCase(
this._repository,
) {
updateFixedNetworks();
}
);

final ChainConfigurationRepository _repository;

Expand Down Expand Up @@ -51,38 +49,50 @@ class ChainConfigurationUseCase extends ReactiveUseCase {
update(networks, _repository.items);
}

void updateFixedNetworks() {
final fixedList = Network.fixedNetworks();
/// If return true then the enabled network props changed
Network? updateNetworks(List<Network> newNetworkList) {
Network? selectedNetwork;

for (int i = 0; i < _repository.items.length; i++) {
final repoItem = _repository.items[i];

final index = Network.fixedNetworks().indexWhere(
final index = newNetworkList.indexWhere(
(element) => element.chainId == repoItem.chainId,
);

if (index != -1) {
// matches
final fixedItem = fixedList.elementAt(index);
if (!repoItem.compareWithOther(fixedItem)) {
_repository.updateItem(repoItem.copyWithOther(fixedItem), i);
final toCompareItem = newNetworkList.elementAt(index);
if (!repoItem.compareWithOther(toCompareItem)) {
final mergedItem = repoItem.copyWithOther(toCompareItem);
if (repoItem.enabled) {
updateSelectedNetwork(mergedItem);
selectedNetwork = mergedItem;
}
_repository.updateItem(mergedItem, i);
}
} else {
// Fixed network does't contain repo Item It means It's deleted
_repository.removeItem(repoItem);
// But we check If It's not a custom network
if (repoItem.networkType != NetworkType.custom) {
_repository.removeItem(repoItem);
}
}
}

// Adding new networks If available
for (Network network in fixedList) {
for (Network network in newNetworkList) {
final foundIndex =
_repository.items.indexWhere((e) => e.chainId == network.chainId);

if (foundIndex == -1) {
_repository.addItem(network);
// Disable the network, Just in case
_repository.addItem(network.copyWith(enabled: false));
}
}

update(networks, _repository.items);
return selectedNetwork;
}

void changeIpfsGateWay(String newIpfsGateWay) {
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void main() {
await appVersionUseCase.checkLatestVersion();

final initializationUseCase = container.read(chainsUseCaseProvider);
initializationUseCase.updateChainsRPCUrls();
initializationUseCase.updateChains();

runApp(
UncontrolledProviderScope(
Expand Down

0 comments on commit b37dd05

Please sign in to comment.