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/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<MaterialAppWithTheme>
// 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) {

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/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<HomeView> {
bool _exitEnabled = false;
final _exchangeDataLoadingService = ExchangeDataLoadingService();
final _buyDataLoadingService = BuyDataLoadingService();
Future<bool> _onWillPop() async {
// 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
void initState() {
_pageController = PageController();
@ -308,6 +320,9 @@ class _HomeViewState extends ConsumerState<HomeView> {
if (next == 1) {
_exchangeDataLoadingService.loadAll(ref);
}
if (next == 2) {
_buyDataLoadingService.loadAll(ref);
}
if (next >= 0 && next <= 2) {
_pageController.animateToPage(
next,

View file

@ -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<HomeViewButtonBar> {
if (selectedIndex != 2) {
ref.read(homeViewPageIndexStateProvider.state).state = 2;
}
await BuyDataLoadingService().loadAll(ref);
},
child: Text(
"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/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<StackPrivacyCalls> {
if (isEasy) {
unawaited(ExchangeDataLoadingService()
.loadAll(ref));
unawaited(
BuyDataLoadingService().loadAll(ref));
ref
.read(priceAnd24hChangeNotifierProvider)
.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/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<DesktopWalletView> {
late final bool _shouldDisableAutoSyncOnLogOut;
final _cnLoadingService = ExchangeDataLoadingService();
final _buyDataLoadingService = BuyDataLoadingService();
Future<void> onBackPressed() async {
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 {
final managerProvider = ref
.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/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';

View file

@ -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<void> loadAll(WidgetRef ref, {Coin? coin}) async {
Future<void> 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<void> _loadFixedRateMarkets(WidgetRef ref, {Coin? coin}) async {
if (ref.read(changeNowFixedInitialLoadStatusStateProvider.state).state ==
ChangeNowLoadStatus.loading) {
// already in progress so just
return;
}
Future<void> _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<void> _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<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,
);
}
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<void>.delayed(const Duration(seconds: 3));
// ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state =
// ChangeNowLoadStatus.failed;
// return;
// }
//
// ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state =
// ChangeNowLoadStatus.success;
}
}

View file

@ -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<BuyResponse<List<Fiat>>> getAllCurrencies({
String? apiKey,
required bool fixedRate,
}) async {
dynamic /*Future<BuyResponse<List<Fiat>>>*/ 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);