show min/max crypto amounts returned by simplex api on quote request

This commit is contained in:
julian 2023-01-30 14:08:21 -06:00
parent 13721df803
commit 4f58483e82
3 changed files with 47 additions and 29 deletions

View file

@ -531,25 +531,25 @@ class _BuyFormState extends ConsumerState<BuyForm> {
}
} 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<dynamic>(
context: context,
barrierDismissible: true,
@ -571,7 +571,7 @@ class _BuyFormState extends ConsumerState<BuyForm> {
height: 24,
),
Text(
errorMessage,
quoteResponse.exception!.errorMessage,
style: STextStyles.smallMed14(context),
),
const SizedBox(
@ -633,10 +633,11 @@ class _BuyFormState extends ConsumerState<BuyForm> {
level: LogLevel.Warning,
);
return BuyResponse(
exception: BuyException(
response.toString(),
BuyExceptionType.generic,
),
exception: response.exception ??
BuyException(
response.toString(),
BuyExceptionType.generic,
),
);
}
}

View file

@ -1,4 +1,8 @@
enum BuyExceptionType { generic, serializeResponseError }
enum BuyExceptionType {
generic,
serializeResponseError,
cryptoAmountOutOfRange,
}
class BuyException implements Exception {
String errorMessage;

View file

@ -210,7 +210,20 @@ class SimplexAPI {
BuyResponse<SimplexQuote> _parseQuote(dynamic jsonArray) {
try {
String cryptoAmount = "${jsonArray['digital_money']['amount']}";
// final Map<String, dynamic> lol =
// Map<String, dynamic>.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(