use simplex_api supported_cryptos and update crypto model

This commit is contained in:
sneurlax 2023-01-20 15:30:35 -06:00
parent 51a9c7e961
commit adf83aa3a6
3 changed files with 99 additions and 24 deletions

View file

@ -5,12 +5,20 @@ class Crypto {
/// Crypto name /// Crypto name
final String name; final String name;
/// Crypto network
final String? network;
/// Crypto contract address
final String? contractAddress;
/// Crypto logo url /// Crypto logo url
final String image; final String image;
Crypto({ Crypto({
required this.ticker, required this.ticker,
required this.name, required this.name,
required this.network,
required this.contractAddress,
required this.image, required this.image,
}); });
@ -19,6 +27,8 @@ class Crypto {
return Crypto( return Crypto(
ticker: json["ticker"] as String, ticker: json["ticker"] as String,
name: json["name"] as String, name: json["name"] as String,
network: json["network"] as String,
contractAddress: json["contractAddress"] as String,
image: json["image"] as String, image: json["image"] as String,
); );
} catch (e) { } catch (e) {
@ -30,6 +40,8 @@ class Crypto {
final map = { final map = {
"ticker": ticker, "ticker": ticker,
"name": name, "name": name,
"network": network,
"contractAddress": contractAddress,
"image": image, "image": image,
}; };
@ -44,6 +56,8 @@ class Crypto {
return Crypto( return Crypto(
ticker: ticker ?? this.ticker, ticker: ticker ?? this.ticker,
name: name ?? this.name, name: name ?? this.name,
network: network ?? this.network,
contractAddress: contractAddress ?? this.contractAddress,
image: image ?? this.image, image: image ?? this.image,
); );
} }

View file

@ -108,7 +108,7 @@ class _BuyFormState extends ConsumerState<BuyForm> {
), ),
), ),
); );
await _loadSimplexCurrencies(); await _loadSimplexCryptos();
shouldPop = true; shouldPop = true;
if (mounted) { if (mounted) {
Navigator.of(context, rootNavigator: isDesktop).pop(); Navigator.of(context, rootNavigator: isDesktop).pop();
@ -212,7 +212,7 @@ class _BuyFormState extends ConsumerState<BuyForm> {
), ),
), ),
); );
await _loadSimplexCurrencies(); await _loadSimplexFiats();
shouldPop = true; shouldPop = true;
if (mounted) { if (mounted) {
Navigator.of(context, rootNavigator: isDesktop).pop(); Navigator.of(context, rootNavigator: isDesktop).pop();
@ -229,12 +229,29 @@ class _BuyFormState extends ConsumerState<BuyForm> {
); );
} }
Future<void> _loadSimplexCurrencies() async { Future<void> _loadSimplexCryptos() async {
final response = await SimplexAPI.instance.getSupported(); final response = await SimplexAPI.instance.getSupportedCryptos();
print('test1');
print(response.value);
if (response.value != null) { if (response.value != null) {
ref.read(simplexProvider).updateSupportedCryptos(response.value!.item1); ref.read(simplexProvider).updateSupportedCryptos(response.value!);
ref.read(simplexProvider).updateSupportedFiats(response.value!.item2); } else {
Logging.instance.log(
"_loadSimplexCurrencies: $response",
level: LogLevel.Warning,
);
}
}
Future<void> _loadSimplexFiats() async {
final response = await SimplexAPI.instance.getSupportedFiats();
print(response.value);
if (response.value != null) {
ref.read(simplexProvider).updateSupportedFiats(response.value!);
} else { } else {
Logging.instance.log( Logging.instance.log(
"_loadSimplexCurrencies: $response", "_loadSimplexCurrencies: $response",

View file

@ -10,7 +10,6 @@ import 'package:stackwallet/models/buy/response_objects/order.dart';
import 'package:stackwallet/models/buy/response_objects/quote.dart'; import 'package:stackwallet/models/buy/response_objects/quote.dart';
import 'package:stackwallet/services/buy/buy_response.dart'; import 'package:stackwallet/services/buy/buy_response.dart';
import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/logger.dart';
import 'package:tuple/tuple.dart';
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';
class SimplexAPI { class SimplexAPI {
@ -28,9 +27,7 @@ class SimplexAPI {
return Uri.https(authority, path, params); return Uri.https(authority, path, params);
} }
Future<BuyResponse<Tuple2<List<Crypto>, List<Fiat>>>> getSupported() async { Future<BuyResponse<List<Crypto>>> getSupportedCryptos() async {
// example for quote courtesy of @danrmiller
// curl -H "Content-Type: application/json" -d '{"digital_currency": "BTC", "fiat_currency": "USD", "requested_currency": "USD", "requested_amount": 100}' http://sandbox-api.stackwallet.com/quote
// official docs reference eg // official docs reference eg
// curl --request GET \ // curl --request GET \
// --url https://sandbox.test-simplexcc.com/v2/supported_crypto_currencies \ // --url https://sandbox.test-simplexcc.com/v2/supported_crypto_currencies \
@ -38,13 +35,11 @@ class SimplexAPI {
try { try {
Map<String, String> headers = { Map<String, String> headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded',
}; };
String data = Uri url = Uri.parse('http://localhost/api.php?ROUTE=supported_cryptos');
'{"digital_currency": "BTC", "fiat_currency": "USD", "requested_currency": "USD", "requested_amount": 100}';
Uri url = Uri.parse('http://sandbox-api.stackwallet.com/quote');
var res = await http.post(url, headers: headers, body: data); var res = await http.post(url, headers: headers);
if (res.statusCode != 200) { if (res.statusCode != 200) {
throw Exception( throw Exception(
@ -53,7 +48,7 @@ class SimplexAPI {
final jsonArray = jsonDecode(res.body); final jsonArray = jsonDecode(res.body);
return await compute(_parseSupported, jsonArray); return await compute(_parseSupportedCryptos, jsonArray);
} 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);
@ -66,22 +61,71 @@ class SimplexAPI {
} }
} }
BuyResponse<Tuple2<List<Crypto>, List<Fiat>>> _parseSupported( BuyResponse<List<Crypto>> _parseSupportedCryptos(dynamic jsonArray) {
dynamic jsonArray) {
try { try {
List<Crypto> cryptos = []; List<Crypto> cryptos = [];
List<Fiat> fiats = []; List<Fiat> fiats = [];
var supportedCryptos =
jsonArray['result']['supported_digital_currencies'];
// TODO map List<String> supportedCryptos to List<Crypto> // TODO map List<String> supportedCryptos to List<Crypto>
for (final ticker in supportedCryptos as List) { for (final crypto in jsonArray as List) {
cryptos.add(Crypto.fromJson({ cryptos.add(Crypto.fromJson({
'ticker': ticker as String, 'ticker': "${crypto['ticker_symbol']}",
'name': ticker, 'name': crypto['name'],
'network': "${crypto['network']}",
'contractAddress': "${crypto['contractAddress']}",
'image': "", 'image': "",
})); }));
} }
return BuyResponse(value: cryptos);
} catch (e, s) {
Logging.instance
.log("_parseSupported exception: $e\n$s", level: LogLevel.Error);
return BuyResponse(
exception: BuyException(
e.toString(),
BuyExceptionType.generic,
),
);
}
}
Future<BuyResponse<List<Fiat>>> getSupportedFiats() async {
try {
Map<String, String> headers = {
'Content-Type': 'application/x-www-form-urlencoded',
};
Uri url = Uri.parse('http://localhost/api.php?ROUTE=supported_fiats');
var res = await http.post(url, headers: headers);
if (res.statusCode != 200) {
throw Exception(
'getAvailableCurrencies exception: statusCode= ${res.statusCode}');
}
final jsonArray = jsonDecode(res.body);
return await compute(_parseSupportedFiats, jsonArray);
} catch (e, s) {
Logging.instance.log("getAvailableCurrencies exception: $e\n$s",
level: LogLevel.Error);
return BuyResponse(
exception: BuyException(
e.toString(),
BuyExceptionType.generic,
),
);
}
}
BuyResponse<List<Fiat>> _parseSupportedFiats(dynamic jsonArray) {
try {
List<Crypto> cryptos = [];
List<Fiat> fiats = [];
print(jsonArray);
var supportedFiats = jsonArray['result']['supported_fiat_currencies']; var supportedFiats = jsonArray['result']['supported_fiat_currencies'];
// TODO map List<String> supportedFiats to List<Fiat> // TODO map List<String> supportedFiats to List<Fiat>
for (final ticker in supportedFiats as List) { for (final ticker in supportedFiats as List) {
@ -92,7 +136,7 @@ class SimplexAPI {
})); }));
} }
return BuyResponse(value: Tuple2(cryptos, fiats)); return BuyResponse(value: fiats);
} catch (e, s) { } catch (e, s) {
Logging.instance Logging.instance
.log("_parseSupported exception: $e\n$s", level: LogLevel.Error); .log("_parseSupported exception: $e\n$s", level: LogLevel.Error);