generic market pair

This commit is contained in:
julian 2022-10-03 09:41:53 -06:00
parent 078259ec0f
commit c182557b30
9 changed files with 95 additions and 81 deletions

View file

@ -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";
}
}

View file

@ -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<String, dynamic> toJson() {
factory Pair.fromMap(Map<String, dynamic> 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<String, dynamic> 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()}";
}

View file

@ -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<ExchangeView> {
_sendFocusNode.unfocus();
_receiveFocusNode.unfocus();
List<AvailableFloatingRatePair> availablePairs = [];
List<Pair> availablePairs = [];
if (fromTicker.isEmpty ||
fromTicker == "-" ||
excludedTicker.isEmpty ||
@ -123,23 +123,23 @@ class _ExchangeViewState extends ConsumerState<ExchangeView> {
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<Currency> 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<ExchangeView> {
.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<ExchangeView> {
.state)
.state;
for (final pair in availableFloatingPairs) {
if (pair.fromTicker == fromTicker &&
pair.toTicker == toTicker) {
if (pair.from == fromTicker &&
pair.to == toTicker) {
isAvailable = true;
break;
}

View file

@ -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<AvailableFloatingRatePair> availablePairs = [];
List<Pair> 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<Currency> 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;
}

View file

@ -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<List<AvailableFloatingRatePair>>(
(ref) => <AvailableFloatingRatePair>[]);
StateProvider<List<Pair>>((ref) => <Pair>[]);

View file

@ -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<ExchangeResponse<List<AvailableFloatingRatePair>>>
getAvailableFloatingRatePairs({
Future<ExchangeResponse<List<Pair>>> getAvailableFloatingRatePairs({
bool includePartners = false,
}) async {
final uri = _buildUri("/market-info/available-pairs",
@ -786,15 +785,23 @@ class ChangeNowAPI {
}
}
ExchangeResponse<List<AvailableFloatingRatePair>>
_parseAvailableFloatingRatePairsJson(List<dynamic> jsonArray) {
ExchangeResponse<List<Pair>> _parseAvailableFloatingRatePairsJson(
List<dynamic> jsonArray) {
try {
List<AvailableFloatingRatePair> pairs = [];
List<Pair> pairs = [];
for (final json in jsonArray) {
try {
final List<String> 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",

View file

@ -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<List<_i14.AvailableFloatingRatePair>>>
getAvailableFloatingRatePairs({bool? includePartners = false}) => (super
.noSuchMethod(
_i5.Future<_i2.ExchangeResponse<List<_i14.Pair>>>
getAvailableFloatingRatePairs({bool? includePartners = false}) =>
(super.noSuchMethod(
Invocation.method(#getAvailableFloatingRatePairs, [],
{#includePartners: includePartners}),
returnValue:
Future<_i2.ExchangeResponse<List<_i14.AvailableFloatingRatePair>>>.value(
_FakeExchangeResponse_0<List<_i14.AvailableFloatingRatePair>>())) as _i5
.Future<_i2.ExchangeResponse<List<_i14.AvailableFloatingRatePair>>>);
returnValue: Future<_i2.ExchangeResponse<List<_i14.Pair>>>.value(
_FakeExchangeResponse_0<List<_i14.Pair>>())) as _i5
.Future<_i2.ExchangeResponse<List<_i14.Pair>>>);
}

View file

@ -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<List<_i22.AvailableFloatingRatePair>>>
getAvailableFloatingRatePairs({bool? includePartners = false}) => (super
.noSuchMethod(
_i7.Future<_i2.ExchangeResponse<List<_i22.Pair>>>
getAvailableFloatingRatePairs({bool? includePartners = false}) =>
(super.noSuchMethod(
Invocation.method(#getAvailableFloatingRatePairs, [],
{#includePartners: includePartners}),
returnValue:
Future<_i2.ExchangeResponse<List<_i22.AvailableFloatingRatePair>>>.value(
_FakeExchangeResponse_0<List<_i22.AvailableFloatingRatePair>>())) as _i7
.Future<_i2.ExchangeResponse<List<_i22.AvailableFloatingRatePair>>>);
returnValue: Future<_i2.ExchangeResponse<List<_i22.Pair>>>.value(
_FakeExchangeResponse_0<List<_i22.Pair>>())) as _i7
.Future<_i2.ExchangeResponse<List<_i22.Pair>>>);
}

View file

@ -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<List<AvailableFloatingRatePair>>());
expect(result.value, isA<List<Pair>>());
});
test(