diff --git a/lib/models/exchange/change_now/available_floating_rate_pair.dart b/lib/models/exchange/change_now/available_floating_rate_pair.dart deleted file mode 100644 index d30b9817e..000000000 --- a/lib/models/exchange/change_now/available_floating_rate_pair.dart +++ /dev/null @@ -1,26 +0,0 @@ -import 'package:flutter/material.dart'; - -class AvailableFloatingRatePair { - final String fromTicker; - final String toTicker; - - AvailableFloatingRatePair({ - required this.fromTicker, - required this.toTicker, - }); - - @override - bool operator ==(other) { - return other is AvailableFloatingRatePair && - fromTicker == other.fromTicker && - toTicker == other.toTicker; - } - - @override - int get hashCode => hashValues(fromTicker, toTicker); - - @override - String toString() { - return "${fromTicker}_$toTicker"; - } -} diff --git a/lib/models/exchange/response_objects/pair.dart b/lib/models/exchange/response_objects/pair.dart index cbd97140a..1f12acf08 100644 --- a/lib/models/exchange/response_objects/pair.dart +++ b/lib/models/exchange/response_objects/pair.dart @@ -1,3 +1,7 @@ +import 'dart:ui'; + +import 'package:stackwallet/utilities/logger.dart'; + class Pair { final String from; final String fromNetwork; @@ -17,7 +21,23 @@ class Pair { required this.floatingRate, }); - Map toJson() { + factory Pair.fromMap(Map map) { + try { + return Pair( + from: map["from"] as String, + fromNetwork: map["fromNetwork"] as String, + to: map["to"] as String, + toNetwork: map["toNetwork"] as String, + fixedRate: map["fixedRate"] as bool, + floatingRate: map["floatingRate"] as bool, + ); + } catch (e, s) { + Logging.instance.log("Pair.fromMap(): $e\n$s", level: LogLevel.Error); + rethrow; + } + } + + Map toMap() { return { "from": from, "fromNetwork": fromNetwork, @@ -29,7 +49,25 @@ class Pair { } @override - String toString() { - return "Pair: ${toJson()}"; - } + bool operator ==(other) => + other is Pair && + from == other.from && + fromNetwork == other.fromNetwork && + to == other.to && + toNetwork == other.toNetwork && + fixedRate == other.fixedRate && + floatingRate == other.floatingRate; + + @override + int get hashCode => hashValues( + from, + fromNetwork, + to, + toNetwork, + fixedRate, + floatingRate, + ); + + @override + String toString() => "Pair: ${toMap()}"; } diff --git a/lib/pages/exchange_view/exchange_view.dart b/lib/pages/exchange_view/exchange_view.dart index dd0b3ad1e..dad79d3c1 100644 --- a/lib/pages/exchange_view/exchange_view.dart +++ b/lib/pages/exchange_view/exchange_view.dart @@ -6,10 +6,10 @@ 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/available_floating_rate_pair.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/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'; import 'package:stackwallet/pages/exchange_view/exchange_coin_selection/floating_rate_currency_selection_view.dart'; @@ -112,7 +112,7 @@ class _ExchangeViewState extends ConsumerState { _sendFocusNode.unfocus(); _receiveFocusNode.unfocus(); - List availablePairs = []; + List availablePairs = []; if (fromTicker.isEmpty || fromTicker == "-" || excludedTicker.isEmpty || @@ -123,23 +123,23 @@ class _ExchangeViewState extends ConsumerState { availablePairs = ref .read(availableFloatingRatePairsStateProvider.state) .state - .where((e) => e.fromTicker == excludedTicker) + .where((e) => e.from == excludedTicker) .toList(growable: false); } else { availablePairs = ref .read(availableFloatingRatePairsStateProvider.state) .state - .where((e) => e.toTicker == excludedTicker) + .where((e) => e.to == excludedTicker) .toList(growable: false); } final List tickers = currencies.where((e) { if (excludedTicker == fromTicker) { return e.ticker != excludedTicker && - availablePairs.where((e2) => e2.toTicker == e.ticker).isNotEmpty; + availablePairs.where((e2) => e2.to == e.ticker).isNotEmpty; } else { return e.ticker != excludedTicker && - availablePairs.where((e2) => e2.fromTicker == e.ticker).isNotEmpty; + availablePairs.where((e2) => e2.from == e.ticker).isNotEmpty; } }).toList(growable: false); @@ -1054,8 +1054,8 @@ class _ExchangeViewState extends ConsumerState { .state) .state .where((e) => - e.toTicker == toTicker && - e.fromTicker == fromTicker); + e.to == toTicker && + e.from == fromTicker); if (available.isNotEmpty) { final availableCurrencies = ref .read( @@ -1223,8 +1223,8 @@ class _ExchangeViewState extends ConsumerState { .state) .state; for (final pair in availableFloatingPairs) { - if (pair.fromTicker == fromTicker && - pair.toTicker == toTicker) { + if (pair.from == fromTicker && + pair.to == toTicker) { isAvailable = true; break; } diff --git a/lib/pages/exchange_view/wallet_initiated_exchange_view.dart b/lib/pages/exchange_view/wallet_initiated_exchange_view.dart index 6652af32b..5aca1b957 100644 --- a/lib/pages/exchange_view/wallet_initiated_exchange_view.dart +++ b/lib/pages/exchange_view/wallet_initiated_exchange_view.dart @@ -5,10 +5,10 @@ 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/available_floating_rate_pair.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/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'; import 'package:stackwallet/pages/exchange_view/exchange_coin_selection/floating_rate_currency_selection_view.dart'; @@ -127,7 +127,7 @@ class _WalletInitiatedExchangeViewState _sendFocusNode.unfocus(); _receiveFocusNode.unfocus(); - List availablePairs = []; + List availablePairs = []; if (fromTicker.isEmpty || fromTicker == "-" || excludedTicker.isEmpty || @@ -138,23 +138,23 @@ class _WalletInitiatedExchangeViewState availablePairs = ref .read(availableFloatingRatePairsStateProvider.state) .state - .where((e) => e.fromTicker == excludedTicker) + .where((e) => e.from == excludedTicker) .toList(growable: false); } else { availablePairs = ref .read(availableFloatingRatePairsStateProvider.state) .state - .where((e) => e.toTicker == excludedTicker) + .where((e) => e.to == excludedTicker) .toList(growable: false); } final List tickers = currencies.where((e) { if (excludedTicker == fromTicker) { return e.ticker != excludedTicker && - availablePairs.where((e2) => e2.toTicker == e.ticker).isNotEmpty; + availablePairs.where((e2) => e2.to == e.ticker).isNotEmpty; } else { return e.ticker != excludedTicker && - availablePairs.where((e2) => e2.fromTicker == e.ticker).isNotEmpty; + availablePairs.where((e2) => e2.from == e.ticker).isNotEmpty; } }).toList(growable: false); @@ -1169,8 +1169,8 @@ class _WalletInitiatedExchangeViewState .state) .state .where((e) => - e.toTicker == toTicker && - e.fromTicker == fromTicker); + e.to == toTicker && + e.from == fromTicker); if (available.isNotEmpty) { final availableCurrencies = ref .read( @@ -1381,8 +1381,8 @@ class _WalletInitiatedExchangeViewState .state) .state; for (final pair in availableFloatingPairs) { - if (pair.fromTicker == fromTicker && - pair.toTicker == toTicker) { + if (pair.from == fromTicker && + pair.to == toTicker) { isAvailable = true; break; } diff --git a/lib/providers/exchange/available_floating_rate_pairs_state_provider.dart b/lib/providers/exchange/available_floating_rate_pairs_state_provider.dart index a157b0727..e65f367b5 100644 --- a/lib/providers/exchange/available_floating_rate_pairs_state_provider.dart +++ b/lib/providers/exchange/available_floating_rate_pairs_state_provider.dart @@ -1,6 +1,5 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:stackwallet/models/exchange/change_now/available_floating_rate_pair.dart'; +import 'package:stackwallet/models/exchange/response_objects/pair.dart'; final availableFloatingRatePairsStateProvider = - StateProvider>( - (ref) => []); + 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 993d9a08e..bf5cde890 100644 --- a/lib/services/exchange/change_now/change_now_api.dart +++ b/lib/services/exchange/change_now/change_now_api.dart @@ -4,13 +4,13 @@ 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/change_now/available_floating_rate_pair.dart'; 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/pair.dart'; import 'package:stackwallet/models/exchange/response_objects/range.dart'; import 'package:stackwallet/services/exchange/exchange_response.dart'; import 'package:stackwallet/utilities/logger.dart'; @@ -749,8 +749,7 @@ class ChangeNowAPI { } } - Future>> - getAvailableFloatingRatePairs({ + Future>> getAvailableFloatingRatePairs({ bool includePartners = false, }) async { final uri = _buildUri("/market-info/available-pairs", @@ -786,15 +785,23 @@ class ChangeNowAPI { } } - ExchangeResponse> - _parseAvailableFloatingRatePairsJson(List jsonArray) { + ExchangeResponse> _parseAvailableFloatingRatePairsJson( + List jsonArray) { try { - List pairs = []; + List pairs = []; for (final json in jsonArray) { try { final List stringPair = (json as String).split("_"); - pairs.add(AvailableFloatingRatePair( - fromTicker: stringPair[0], toTicker: stringPair[1])); + pairs.add( + Pair( + from: stringPair[0], + to: stringPair[1], + fromNetwork: "", + toNetwork: "", + fixedRate: false, + floatingRate: true, + ), + ); } catch (_) { return ExchangeResponse( exception: ExchangeException("Failed to serialize $json", 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 450b9891b..94ec0ad5d 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 @@ -7,8 +7,6 @@ import 'dart:async' as _i5; import 'package:decimal/decimal.dart' as _i7; import 'package:http/http.dart' as _i4; import 'package:mockito/mockito.dart' as _i1; -import 'package:stackwallet/models/exchange/change_now/available_floating_rate_pair.dart' - as _i14; import 'package:stackwallet/models/exchange/change_now/cn_exchange_estimate.dart' as _i10; import 'package:stackwallet/models/exchange/change_now/estimated_exchange_amount.dart' @@ -21,6 +19,7 @@ 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/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' as _i3; @@ -244,13 +243,12 @@ class MockChangeNowAPI extends _i1.Mock implements _i3.ChangeNowAPI { _FakeExchangeResponse_0<_i13.ExchangeTransactionStatus>())) as _i5 .Future<_i2.ExchangeResponse<_i13.ExchangeTransactionStatus>>); @override - _i5.Future<_i2.ExchangeResponse>> - getAvailableFloatingRatePairs({bool? includePartners = false}) => (super - .noSuchMethod( + _i5.Future<_i2.ExchangeResponse>> + getAvailableFloatingRatePairs({bool? includePartners = false}) => + (super.noSuchMethod( Invocation.method(#getAvailableFloatingRatePairs, [], {#includePartners: includePartners}), - returnValue: - Future<_i2.ExchangeResponse>>.value( - _FakeExchangeResponse_0>())) as _i5 - .Future<_i2.ExchangeResponse>>); + returnValue: Future<_i2.ExchangeResponse>>.value( + _FakeExchangeResponse_0>())) as _i5 + .Future<_i2.ExchangeResponse>>); } diff --git a/test/screen_tests/exchange/exchange_view_test.mocks.dart b/test/screen_tests/exchange/exchange_view_test.mocks.dart index 2665436fb..c0151e028 100644 --- a/test/screen_tests/exchange/exchange_view_test.mocks.dart +++ b/test/screen_tests/exchange/exchange_view_test.mocks.dart @@ -8,8 +8,6 @@ import 'dart:ui' as _i8; import 'package:decimal/decimal.dart' as _i15; import 'package:http/http.dart' as _i13; import 'package:mockito/mockito.dart' as _i1; -import 'package:stackwallet/models/exchange/change_now/available_floating_rate_pair.dart' - as _i22; import 'package:stackwallet/models/exchange/change_now/cn_exchange_estimate.dart' as _i18; import 'package:stackwallet/models/exchange/change_now/estimated_exchange_amount.dart' @@ -22,6 +20,7 @@ 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/pair.dart' as _i22; import 'package:stackwallet/models/exchange/response_objects/range.dart' as _i16; import 'package:stackwallet/models/exchange/response_objects/trade.dart' @@ -554,13 +553,12 @@ class MockChangeNowAPI extends _i1.Mock implements _i12.ChangeNowAPI { _FakeExchangeResponse_0<_i21.ExchangeTransactionStatus>())) as _i7 .Future<_i2.ExchangeResponse<_i21.ExchangeTransactionStatus>>); @override - _i7.Future<_i2.ExchangeResponse>> - getAvailableFloatingRatePairs({bool? includePartners = false}) => (super - .noSuchMethod( + _i7.Future<_i2.ExchangeResponse>> + getAvailableFloatingRatePairs({bool? includePartners = false}) => + (super.noSuchMethod( Invocation.method(#getAvailableFloatingRatePairs, [], {#includePartners: includePartners}), - returnValue: - Future<_i2.ExchangeResponse>>.value( - _FakeExchangeResponse_0>())) as _i7 - .Future<_i2.ExchangeResponse>>); + returnValue: Future<_i2.ExchangeResponse>>.value( + _FakeExchangeResponse_0>())) as _i7 + .Future<_i2.ExchangeResponse>>); } diff --git a/test/services/change_now/change_now_test.dart b/test/services/change_now/change_now_test.dart index 2447f4744..e9e013245 100644 --- a/test/services/change_now/change_now_test.dart +++ b/test/services/change_now/change_now_test.dart @@ -5,10 +5,10 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:http/http.dart'; import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.dart'; -import 'package:stackwallet/models/exchange/change_now/available_floating_rate_pair.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/response_objects/pair.dart'; import 'package:stackwallet/services/exchange/change_now/change_now_api.dart'; import 'package:stackwallet/services/exchange/exchange_response.dart'; @@ -741,7 +741,7 @@ void main() { expect(result.exception, null); expect(result.value == null, false); - expect(result.value, isA>()); + expect(result.value, isA>()); }); test(