some estimated exchange refactoring, fetch estimated rate on reversing trade pair,

This commit is contained in:
julian 2022-08-31 12:06:57 -06:00
parent f63c5e5cc7
commit b49955b234
8 changed files with 120 additions and 15 deletions

View file

@ -33,7 +33,7 @@ import 'package:stackwallet/providers/exchange/available_currencies_state_provid
import 'package:stackwallet/providers/exchange/available_floating_rate_pairs_state_provider.dart';
import 'package:stackwallet/providers/exchange/change_now_provider.dart';
import 'package:stackwallet/providers/exchange/changenow_initial_load_status.dart';
import 'package:stackwallet/providers/exchange/exchange_form_provider.dart';
import 'package:stackwallet/providers/exchange/estimate_rate_exchange_form_provider.dart';
import 'package:stackwallet/providers/exchange/fixed_rate_exchange_form_provider.dart';
import 'package:stackwallet/providers/exchange/fixed_rate_market_pairs_provider.dart';
import 'package:stackwallet/providers/global/auto_swb_service_provider.dart';

View file

@ -4,7 +4,7 @@ import 'package:stackwallet/models/exchange/change_now/currency.dart';
import 'package:stackwallet/services/change_now/change_now.dart';
import 'package:stackwallet/utilities/logger.dart';
class ExchangeFormState extends ChangeNotifier {
class EstimatedRateExchangeFormState extends ChangeNotifier {
Decimal? _fromAmount;
Decimal? _toAmount;
@ -24,6 +24,18 @@ class ExchangeFormState extends ChangeNotifier {
_to = to;
}
void clearAmounts(bool shouldNotifyListeners) {
_fromAmount = null;
_toAmount = null;
_minFromAmount = null;
_minToAmount = null;
rate = null;
if (shouldNotifyListeners) {
notifyListeners();
}
}
Future<void> swap() async {
final Decimal? newToAmount = _fromAmount;
final Decimal? newFromAmount = _toAmount;
@ -31,9 +43,9 @@ class ExchangeFormState extends ChangeNotifier {
final Decimal? newMinFromAmount = _minToAmount;
final Decimal? newMinToAmount = _minFromAmount;
final Decimal? newRate = rate == null
? rate
: (Decimal.one / rate!).toDecimal(scaleOnInfinitePrecision: 12);
// final Decimal? newRate = rate == null
// ? rate
// : (Decimal.one / rate!).toDecimal(scaleOnInfinitePrecision: 12);
final Currency? newTo = from;
final Currency? newFrom = to;
@ -44,18 +56,54 @@ class ExchangeFormState extends ChangeNotifier {
_minToAmount = newMinToAmount;
_minFromAmount = newMinFromAmount;
rate = newRate;
// rate = newRate;
_to = newTo;
_from = newFrom;
await _updateMinFromAmount(shouldNotifyListeners: false);
rate = null;
if (_fromAmount != null) {
Decimal? amt;
if (_minFromAmount != null) {
if (_minFromAmount! > _fromAmount!) {
amt = await getStandardEstimatedToAmount(
fromAmount: _minFromAmount!, from: _from!, to: _to!);
if (amt != null) {
rate =
(amt / _minFromAmount!).toDecimal(scaleOnInfinitePrecision: 12);
}
} else {
amt = await getStandardEstimatedToAmount(
fromAmount: _fromAmount!, from: _from!, to: _to!);
if (amt != null) {
rate = (amt / _fromAmount!).toDecimal(scaleOnInfinitePrecision: 12);
}
}
}
if (rate != null) {
_toAmount = (_fromAmount! * rate!);
}
} else {
if (_minFromAmount != null) {
Decimal? amt = await getStandardEstimatedToAmount(
fromAmount: _minFromAmount!, from: _from!, to: _to!);
if (amt != null) {
rate =
(amt / _minFromAmount!).toDecimal(scaleOnInfinitePrecision: 12);
}
}
}
notifyListeners();
}
String get fromAmountString =>
_fromAmount == null ? "-" : _fromAmount!.toStringAsFixed(8);
_fromAmount == null ? "" : _fromAmount!.toStringAsFixed(8);
String get toAmountString =>
_toAmount == null ? "-" : _toAmount!.toStringAsFixed(8);
_toAmount == null ? "" : _toAmount!.toStringAsFixed(8);
Future<void> updateTo(Currency to, bool shouldNotifyListeners) async {
try {

View file

@ -4,7 +4,7 @@ import 'package:stackwallet/providers/exchange/available_currencies_state_provid
import 'package:stackwallet/providers/exchange/available_floating_rate_pairs_state_provider.dart';
import 'package:stackwallet/providers/exchange/change_now_provider.dart';
import 'package:stackwallet/providers/exchange/changenow_initial_load_status.dart';
import 'package:stackwallet/providers/exchange/exchange_form_provider.dart';
import 'package:stackwallet/providers/exchange/estimate_rate_exchange_form_provider.dart';
import 'package:stackwallet/providers/exchange/fixed_rate_exchange_form_provider.dart';
import 'package:stackwallet/providers/exchange/fixed_rate_market_pairs_provider.dart';
import 'package:stackwallet/utilities/cfcolors.dart';

View file

@ -20,7 +20,7 @@ import 'package:stackwallet/providers/exchange/available_currencies_state_provid
import 'package:stackwallet/providers/exchange/available_floating_rate_pairs_state_provider.dart';
import 'package:stackwallet/providers/exchange/change_now_provider.dart';
import 'package:stackwallet/providers/exchange/changenow_initial_load_status.dart';
import 'package:stackwallet/providers/exchange/exchange_form_provider.dart';
import 'package:stackwallet/providers/exchange/estimate_rate_exchange_form_provider.dart';
import 'package:stackwallet/providers/exchange/exchange_send_from_wallet_id_provider.dart';
import 'package:stackwallet/providers/exchange/fixed_rate_exchange_form_provider.dart';
import 'package:stackwallet/providers/exchange/fixed_rate_market_pairs_provider.dart';
@ -32,6 +32,7 @@ import 'package:stackwallet/utilities/cfcolors.dart';
import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/enums/flush_bar_type.dart';
import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/widgets/custom_loading_overlay.dart';
import 'package:stackwallet/widgets/loading_indicator.dart';
import 'package:stackwallet/widgets/stack_dialog.dart';
import 'package:stackwallet/widgets/trade_card.dart';
@ -57,6 +58,23 @@ class _ExchangeViewState extends ConsumerState<ExchangeView> {
_sendFocusNode.unfocus();
_receiveFocusNode.unfocus();
unawaited(
showDialog<void>(
context: context,
barrierDismissible: false,
builder: (_) => WillPopScope(
onWillPop: () async => false,
child: Container(
color: CFColors.stackAccent.withOpacity(0.8),
child: const CustomLoadingOverlay(
message: "Updating exchange rate",
eventBus: null,
),
),
),
),
);
if (ref.watch(prefsChangeNotifierProvider
.select((pref) => pref.exchangeRateType)) ==
ExchangeRateType.estimated) {
@ -76,6 +94,9 @@ class _ExchangeViewState extends ConsumerState<ExchangeView> {
}
}
}
if (mounted) {
Navigator.of(context).pop();
}
_swapLock = false;
}
@ -199,6 +220,17 @@ class _ExchangeViewState extends ConsumerState<ExchangeView> {
void initState() {
_sendController = TextEditingController();
_receiveController = TextEditingController();
final isEstimated =
ref.read(prefsChangeNotifierProvider).exchangeRateType ==
ExchangeRateType.estimated;
_sendController.text = isEstimated
? ref.read(estimatedRateExchangeFormProvider).fromAmountString
: ref.read(fixedRateExchangeFormProvider).fromAmountString;
_receiveController.text = isEstimated
? ref.read(estimatedRateExchangeFormProvider).toAmountString
: ref.read(fixedRateExchangeFormProvider).toAmountString;
super.initState();
}

View file

@ -19,7 +19,7 @@ import 'package:stackwallet/pages/exchange_view/sub_widgets/step_row.dart';
import 'package:stackwallet/providers/exchange/available_currencies_state_provider.dart';
import 'package:stackwallet/providers/exchange/available_floating_rate_pairs_state_provider.dart';
import 'package:stackwallet/providers/exchange/change_now_provider.dart';
import 'package:stackwallet/providers/exchange/exchange_form_provider.dart';
import 'package:stackwallet/providers/exchange/estimate_rate_exchange_form_provider.dart';
import 'package:stackwallet/providers/exchange/exchange_send_from_wallet_id_provider.dart';
import 'package:stackwallet/providers/exchange/fixed_rate_exchange_form_provider.dart';
import 'package:stackwallet/providers/exchange/fixed_rate_market_pairs_provider.dart';
@ -74,6 +74,23 @@ class _WalletInitiatedExchangeViewState
_sendFocusNode.unfocus();
_receiveFocusNode.unfocus();
unawaited(
showDialog<void>(
context: context,
barrierDismissible: false,
builder: (_) => WillPopScope(
onWillPop: () async => false,
child: Container(
color: CFColors.stackAccent.withOpacity(0.8),
child: const CustomLoadingOverlay(
message: "Updating exchange rate",
eventBus: null,
),
),
),
),
);
if (ref.watch(prefsChangeNotifierProvider
.select((pref) => pref.exchangeRateType)) ==
ExchangeRateType.estimated) {
@ -93,6 +110,9 @@ class _WalletInitiatedExchangeViewState
}
}
}
if (mounted) {
Navigator.of(context).pop();
}
_swapLock = false;
}
@ -218,6 +238,11 @@ class _WalletInitiatedExchangeViewState
coin = widget.coin;
_sendController = TextEditingController();
_receiveController = TextEditingController();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
ref.read(estimatedRateExchangeFormProvider).clearAmounts(true);
// ref.read(fixedRateExchangeFormProvider);
});
super.initState();
}

