From cf7433655c50cb7d550be04a14bd7b31d477e7eb Mon Sep 17 00:00:00 2001 From: julian Date: Thu, 23 May 2024 11:26:58 -0600 Subject: [PATCH] centralize isStackCoin check functionality --- lib/app_config.dart | 17 +++++++ lib/models/isar/exchange_cache/currency.dart | 12 +---- lib/pages/buy_view/buy_form.dart | 28 ++++------- .../sub_widgets/crypto_selection_view.dart | 11 ----- .../exchange_currency_selection_view.dart | 46 ++++++++++--------- .../exchange_step_views/step_2_view.dart | 24 ++++------ .../exchange_view/trade_details_view.dart | 18 ++------ .../subwidgets/desktop_step_2.dart | 17 ++----- lib/services/buy/simplex/simplex_api.dart | 20 ++------ .../majestic_bank/majestic_bank_exchange.dart | 6 ++- .../simpleswap/simpleswap_exchange.dart | 4 +- .../exchange/trocador/trocador_exchange.dart | 13 ++++-- .../textfields/exchange_textfield.dart | 5 +- 13 files changed, 92 insertions(+), 129 deletions(-) diff --git a/lib/app_config.dart b/lib/app_config.dart index efe6b067e..9374d94bd 100644 --- a/lib/app_config.dart +++ b/lib/app_config.dart @@ -37,6 +37,23 @@ abstract class AppConfig { } } + static bool isStackCoin(String? ticker) { + if (ticker == null) { + return false; + } + + if (getCryptoCurrencyForTicker(ticker, caseInsensitive: false) != null) { + return true; + } + + try { + getCryptoCurrencyByPrettyName(ticker); + return true; + } catch (_) { + return false; + } + } + /// Fuzzy logic. Use with caution!! @Deprecated("dangerous") static CryptoCurrency getCryptoCurrencyByPrettyName(final String prettyName) { diff --git a/lib/models/isar/exchange_cache/currency.dart b/lib/models/isar/exchange_cache/currency.dart index f0e9ee132..71c7890ce 100644 --- a/lib/models/isar/exchange_cache/currency.dart +++ b/lib/models/isar/exchange_cache/currency.dart @@ -9,6 +9,7 @@ */ import 'package:isar/isar.dart'; + import '../../../app_config.dart'; import 'pair.dart'; @@ -98,7 +99,7 @@ class Currency { rateType: rateType, isAvailable: json["isAvailable"] as bool?, isStackCoin: - json["isStackCoin"] as bool? ?? Currency.checkIsStackCoin(ticker), + json["isStackCoin"] as bool? ?? AppConfig.isStackCoin(ticker), tokenContract: json["tokenContract"] as String?, )..id = json["id"] as int?; } catch (e) { @@ -158,13 +159,4 @@ class Currency { String toString() { return "Currency: ${toJson()}"; } - - static bool checkIsStackCoin(String ticker) { - try { - AppConfig.getCryptoCurrencyForTicker(ticker); - return true; - } catch (_) { - return false; - } - } } diff --git a/lib/pages/buy_view/buy_form.dart b/lib/pages/buy_view/buy_form.dart index b1068150e..1baf97db3 100644 --- a/lib/pages/buy_view/buy_form.dart +++ b/lib/pages/buy_view/buy_form.dart @@ -16,17 +16,13 @@ import 'package:flutter/services.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_svg/svg.dart'; import 'package:intl/intl.dart'; + import '../../app_config.dart'; import '../../models/buy/response_objects/crypto.dart'; import '../../models/buy/response_objects/fiat.dart'; import '../../models/buy/response_objects/quote.dart'; import '../../models/contact_address_entry.dart'; import '../../models/isar/models/ethereum/eth_contract.dart'; -import '../address_book_views/address_book_view.dart'; -import 'buy_quote_preview.dart'; -import 'sub_widgets/crypto_selection_view.dart'; -import 'sub_widgets/fiat_selection_view.dart'; -import '../exchange_view/choose_from_stack_view.dart'; import '../../pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/address_book_address_chooser/address_book_address_chooser.dart'; import '../../providers/providers.dart'; import '../../services/buy/buy_response.dart'; @@ -56,6 +52,11 @@ import '../../widgets/rounded_white_container.dart'; import '../../widgets/stack_dialog.dart'; import '../../widgets/stack_text_field.dart'; import '../../widgets/textfield_icon_button.dart'; +import '../address_book_views/address_book_view.dart'; +import '../exchange_view/choose_from_stack_view.dart'; +import 'buy_quote_preview.dart'; +import 'sub_widgets/crypto_selection_view.dart'; +import 'sub_widgets/fiat_selection_view.dart'; class BuyForm extends ConsumerStatefulWidget { const BuyForm({ @@ -409,17 +410,6 @@ class _BuyFormState extends ConsumerState { // return null; // } - bool isStackCoin(String? ticker) { - if (ticker == null) return false; - - try { - AppConfig.getCryptoCurrencyForTicker(ticker); - return true; - } on ArgumentError catch (_) { - return false; - } - } - Future previewQuote(SimplexQuote quote) async { bool shouldPop = false; unawaited( @@ -1163,7 +1153,7 @@ class _BuyFormState extends ConsumerState { Theme.of(context).extension()!.textDark3, ), ), - if (isStackCoin(selectedCrypto?.ticker)) + if (AppConfig.isStackCoin(selectedCrypto?.ticker)) CustomTextButton( text: "Choose from Stack", onTap: () { @@ -1297,7 +1287,7 @@ class _BuyFormState extends ConsumerState { : const XIcon(), ), if (_receiveAddressController.text.isEmpty && - isStackCoin(selectedCrypto?.ticker) && + AppConfig.isStackCoin(selectedCrypto?.ticker) && isDesktop) TextFieldIconButton( key: const Key("buyViewAddressBookButtonKey"), @@ -1358,7 +1348,7 @@ class _BuyFormState extends ConsumerState { child: const AddressBookIcon(), ), if (_receiveAddressController.text.isEmpty && - isStackCoin(selectedCrypto?.ticker) && + AppConfig.isStackCoin(selectedCrypto?.ticker) && !isDesktop) TextFieldIconButton( key: const Key("buyViewAddressBookButtonKey"), diff --git a/lib/pages/buy_view/sub_widgets/crypto_selection_view.dart b/lib/pages/buy_view/sub_widgets/crypto_selection_view.dart index 47cd3b3c7..3bc0b220b 100644 --- a/lib/pages/buy_view/sub_widgets/crypto_selection_view.dart +++ b/lib/pages/buy_view/sub_widgets/crypto_selection_view.dart @@ -267,17 +267,6 @@ class _CryptoSelectionViewState extends ConsumerState { } } -bool isStackCoin(String? ticker) { - if (ticker == null) return false; - - try { - AppConfig.getCryptoCurrencyForTicker(ticker); - return true; - } on ArgumentError catch (_) { - return false; - } -} - // make a stateless widget that takes in string and double (won't ever be null) // class getIconForTicker extends ConsumerWidget{ // const getIconForTicker({ diff --git a/lib/pages/exchange_view/exchange_coin_selection/exchange_currency_selection_view.dart b/lib/pages/exchange_view/exchange_coin_selection/exchange_currency_selection_view.dart index 7bda1abbf..ad13cebf4 100644 --- a/lib/pages/exchange_view/exchange_coin_selection/exchange_currency_selection_view.dart +++ b/lib/pages/exchange_view/exchange_coin_selection/exchange_currency_selection_view.dart @@ -13,16 +13,16 @@ import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_svg/svg.dart'; import 'package:isar/isar.dart'; + +import '../../../app_config.dart'; import '../../../exceptions/exchange/unsupported_currency_exception.dart'; import '../../../models/isar/exchange_cache/currency.dart'; import '../../../models/isar/exchange_cache/pair.dart'; -import '../../buy_view/sub_widgets/crypto_selection_view.dart'; import '../../../services/exchange/change_now/change_now_exchange.dart'; import '../../../services/exchange/exchange.dart'; import '../../../services/exchange/exchange_data_loading_service.dart'; import '../../../services/exchange/majestic_bank/majestic_bank_exchange.dart'; import '../../../services/exchange/trocador/trocador_exchange.dart'; -import '../../../app_config.dart'; import '../../../themes/stack_colors.dart'; import '../../../utilities/assets.dart'; import '../../../utilities/constants.dart'; @@ -41,6 +41,7 @@ import '../../../widgets/rounded_white_container.dart'; import '../../../widgets/stack_dialog.dart'; import '../../../widgets/stack_text_field.dart'; import '../../../widgets/textfield_icon_button.dart'; +import '../../buy_view/sub_widgets/crypto_selection_view.dart'; class ExchangeCurrencySelectionView extends StatefulWidget { const ExchangeCurrencySelectionView({ @@ -418,27 +419,28 @@ class _ExchangeCurrencySelectionViewState SizedBox( width: 24, height: 24, - child: isStackCoin(items[index].ticker) - ? CoinIconForTicker( - ticker: items[index].ticker, - size: 24, - ) - // ? getIconForTicker( - // items[index].ticker, - // size: 24, - // ) - : hasImageUrl - ? SvgPicture.network( - items[index].image, - width: 24, - height: 24, - placeholderBuilder: (_) => - const LoadingIndicator(), + child: + AppConfig.isStackCoin(items[index].ticker) + ? CoinIconForTicker( + ticker: items[index].ticker, + size: 24, ) - : const SizedBox( - width: 24, - height: 24, - ), + // ? getIconForTicker( + // items[index].ticker, + // size: 24, + // ) + : hasImageUrl + ? SvgPicture.network( + items[index].image, + width: 24, + height: 24, + placeholderBuilder: (_) => + const LoadingIndicator(), + ) + : const SizedBox( + width: 24, + height: 24, + ), ), const SizedBox( width: 10, diff --git a/lib/pages/exchange_view/exchange_step_views/step_2_view.dart b/lib/pages/exchange_view/exchange_step_views/step_2_view.dart index e2ed07229..6071c4556 100644 --- a/lib/pages/exchange_view/exchange_step_views/step_2_view.dart +++ b/lib/pages/exchange_view/exchange_step_views/step_2_view.dart @@ -11,13 +11,9 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; + import '../../../app_config.dart'; import '../../../models/exchange/incomplete_exchange.dart'; -import '../../address_book_views/address_book_view.dart'; -import '../../address_book_views/subviews/contact_popup.dart'; -import '../choose_from_stack_view.dart'; -import 'step_3_view.dart'; -import '../sub_widgets/step_row.dart'; import '../../../providers/providers.dart'; import '../../../services/exchange/majestic_bank/majestic_bank_exchange.dart'; import '../../../themes/stack_colors.dart'; @@ -38,6 +34,11 @@ import '../../../widgets/icon_widgets/x_icon.dart'; import '../../../widgets/rounded_white_container.dart'; import '../../../widgets/stack_text_field.dart'; import '../../../widgets/textfield_icon_button.dart'; +import '../../address_book_views/address_book_view.dart'; +import '../../address_book_views/subviews/contact_popup.dart'; +import '../choose_from_stack_view.dart'; +import '../sub_widgets/step_row.dart'; +import 'step_3_view.dart'; class Step2View extends ConsumerStatefulWidget { const Step2View({ @@ -70,15 +71,6 @@ class _Step2ViewState extends ConsumerState { bool enableNext = false; - bool isStackCoin(String ticker) { - try { - AppConfig.getCryptoCurrencyForTicker(ticker); - return true; - } on ArgumentError catch (_) { - return false; - } - } - @override void initState() { model = widget.model; @@ -202,7 +194,7 @@ class _Step2ViewState extends ConsumerState { "Recipient Wallet", style: STextStyles.smallMed12(context), ), - if (isStackCoin(model.receiveTicker)) + if (AppConfig.isStackCoin(model.receiveTicker)) CustomTextButton( text: "Choose from Stack", onTap: () { @@ -488,7 +480,7 @@ class _Step2ViewState extends ConsumerState { "Refund Wallet (required)", style: STextStyles.smallMed12(context), ), - if (isStackCoin(model.sendTicker)) + if (AppConfig.isStackCoin(model.sendTicker)) CustomTextButton( text: "Choose from Stack", onTap: () { diff --git a/lib/pages/exchange_view/trade_details_view.dart b/lib/pages/exchange_view/trade_details_view.dart index ae9fad302..68a75704a 100644 --- a/lib/pages/exchange_view/trade_details_view.dart +++ b/lib/pages/exchange_view/trade_details_view.dart @@ -86,18 +86,6 @@ class _TradeDetailsViewState extends ConsumerState { late final Transaction? transactionIfSentFromStack; late final String? walletId; - bool isStackCoin(String ticker) { - try { - try { - AppConfig.getCryptoCurrencyForTicker(ticker); - } catch (_) {} - AppConfig.getCryptoCurrencyByPrettyName(ticker); - return true; - } on ArgumentError catch (_) { - return false; - } - } - @override initState() { tradeId = widget.tradeId; @@ -260,7 +248,7 @@ class _TradeDetailsViewState extends ConsumerState { ), ), if (!hasTx && - isStackCoin(trade.payInCurrency) && + AppConfig.isStackCoin(trade.payInCurrency) && (trade.status == "New" || trade.status == "new" || trade.status == "waiting" || @@ -269,7 +257,7 @@ class _TradeDetailsViewState extends ConsumerState { height: 32, ), if (!hasTx && - isStackCoin(trade.payInCurrency) && + AppConfig.isStackCoin(trade.payInCurrency) && (trade.status == "New" || trade.status == "new" || trade.status == "waiting" || @@ -1372,7 +1360,7 @@ class _TradeDetailsViewState extends ConsumerState { ), if (!isDesktop && !hasTx && - isStackCoin(trade.payInCurrency) && + AppConfig.isStackCoin(trade.payInCurrency) && (trade.status == "New" || trade.status == "new" || trade.status == "waiting" || diff --git a/lib/pages_desktop_specific/desktop_exchange/exchange_steps/subwidgets/desktop_step_2.dart b/lib/pages_desktop_specific/desktop_exchange/exchange_steps/subwidgets/desktop_step_2.dart index c6de52ba3..a1ff7fb8d 100644 --- a/lib/pages_desktop_specific/desktop_exchange/exchange_steps/subwidgets/desktop_step_2.dart +++ b/lib/pages_desktop_specific/desktop_exchange/exchange_steps/subwidgets/desktop_step_2.dart @@ -58,15 +58,6 @@ class _DesktopStep2State extends ConsumerState { late final FocusNode _toFocusNode; late final FocusNode _refundFocusNode; - bool isStackCoin(String ticker) { - try { - AppConfig.getCryptoCurrencyForTicker(ticker); - return true; - } on ArgumentError catch (_) { - return false; - } - } - void selectRecipientAddressFromStack() async { try { final coin = AppConfig.getCryptoCurrencyForTicker( @@ -311,7 +302,7 @@ class _DesktopStep2State extends ConsumerState { .textFieldActiveSearchIconRight, ), ), - if (isStackCoin( + if (AppConfig.isStackCoin( ref.watch( desktopExchangeModelProvider .select((value) => value!.receiveTicker), @@ -416,7 +407,7 @@ class _DesktopStep2State extends ConsumerState { : const XIcon(), ), if (_toController.text.isEmpty && - isStackCoin( + AppConfig.isStackCoin( ref.watch( desktopExchangeModelProvider .select((value) => value!.receiveTicker), @@ -458,7 +449,7 @@ class _DesktopStep2State extends ConsumerState { .textFieldActiveSearchIconRight, ), ), - if (isStackCoin( + if (AppConfig.isStackCoin( ref.watch( desktopExchangeModelProvider .select((value) => value!.sendTicker), @@ -565,7 +556,7 @@ class _DesktopStep2State extends ConsumerState { : const XIcon(), ), if (_refundController.text.isEmpty && - isStackCoin( + AppConfig.isStackCoin( ref.watch( desktopExchangeModelProvider .select((value) => value!.sendTicker), diff --git a/lib/services/buy/simplex/simplex_api.dart b/lib/services/buy/simplex/simplex_api.dart index 6c3f52576..5bbb82c1c 100644 --- a/lib/services/buy/simplex/simplex_api.dart +++ b/lib/services/buy/simplex/simplex_api.dart @@ -12,18 +12,19 @@ import 'dart:async'; import 'dart:convert'; import 'package:decimal/decimal.dart'; +import 'package:url_launcher/url_launcher.dart'; + import '../../../app_config.dart'; import '../../../models/buy/response_objects/crypto.dart'; import '../../../models/buy/response_objects/fiat.dart'; import '../../../models/buy/response_objects/order.dart'; import '../../../models/buy/response_objects/quote.dart'; import '../../../networking/http.dart'; -import '../buy_response.dart'; -import '../../tor_service.dart'; import '../../../utilities/enums/fiat_enum.dart'; import '../../../utilities/logger.dart'; import '../../../utilities/prefs.dart'; -import 'package:url_launcher/url_launcher.dart'; +import '../../tor_service.dart'; +import '../buy_response.dart'; class SimplexAPI { static const String authority = "buycrypto.stackwallet.com"; @@ -90,7 +91,7 @@ class SimplexAPI { for (final crypto in jsonArray as List) { // TODO validate jsonArray - if (isStackCoin("${crypto['ticker_symbol']}")) { + if (AppConfig.isStackCoin("${crypto['ticker_symbol']}")) { cryptos.add( Crypto.fromJson({ 'ticker': "${crypto['ticker_symbol']}", @@ -400,14 +401,3 @@ class SimplexAPI { String timeZoneFormatter(Duration offset) => "${offset.isNegative ? "-" : "+"}${offset.inHours.abs().toString().padLeft(2, "0")}:${(offset.inMinutes - offset.inHours * 60).abs().toString().padLeft(2, "0")}"; } - -bool isStackCoin(String? ticker) { - if (ticker == null) return false; - - try { - AppConfig.getCryptoCurrencyForTicker(ticker); - return true; - } on ArgumentError catch (_) { - return false; - } -} diff --git a/lib/services/exchange/majestic_bank/majestic_bank_exchange.dart b/lib/services/exchange/majestic_bank/majestic_bank_exchange.dart index 725942193..351608759 100644 --- a/lib/services/exchange/majestic_bank/majestic_bank_exchange.dart +++ b/lib/services/exchange/majestic_bank/majestic_bank_exchange.dart @@ -9,6 +9,9 @@ */ import 'package:decimal/decimal.dart'; +import 'package:uuid/uuid.dart'; + +import '../../../app_config.dart'; import '../../../exceptions/exchange/exchange_exception.dart'; import '../../../exceptions/exchange/majestic_bank/mb_exception.dart'; import '../../../models/exchange/majestic_bank/mb_order.dart'; @@ -20,7 +23,6 @@ import '../../../models/isar/exchange_cache/pair.dart'; import '../exchange.dart'; import '../exchange_response.dart'; import 'majestic_bank_api.dart'; -import 'package:uuid/uuid.dart'; class MajesticBankExchange extends Exchange { MajesticBankExchange._(); @@ -140,7 +142,7 @@ class MajesticBankExchange extends Exchange { isFiat: false, rateType: SupportedRateType.both, isAvailable: true, - isStackCoin: Currency.checkIsStackCoin(limit.currency), + isStackCoin: AppConfig.isStackCoin(limit.currency), tokenContract: null, ); currencies.add(currency); diff --git a/lib/services/exchange/simpleswap/simpleswap_exchange.dart b/lib/services/exchange/simpleswap/simpleswap_exchange.dart index f897bc3f6..13d8029d5 100644 --- a/lib/services/exchange/simpleswap/simpleswap_exchange.dart +++ b/lib/services/exchange/simpleswap/simpleswap_exchange.dart @@ -9,6 +9,8 @@ */ import 'package:decimal/decimal.dart'; + +import '../../../app_config.dart'; import '../../../models/exchange/response_objects/estimate.dart'; import '../../../models/exchange/response_objects/range.dart'; import '../../../models/exchange/response_objects/trade.dart'; @@ -76,7 +78,7 @@ class SimpleSwapExchange extends Exchange { ? SupportedRateType.both : SupportedRateType.estimated, isAvailable: true, - isStackCoin: Currency.checkIsStackCoin(e.symbol), + isStackCoin: AppConfig.isStackCoin(e.symbol), tokenContract: null, ), ) diff --git a/lib/services/exchange/trocador/trocador_exchange.dart b/lib/services/exchange/trocador/trocador_exchange.dart index a74f0a0cc..701bca7cb 100644 --- a/lib/services/exchange/trocador/trocador_exchange.dart +++ b/lib/services/exchange/trocador/trocador_exchange.dart @@ -11,6 +11,9 @@ import 'dart:math'; import 'package:decimal/decimal.dart'; +import 'package:uuid/uuid.dart'; + +import '../../../app_config.dart'; import '../../../exceptions/exchange/exchange_exception.dart'; import '../../../models/exchange/response_objects/estimate.dart'; import '../../../models/exchange/response_objects/range.dart'; @@ -22,7 +25,6 @@ import '../exchange_response.dart'; import 'response_objects/trocador_coin.dart'; import 'response_objects/trocador_quote.dart'; import 'trocador_api.dart'; -import 'package:uuid/uuid.dart'; class TrocadorExchange extends Exchange { TrocadorExchange._(); @@ -117,7 +119,8 @@ class TrocadorExchange extends Exchange { @override Future>> getAllCurrencies( - bool fixedRate) async { + bool fixedRate, + ) async { _cachedCurrencies ??= (await TrocadorAPI.getCoins(isOnion: false)).value; _cachedCurrencies?.removeWhere((e) => e.network != onlySupportedNetwork); @@ -132,7 +135,7 @@ class TrocadorExchange extends Exchange { image: e.image, isFiat: false, rateType: SupportedRateType.both, - isStackCoin: Currency.checkIsStackCoin(e.ticker), + isStackCoin: AppConfig.isStackCoin(e.ticker), tokenContract: null, isAvailable: true, ), @@ -267,7 +270,9 @@ class TrocadorExchange extends Exchange { @override Future>> getPairedCurrencies( - String forCurrency, bool fixedRate) async { + String forCurrency, + bool fixedRate, + ) async { // TODO: implement getPairedCurrencies throw UnimplementedError(); } diff --git a/lib/widgets/textfields/exchange_textfield.dart b/lib/widgets/textfields/exchange_textfield.dart index 7d1eb7915..0266d6637 100644 --- a/lib/widgets/textfields/exchange_textfield.dart +++ b/lib/widgets/textfields/exchange_textfield.dart @@ -11,6 +11,8 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_svg/svg.dart'; + +import '../../app_config.dart'; import '../../models/exchange/aggregate_currency.dart'; import '../../pages/buy_view/sub_widgets/crypto_selection_view.dart'; import '../../providers/global/locale_provider.dart'; @@ -173,7 +175,8 @@ class _ExchangeTextFieldState extends ConsumerState { ), child: Builder( builder: (context) { - if (isStackCoin(widget.currency?.ticker)) { + if (AppConfig.isStackCoin( + widget.currency?.ticker)) { return Center( child: CoinIconForTicker( size: 18,