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

View file

@ -108,7 +108,7 @@ class _BuyFormState extends ConsumerState<BuyForm> {
),
),
);
await _loadSimplexCurrencies();
await _loadSimplexCryptos();
shouldPop = true;
if (mounted) {
Navigator.of(context, rootNavigator: isDesktop).pop();
@ -212,7 +212,7 @@ class _BuyFormState extends ConsumerState<BuyForm> {
),
),
);
await _loadSimplexCurrencies();
await _loadSimplexFiats();
shouldPop = true;
if (mounted) {
Navigator.of(context, rootNavigator: isDesktop).pop();
@ -229,12 +229,29 @@ class _BuyFormState extends ConsumerState<BuyForm> {
);
}
Future<void> _loadSimplexCurrencies() async {
final response = await SimplexAPI.instance.getSupported();
Future<void> _loadSimplexCryptos() async {
final response = await SimplexAPI.instance.getSupportedCryptos();
print('test1');
print(response.value);
if (response.value != null) {
ref.read(simplexProvider).updateSupportedCryptos(response.value!.item1);
ref.read(simplexProvider).updateSupportedFiats(response.value!.item2);
ref.read(simplexProvider).updateSupportedCryptos(response.value!);
} 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 {
Logging.instance.log(
"_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/services/buy/buy_response.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:tuple/tuple.dart';
import 'package:url_launcher/url_launcher.dart';
class SimplexAPI {
@ -28,9 +27,7 @@ class SimplexAPI {
return Uri.https(authority, path, params);
}
Future<BuyResponse<Tuple2<List<Crypto>, List<Fiat>>>> getSupported() 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
Future<BuyResponse<List<Crypto>>> getSupportedCryptos() async {
// official docs reference eg
// curl --request GET \
// --url https://sandbox.test-simplexcc.com/v2/supported_crypto_currencies \
@ -38,13 +35,11 @@ class SimplexAPI {
try {
Map<String, String> headers = {
'Content-Type': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
};
String data =
'{"digital_currency": "BTC", "fiat_currency": "USD", "requested_currency": "USD", "requested_amount": 100}';
Uri url = Uri.parse('http://sandbox-api.stackwallet.com/quote');
Uri url = Uri.parse('http://localhost/api.php?ROUTE=supported_cryptos');
var res = await http.post(url, headers: headers, body: data);
var res = await http.post(url, headers: headers);
if (res.statusCode != 200) {
throw Exception(
@ -53,7 +48,7 @@ class SimplexAPI {
final jsonArray = jsonDecode(res.body);
return await compute(_parseSupported, jsonArray);
return await compute(_parseSupportedCryptos, jsonArray);
} catch (e, s) {
Logging.instance.log("getAvailableCurrencies exception: $e\n$s",
level: LogLevel.Error);
@ -66,22 +61,71 @@ class SimplexAPI {
}
}
BuyResponse<Tuple2<List<Crypto>, List<Fiat>>> _parseSupported(
dynamic jsonArray) {
BuyResponse<List<Crypto>> _parseSupportedCryptos(dynamic jsonArray) {
try {
List<Crypto> cryptos = [];
List<Fiat> fiats = [];
var supportedCryptos =
jsonArray['result']['supported_digital_currencies'];
// 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({
'ticker': ticker as String,
'name': ticker,
'ticker': "${crypto['ticker_symbol']}",
'name': crypto['name'],
'network': "${crypto['network']}",
'contractAddress': "${crypto['contractAddress']}",
'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'];
// TODO map List<String> supportedFiats to List<Fiat>
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) {
Logging.instance
.log("_parseSupported exception: $e\n$s", level: LogLevel.Error);