From 8e8816fe059e033fa3260e3224ee4de8c6baf3a1 Mon Sep 17 00:00:00 2001 From: sneurlax Date: Wed, 11 Jan 2023 18:13:34 -0600 Subject: [PATCH] first simplex service stash --- lib/main.dart | 11 +- lib/pages/home_view/home_view.dart | 15 ++ .../sub_widgets/home_view_button_bar.dart | 2 + lib/pages/stack_privacy_calls.dart | 3 + .../wallet_view/desktop_wallet_view.dart | 12 + .../buy/simplex_initial_load_status.dart | 11 + lib/providers/providers.dart | 1 + .../buy/buy_data_loading_service.dart | 250 +++++------------- lib/services/buy/simplex/simplex_api.dart | 11 +- 9 files changed, 127 insertions(+), 189 deletions(-) create mode 100644 lib/providers/buy/simplex_initial_load_status.dart diff --git a/lib/main.dart b/lib/main.dart index 4303889b7..575aed02d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -41,6 +41,7 @@ import 'package:stackwallet/providers/global/trades_service_provider.dart'; import 'package:stackwallet/providers/providers.dart'; import 'package:stackwallet/providers/ui/color_theme_provider.dart'; import 'package:stackwallet/route_generator.dart'; +import 'package:stackwallet/services/buy/buy_data_loading_service.dart'; import 'package:stackwallet/services/debug_service.dart'; import 'package:stackwallet/services/exchange/change_now/change_now_exchange.dart'; import 'package:stackwallet/services/exchange/exchange_data_loading_service.dart'; @@ -287,10 +288,14 @@ class _MaterialAppWithThemeState extends ConsumerState // unawaited(_nodeService.updateCommunityNodes()); // run without awaiting - if (Constants.enableExchange && - ref.read(prefsChangeNotifierProvider).externalCalls && + if (ref.read(prefsChangeNotifierProvider).externalCalls && await ref.read(prefsChangeNotifierProvider).isExternalCallsSet()) { - unawaited(ExchangeDataLoadingService().loadAll(ref)); + if (Constants.enableExchange) { + unawaited(ExchangeDataLoadingService().loadAll(ref)); + } + if (Constants.enableBuy) { + unawaited(BuyDataLoadingService().loadAll(ref)); + } } if (ref.read(prefsChangeNotifierProvider).isAutoBackupEnabled) { diff --git a/lib/pages/home_view/home_view.dart b/lib/pages/home_view/home_view.dart index ca55bb1a0..f1629c2a3 100644 --- a/lib/pages/home_view/home_view.dart +++ b/lib/pages/home_view/home_view.dart @@ -15,6 +15,7 @@ import 'package:stackwallet/providers/global/notifications_provider.dart'; import 'package:stackwallet/providers/global/prefs_provider.dart'; import 'package:stackwallet/providers/ui/home_view_index_provider.dart'; import 'package:stackwallet/providers/ui/unread_notifications_provider.dart'; +import 'package:stackwallet/services/buy/buy_data_loading_service.dart'; import 'package:stackwallet/services/exchange/exchange_data_loading_service.dart'; import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/constants.dart'; @@ -46,6 +47,7 @@ class _HomeViewState extends ConsumerState { bool _exitEnabled = false; final _exchangeDataLoadingService = ExchangeDataLoadingService(); + final _buyDataLoadingService = BuyDataLoadingService(); Future _onWillPop() async { // go to home view when tapping back on the main exchange view @@ -93,6 +95,16 @@ class _HomeViewState extends ConsumerState { } } + void _loadSimplexData() { + // unawaited future + if (ref.read(prefsChangeNotifierProvider).externalCalls) { + _buyDataLoadingService.loadAll(ref); + } else { + Logging.instance.log("User does not want to use external calls", + level: LogLevel.Info); + } + } + @override void initState() { _pageController = PageController(); @@ -308,6 +320,9 @@ class _HomeViewState extends ConsumerState { if (next == 1) { _exchangeDataLoadingService.loadAll(ref); } + if (next == 2) { + _buyDataLoadingService.loadAll(ref); + } if (next >= 0 && next <= 2) { _pageController.animateToPage( next, diff --git a/lib/pages/home_view/sub_widgets/home_view_button_bar.dart b/lib/pages/home_view/sub_widgets/home_view_button_bar.dart index 4ac1c82cc..2b8461476 100644 --- a/lib/pages/home_view/sub_widgets/home_view_button_bar.dart +++ b/lib/pages/home_view/sub_widgets/home_view_button_bar.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:stackwallet/providers/providers.dart'; +import 'package:stackwallet/services/buy/buy_data_loading_service.dart'; import 'package:stackwallet/services/exchange/exchange_data_loading_service.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; @@ -148,6 +149,7 @@ class _HomeViewButtonBarState extends ConsumerState { if (selectedIndex != 2) { ref.read(homeViewPageIndexStateProvider.state).state = 2; } + await BuyDataLoadingService().loadAll(ref); }, child: Text( "Buy", diff --git a/lib/pages/stack_privacy_calls.dart b/lib/pages/stack_privacy_calls.dart index 1a5e23e98..2ff954835 100644 --- a/lib/pages/stack_privacy_calls.dart +++ b/lib/pages/stack_privacy_calls.dart @@ -8,6 +8,7 @@ import 'package:stackwallet/pages/pinpad_views/create_pin_view.dart'; import 'package:stackwallet/pages_desktop_specific/password/create_password_view.dart'; import 'package:stackwallet/providers/global/prefs_provider.dart'; import 'package:stackwallet/providers/global/price_provider.dart'; +import 'package:stackwallet/services/buy/buy_data_loading_service.dart'; import 'package:stackwallet/services/exchange/exchange_data_loading_service.dart'; import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/constants.dart'; @@ -232,6 +233,8 @@ class _StackPrivacyCalls extends ConsumerState { if (isEasy) { unawaited(ExchangeDataLoadingService() .loadAll(ref)); + unawaited( + BuyDataLoadingService().loadAll(ref)); ref .read(priceAnd24hChangeNotifierProvider) .start(true); diff --git a/lib/pages_desktop_specific/my_stack_view/wallet_view/desktop_wallet_view.dart b/lib/pages_desktop_specific/my_stack_view/wallet_view/desktop_wallet_view.dart index 7e57beac9..d27537a71 100644 --- a/lib/pages_desktop_specific/my_stack_view/wallet_view/desktop_wallet_view.dart +++ b/lib/pages_desktop_specific/my_stack_view/wallet_view/desktop_wallet_view.dart @@ -17,6 +17,7 @@ import 'package:stackwallet/pages_desktop_specific/my_stack_view/wallet_view/sub import 'package:stackwallet/providers/global/auto_swb_service_provider.dart'; import 'package:stackwallet/providers/providers.dart'; import 'package:stackwallet/providers/ui/transaction_filter_provider.dart'; +import 'package:stackwallet/services/buy/buy_data_loading_service.dart'; import 'package:stackwallet/services/coins/firo/firo_wallet.dart'; import 'package:stackwallet/services/event_bus/events/global/wallet_sync_status_changed_event.dart'; import 'package:stackwallet/services/event_bus/global_event_bus.dart'; @@ -64,6 +65,7 @@ class _DesktopWalletViewState extends ConsumerState { late final bool _shouldDisableAutoSyncOnLogOut; final _cnLoadingService = ExchangeDataLoadingService(); + final _buyDataLoadingService = BuyDataLoadingService(); Future onBackPressed() async { await _logout(); @@ -103,6 +105,16 @@ class _DesktopWalletViewState extends ConsumerState { } } + void _loadSimplexData() { + // unawaited future + if (ref.read(prefsChangeNotifierProvider).externalCalls) { + _buyDataLoadingService.loadAll(ref); + } else { + Logging.instance.log("User does not want to use external calls", + level: LogLevel.Info); + } + } + void _onExchangePressed(BuildContext context) async { final managerProvider = ref .read(walletsChangeNotifierProvider) diff --git a/lib/providers/buy/simplex_initial_load_status.dart b/lib/providers/buy/simplex_initial_load_status.dart new file mode 100644 index 000000000..9571f5b64 --- /dev/null +++ b/lib/providers/buy/simplex_initial_load_status.dart @@ -0,0 +1,11 @@ +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +enum SimplexLoadStatus { + waiting, + loading, + success, + failed, +} + +final simplexLoadStatusStateProvider = + StateProvider((ref) => SimplexLoadStatus.waiting); diff --git a/lib/providers/providers.dart b/lib/providers/providers.dart index 8f3f2fbfd..98c6de26f 100644 --- a/lib/providers/providers.dart +++ b/lib/providers/providers.dart @@ -1,4 +1,5 @@ export './buy/buy_form_state_provider.dart'; +export './buy/simplex_initial_load_status.dart'; export './exchange/available_changenow_currencies_provider.dart'; export './exchange/available_simpleswap_currencies_provider.dart'; export './exchange/changenow_initial_load_status.dart'; diff --git a/lib/services/buy/buy_data_loading_service.dart b/lib/services/buy/buy_data_loading_service.dart index 70ae132df..f4ee5cd37 100644 --- a/lib/services/buy/buy_data_loading_service.dart +++ b/lib/services/buy/buy_data_loading_service.dart @@ -1,17 +1,13 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:stackwallet/providers/providers.dart'; import 'package:stackwallet/services/buy/simplex/simplex_api.dart'; -import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/logger.dart'; class BuyDataLoadingService { - Future loadAll(WidgetRef ref, {Coin? coin}) async { + Future loadAll(WidgetRef ref) async { try { await Future.wait([ - _loadFixedRateMarkets(ref, coin: coin), - _loadChangeNowStandardCurrencies(ref, coin: coin), - loadSimpleswapFixedRateCurrencies(ref), - loadSimpleswapFloatingRateCurrencies(ref), + _loadSimplexCurrencies(ref), ]); } catch (e, s) { Logging.instance.log("BuyDataLoadingService.loadAll failed: $e\n$s", @@ -19,183 +15,77 @@ class BuyDataLoadingService { } } - Future _loadFixedRateMarkets(WidgetRef ref, {Coin? coin}) async { - if (ref.read(changeNowFixedInitialLoadStatusStateProvider.state).state == - ChangeNowLoadStatus.loading) { - // already in progress so just - return; - } + Future _loadSimplexCurrencies(WidgetRef ref) async { + // if (ref + // .read(changeNowEstimatedInitialLoadStatusStateProvider.state) + // .state == + // ChangeNowLoadStatus.loading) { + // // already in progress so just + // return; + // } - ref.read(changeNowFixedInitialLoadStatusStateProvider.state).state = - ChangeNowLoadStatus.loading; + ref.read(simplexLoadStatusStateProvider.state).state = + SimplexLoadStatus.loading; - final response3 = - await ChangeNowAPI.instance.getAvailableFixedRateMarkets(); - if (response3.value != null) { - ref - .read(availableChangeNowCurrenciesProvider) - .updateMarkets(response3.value!); + print(11); - if (ref.read(buyFormStateProvider).market == null) { - String fromTicker = "btc"; - String toTicker = "xmr"; + final response = await SimplexAPI.instance.getSupported(); - if (coin != null) { - fromTicker = coin.ticker.toLowerCase(); - } - - final matchingMarkets = response3.value! - .where((e) => e.to == toTicker && e.from == fromTicker); - if (matchingMarkets.isNotEmpty) { - await ref - .read(buyFormStateProvider) - .updateMarket(matchingMarkets.first, true); - } - } - } else { - Logging.instance.log( - "Failed to load changeNOW fixed rate markets: ${response3.exception?.errorMessage}", - level: LogLevel.Error); - - ref.read(changeNowFixedInitialLoadStatusStateProvider.state).state = - ChangeNowLoadStatus.failed; - return; - } - - ref.read(changeNowFixedInitialLoadStatusStateProvider.state).state = - ChangeNowLoadStatus.success; - } - - Future _loadChangeNowStandardCurrencies( - WidgetRef ref, { - Coin? coin, - }) async { - if (ref - .read(changeNowEstimatedInitialLoadStatusStateProvider.state) - .state == - ChangeNowLoadStatus.loading) { - // already in progress so just - return; - } - - ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state = - ChangeNowLoadStatus.loading; - - final response = await ChangeNowAPI.instance.getAvailableCurrencies(); - final response2 = - await ChangeNowAPI.instance.getAvailableFloatingRatePairs(); - if (response.value != null) { - ref - .read(availableChangeNowCurrenciesProvider) - .updateCurrencies(response.value!); - - if (response2.value != null) { - ref - .read(availableChangeNowCurrenciesProvider) - .updateFloatingPairs(response2.value!); - - String fromTicker = "btc"; - String toTicker = "xmr"; - - if (coin != null) { - fromTicker = coin.ticker.toLowerCase(); - } - - if (response.value!.length > 1) { - if (ref.read(buyFormStateProvider).from == null) { - if (response.value! - .where((e) => e.ticker == fromTicker) - .isNotEmpty) { - await ref.read(buyFormStateProvider).updateFrom( - response.value!.firstWhere((e) => e.ticker == fromTicker), - false); - } - } - if (ref.read(buyFormStateProvider).to == null) { - if (response.value!.where((e) => e.ticker == toTicker).isNotEmpty) { - await ref.read(buyFormStateProvider).updateTo( - response.value!.firstWhere((e) => e.ticker == toTicker), - false); - } - } - } - } else { - Logging.instance.log( - "Failed to load changeNOW available floating rate pairs: ${response2.exception?.errorMessage}", - level: LogLevel.Error); - ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state = - ChangeNowLoadStatus.failed; - return; - } - } else { - Logging.instance.log( - "Failed to load changeNOW currencies: ${response.exception?.errorMessage}", - level: LogLevel.Error); - await Future.delayed(const Duration(seconds: 3)); - ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state = - ChangeNowLoadStatus.failed; - return; - } - - ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state = - ChangeNowLoadStatus.success; - } - - Future loadSimpleswapFloatingRateCurrencies(WidgetRef ref) async { - final buy = SimpleSwapBuy(); - final responseCurrencies = await buy.getAllCurrencies(false); - - if (responseCurrencies.value != null) { - ref - .read(availableSimpleswapCurrenciesProvider) - .updateFloatingCurrencies(responseCurrencies.value!); - - final responsePairs = await buy.getAllPairs(false); - - if (responsePairs.value != null) { - ref - .read(availableSimpleswapCurrenciesProvider) - .updateFloatingPairs(responsePairs.value!); - } else { - Logging.instance.log( - "loadSimpleswapFloatingRateCurrencies: $responsePairs", - level: LogLevel.Warning, - ); - } - } else { - Logging.instance.log( - "loadSimpleswapFloatingRateCurrencies: $responseCurrencies", - level: LogLevel.Warning, - ); - } - } - - Future loadSimpleswapFixedRateCurrencies(WidgetRef ref) async { - final buy = SimpleSwapBuy(); - final responseCurrencies = await buy.getAllCurrencies(true); - - if (responseCurrencies.value != null) { - ref - .read(availableSimpleswapCurrenciesProvider) - .updateFixedCurrencies(responseCurrencies.value!); - - final responsePairs = await buy.getAllPairs(true); - - if (responsePairs.value != null) { - ref - .read(availableSimpleswapCurrenciesProvider) - .updateFixedPairs(responsePairs.value!); - } else { - Logging.instance.log( - "loadSimpleswapFixedRateCurrencies: $responsePairs", - level: LogLevel.Warning, - ); - } - } else { - Logging.instance.log( - "loadSimpleswapFixedRateCurrencies: $responseCurrencies", - level: LogLevel.Warning, - ); - } + return; + // if (response.value != null) { + // ref + // .read(availableChangeNowCurrenciesProvider) + // .updateCurrencies(response.value!); + // + // if (response2.value != null) { + // ref + // .read(availableChangeNowCurrenciesProvider) + // .updateFloatingPairs(response2.value!); + // + // String fromTicker = "btc"; + // String toTicker = "xmr"; + // + // if (coin != null) { + // fromTicker = coin.ticker.toLowerCase(); + // } + // + // if (response.value!.length > 1) { + // if (ref.read(buyFormStateProvider).from == null) { + // if (response.value! + // .where((e) => e.ticker == fromTicker) + // .isNotEmpty) { + // await ref.read(buyFormStateProvider).updateFrom( + // response.value!.firstWhere((e) => e.ticker == fromTicker), + // false); + // } + // } + // if (ref.read(buyFormStateProvider).to == null) { + // if (response.value!.where((e) => e.ticker == toTicker).isNotEmpty) { + // await ref.read(buyFormStateProvider).updateTo( + // response.value!.firstWhere((e) => e.ticker == toTicker), + // false); + // } + // } + // } + // } else { + // Logging.instance.log( + // "Failed to load changeNOW available floating rate pairs: ${response2.exception?.errorMessage}", + // level: LogLevel.Error); + // ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state = + // ChangeNowLoadStatus.failed; + // return; + // } + // } else { + // Logging.instance.log( + // "Failed to load changeNOW currencies: ${response.exception?.errorMessage}", + // level: LogLevel.Error); + // await Future.delayed(const Duration(seconds: 3)); + // ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state = + // ChangeNowLoadStatus.failed; + // return; + // } + // + // ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state = + // ChangeNowLoadStatus.success; } } diff --git a/lib/services/buy/simplex/simplex_api.dart b/lib/services/buy/simplex/simplex_api.dart index fbc97cfe7..38b694e91 100644 --- a/lib/services/buy/simplex/simplex_api.dart +++ b/lib/services/buy/simplex/simplex_api.dart @@ -1,6 +1,5 @@ import 'dart:convert'; -import 'package:flutter/foundation.dart'; import 'package:http/http.dart' as http; // import 'package:stackwallet/models/exchange/response_objects/fixed_rate_market.dart'; // import 'package:stackwallet/models/exchange/response_objects/pair.dart'; @@ -140,16 +139,16 @@ class SimplexAPI { // } // } - Future>> getAllCurrencies({ - String? apiKey, - required bool fixedRate, - }) async { + dynamic /*Future>>*/ getSupported() async { final uri = _buildUri("/quote", {}); try { final jsonArray = await _makeGetRequest(uri); - return await compute(_parseAvailableCurrenciesJson, jsonArray as List); + print(jsonArray); + return null; + + // return await compute(_parseAvailableCurrenciesJson, jsonArray as List); } catch (e, s) { Logging.instance.log("getAvailableCurrencies exception: $e\n$s", level: LogLevel.Error);