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/simplex_initial_load_status.dart';
export './exchange/available_changenow_currencies_provider.dart';

View file

@ -16,10 +16,9 @@ class BuyDataLoadingService {
}
Future<void> _loadSimplexCurrencies(WidgetRef ref) async {
// if (ref
// .read(changeNowEstimatedInitialLoadStatusStateProvider.state)
// .state ==
// ChangeNowLoadStatus.loading) {
bool error = false;
// if (ref.read(simplexLoadStatusStateProvider.state).state ==
// SimplexLoadStatus.loading) {
// // already in progress so just
// return;
// }
@ -27,65 +26,38 @@ class BuyDataLoadingService {
ref.read(simplexLoadStatusStateProvider.state).state =
SimplexLoadStatus.loading;
print(11);
final response = await SimplexAPI.instance.getSupported();
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;
if (response['supportedCryptos'] != null) {
print(response['supportedCryptos']);
ref
.read(availableSimplexCurrenciesProvider)
.updateSupportedCryptos(response['supportedCryptos']!);
} else {
error = true;
Logging.instance.log(
"_loadSimplexCurrencies: $response",
level: LogLevel.Warning,
);
}
if (response['supportedFiats'] != null) {
print(response['supportedFiats']);
ref
.read(availableSimplexCurrenciesProvider)
.updateSupportedFiats(response['supportedFiats']!);
} else {
error = true;
Logging.instance.log(
"_loadSimplexCurrencies: $response",
level: LogLevel.Warning,
);
}
if (error) {
// _loadSimplexCurrencies() again?
} else {
ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state =
ChangeNowLoadStatus.success;
}
}
}

View file

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