From ac72252fd2ee92a5517336d7a8faaaa36a88d8f1 Mon Sep 17 00:00:00 2001 From: NIK Date: Sat, 22 Jun 2024 08:32:05 +0800 Subject: [PATCH] errors fix, formatting --- analysis_options.yaml | 5 -- lib/datasource/remote/model/token_model.dart | 2 +- .../get_token_models_use_case.dart | 54 +++++++++---------- 3 files changed, 27 insertions(+), 34 deletions(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index 2f44248af..ec00eab70 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -32,7 +32,6 @@ linter: always_put_control_body_on_new_line: true use_string_buffers: false # - always_put_required_named_parameters_first # we prefer having parameters in the same order as fields https://github.com/flutter/flutter/issues/10219 - always_require_non_null_named_parameters: true always_specify_types: false always_use_package_imports: true annotate_overrides: true @@ -97,13 +96,10 @@ linter: hash_and_equals: true implementation_imports: true # - invariant_booleans # too many false positives: https://github.com/dart-lang/linter/issues/811 - iterable_contains_unrelated_type: true # - join_return_with_assignment # not required by flutter style leading_newlines_in_multiline_strings: true library_names: true library_prefixes: true - #- lines_longer_than_80_chars # not required by flutter style - list_remove_unrelated_type: true # - literal_only_boolean_expressions # too many false positives: https://github.com/dart-lang/sdk/issues/34181 missing_whitespace_between_adjacent_strings: true no_adjacent_strings_in_list: true @@ -134,7 +130,6 @@ linter: prefer_constructors_over_static_methods: true prefer_contains: true # - prefer_double_quotes # opposite of prefer_single_quotes - prefer_equal_for_default_values: true # - prefer_expression_function_bodies # conflicts with https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#consider-using--for-short-functions-and-methods prefer_final_fields: true prefer_final_in_for_each: true diff --git a/lib/datasource/remote/model/token_model.dart b/lib/datasource/remote/model/token_model.dart index f8dfd47c7..b76c14a59 100644 --- a/lib/datasource/remote/model/token_model.dart +++ b/lib/datasource/remote/model/token_model.dart @@ -116,7 +116,7 @@ class TokenModel extends Equatable { static String getAssetString(String? id, double quantity) { if (id != null && TokenModel.fromId(id) != null && contractPrecisions.containsKey(id)) { - final symbol = TokenModel.fromId(id)!.symbol; + final symbol = TokenModel.fromId(id)?.symbol; return symbol == null ? "" : "${quantity.toStringAsFixed(contractPrecisions[id]!)} $symbol"; } else { return ""; diff --git a/lib/domain-shared/shared_use_cases/get_token_models_use_case.dart b/lib/domain-shared/shared_use_cases/get_token_models_use_case.dart index 8f6d45659..22d7c8518 100644 --- a/lib/domain-shared/shared_use_cases/get_token_models_use_case.dart +++ b/lib/domain-shared/shared_use_cases/get_token_models_use_case.dart @@ -1,10 +1,9 @@ import 'package:collection/collection.dart'; import 'package:dynamic_parallel_queue/dynamic_parallel_queue.dart'; -import 'package:seeds/datasource/remote/api/tokenmodels_repository.dart'; import 'package:seeds/datasource/remote/api/stat_repository.dart'; +import 'package:seeds/datasource/remote/api/tokenmodels_repository.dart'; import 'package:seeds/datasource/remote/model/token_model.dart'; import 'package:seeds/domain-shared/base_use_case.dart'; -import 'package:seeds/screens/wallet/components/tokens_cards/interactor/usecases/load_token_balances_use_case.dart'; class TokenModelSelector { final List acceptList; @@ -23,25 +22,24 @@ class GetTokenModelsUseCase extends InputUseCase, TokenModelSel Future>> run(TokenModelSelector input) async { print("[http] importing token models"); final idSet = {}; - final useCaseMap = >{} ; + final useCaseMap = >{}; + /// accumulate accepted token id's in idSet /// record valid usecases (from both acceptList and infoList) for each token in useCaseMap - for(final useCase in input.acceptList + (input.infoList ?? [])) { + for (final useCase in input.acceptList + (input.infoList ?? [])) { bool more = true; int lastRetrieved = -1; fetchOneUseCase: - while(more) { - final acceptedTokenIdsResult = await TokenModelsRepository() - .getAcceptedTokenIds(useCase, lastRetrieved+1); - if(acceptedTokenIdsResult.isError) { + while (more) { + final acceptedTokenIdsResult = await TokenModelsRepository().getAcceptedTokenIds(useCase, lastRetrieved + 1); + if (acceptedTokenIdsResult.isError) { break fetchOneUseCase; } final resultValue = acceptedTokenIdsResult.asValue!.value; more = resultValue['more']; final acceptances = resultValue['rows'].toList(); - final tokenIds = List.from( - acceptances.map((row) => row['token_id']).toList()); - if(tokenIds.isEmpty) { + final tokenIds = List.from(acceptances.map((row) => row['token_id']).toList()); + if (tokenIds.isEmpty) { continue; } for (final id in tokenIds) { @@ -59,9 +57,8 @@ class GetTokenModelsUseCase extends InputUseCase, TokenModelSel remainingIds.sort(); bool more = true; final rv = []; - while(more && remainingIds.isNotEmpty) { - final allTokensResult = await TokenModelsRepository() - .getMasterTokenTable(remainingIds[0]); + while (more && remainingIds.isNotEmpty) { + final allTokensResult = await TokenModelsRepository().getMasterTokenTable(remainingIds[0]); if (allTokensResult.isError) { return Result.error("failed to get master token list"); } @@ -77,30 +74,31 @@ class GetTokenModelsUseCase extends InputUseCase, TokenModelSel return false; } }).toList(); + /// retrieve entire list of tokens from master list, then filter by idSet; paginate by "more" for (final token in tokens) { token['usecases'] = useCaseMap[token['id']]; } final StatRepository _statRepository = StatRepository(); List theseTokens = []; + /// verify token contract on chain and get contract precision loadData(token) async { TokenModel? tm = TokenModel.fromJson(token as Map); if (tm != null) { - await _statRepository - .getTokenStat(tokenContract: tm.contract, symbol: tm.symbol) - .then( - (stats) async { - if (stats.asValue != null) { - final supply = stats.asValue!.value.supplyString; - tm.setPrecisionFromString(supply); - theseTokens.add(tm); - print("supply: $supply"); - } - }, - ).catchError((dynamic error) => _statRepository.mapHttpError(error)); + await _statRepository.getTokenStat(tokenContract: tm.contract, symbol: tm.symbol).then( + (stats) async { + if (stats.asValue != null) { + final supply = stats.asValue!.value.supplyString; + tm.setPrecisionFromString(supply); + theseTokens.add(tm); + print("supply: $supply"); + } + }, + ).catchError((dynamic error) => _statRepository.mapHttpError(error)); } } + final queue = Queue(parallel: 5); for (final dynamic token in tokens) { queue.add(() async { @@ -109,9 +107,9 @@ class GetTokenModelsUseCase extends InputUseCase, TokenModelSel } await queue.whenComplete(); rv.addAll(theseTokens.whereNotNull()); - /// build a TokenModel from each selected token's metadata + + /// build a TokenModel from each selected token's metadata } return Result.value(rv); } - }