diff --git a/lib/models/buy/response_objects/crypto.dart b/lib/models/buy/response_objects/crypto.dart index 31693eb3c..10b6d2bd2 100644 --- a/lib/models/buy/response_objects/crypto.dart +++ b/lib/models/buy/response_objects/crypto.dart @@ -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, ); } diff --git a/lib/pages/buy_view/buy_form.dart b/lib/pages/buy_view/buy_form.dart index d61714d9c..1ea779b66 100644 --- a/lib/pages/buy_view/buy_form.dart +++ b/lib/pages/buy_view/buy_form.dart @@ -108,7 +108,7 @@ class _BuyFormState extends ConsumerState { ), ), ); - await _loadSimplexCurrencies(); + await _loadSimplexCryptos(); shouldPop = true; if (mounted) { Navigator.of(context, rootNavigator: isDesktop).pop(); @@ -212,7 +212,7 @@ class _BuyFormState extends ConsumerState { ), ), ); - await _loadSimplexCurrencies(); + await _loadSimplexFiats(); shouldPop = true; if (mounted) { Navigator.of(context, rootNavigator: isDesktop).pop(); @@ -229,12 +229,29 @@ class _BuyFormState extends ConsumerState { ); } - Future _loadSimplexCurrencies() async { - final response = await SimplexAPI.instance.getSupported(); + Future _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 _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", diff --git a/lib/services/buy/simplex/simplex_api.dart b/lib/services/buy/simplex/simplex_api.dart index e807657e6..5230e3883 100644 --- a/lib/services/buy/simplex/simplex_api.dart +++ b/lib/services/buy/simplex/simplex_api.dart @@ -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, List>>> 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>> 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 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, List>> _parseSupported( - dynamic jsonArray) { + BuyResponse> _parseSupportedCryptos(dynamic jsonArray) { try { List cryptos = []; List fiats = []; - var supportedCryptos = - jsonArray['result']['supported_digital_currencies']; // TODO map List supportedCryptos to List - 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>> getSupportedFiats() async { + try { + Map 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> _parseSupportedFiats(dynamic jsonArray) { + try { + List cryptos = []; + List fiats = []; + + print(jsonArray); + var supportedFiats = jsonArray['result']['supported_fiat_currencies']; // TODO map List supportedFiats to 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) { Logging.instance .log("_parseSupported exception: $e\n$s", level: LogLevel.Error);