add available simplex currencies provider

This commit is contained in:
sneurlax 2023-01-12 20:21:19 -06:00
parent b18ee6ba19
commit 44d3c26385
5 changed files with 121 additions and 124 deletions

View file

@ -0,0 +1,16 @@
// import 'package:stackwallet/models/buy/response_objects/crypto.dart';
// import 'package:stackwallet/models/buy/response_objects/fiat.dart';
// import 'package:stackwallet/models/buy/response_objects/pair.dart';
class SimplexAvailableCurrencies {
dynamic supportedCryptos = [];
dynamic supportedFiats = [];
void updateSupportedCryptos(dynamic newCryptos) {
supportedCryptos = newCryptos;
}
void updateSupportedFiats(dynamic newFiats) {
supportedFiats = newFiats;
}
}

View file

@ -0,0 +1,6 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:stackwallet/models/buy/simplex/simplex_supported_currencies.dart';
final availableSimplexCurrenciesProvider = Provider<SimplexAvailableCurrencies>(
(ref) => SimplexAvailableCurrencies(),
);

View file

@ -1,3 +1,4 @@
export './buy/available_simplex_currencies_provider.dart';
export './buy/buy_form_state_provider.dart'; export './buy/buy_form_state_provider.dart';
export './buy/simplex_initial_load_status.dart'; export './buy/simplex_initial_load_status.dart';
export './exchange/available_changenow_currencies_provider.dart'; export './exchange/available_changenow_currencies_provider.dart';

View file

