From 4f58483e82223e34b2356e5bd2bc2c27f33979c3 Mon Sep 17 00:00:00 2001 From: julian Date: Mon, 30 Jan 2023 14:08:21 -0600 Subject: [PATCH] show min/max crypto amounts returned by simplex api on quote request --- lib/pages/buy_view/buy_form.dart | 49 ++++++++++++----------- lib/services/buy/buy_response.dart | 6 ++- lib/services/buy/simplex/simplex_api.dart | 21 ++++++++-- 3 files changed, 47 insertions(+), 29 deletions(-) diff --git a/lib/pages/buy_view/buy_form.dart b/lib/pages/buy_view/buy_form.dart index a46238188..051afcf71 100644 --- a/lib/pages/buy_view/buy_form.dart +++ b/lib/pages/buy_view/buy_form.dart @@ -531,25 +531,25 @@ class _BuyFormState extends ConsumerState { } } else { // Error; probably amount out of bounds - String errorMessage = "${quoteResponse.exception?.errorMessage}"; - if (errorMessage.contains('must be between')) { - errorMessage = errorMessage.substring( - errorMessage.indexOf('getQuote exception: ') + 20, - errorMessage.indexOf(", value: null")); - _BuyFormState.boundedCryptoTicker = errorMessage.substring( - errorMessage.indexOf('The ') + 4, - errorMessage.indexOf(' amount must be between')); - _BuyFormState.minCrypto = Decimal.parse(errorMessage.substring( - errorMessage.indexOf('must be between ') + 16, - errorMessage.indexOf(' and '))); - _BuyFormState.maxCrypto = Decimal.parse(errorMessage.substring( - errorMessage.indexOf("$minCrypto and ") + "$minCrypto and ".length, - errorMessage.length)); - if (Decimal.parse(_buyAmountController.text) > - _BuyFormState.maxCrypto) { - _buyAmountController.text = _BuyFormState.maxCrypto.toString(); - } - } + // String errorMessage = "${quoteResponse.exception?.errorMessage}"; + // if (errorMessage.contains('must be between')) { + // errorMessage = errorMessage.substring( + // errorMessage.indexOf('getQuote exception: ') + 20, + // errorMessage.indexOf(", value: null")); + // _BuyFormState.boundedCryptoTicker = errorMessage.substring( + // errorMessage.indexOf('The ') + 4, + // errorMessage.indexOf(' amount must be between')); + // _BuyFormState.minCrypto = Decimal.parse(errorMessage.substring( + // errorMessage.indexOf('must be between ') + 16, + // errorMessage.indexOf(' and '))); + // _BuyFormState.maxCrypto = Decimal.parse(errorMessage.substring( + // errorMessage.indexOf("$minCrypto and ") + "$minCrypto and ".length, + // errorMessage.length)); + // if (Decimal.parse(_buyAmountController.text) > + // _BuyFormState.maxCrypto) { + // _buyAmountController.text = _BuyFormState.maxCrypto.toString(); + // } + // } await showDialog( context: context, barrierDismissible: true, @@ -571,7 +571,7 @@ class _BuyFormState extends ConsumerState { height: 24, ), Text( - errorMessage, + quoteResponse.exception!.errorMessage, style: STextStyles.smallMed14(context), ), const SizedBox( @@ -633,10 +633,11 @@ class _BuyFormState extends ConsumerState { level: LogLevel.Warning, ); return BuyResponse( - exception: BuyException( - response.toString(), - BuyExceptionType.generic, - ), + exception: response.exception ?? + BuyException( + response.toString(), + BuyExceptionType.generic, + ), ); } } diff --git a/lib/services/buy/buy_response.dart b/lib/services/buy/buy_response.dart index 3198d0a19..6510ece4c 100644 --- a/lib/services/buy/buy_response.dart +++ b/lib/services/buy/buy_response.dart @@ -1,4 +1,8 @@ -enum BuyExceptionType { generic, serializeResponseError } +enum BuyExceptionType { + generic, + serializeResponseError, + cryptoAmountOutOfRange, +} class BuyException implements Exception { String errorMessage; diff --git a/lib/services/buy/simplex/simplex_api.dart b/lib/services/buy/simplex/simplex_api.dart index 4060567f0..85b4d5d9e 100644 --- a/lib/services/buy/simplex/simplex_api.dart +++ b/lib/services/buy/simplex/simplex_api.dart @@ -210,7 +210,20 @@ class SimplexAPI { BuyResponse _parseQuote(dynamic jsonArray) { try { - String cryptoAmount = "${jsonArray['digital_money']['amount']}"; + // final Map lol = + // Map.from(jsonArray as Map); + + String? cryptoAmount = jsonArray['digital_money']?['amount'] as String?; + + if (cryptoAmount == null) { + String error = jsonArray['error'] as String; + return BuyResponse( + exception: BuyException( + error, + BuyExceptionType.cryptoAmountOutOfRange, + ), + ); + } SimplexQuote quote = jsonArray['quote'] as SimplexQuote; final SimplexQuote _quote = SimplexQuote( @@ -277,9 +290,9 @@ class SimplexAPI { } final jsonArray = jsonDecode(res.body); // TODO check if valid json if (jsonArray.containsKey('error') as bool) { - if (jsonArray['error'] == true || jsonArray['error'] == 'true') { - throw Exception(jsonArray['message']); - } + if (jsonArray['error'] == true || jsonArray['error'] == 'true') { + throw Exception(jsonArray['message']); + } } SimplexOrder _order = SimplexOrder(