View file

@ -6,7 +6,7 @@ import 'package:stackwallet/pages/exchange_view/exchange_view.dart';
import 'package:stackwallet/providers/exchange/available_currencies_state_provider.dart';
import 'package:stackwallet/providers/exchange/available_floating_rate_pairs_state_provider.dart';
import 'package:stackwallet/providers/exchange/change_now_provider.dart';
import 'package:stackwallet/providers/exchange/exchange_form_provider.dart';
import 'package:stackwallet/providers/exchange/estimate_rate_exchange_form_provider.dart';
import 'package:stackwallet/providers/exchange/fixed_rate_exchange_form_provider.dart';
import 'package:stackwallet/providers/exchange/fixed_rate_market_pairs_provider.dart';
import 'package:stackwallet/providers/providers.dart';
@ -79,7 +79,7 @@ class _HomeViewButtonBarState extends ConsumerState<HomeViewButtonBar> {
builder: (_) => StackDialog(
title: "Failed to fetch available currencies",
message:
"${response.exception?.toString()}\n\n${response2.exception?.toString()}",
"${response.exception?.toString()}\n\n${response2.exception?.toString()}",
),
));
}

View file

@ -17,7 +17,7 @@ import 'package:stackwallet/pages/wallet_view/sub_widgets/wallet_navigation_bar.
import 'package:stackwallet/pages/wallet_view/sub_widgets/wallet_summary.dart';
import 'package:stackwallet/pages/wallet_view/transaction_views/all_transactions_view.dart';
import 'package:stackwallet/providers/exchange/available_currencies_state_provider.dart';
import 'package:stackwallet/providers/exchange/exchange_form_provider.dart';
import 'package:stackwallet/providers/exchange/estimate_rate_exchange_form_provider.dart';
import 'package:stackwallet/providers/global/auto_swb_service_provider.dart';
import 'package:stackwallet/providers/providers.dart';
import 'package:stackwallet/providers/ui/transaction_filter_provider.dart';

View file

@ -2,4 +2,4 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:stackwallet/models/exchange/exchange_form_state.dart';
final estimatedRateExchangeFormProvider =
ChangeNotifierProvider((ref) => ExchangeFormState());
ChangeNotifierProvider((ref) => EstimatedRateExchangeFormState());