first simplex service stash

This commit is contained in:
sneurlax 2023-01-11 18:13:34 -06:00
parent 7c58a3a47e
commit 8e8816fe05
9 changed files with 127 additions and 189 deletions

View file

@ -41,6 +41,7 @@ import 'package:stackwallet/providers/global/trades_service_provider.dart';
import 'package:stackwallet/providers/providers.dart'; import 'package:stackwallet/providers/providers.dart';
import 'package:stackwallet/providers/ui/color_theme_provider.dart'; import 'package:stackwallet/providers/ui/color_theme_provider.dart';
import 'package:stackwallet/route_generator.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/debug_service.dart';
import 'package:stackwallet/services/exchange/change_now/change_now_exchange.dart'; import 'package:stackwallet/services/exchange/change_now/change_now_exchange.dart';
import 'package:stackwallet/services/exchange/exchange_data_loading_service.dart'; import 'package:stackwallet/services/exchange/exchange_data_loading_service.dart';
@ -287,10 +288,14 @@ class _MaterialAppWithThemeState extends ConsumerState<MaterialAppWithTheme>
// unawaited(_nodeService.updateCommunityNodes()); // unawaited(_nodeService.updateCommunityNodes());
// run without awaiting // run without awaiting
if (Constants.enableExchange && if (ref.read(prefsChangeNotifierProvider).externalCalls &&
ref.read(prefsChangeNotifierProvider).externalCalls &&
await ref.read(prefsChangeNotifierProvider).isExternalCallsSet()) { 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) { if (ref.read(prefsChangeNotifierProvider).isAutoBackupEnabled) {

View file

@ -15,6 +15,7 @@ import 'package:stackwallet/providers/global/notifications_provider.dart';
import 'package:stackwallet/providers/global/prefs_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/home_view_index_provider.dart';
import 'package:stackwallet/providers/ui/unread_notifications_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/services/exchange/exchange_data_loading_service.dart';
import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/constants.dart'; import 'package:stackwallet/utilities/constants.dart';
@ -46,6 +47,7 @@ class _HomeViewState extends ConsumerState<HomeView> {
bool _exitEnabled = false; bool _exitEnabled = false;
final _exchangeDataLoadingService = ExchangeDataLoadingService(); final _exchangeDataLoadingService = ExchangeDataLoadingService();
final _buyDataLoadingService = BuyDataLoadingService();
Future<bool> _onWillPop() async { Future<bool> _onWillPop() async {
// go to home view when tapping back on the main exchange view // go to home view when tapping back on the main exchange view
@ -93,6 +95,16 @@ class _HomeViewState extends ConsumerState<HomeView> {
} }
} }
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 @override
void initState() { void initState() {
_pageController = PageController(); _pageController = PageController();
@ -308,6 +320,9 @@ class _HomeViewState extends ConsumerState<HomeView> {
if (next == 1) { if (next == 1) {
_exchangeDataLoadingService.loadAll(ref); _exchangeDataLoadingService.loadAll(ref);
} }
if (next == 2) {
_buyDataLoadingService.loadAll(ref);
}
if (next >= 0 && next <= 2) { if (next >= 0 && next <= 2) {
_pageController.animateToPage( _pageController.animateToPage(
next, next,

View file

@ -1,6 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:stackwallet/providers/providers.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/services/exchange/exchange_data_loading_service.dart';
import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/utilities/theme/stack_colors.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart';
@ -148,6 +149,7 @@ class _HomeViewButtonBarState extends ConsumerState<HomeViewButtonBar> {
if (selectedIndex != 2) { if (selectedIndex != 2) {
ref.read(homeViewPageIndexStateProvider.state).state = 2; ref.read(homeViewPageIndexStateProvider.state).state = 2;
} }
await BuyDataLoadingService().loadAll(ref);
}, },
child: Text( child: Text(
"Buy", "Buy",

View file

@ -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/pages_desktop_specific/password/create_password_view.dart';
import 'package:stackwallet/providers/global/prefs_provider.dart'; import 'package:stackwallet/providers/global/prefs_provider.dart';
import 'package:stackwallet/providers/global/price_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/services/exchange/exchange_data_loading_service.dart';
import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/constants.dart'; import 'package:stackwallet/utilities/constants.dart';
@ -232,6 +233,8 @@ class _StackPrivacyCalls extends ConsumerState<StackPrivacyCalls> {
if (isEasy) { if (isEasy) {
unawaited(ExchangeDataLoadingService() unawaited(ExchangeDataLoadingService()
.loadAll(ref)); .loadAll(ref));
unawaited(
BuyDataLoadingService().loadAll(ref));
ref ref
.read(priceAnd24hChangeNotifierProvider) .read(priceAnd24hChangeNotifierProvider)
.start(true); .start(true);

View file

@ -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/global/auto_swb_service_provider.dart';
import 'package:stackwallet/providers/providers.dart'; import 'package:stackwallet/providers/providers.dart';
import 'package:stackwallet/providers/ui/transaction_filter_provider.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/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/events/global/wallet_sync_status_changed_event.dart';
import 'package:stackwallet/services/event_bus/global_event_bus.dart'; import 'package:stackwallet/services/event_bus/global_event_bus.dart';
@ -64,6 +65,7 @@ class _DesktopWalletViewState extends ConsumerState<DesktopWalletView> {
late final bool _shouldDisableAutoSyncOnLogOut; late final bool _shouldDisableAutoSyncOnLogOut;
final _cnLoadingService = ExchangeDataLoadingService(); final _cnLoadingService = ExchangeDataLoadingService();
final _buyDataLoadingService = BuyDataLoadingService();
Future<void> onBackPressed() async { Future<void> onBackPressed() async {
await _logout(); await _logout();
@ -103,6 +105,16 @@ class _DesktopWalletViewState extends ConsumerState<DesktopWalletView> {
} }
} }
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 { void _onExchangePressed(BuildContext context) async {
final managerProvider = ref final managerProvider = ref
.read(walletsChangeNotifierProvider) .read(walletsChangeNotifierProvider)

View file

@ -0,0 +1,11 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
enum SimplexLoadStatus {
waiting,
loading,
success,
failed,
}
final simplexLoadStatusStateProvider =
StateProvider<SimplexLoadStatus>((ref) => SimplexLoadStatus.waiting);

View file

@ -1,4 +1,5 @@
export './buy/buy_form_state_provider.dart'; export './buy/buy_form_state_provider.dart';
export './buy/simplex_initial_load_status.dart';
export './exchange/available_changenow_currencies_provider.dart'; export './exchange/available_changenow_currencies_provider.dart';
export './exchange/available_simpleswap_currencies_provider.dart'; export './exchange/available_simpleswap_currencies_provider.dart';
export './exchange/changenow_initial_load_status.dart'; export './exchange/changenow_initial_load_status.dart';

View file

@ -1,17 +1,13 @@
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:stackwallet/providers/providers.dart'; import 'package:stackwallet/providers/providers.dart';
import 'package:stackwallet/services/buy/simplex/simplex_api.dart'; import 'package:stackwallet/services/buy/simplex/simplex_api.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/logger.dart';
class BuyDataLoadingService { class BuyDataLoadingService {
Future<void> loadAll(WidgetRef ref, {Coin? coin}) async { Future<void> loadAll(WidgetRef ref) async {
try { try {
await Future.wait([ await Future.wait([
_loadFixedRateMarkets(ref, coin: coin), _loadSimplexCurrencies(ref),
_loadChangeNowStandardCurrencies(ref, coin: coin),
loadSimpleswapFixedRateCurrencies(ref),
loadSimpleswapFloatingRateCurrencies(ref),
]); ]);
} catch (e, s) { } catch (e, s) {
Logging.instance.log("BuyDataLoadingService.loadAll failed: $e\n$s", Logging.instance.log("BuyDataLoadingService.loadAll failed: $e\n$s",
@ -19,183 +15,77 @@ class BuyDataLoadingService {
} }
} }
Future<void> _loadFixedRateMarkets(WidgetRef ref, {Coin? coin}) async { Future<void> _loadSimplexCurrencies(WidgetRef ref) async {
if (ref.read(changeNowFixedInitialLoadStatusStateProvider.state).state == // if (ref
ChangeNowLoadStatus.loading) { // .read(changeNowEstimatedInitialLoadStatusStateProvider.state)
// already in progress so just // .state ==
return; // ChangeNowLoadStatus.loading) {
} // // already in progress so just
// return;
// }
ref.read(changeNowFixedInitialLoadStatusStateProvider.state).state = ref.read(simplexLoadStatusStateProvider.state).state =
ChangeNowLoadStatus.loading; SimplexLoadStatus.loading;
final response3 = print(11);
await ChangeNowAPI.instance.getAvailableFixedRateMarkets();
if (response3.value != null) {
ref
.read(availableChangeNowCurrenciesProvider)
.updateMarkets(response3.value!);
if (ref.read(buyFormStateProvider).market == null) { final response = await SimplexAPI.instance.getSupported();
String fromTicker = "btc";
String toTicker = "xmr";
if (coin != null) { return;
fromTicker = coin.ticker.toLowerCase(); // if (response.value != null) {
} // ref
// .read(availableChangeNowCurrenciesProvider)
final matchingMarkets = response3.value! // .updateCurrencies(response.value!);
.where((e) => e.to == toTicker && e.from == fromTicker); //
if (matchingMarkets.isNotEmpty) { // if (response2.value != null) {
await ref // ref
.read(buyFormStateProvider) // .read(availableChangeNowCurrenciesProvider)
.updateMarket(matchingMarkets.first, true); // .updateFloatingPairs(response2.value!);
} //
} // String fromTicker = "btc";
} else { // String toTicker = "xmr";
Logging.instance.log( //
"Failed to load changeNOW fixed rate markets: ${response3.exception?.errorMessage}", // if (coin != null) {
level: LogLevel.Error); // fromTicker = coin.ticker.toLowerCase();
// }
ref.read(changeNowFixedInitialLoadStatusStateProvider.state).state = //
ChangeNowLoadStatus.failed; // if (response.value!.length > 1) {
return; // if (ref.read(buyFormStateProvider).from == null) {
} // if (response.value!
// .where((e) => e.ticker == fromTicker)
ref.read(changeNowFixedInitialLoadStatusStateProvider.state).state = // .isNotEmpty) {
ChangeNowLoadStatus.success; // await ref.read(buyFormStateProvider).updateFrom(
} // response.value!.firstWhere((e) => e.ticker == fromTicker),
// false);
Future<void> _loadChangeNowStandardCurrencies( // }
WidgetRef ref, { // }
Coin? coin, // if (ref.read(buyFormStateProvider).to == null) {
}) async { // if (response.value!.where((e) => e.ticker == toTicker).isNotEmpty) {
if (ref // await ref.read(buyFormStateProvider).updateTo(
.read(changeNowEstimatedInitialLoadStatusStateProvider.state) // response.value!.firstWhere((e) => e.ticker == toTicker),
.state == // false);
ChangeNowLoadStatus.loading) { // }
// already in progress so just // }
return; // }
} // } else {
// Logging.instance.log(
ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state = // "Failed to load changeNOW available floating rate pairs: ${response2.exception?.errorMessage}",
ChangeNowLoadStatus.loading; // level: LogLevel.Error);
// ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state =
final response = await ChangeNowAPI.instance.getAvailableCurrencies(); // ChangeNowLoadStatus.failed;
final response2 = // return;
await ChangeNowAPI.instance.getAvailableFloatingRatePairs(); // }
if (response.value != null) { // } else {
ref // Logging.instance.log(
.read(availableChangeNowCurrenciesProvider) // "Failed to load changeNOW currencies: ${response.exception?.errorMessage}",
.updateCurrencies(response.value!); // level: LogLevel.Error);
// await Future<void>.delayed(const Duration(seconds: 3));
if (response2.value != null) { // ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state =
ref // ChangeNowLoadStatus.failed;
.read(availableChangeNowCurrenciesProvider) // return;
.updateFloatingPairs(response2.value!); // }
//
String fromTicker = "btc"; // ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state =
String toTicker = "xmr"; // ChangeNowLoadStatus.success;
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<void>.delayed(const Duration(seconds: 3));
ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state =
ChangeNowLoadStatus.failed;
return;
}
ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state =
ChangeNowLoadStatus.success;
}
Future<void> 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<void> 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,
);
}
} }
} }

View file

@ -1,6 +1,5 @@
import 'dart:convert'; import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http; 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/fixed_rate_market.dart';
// import 'package:stackwallet/models/exchange/response_objects/pair.dart'; // import 'package:stackwallet/models/exchange/response_objects/pair.dart';
@ -140,16 +139,16 @@ class SimplexAPI {
// } // }
// } // }
Future<BuyResponse<List<Fiat>>> getAllCurrencies({ dynamic /*Future<BuyResponse<List<Fiat>>>*/ getSupported() async {
String? apiKey,
required bool fixedRate,
}) async {
final uri = _buildUri("/quote", {}); final uri = _buildUri("/quote", {});
try { try {
final jsonArray = await _makeGetRequest(uri); 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) { } catch (e, s) {
Logging.instance.log("getAvailableCurrencies exception: $e\n$s", Logging.instance.log("getAvailableCurrencies exception: $e\n$s",
level: LogLevel.Error); level: LogLevel.Error);