@ -16,10 +16,9 @@ class BuyDataLoadingService {
} }
Future<void> _loadSimplexCurrencies(WidgetRef ref) async { Future<void> _loadSimplexCurrencies(WidgetRef ref) async {
// if (ref bool error = false;
// .read(changeNowEstimatedInitialLoadStatusStateProvider.state) // if (ref.read(simplexLoadStatusStateProvider.state).state ==
// .state == // SimplexLoadStatus.loading) {
// ChangeNowLoadStatus.loading) {
// // already in progress so just // // already in progress so just
// return; // return;
// } // }
@ -27,65 +26,38 @@ class BuyDataLoadingService {
ref.read(simplexLoadStatusStateProvider.state).state = ref.read(simplexLoadStatusStateProvider.state).state =
SimplexLoadStatus.loading; SimplexLoadStatus.loading;
print(11);
final response = await SimplexAPI.instance.getSupported(); final response = await SimplexAPI.instance.getSupported();
return; if (response['supportedCryptos'] != null) {
// if (response.value != null) { print(response['supportedCryptos']);
// ref ref
// .read(availableChangeNowCurrenciesProvider) .read(availableSimplexCurrenciesProvider)
// .updateCurrencies(response.value!); .updateSupportedCryptos(response['supportedCryptos']!);
// } else {
// if (response2.value != null) { error = true;
// ref Logging.instance.log(
// .read(availableChangeNowCurrenciesProvider) "_loadSimplexCurrencies: $response",
// .updateFloatingPairs(response2.value!); level: LogLevel.Warning,
// );
// String fromTicker = "btc"; }
// String toTicker = "xmr"; if (response['supportedFiats'] != null) {
// print(response['supportedFiats']);
// if (coin != null) { ref
// fromTicker = coin.ticker.toLowerCase(); .read(availableSimplexCurrenciesProvider)
// } .updateSupportedFiats(response['supportedFiats']!);
// } else {
// if (response.value!.length > 1) { error = true;
// if (ref.read(buyFormStateProvider).from == null) { Logging.instance.log(
// if (response.value! "_loadSimplexCurrencies: $response",
// .where((e) => e.ticker == fromTicker) level: LogLevel.Warning,
// .isNotEmpty) { );
// await ref.read(buyFormStateProvider).updateFrom( }
// response.value!.firstWhere((e) => e.ticker == fromTicker),
// false); if (error) {
// } // _loadSimplexCurrencies() again?
// } } else {
// if (ref.read(buyFormStateProvider).to == null) { ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state =
// if (response.value!.where((e) => e.ticker == toTicker).isNotEmpty) { ChangeNowLoadStatus.success;
// 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

@ -26,53 +26,53 @@ class SimplexAPI {
return Uri.https(authority, path, params); return Uri.https(authority, path, params);
} }
Future<dynamic> _makeGetRequest(Uri uri) async { // Future<dynamic> _makeGetRequest(Uri uri) async {
final client = this.client ?? http.Client(); // final client = this.client ?? http.Client();
int code = -1; // int code = -1;
try { // try {
final response = await client.get( // final response = await client.get(
uri, // uri,
); // );
//
code = response.statusCode; // code = response.statusCode;
//
final parsed = jsonDecode(response.body); // final parsed = jsonDecode(response.body);
//
return parsed; // return parsed;
} catch (e, s) { // } catch (e, s) {
Logging.instance.log( // Logging.instance.log(
"_makeRequest($uri) HTTP:$code threw: $e\n$s", // "_makeRequest($uri) HTTP:$code threw: $e\n$s",
level: LogLevel.Error, // level: LogLevel.Error,
); // );
rethrow; // rethrow;
} // }
} // }
//
Future<dynamic> _makePostRequest( // Future<dynamic> _makePostRequest(
Uri uri, // Uri uri,
Map<String, dynamic> body, // Map<String, dynamic> body,
) async { // ) async {
final client = this.client ?? http.Client(); // final client = this.client ?? http.Client();
try { // try {
final response = await client.post( // final response = await client.post(
uri, // uri,
headers: {'Content-Type': 'application/json'}, // headers: {'Content-Type': 'application/json'},
body: jsonEncode(body), // body: jsonEncode(body),
); // );
//
if (response.statusCode == 200) { // if (response.statusCode == 200) {
final parsed = jsonDecode(response.body); // final parsed = jsonDecode(response.body);
return parsed; // return parsed;
} // }
//
throw Exception("response: ${response.body}"); // throw Exception("response: ${response.body}");
} catch (e, s) { // } catch (e, s) {
Logging.instance // Logging.instance
.log("_makeRequest($uri) threw: $e\n$s", level: LogLevel.Error); // .log("_makeRequest($uri) threw: $e\n$s", level: LogLevel.Error);
rethrow; // rethrow;
} // }
} // }
//
// Future<ExchangeResponse<Trade>> createNewExchange({ // Future<ExchangeResponse<Trade>> createNewExchange({
// required bool isFixedRate, // required bool isFixedRate,
// required String currencyFrom, // required String currencyFrom,
@ -163,11 +163,8 @@ class SimplexAPI {
'getAvailableCurrencies exception: statusCode= ${res.statusCode}'); 'getAvailableCurrencies exception: statusCode= ${res.statusCode}');
} }
print(res.body);
dynamic jsonArray = jsonDecode(res.body); dynamic jsonArray = jsonDecode(res.body);
print(jsonArray);
return await compute(_parseSupported, jsonArray); return await compute(_parseSupported, jsonArray);
} catch (e, s) { } catch (e, s) {
Logging.instance.log("getAvailableCurrencies exception: $e\n$s", Logging.instance.log("getAvailableCurrencies exception: $e\n$s",
@ -181,25 +178,30 @@ class SimplexAPI {
} }
} }
BuyResponse<List<Fiat>> _parseSupported(dynamic jsonArray) { dynamic /*BuyResponse<List<Fiat>>*/ _parseSupported(dynamic jsonArray) {
try { try {
List<Fiat> cryptos = []; List<Fiat> cryptos = [];
List<Fiat> fiats = []; List<Fiat> fiats = [];
for (final json in jsonArray) { var supportedCryptos =
dynamic supportedCryptos =
jsonArray['result']['supported_digital_currencies']; jsonArray['result']['supported_digital_currencies'];
print(supportedCryptos); // for (final ticker in supportedCryptos as List) {
return BuyResponse( // cryptos.add(Fiat.fromString(ticker as String));
exception: BuyException("Failed to serialize $json", // }
BuyExceptionType.serializeResponseError)); var supportedFiats = jsonArray['result']['supported_fiat_currencies'];
dynamic supportedFiats = jsonArray['result']['supported_fiat_currencies']; // for (final ticker in supportedFiats as List) {
print(supportedFiats); // fiats.add(Fiat.fromString(ticker as String));
// }
return BuyResponse(value: cryptos); var supported = {
'supportedCryptos': supportedCryptos,
'supportedFiats': supportedFiats
};
return supported;
} catch (e, s) { } catch (e, s) {
Logging.instance.log("_parseAvailableCurrenciesJson exception: $e\n$s", Logging.instance
level: LogLevel.Error); .log("_parseSupported exception: $e\n$s", level: LogLevel.Error);
return BuyResponse( return BuyResponse(
exception: BuyException( exception: BuyException(
e.toString(), e.toString(),