diff --git a/lib/models/exchange/fixed_rate_exchange_form_state.dart b/lib/models/exchange/fixed_rate_exchange_form_state.dart index 1ae6fac96..65104ef8b 100644 --- a/lib/models/exchange/fixed_rate_exchange_form_state.dart +++ b/lib/models/exchange/fixed_rate_exchange_form_state.dart @@ -1,7 +1,7 @@ import 'package:decimal/decimal.dart'; import 'package:flutter/cupertino.dart'; import 'package:stackwallet/models/exchange/change_now/cn_exchange_estimate.dart'; -import 'package:stackwallet/models/exchange/change_now/fixed_rate_market.dart'; +import 'package:stackwallet/models/exchange/response_objects/fixed_rate_market.dart'; import 'package:stackwallet/services/exchange/change_now/change_now_api.dart'; import 'package:stackwallet/utilities/logger.dart'; diff --git a/lib/models/exchange/response_objects/estimate.dart b/lib/models/exchange/response_objects/estimate.dart new file mode 100644 index 000000000..7df490079 --- /dev/null +++ b/lib/models/exchange/response_objects/estimate.dart @@ -0,0 +1,46 @@ +import 'package:decimal/decimal.dart'; +import 'package:stackwallet/utilities/logger.dart'; + +class Estimate { + final Decimal estimatedAmount; + final bool fixedRate; + final bool reversed; + final String? warningMessage; + final String? rateId; + + Estimate({ + required this.estimatedAmount, + required this.fixedRate, + required this.reversed, + this.warningMessage, + this.rateId, + }); + + factory Estimate.fromMap(Map map) { + try { + return Estimate( + estimatedAmount: Decimal.parse(map["estimatedAmount"] as String), + fixedRate: map["fixedRate"] as bool, + reversed: map["reversed"] as bool, + warningMessage: map["warningMessage"] as String?, + rateId: map["rateId"] as String?, + ); + } catch (e, s) { + Logging.instance.log("Estimate.fromMap(): $e\n$s", level: LogLevel.Error); + rethrow; + } + } + + Map toMap() { + return { + "estimatedAmount": estimatedAmount.toString(), + "fixedRate": fixedRate, + "reversed": reversed, + "warningMessage": warningMessage, + "rateId": rateId, + }; + } + + @override + String toString() => "Estimate: ${toMap()}"; +} diff --git a/lib/models/exchange/change_now/fixed_rate_market.dart b/lib/models/exchange/response_objects/fixed_rate_market.dart similarity index 77% rename from lib/models/exchange/change_now/fixed_rate_market.dart rename to lib/models/exchange/response_objects/fixed_rate_market.dart index 9b8b2db80..7e2b363c9 100644 --- a/lib/models/exchange/change_now/fixed_rate_market.dart +++ b/lib/models/exchange/response_objects/fixed_rate_market.dart @@ -1,4 +1,5 @@ import 'package:decimal/decimal.dart'; +import 'package:stackwallet/utilities/logger.dart'; class FixedRateMarket { /// Currency ticker @@ -20,7 +21,7 @@ class FixedRateMarket { /// Network fee for transferring funds between wallets, it should /// be deducted from the result. - final Decimal minerFee; + final Decimal? minerFee; FixedRateMarket({ required this.from, @@ -31,7 +32,7 @@ class FixedRateMarket { required this.minerFee, }); - factory FixedRateMarket.fromJson(Map json) { + factory FixedRateMarket.fromMap(Map json) { try { return FixedRateMarket( from: json["from"] as String, @@ -39,15 +40,19 @@ class FixedRateMarket { min: Decimal.parse(json["min"].toString()), max: Decimal.parse(json["max"].toString()), rate: Decimal.parse(json["rate"].toString()), - minerFee: Decimal.parse(json["minerFee"].toString()), + minerFee: Decimal.tryParse(json["minerFee"].toString()), + ); + } catch (e, s) { + Logging.instance.log( + "FixedRateMarket.fromMap(): $e\n$s", + level: LogLevel.Error, ); - } catch (e) { rethrow; } } - Map toJson() { - final map = { + Map toMap() { + return { "from": from, "to": to, "min": min, @@ -55,8 +60,6 @@ class FixedRateMarket { "rate": rate, "minerFee": minerFee, }; - - return map; } FixedRateMarket copyWith({ @@ -78,7 +81,5 @@ class FixedRateMarket { } @override - String toString() { - return "FixedRateMarket: ${toJson()}"; - } + String toString() => "FixedRateMarket: ${toMap()}"; } diff --git a/lib/pages/exchange_view/exchange_coin_selection/fixed_rate_pair_coin_selection_view.dart b/lib/pages/exchange_view/exchange_coin_selection/fixed_rate_pair_coin_selection_view.dart index b3fe0c3cc..d7577e960 100644 --- a/lib/pages/exchange_view/exchange_coin_selection/fixed_rate_pair_coin_selection_view.dart +++ b/lib/pages/exchange_view/exchange_coin_selection/fixed_rate_pair_coin_selection_view.dart @@ -1,8 +1,8 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_svg/svg.dart'; -import 'package:stackwallet/models/exchange/change_now/fixed_rate_market.dart'; import 'package:stackwallet/models/exchange/response_objects/currency.dart'; +import 'package:stackwallet/models/exchange/response_objects/fixed_rate_market.dart'; import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/constants.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; diff --git a/lib/pages/exchange_view/exchange_view.dart b/lib/pages/exchange_view/exchange_view.dart index dad79d3c1..e9319071d 100644 --- a/lib/pages/exchange_view/exchange_view.dart +++ b/lib/pages/exchange_view/exchange_view.dart @@ -6,9 +6,9 @@ import 'package:flutter/services.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:flutter_svg/svg.dart'; -import 'package:stackwallet/models/exchange/change_now/fixed_rate_market.dart'; import 'package:stackwallet/models/exchange/incomplete_exchange.dart'; import 'package:stackwallet/models/exchange/response_objects/currency.dart'; +import 'package:stackwallet/models/exchange/response_objects/fixed_rate_market.dart'; import 'package:stackwallet/models/exchange/response_objects/pair.dart'; import 'package:stackwallet/notifications/show_flush_bar.dart'; import 'package:stackwallet/pages/exchange_view/exchange_coin_selection/fixed_rate_pair_coin_selection_view.dart'; @@ -1276,15 +1276,17 @@ class _ExchangeViewState extends ConsumerState { } String rate = - "1 ${fromTicker.toUpperCase()} ~${(response.value! / sendAmount).toDecimal(scaleOnInfinitePrecision: 8).toStringAsFixed(8)} ${toTicker.toUpperCase()}"; + "1 ${fromTicker.toUpperCase()} ~${(response.value!.estimatedAmount / sendAmount).toDecimal(scaleOnInfinitePrecision: 8).toStringAsFixed(8)} ${toTicker.toUpperCase()}"; final model = IncompleteExchangeModel( sendTicker: fromTicker.toUpperCase(), receiveTicker: toTicker.toUpperCase(), rateInfo: rate, sendAmount: sendAmount, - receiveAmount: response.value!, + receiveAmount: + response.value!.estimatedAmount, rateType: rateType, + rateId: response.value!.rateId, ); if (mounted) { @@ -1341,51 +1343,50 @@ class _ExchangeViewState extends ConsumerState { ), )); return; + } else if (response.value!.warningMessage != + null && + response + .value!.warningMessage!.isNotEmpty) { + shouldCancel = await showDialog( + context: context, + barrierDismissible: true, + builder: (_) => StackDialog( + title: + "Failed to update trade estimate", + message: + "${response.value!.warningMessage!}\n\nDo you want to attempt trade anyways?", + leftButton: TextButton( + style: Theme.of(context) + .extension()! + .getSecondaryEnabledButtonColor( + context), + child: Text( + "Cancel", + style: STextStyles.itemSubtitle12( + context), + ), + onPressed: () { + // notify return to cancel + Navigator.of(context).pop(true); + }, + ), + rightButton: TextButton( + style: Theme.of(context) + .extension()! + .getPrimaryEnabledButtonColor( + context), + child: Text( + "Attempt", + style: STextStyles.button(context), + ), + onPressed: () { + // continue and try to attempt trade + Navigator.of(context).pop(false); + }, + ), + ), + ); } - // else if (response.value!.warningMessage != - // null && - // response - // .value!.warningMessage!.isNotEmpty) { - // shouldCancel = await showDialog( - // context: context, - // barrierDismissible: true, - // builder: (_) => StackDialog( - // title: - // "Failed to update trade estimate", - // message: - // "${response.value!.warningMessage!}\n\nDo you want to attempt trade anyways?", - // leftButton: TextButton( - // style: Theme.of(context) - // .extension()! - // .getSecondaryEnabledButtonColor( - // context), - // child: Text( - // "Cancel", - // style: STextStyles.itemSubtitle12( - // context), - // ), - // onPressed: () { - // // notify return to cancel - // Navigator.of(context).pop(true); - // }, - // ), - // rightButton: TextButton( - // style: Theme.of(context) - // .extension()! - // .getPrimaryEnabledButtonColor( - // context), - // child: Text( - // "Attempt", - // style: STextStyles.button(context), - // ), - // onPressed: () { - // // continue and try to attempt trade - // Navigator.of(context).pop(false); - // }, - // ), - // ), - // ); - // } if (shouldCancel is bool && shouldCancel) { return; @@ -1399,8 +1400,10 @@ class _ExchangeViewState extends ConsumerState { receiveTicker: toTicker, rateInfo: rate, sendAmount: sendAmount, - receiveAmount: response.value!, + receiveAmount: + response.value!.estimatedAmount, rateType: rateType, + rateId: response.value!.rateId, ); if (mounted) { diff --git a/lib/pages/exchange_view/wallet_initiated_exchange_view.dart b/lib/pages/exchange_view/wallet_initiated_exchange_view.dart index 5aca1b957..dc7c4f2dd 100644 --- a/lib/pages/exchange_view/wallet_initiated_exchange_view.dart +++ b/lib/pages/exchange_view/wallet_initiated_exchange_view.dart @@ -5,9 +5,9 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_svg/svg.dart'; -import 'package:stackwallet/models/exchange/change_now/fixed_rate_market.dart'; import 'package:stackwallet/models/exchange/incomplete_exchange.dart'; import 'package:stackwallet/models/exchange/response_objects/currency.dart'; +import 'package:stackwallet/models/exchange/response_objects/fixed_rate_market.dart'; import 'package:stackwallet/models/exchange/response_objects/pair.dart'; import 'package:stackwallet/notifications/show_flush_bar.dart'; import 'package:stackwallet/pages/exchange_view/exchange_coin_selection/fixed_rate_pair_coin_selection_view.dart'; @@ -1431,15 +1431,17 @@ class _WalletInitiatedExchangeViewState } String rate = - "1 ${fromTicker.toUpperCase()} ~${(response.value! / sendAmount).toDecimal(scaleOnInfinitePrecision: 8).toStringAsFixed(8)} ${toTicker.toUpperCase()}"; + "1 ${fromTicker.toUpperCase()} ~${(response.value!.estimatedAmount / sendAmount).toDecimal(scaleOnInfinitePrecision: 8).toStringAsFixed(8)} ${toTicker.toUpperCase()}"; final model = IncompleteExchangeModel( sendTicker: fromTicker.toUpperCase(), receiveTicker: toTicker.toUpperCase(), rateInfo: rate, sendAmount: sendAmount, - receiveAmount: response.value!, + receiveAmount: + response.value!.estimatedAmount, rateType: rateType, + rateId: response.value!.rateId, ); if (mounted) { @@ -1497,52 +1499,51 @@ class _WalletInitiatedExchangeViewState ), )); return; + } else if (response.value!.warningMessage != + null && + response.value!.warningMessage! + .isNotEmpty) { + shouldCancel = await showDialog( + context: context, + barrierDismissible: true, + builder: (_) => StackDialog( + title: + "Failed to update trade estimate", + message: + "${response.value!.warningMessage!}\n\nDo you want to attempt trade anyways?", + leftButton: TextButton( + style: Theme.of(context) + .extension()! + .getSecondaryEnabledButtonColor( + context), + child: Text( + "Cancel", + style: STextStyles.itemSubtitle12( + context), + ), + onPressed: () { + // notify return to cancel + Navigator.of(context).pop(true); + }, + ), + rightButton: TextButton( + style: Theme.of(context) + .extension()! + .getPrimaryEnabledButtonColor( + context), + child: Text( + "Attempt", + style: + STextStyles.button(context), + ), + onPressed: () { + // continue and try to attempt trade + Navigator.of(context).pop(false); + }, + ), + ), + ); } - // else if (response.value!.warningMessage != - // null && - // response.value!.warningMessage! - // .isNotEmpty) { - // shouldCancel = await showDialog( - // context: context, - // barrierDismissible: true, - // builder: (_) => StackDialog( - // title: - // "Failed to update trade estimate", - // message: - // "${response.value!.warningMessage!}\n\nDo you want to attempt trade anyways?", - // leftButton: TextButton( - // style: Theme.of(context) - // .extension()! - // .getSecondaryEnabledButtonColor( - // context), - // child: Text( - // "Cancel", - // style: STextStyles.itemSubtitle12( - // context), - // ), - // onPressed: () { - // // notify return to cancel - // Navigator.of(context).pop(true); - // }, - // ), - // rightButton: TextButton( - // style: Theme.of(context) - // .extension()! - // .getPrimaryEnabledButtonColor( - // context), - // child: Text( - // "Attempt", - // style: - // STextStyles.button(context), - // ), - // onPressed: () { - // // continue and try to attempt trade - // Navigator.of(context).pop(false); - // }, - // ), - // ), - // ); - // } if (shouldCancel is bool && shouldCancel) { return; @@ -1556,8 +1557,10 @@ class _WalletInitiatedExchangeViewState receiveTicker: toTicker, rateInfo: rate, sendAmount: sendAmount, - receiveAmount: response.value!, + receiveAmount: + response.value!.estimatedAmount, rateType: rateType, + rateId: response.value!.rateId, ); if (mounted) { diff --git a/lib/providers/exchange/fixed_rate_market_pairs_provider.dart b/lib/providers/exchange/fixed_rate_market_pairs_provider.dart index 019714ea9..1b2cbdaca 100644 --- a/lib/providers/exchange/fixed_rate_market_pairs_provider.dart +++ b/lib/providers/exchange/fixed_rate_market_pairs_provider.dart @@ -1,5 +1,5 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:stackwallet/models/exchange/change_now/fixed_rate_market.dart'; +import 'package:stackwallet/models/exchange/response_objects/fixed_rate_market.dart'; final fixedRateMarketPairsStateProvider = StateProvider>((ref) => []); diff --git a/lib/services/exchange/change_now/change_now_api.dart b/lib/services/exchange/change_now/change_now_api.dart index bf5cde890..7c7f67974 100644 --- a/lib/services/exchange/change_now/change_now_api.dart +++ b/lib/services/exchange/change_now/change_now_api.dart @@ -8,8 +8,9 @@ import 'package:stackwallet/models/exchange/change_now/cn_exchange_estimate.dart import 'package:stackwallet/models/exchange/change_now/estimated_exchange_amount.dart'; import 'package:stackwallet/models/exchange/change_now/exchange_transaction.dart'; import 'package:stackwallet/models/exchange/change_now/exchange_transaction_status.dart'; -import 'package:stackwallet/models/exchange/change_now/fixed_rate_market.dart'; import 'package:stackwallet/models/exchange/response_objects/currency.dart'; +import 'package:stackwallet/models/exchange/response_objects/estimate.dart'; +import 'package:stackwallet/models/exchange/response_objects/fixed_rate_market.dart'; import 'package:stackwallet/models/exchange/response_objects/pair.dart'; import 'package:stackwallet/models/exchange/response_objects/range.dart'; import 'package:stackwallet/services/exchange/exchange_response.dart'; @@ -309,7 +310,7 @@ class ChangeNowAPI { /// Get estimated amount of [toTicker] cryptocurrency to receive /// for [fromAmount] of [fromTicker] - Future> getEstimatedExchangeAmount({ + Future> getEstimatedExchangeAmount({ required String fromTicker, required String toTicker, required Decimal fromAmount, @@ -329,7 +330,15 @@ class ChangeNowAPI { try { final value = EstimatedExchangeAmount.fromJson( Map.from(json as Map)); - return ExchangeResponse(value: value); + return ExchangeResponse( + value: Estimate( + estimatedAmount: value.estimatedAmount, + fixedRate: false, + reversed: false, + rateId: value.rateId, + warningMessage: value.warningMessage, + ), + ); } catch (_) { return ExchangeResponse( exception: ExchangeException( @@ -352,8 +361,7 @@ class ChangeNowAPI { /// Get estimated amount of [toTicker] cryptocurrency to receive /// for [fromAmount] of [fromTicker] - Future> - getEstimatedExchangeAmountFixedRate({ + Future> getEstimatedExchangeAmountFixedRate({ required String fromTicker, required String toTicker, required Decimal fromAmount, @@ -382,7 +390,15 @@ class ChangeNowAPI { try { final value = EstimatedExchangeAmount.fromJson( Map.from(json as Map)); - return ExchangeResponse(value: value); + return ExchangeResponse( + value: Estimate( + estimatedAmount: value.estimatedAmount, + fixedRate: true, + reversed: reversed, + rateId: value.rateId, + warningMessage: value.warningMessage, + ), + ); } catch (_) { return ExchangeResponse( exception: ExchangeException( @@ -574,7 +590,7 @@ class ChangeNowAPI { for (final json in jsonArray) { try { markets.add( - FixedRateMarket.fromJson(Map.from(json as Map))); + FixedRateMarket.fromMap(Map.from(json as Map))); } catch (_) { return ExchangeResponse( exception: ExchangeException("Failed to serialize $json", diff --git a/lib/services/exchange/change_now/change_now_exchange.dart b/lib/services/exchange/change_now/change_now_exchange.dart index 5ff68de4a..ba6ce3f0f 100644 --- a/lib/services/exchange/change_now/change_now_exchange.dart +++ b/lib/services/exchange/change_now/change_now_exchange.dart @@ -1,7 +1,7 @@ import 'package:decimal/decimal.dart'; -import 'package:stackwallet/models/exchange/change_now/estimated_exchange_amount.dart'; import 'package:stackwallet/models/exchange/change_now/exchange_transaction.dart'; import 'package:stackwallet/models/exchange/response_objects/currency.dart'; +import 'package:stackwallet/models/exchange/response_objects/estimate.dart'; import 'package:stackwallet/models/exchange/response_objects/pair.dart'; import 'package:stackwallet/models/exchange/response_objects/range.dart'; import 'package:stackwallet/models/exchange/response_objects/trade.dart'; @@ -87,14 +87,14 @@ class ChangeNowExchange extends Exchange { } @override - Future> getEstimate( + Future> getEstimate( String from, String to, Decimal amount, bool fixedRate, bool reversed, ) async { - late final ExchangeResponse response; + late final ExchangeResponse response; if (fixedRate) { response = await ChangeNowAPI.instance.getEstimatedExchangeAmountFixedRate( @@ -110,10 +110,7 @@ class ChangeNowExchange extends Exchange { fromAmount: amount, ); } - if (response.exception != null) { - return ExchangeResponse(exception: response.exception); - } - return ExchangeResponse(value: response.value?.estimatedAmount); + return response; } @override diff --git a/lib/services/exchange/exchange.dart b/lib/services/exchange/exchange.dart index 8771fffd5..c3210cddc 100644 --- a/lib/services/exchange/exchange.dart +++ b/lib/services/exchange/exchange.dart @@ -1,5 +1,6 @@ import 'package:decimal/decimal.dart'; import 'package:stackwallet/models/exchange/response_objects/currency.dart'; +import 'package:stackwallet/models/exchange/response_objects/estimate.dart'; import 'package:stackwallet/models/exchange/response_objects/pair.dart'; import 'package:stackwallet/models/exchange/response_objects/range.dart'; import 'package:stackwallet/models/exchange/response_objects/trade.dart'; @@ -28,7 +29,7 @@ abstract class Exchange { bool fixedRate, ); - Future> getEstimate( + Future> getEstimate( String from, String to, Decimal amount, diff --git a/lib/services/exchange/simpleswap/simpleswap_api.dart b/lib/services/exchange/simpleswap/simpleswap_api.dart index 2df5d16a1..4c7882011 100644 --- a/lib/services/exchange/simpleswap/simpleswap_api.dart +++ b/lib/services/exchange/simpleswap/simpleswap_api.dart @@ -4,6 +4,7 @@ import 'package:decimal/decimal.dart'; import 'package:flutter/foundation.dart'; import 'package:http/http.dart' as http; import 'package:stackwallet/external_api_keys.dart'; +import 'package:stackwallet/models/exchange/response_objects/fixed_rate_market.dart'; import 'package:stackwallet/models/exchange/response_objects/pair.dart'; import 'package:stackwallet/models/exchange/response_objects/range.dart'; import 'package:stackwallet/models/exchange/response_objects/trade.dart'; @@ -37,7 +38,6 @@ class SimpleSwapAPI { ); final parsed = jsonDecode(response.body); - print("PARSED: $parsed"); return parsed; } catch (e, s) { @@ -99,9 +99,6 @@ class SimpleSwapAPI { try { final jsonObject = await _makePostRequest(uri, body); - print("================================"); - print(jsonObject); - print("================================"); final json = Map.from(jsonObject as Map); final trade = Trade( @@ -427,4 +424,76 @@ class SimpleSwapAPI { ); } } + + Future>> getFixedRateMarketInfo({ + String? apiKey, + }) async { + final uri = _buildUri( + "/get_market_info", + null, + // { + // "api_key": apiKey ?? kSimpleSwapApiKey, + // "fixed": isFixedRate.toString(), + // "currency_from": currencyFrom, + // "currency_to": currencyTo, + // }, + ); + + try { + final jsonArray = await _makeGetRequest(uri); + + try { + final result = await compute( + _parseFixedRateMarketsJson, + jsonArray as List, + ); + return result; + } catch (e, s) { + Logging.instance.log("getAvailableFixedRateMarkets exception: $e\n$s", + level: LogLevel.Error); + return ExchangeResponse( + exception: ExchangeException( + "Error: $jsonArray", + ExchangeExceptionType.serializeResponseError, + ), + ); + } + } catch (e, s) { + Logging.instance.log("getAvailableFixedRateMarkets exception: $e\n$s", + level: LogLevel.Error); + return ExchangeResponse( + exception: ExchangeException( + e.toString(), + ExchangeExceptionType.generic, + ), + ); + } + } + + ExchangeResponse> _parseFixedRateMarketsJson( + List jsonArray) { + try { + final List markets = []; + for (final json in jsonArray) { + try { + final map = Map.from(json as Map); + markets.add(FixedRateMarket( + from: map["currency_from"] as String, + to: map["currency_to"] as String, + min: Decimal.parse(map["min"] as String), + max: Decimal.parse(map["max"] as String), + rate: Decimal.parse(map["rate"] as String), + minerFee: null, + )); + } catch (_) { + return ExchangeResponse( + exception: ExchangeException("Failed to serialize $json", + ExchangeExceptionType.serializeResponseError)); + } + } + return ExchangeResponse(value: markets); + } catch (_) { + rethrow; + } + } } diff --git a/lib/services/exchange/simpleswap/simpleswap_exchange.dart b/lib/services/exchange/simpleswap/simpleswap_exchange.dart index e8e79b06d..1fbfa4f3c 100644 --- a/lib/services/exchange/simpleswap/simpleswap_exchange.dart +++ b/lib/services/exchange/simpleswap/simpleswap_exchange.dart @@ -1,5 +1,6 @@ import 'package:decimal/decimal.dart'; import 'package:stackwallet/models/exchange/response_objects/currency.dart'; +import 'package:stackwallet/models/exchange/response_objects/estimate.dart'; import 'package:stackwallet/models/exchange/response_objects/pair.dart'; import 'package:stackwallet/models/exchange/response_objects/range.dart'; import 'package:stackwallet/models/exchange/response_objects/trade.dart'; @@ -76,7 +77,7 @@ class SimpleSwapExchange extends Exchange { } @override - Future> getEstimate( + Future> getEstimate( String from, String to, Decimal amount, @@ -89,10 +90,18 @@ class SimpleSwapExchange extends Exchange { currencyTo: to, amount: amount.toString(), ); + if (response.exception != null) { + return ExchangeResponse( + exception: response.exception, + ); + } return ExchangeResponse( - value: Decimal.tryParse(response.value ?? ""), - exception: response.exception, + value: Estimate( + estimatedAmount: Decimal.parse(response.value!), + fixedRate: fixedRate, + reversed: reversed, + ), ); } diff --git a/test/models/exchange/estimated_rate_exchange_form_state_test.dart b/test/models/exchange/estimated_rate_exchange_form_state_test.dart index 6386d533f..21af573eb 100644 --- a/test/models/exchange/estimated_rate_exchange_form_state_test.dart +++ b/test/models/exchange/estimated_rate_exchange_form_state_test.dart @@ -2,9 +2,9 @@ import 'package:decimal/decimal.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.dart'; -import 'package:stackwallet/models/exchange/change_now/estimated_exchange_amount.dart'; import 'package:stackwallet/models/exchange/estimated_rate_exchange_form_state.dart'; import 'package:stackwallet/models/exchange/response_objects/currency.dart'; +import 'package:stackwallet/models/exchange/response_objects/estimate.dart'; import 'package:stackwallet/services/exchange/change_now/change_now_api.dart'; import 'package:stackwallet/services/exchange/exchange_response.dart'; @@ -186,8 +186,9 @@ void main() { toTicker: "xmr", fromAmount: Decimal.parse("110.10"))) .thenAnswer((_) async => ExchangeResponse( - value: EstimatedExchangeAmount( - transactionSpeedForecast: '10-60', + value: Estimate( + reversed: false, + fixedRate: false, rateId: 'some rate id', warningMessage: '', estimatedAmount: Decimal.parse("302.002348"), diff --git a/test/models/exchange/estimated_rate_exchange_form_state_test.mocks.dart b/test/models/exchange/estimated_rate_exchange_form_state_test.mocks.dart index 94ec0ad5d..82a7eceb7 100644 --- a/test/models/exchange/estimated_rate_exchange_form_state_test.mocks.dart +++ b/test/models/exchange/estimated_rate_exchange_form_state_test.mocks.dart @@ -9,16 +9,16 @@ import 'package:http/http.dart' as _i4; import 'package:mockito/mockito.dart' as _i1; import 'package:stackwallet/models/exchange/change_now/cn_exchange_estimate.dart' as _i10; -import 'package:stackwallet/models/exchange/change_now/estimated_exchange_amount.dart' - as _i9; import 'package:stackwallet/models/exchange/change_now/exchange_transaction.dart' as _i12; import 'package:stackwallet/models/exchange/change_now/exchange_transaction_status.dart' as _i13; -import 'package:stackwallet/models/exchange/change_now/fixed_rate_market.dart' - as _i11; import 'package:stackwallet/models/exchange/response_objects/currency.dart' as _i6; +import 'package:stackwallet/models/exchange/response_objects/estimate.dart' + as _i9; +import 'package:stackwallet/models/exchange/response_objects/fixed_rate_market.dart' + as _i11; import 'package:stackwallet/models/exchange/response_objects/pair.dart' as _i14; import 'package:stackwallet/models/exchange/response_objects/range.dart' as _i8; import 'package:stackwallet/services/exchange/change_now/change_now_api.dart' @@ -97,26 +97,23 @@ class MockChangeNowAPI extends _i1.Mock implements _i3.ChangeNowAPI { _FakeExchangeResponse_0<_i8.Range>())) as _i5.Future<_i2.ExchangeResponse<_i8.Range>>); @override - _i5.Future<_i2.ExchangeResponse<_i9.EstimatedExchangeAmount>> - getEstimatedExchangeAmount( - {String? fromTicker, - String? toTicker, - _i7.Decimal? fromAmount, - String? apiKey}) => - (super.noSuchMethod( - Invocation.method(#getEstimatedExchangeAmount, [], { - #fromTicker: fromTicker, - #toTicker: toTicker, - #fromAmount: fromAmount, - #apiKey: apiKey - }), - returnValue: Future< - _i2.ExchangeResponse< - _i9.EstimatedExchangeAmount>>.value( - _FakeExchangeResponse_0<_i9.EstimatedExchangeAmount>())) - as _i5.Future<_i2.ExchangeResponse<_i9.EstimatedExchangeAmount>>); + _i5.Future<_i2.ExchangeResponse<_i9.Estimate>> getEstimatedExchangeAmount( + {String? fromTicker, + String? toTicker, + _i7.Decimal? fromAmount, + String? apiKey}) => + (super.noSuchMethod( + Invocation.method(#getEstimatedExchangeAmount, [], { + #fromTicker: fromTicker, + #toTicker: toTicker, + #fromAmount: fromAmount, + #apiKey: apiKey + }), + returnValue: Future<_i2.ExchangeResponse<_i9.Estimate>>.value( + _FakeExchangeResponse_0<_i9.Estimate>())) + as _i5.Future<_i2.ExchangeResponse<_i9.Estimate>>); @override - _i5.Future<_i2.ExchangeResponse<_i9.EstimatedExchangeAmount>> + _i5.Future<_i2.ExchangeResponse<_i9.Estimate>> getEstimatedExchangeAmountFixedRate( {String? fromTicker, String? toTicker, @@ -131,11 +128,9 @@ class MockChangeNowAPI extends _i1.Mock implements _i3.ChangeNowAPI { #reversed: reversed, #apiKey: apiKey }), - returnValue: Future< - _i2.ExchangeResponse< - _i9.EstimatedExchangeAmount>>.value( - _FakeExchangeResponse_0<_i9.EstimatedExchangeAmount>())) - as _i5.Future<_i2.ExchangeResponse<_i9.EstimatedExchangeAmount>>); + returnValue: Future<_i2.ExchangeResponse<_i9.Estimate>>.value( + _FakeExchangeResponse_0<_i9.Estimate>())) + as _i5.Future<_i2.ExchangeResponse<_i9.Estimate>>); @override _i5.Future<_i2.ExchangeResponse<_i10.CNExchangeEstimate>> getEstimatedExchangeAmountV2( diff --git a/test/screen_tests/exchange/exchange_view_test.mocks.dart b/test/screen_tests/exchange/exchange_view_test.mocks.dart index c0151e028..da0972645 100644 --- a/test/screen_tests/exchange/exchange_view_test.mocks.dart +++ b/test/screen_tests/exchange/exchange_view_test.mocks.dart @@ -10,16 +10,16 @@ import 'package:http/http.dart' as _i13; import 'package:mockito/mockito.dart' as _i1; import 'package:stackwallet/models/exchange/change_now/cn_exchange_estimate.dart' as _i18; -import 'package:stackwallet/models/exchange/change_now/estimated_exchange_amount.dart' - as _i17; import 'package:stackwallet/models/exchange/change_now/exchange_transaction.dart' as _i20; import 'package:stackwallet/models/exchange/change_now/exchange_transaction_status.dart' as _i21; -import 'package:stackwallet/models/exchange/change_now/fixed_rate_market.dart' - as _i19; import 'package:stackwallet/models/exchange/response_objects/currency.dart' as _i14; +import 'package:stackwallet/models/exchange/response_objects/estimate.dart' + as _i17; +import 'package:stackwallet/models/exchange/response_objects/fixed_rate_market.dart' + as _i19; import 'package:stackwallet/models/exchange/response_objects/pair.dart' as _i22; import 'package:stackwallet/models/exchange/response_objects/range.dart' as _i16; @@ -405,27 +405,23 @@ class MockChangeNowAPI extends _i1.Mock implements _i12.ChangeNowAPI { _FakeExchangeResponse_0<_i16.Range>())) as _i7.Future<_i2.ExchangeResponse<_i16.Range>>); @override - _i7.Future<_i2.ExchangeResponse<_i17.EstimatedExchangeAmount>> - getEstimatedExchangeAmount( - {String? fromTicker, - String? toTicker, - _i15.Decimal? fromAmount, - String? apiKey}) => - (super.noSuchMethod( - Invocation.method(#getEstimatedExchangeAmount, [], { - #fromTicker: fromTicker, - #toTicker: toTicker, - #fromAmount: fromAmount, - #apiKey: apiKey - }), - returnValue: Future< - _i2.ExchangeResponse< - _i17.EstimatedExchangeAmount>>.value( - _FakeExchangeResponse_0<_i17.EstimatedExchangeAmount>())) - as _i7 - .Future<_i2.ExchangeResponse<_i17.EstimatedExchangeAmount>>); + _i7.Future<_i2.ExchangeResponse<_i17.Estimate>> getEstimatedExchangeAmount( + {String? fromTicker, + String? toTicker, + _i15.Decimal? fromAmount, + String? apiKey}) => + (super.noSuchMethod( + Invocation.method(#getEstimatedExchangeAmount, [], { + #fromTicker: fromTicker, + #toTicker: toTicker, + #fromAmount: fromAmount, + #apiKey: apiKey + }), + returnValue: Future<_i2.ExchangeResponse<_i17.Estimate>>.value( + _FakeExchangeResponse_0<_i17.Estimate>())) + as _i7.Future<_i2.ExchangeResponse<_i17.Estimate>>); @override - _i7.Future<_i2.ExchangeResponse<_i17.EstimatedExchangeAmount>> + _i7.Future<_i2.ExchangeResponse<_i17.Estimate>> getEstimatedExchangeAmountFixedRate( {String? fromTicker, String? toTicker, @@ -433,19 +429,16 @@ class MockChangeNowAPI extends _i1.Mock implements _i12.ChangeNowAPI { bool? reversed, String? apiKey}) => (super.noSuchMethod( - Invocation.method(#getEstimatedExchangeAmountFixedRate, [], { - #fromTicker: fromTicker, - #toTicker: toTicker, - #fromAmount: fromAmount, - #reversed: reversed, - #apiKey: apiKey - }), - returnValue: Future< - _i2.ExchangeResponse< - _i17.EstimatedExchangeAmount>>.value( - _FakeExchangeResponse_0<_i17.EstimatedExchangeAmount>())) - as _i7 - .Future<_i2.ExchangeResponse<_i17.EstimatedExchangeAmount>>); + Invocation.method(#getEstimatedExchangeAmountFixedRate, [], { + #fromTicker: fromTicker, + #toTicker: toTicker, + #fromAmount: fromAmount, + #reversed: reversed, + #apiKey: apiKey + }), + returnValue: Future<_i2.ExchangeResponse<_i17.Estimate>>.value( + _FakeExchangeResponse_0<_i17.Estimate>())) as _i7 + .Future<_i2.ExchangeResponse<_i17.Estimate>>); @override _i7.Future<_i2.ExchangeResponse<_i18.CNExchangeEstimate>> getEstimatedExchangeAmountV2(