WIP exchange refactor and clean up

This commit is contained in:
julian 2023-02-05 17:50:40 -06:00
parent 4345a67d3f
commit 10605d96a2
15 changed files with 164 additions and 727 deletions

View file

@ -1,24 +0,0 @@
import 'package:stackwallet/models/exchange/response_objects/fixed_rate_market.dart';
import 'package:stackwallet/models/isar/exchange_cache/currency.dart';
import 'package:stackwallet/models/isar/exchange_cache/pair.dart';
class CNAvailableCurrencies {
final List<Currency> currencies = [];
final List<Pair> pairs = [];
final List<FixedRateMarket> markets = [];
void updateCurrencies(List<Currency> newCurrencies) {
currencies.clear();
currencies.addAll(newCurrencies);
}
void updateFloatingPairs(List<Pair> newPairs) {
pairs.clear();
pairs.addAll(newPairs);
}
void updateMarkets(List<FixedRateMarket> newMarkets) {
markets.clear();
markets.addAll(newMarkets);
}
}

View file

@ -1,436 +1,3 @@
// import 'package:decimal/decimal.dart';
// import 'package:flutter/cupertino.dart';
// import 'package:flutter/material.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/pages/exchange_view/sub_widgets/exchange_rate_sheet.dart';
// import 'package:stackwallet/services/exchange/change_now/change_now_exchange.dart';
// import 'package:stackwallet/services/exchange/exchange.dart';
// // import 'package:stackwallet/services/exchange/simpleswap/simpleswap_exchange.dart';
// import 'package:stackwallet/utilities/logger.dart';
//
// class ExchangeFormState extends ChangeNotifier {
// Exchange? _exchange;
// Exchange? get exchange => _exchange;
// set exchange(Exchange? value) {
// _exchange = value;
// _onExchangeTypeChanged();
// }
//
// ExchangeRateType _exchangeType = ExchangeRateType.estimated;
// ExchangeRateType get exchangeType => _exchangeType;
// set exchangeType(ExchangeRateType value) {
// _exchangeType = value;
// _onExchangeRateTypeChanged();
// }
//
// bool reversed = false;
//
// Decimal? fromAmount;
// Decimal? toAmount;
//
// Decimal? minAmount;
// Decimal? maxAmount;
//
// Decimal? rate;
// Estimate? estimate;
//
// FixedRateMarket? _market;
// FixedRateMarket? get market => _market;
//
// Currency? _from;
// Currency? _to;
//
// @override
// String toString() {
// return 'ExchangeFormState: {_exchange: $_exchange, _exchangeType: $_exchangeType, reversed: $reversed, fromAmount: $fromAmount, toAmount: $toAmount, minAmount: $minAmount, maxAmount: $maxAmount, rate: $rate, estimate: $estimate, _market: $_market, _from: $_from, _to: $_to, _onError: $_onError}';
// }
//
// String? get fromTicker {
// switch (exchangeType) {
// case ExchangeRateType.estimated:
// return _from?.ticker;
// case ExchangeRateType.fixed:
// switch (exchange?.name) {
// // case SimpleSwapExchange.exchangeName:
// // return _from?.ticker;
// case ChangeNowExchange.exchangeName:
// return market?.from;
// default:
// return null;
// }
// }
// }
//
// String? get toTicker {
// switch (exchangeType) {
// case ExchangeRateType.estimated:
// return _to?.ticker;
// case ExchangeRateType.fixed:
// switch (exchange?.name) {
// // case SimpleSwapExchange.exchangeName:
// // return _to?.ticker;
// case ChangeNowExchange.exchangeName:
// return market?.to;
// default:
// return null;
// }
// }
// }
//
// void Function(String)? _onError;
//
// Currency? get from => _from;
// Currency? get to => _to;
//
// void setCurrencies(Currency from, Currency to) {
// _from = from;
// _to = to;
// }
//
// String get warning {
// if (reversed) {
// if (toTicker != null && toAmount != null) {
// if (minAmount != null &&
// toAmount! < minAmount! &&
// toAmount! > Decimal.zero) {
// return "Minimum amount ${minAmount!.toString()} ${toTicker!.toUpperCase()}";
// } else if (maxAmount != null && toAmount! > maxAmount!) {
// return "Maximum amount ${maxAmount!.toString()} ${toTicker!.toUpperCase()}";
// }
// }
// } else {
// if (fromTicker != null && fromAmount != null) {
// if (minAmount != null &&
// fromAmount! < minAmount! &&
// fromAmount! > Decimal.zero) {
// return "Minimum amount ${minAmount!.toString()} ${fromTicker!.toUpperCase()}";
// } else if (maxAmount != null && fromAmount! > maxAmount!) {
// return "Maximum amount ${maxAmount!.toString()} ${fromTicker!.toUpperCase()}";
// }
// }
// }
//
// return "";
// }
//
// String get fromAmountString => fromAmount?.toStringAsFixed(8) ?? "";
// String get toAmountString => toAmount?.toStringAsFixed(8) ?? "";
//
// bool get canExchange {
// if (exchange?.name == ChangeNowExchange.exchangeName &&
// exchangeType == ExchangeRateType.fixed) {
// return _market != null &&
// fromAmount != null &&
// toAmount != null &&
// warning.isEmpty;
// } else {
// return fromAmount != null &&
// fromAmount != Decimal.zero &&
// toAmount != null &&
// rate != null &&
// warning.isEmpty;
// }
// }
//
// void clearAmounts(bool shouldNotifyListeners) {
// fromAmount = null;
// toAmount = null;
// minAmount = null;
// maxAmount = null;
// rate = null;
//
// if (shouldNotifyListeners) {
// notifyListeners();
// }
// }
//
// Future<void> setFromAmountAndCalculateToAmount(
// Decimal newFromAmount,
// bool shouldNotifyListeners,
// ) async {
// if (newFromAmount == Decimal.zero) {
// toAmount = Decimal.zero;
// }
//
// fromAmount = newFromAmount;
// reversed = false;
//
// await updateRanges(shouldNotifyListeners: false);
//
// await updateEstimate(
// shouldNotifyListeners: false,
// reversed: reversed,
// );
//
// if (shouldNotifyListeners) {
// notifyListeners();
// }
// }
//
// Future<void> setToAmountAndCalculateFromAmount(
// Decimal newToAmount,
// bool shouldNotifyListeners,
// ) async {
// if (newToAmount == Decimal.zero) {
// fromAmount = Decimal.zero;
// }
//
// toAmount = newToAmount;
// reversed = true;
//
// await updateRanges(shouldNotifyListeners: false);
//
// await updateEstimate(
// shouldNotifyListeners: false,
// reversed: reversed,
// );
//
// if (shouldNotifyListeners) {
// notifyListeners();
// }
// }
//
// Future<void> updateTo(Currency to, bool shouldNotifyListeners) async {
// try {
// _to = to;
// if (_from == null) {
// rate = null;
// notifyListeners();
// return;
// }
//
// await updateRanges(shouldNotifyListeners: false);
//
// await updateEstimate(
// shouldNotifyListeners: false,
// reversed: reversed,
// );
//
// //todo: check if print needed
// // debugPrint(
// // "_updated TO: _from=${_from!.ticker} _to=${_to!.ticker} _fromAmount=$fromAmount _toAmount=$toAmount rate:$rate for: $exchange");
//
// if (shouldNotifyListeners) {
// notifyListeners();
// }
// } catch (e, s) {
// Logging.instance.log("$e\n$s", level: LogLevel.Error);
// }
// }
//
// Future<void> updateFrom(Currency from, bool shouldNotifyListeners) async {
// try {
// _from = from;
//
// if (_to == null) {
// rate = null;
// notifyListeners();
// return;
// }
//
// await updateRanges(shouldNotifyListeners: false);
//
// await updateEstimate(
// shouldNotifyListeners: false,
// reversed: reversed,
// );
//
// //todo: check if print needed
// // debugPrint(
// // "_updated FROM: _from=${_from!.ticker} _to=${_to!.ticker} _fromAmount=$fromAmount _toAmount=$toAmount rate:$rate for: $exchange");
// if (shouldNotifyListeners) {
// notifyListeners();
// }
// } catch (e, s) {
// Logging.instance.log("$e\n$s", level: LogLevel.Error);
// }
// }
//
// Future<void> updateMarket(
// FixedRateMarket? market,
// bool shouldNotifyListeners,
// ) async {
// _market = market;
//
// if (_market == null) {
// fromAmount = null;
// toAmount = null;
// } else {
// if (fromAmount != null) {
// if (fromAmount! <= Decimal.zero) {
// toAmount = Decimal.zero;
// } else {
// await updateRanges(shouldNotifyListeners: false);
// await updateEstimate(
// shouldNotifyListeners: false,
// reversed: reversed,
// );
// }
// }
// }
//
// if (shouldNotifyListeners) {
// notifyListeners();
// }
// }
//
// void _onExchangeRateTypeChanged() {
// print("_onExchangeRateTypeChanged");
// updateRanges(shouldNotifyListeners: true).then(
// (_) => updateEstimate(
// shouldNotifyListeners: true,
// reversed: reversed,
// ),
// );
// }
//
// void _onExchangeTypeChanged() {
// updateRanges(shouldNotifyListeners: true).then(
// (_) => updateEstimate(
// shouldNotifyListeners: true,
// reversed: reversed,
// ),
// );
// }
//
// Future<void> updateRanges({required bool shouldNotifyListeners}) async {
// // if (exchange?.name == SimpleSwapExchange.exchangeName) {
// // reversed = false;
// // }
// final _fromTicker = reversed ? toTicker : fromTicker;
// final _toTicker = reversed ? fromTicker : toTicker;
// if (_fromTicker == null || _toTicker == null) {
// Logging.instance.log(
// "Tried to $runtimeType.updateRanges where (from: $_fromTicker || to: $_toTicker) for: $exchange",
// level: LogLevel.Info,
// );
// return;
// }
// final response = await exchange?.getRange(
// _fromTicker,
// _toTicker,
// exchangeType == ExchangeRateType.fixed,
// );
//
// if (response?.value == null) {
// Logging.instance.log(
// "Tried to $runtimeType.updateRanges for: $exchange where response: $response",
// level: LogLevel.Info,
// );
// return;
// }
//
// final range = response!.value!;
//
// minAmount = range.min;
// maxAmount = range.max;
//
// //todo: check if print needed
// // debugPrint(
// // "updated range for: $exchange for $_fromTicker-$_toTicker: $range");
//
// if (shouldNotifyListeners) {
// notifyListeners();
// }
// }
//
// Future<void> updateEstimate({
// required bool shouldNotifyListeners,
// required bool reversed,
// }) async {
// // if (exchange?.name == SimpleSwapExchange.exchangeName) {
// // reversed = false;
// // }
// final amount = reversed ? toAmount : fromAmount;
// if (fromTicker == null ||
// toTicker == null ||
// amount == null ||
// amount <= Decimal.zero) {
// Logging.instance.log(
// "Tried to $runtimeType.updateEstimate for: $exchange where (from: $fromTicker || to: $toTicker || amount: $amount)",
// level: LogLevel.Info,
// );
// return;
// }
// final response = await exchange?.getEstimate(
// fromTicker!,
// toTicker!,
// amount,
// exchangeType == ExchangeRateType.fixed,
// reversed,
// );
//
// if (response?.value == null) {
// Logging.instance.log(
// "Tried to $runtimeType.updateEstimate for: $exchange where response: $response",
// level: LogLevel.Info,
// );
// return;
// }
//
// estimate = response!.value!;
//
// if (reversed) {
// fromAmount = estimate!.estimatedAmount;
// } else {
// toAmount = estimate!.estimatedAmount;
// }
//
// rate = (toAmount! / fromAmount!).toDecimal(scaleOnInfinitePrecision: 12);
//
// //todo: check if print needed
// // debugPrint(
// // "updated estimate for: $exchange for $fromTicker-$toTicker: $estimate");
//
// if (shouldNotifyListeners) {
// notifyListeners();
// }
// }
//
// void setOnError({
// required void Function(String)? onError,
// bool shouldNotifyListeners = false,
// }) {
// _onError = onError;
// if (shouldNotifyListeners) {
// notifyListeners();
// }
// }
//
// Future<void> swap({FixedRateMarket? market}) async {
// final Decimal? newToAmount = fromAmount;
// final Decimal? newFromAmount = toAmount;
//
// fromAmount = newFromAmount;
// toAmount = newToAmount;
//
// minAmount = null;
// maxAmount = null;
//
// if (exchangeType == ExchangeRateType.fixed &&
// exchange?.name == ChangeNowExchange.exchangeName) {
// await updateMarket(market, false);
// } else {
// final Currency? newTo = from;
// final Currency? newFrom = to;
//
// _to = newTo;
// _from = newFrom;
//
// await updateRanges(shouldNotifyListeners: false);
//
// await updateEstimate(
// shouldNotifyListeners: false,
// reversed: reversed,
// );
// }
//
// notifyListeners();
// }
// }
import 'package:decimal/decimal.dart';
import 'package:flutter/foundation.dart';
import 'package:stackwallet/models/exchange/response_objects/estimate.dart';
@ -445,12 +12,6 @@ class ExchangeFormState extends ChangeNotifier {
Exchange? _exchange;
Exchange get exchange => _exchange ??= Exchange.defaultExchange;
set exchange(Exchange value) {
_exchange = value;
_updateRangesAndEstimate(
shouldNotifyListeners: true,
);
}
Estimate? _estimate;
Estimate? get estimate => _estimate;
@ -577,6 +138,23 @@ class ExchangeFormState extends ChangeNotifier {
// public state updaters
//============================================================================
Future<void> updateExchange({
required Exchange exchange,
required bool shouldUpdateData,
required bool shouldNotifyListeners,
}) async {
_exchange = exchange;
if (shouldUpdateData) {
await _updateRangesAndEstimate(
shouldNotifyListeners: false,
);
}
if (shouldNotifyListeners) {
notifyListeners();
}
}
void setCurrencies(Currency from, Currency to) {
_sendCurrency = from;
_receiveCurrency = to;
@ -660,6 +238,9 @@ class ExchangeFormState extends ChangeNotifier {
) async {
try {
_sendCurrency = sendCurrency;
_minSendAmount = null;
_maxSendAmount = null;
if (_receiveCurrency == null) {
_rate = null;
} else {
@ -681,6 +262,8 @@ class ExchangeFormState extends ChangeNotifier {
) async {
try {
_receiveCurrency = receiveCurrency;
_minReceiveAmount = null;
_maxReceiveAmount = null;
if (_sendCurrency == null) {
_rate = null;

View file

@ -1,30 +0,0 @@
import 'package:stackwallet/models/isar/exchange_cache/currency.dart';
import 'package:stackwallet/models/isar/exchange_cache/pair.dart';
class MBAvailableCurrencies {
final List<Currency> currencies = [];
// final List<Currency> fixedRateCurrencies = [];
final List<Pair> pairs = [];
// final List<Pair> fixedRatePairs = [];
// void updateFloatingCurrencies(List<Currency> newCurrencies) {
// floatingRateCurrencies.clear();
// floatingRateCurrencies.addAll(newCurrencies);
// }
void updateCurrencies(List<Currency> newCurrencies) {
currencies.clear();
currencies.addAll(newCurrencies);
}
// void updateFloatingPairs(List<Pair> newPairs) {
// floatingRatePairs.clear();
// floatingRatePairs.addAll(newPairs);
// }
void updatePairs(List<Pair> newPairs) {
pairs.clear();
pairs.addAll(newPairs);
}
}

View file

@ -1,30 +0,0 @@
import 'package:stackwallet/models/isar/exchange_cache/currency.dart';
import 'package:stackwallet/models/isar/exchange_cache/pair.dart';
class SPAvailableCurrencies {
final List<Currency> floatingRateCurrencies = [];
final List<Currency> fixedRateCurrencies = [];
final List<Pair> floatingRatePairs = [];
final List<Pair> fixedRatePairs = [];
void updateFloatingCurrencies(List<Currency> newCurrencies) {
floatingRateCurrencies.clear();
floatingRateCurrencies.addAll(newCurrencies);
}
void updateFixedCurrencies(List<Currency> newCurrencies) {
fixedRateCurrencies.clear();
fixedRateCurrencies.addAll(newCurrencies);
}
void updateFloatingPairs(List<Pair> newPairs) {
floatingRatePairs.clear();
floatingRatePairs.addAll(newPairs);
}
void updateFixedPairs(List<Pair> newPairs) {
fixedRatePairs.clear();
fixedRatePairs.addAll(newPairs);
}
}

View file

@ -52,7 +52,9 @@ class _ExchangeCurrencySelectionViewState
.where()
.exchangeNameEqualTo(widget.exchangeName)
.filter()
.supportsFixedRateEqualTo(widget.isFixedRate)
.group((q) => widget.isFixedRate
? q.supportsFixedRateEqualTo(true)
: q.supportsEstimatedRateEqualTo(true))
.and()
.group((q) => q
.nameContains(text, caseSensitive: false)
@ -81,7 +83,9 @@ class _ExchangeCurrencySelectionViewState
.where()
.exchangeNameEqualTo(widget.exchangeName)
.filter()
.supportsFixedRateEqualTo(widget.isFixedRate);
.group((q) => widget.isFixedRate
? q.supportsFixedRateEqualTo(true)
: q.supportsEstimatedRateEqualTo(true));
if (widget.paired != null) {
_currencies = query

View file

@ -17,11 +17,9 @@ import 'package:stackwallet/pages/exchange_view/sub_widgets/exchange_provider_op
import 'package:stackwallet/pages/exchange_view/sub_widgets/exchange_rate_sheet.dart';
import 'package:stackwallet/pages/exchange_view/sub_widgets/rate_type_toggle.dart';
import 'package:stackwallet/pages_desktop_specific/desktop_exchange/exchange_steps/step_scaffold.dart';
import 'package:stackwallet/providers/exchange/available_majesticbank_currencies_provider.dart';
import 'package:stackwallet/providers/providers.dart';
import 'package:stackwallet/services/exchange/change_now/change_now_exchange.dart';
import 'package:stackwallet/services/exchange/exchange_data_loading_service.dart';
import 'package:stackwallet/services/exchange/majestic_bank/majestic_bank_exchange.dart';
// import 'package:stackwallet/services/exchange/simpleswap/simpleswap_exchange.dart';
import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/constants.dart';
@ -527,44 +525,24 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
ref.read(exchangeFormStateProvider(rateType)).sendAmount!;
final estimate = ref.read(exchangeFormStateProvider(rateType)).estimate!;
final exchangeName = ref.read(currentExchangeNameStateProvider.state).state;
String rate;
switch (rateType) {
case ExchangeRateType.estimated:
bool isAvailable = false;
late final Iterable<Pair> availableFloatingPairs;
final pair = await ExchangeDataLoadingService.instance.isar.pairs
.where()
.exchangeNameEqualTo(exchangeName)
.filter()
.floatingRateEqualTo(true)
.and()
.fromEqualTo(fromTicker, caseSensitive: false)
.and()
.toEqualTo(toTicker, caseSensitive: false)
.findFirst();
switch (ref.read(currentExchangeNameStateProvider.state).state) {
case ChangeNowExchange.exchangeName:
availableFloatingPairs = ref
.read(availableChangeNowCurrenciesProvider)
.pairs
.where((e) => e.to == toTicker && e.from == fromTicker);
break;
case MajesticBankExchange.exchangeName:
availableFloatingPairs = ref
.read(availableMajesticBankCurrenciesProvider)
.pairs
.where((e) => e.to == toTicker && e.from == fromTicker);
break;
// case SimpleSwapExchange.exchangeName:
// availableFloatingPairs = ref
// .read(availableSimpleswapCurrenciesProvider)
// .floatingRatePairs
// .where((e) => e.to == toTicker && e.from == fromTicker);
// break;
default:
availableFloatingPairs = [];
}
for (final pair in availableFloatingPairs) {
if (pair.from == fromTicker && pair.to == toTicker) {
isAvailable = true;
break;
}
}
if (!isAvailable) {
if (pair == null) {
unawaited(
showDialog<dynamic>(
context: context,
@ -885,8 +863,11 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
// provider for simpleswap; not called rn
ref.listen<String>(currentExchangeNameStateProvider, (previous, next) {
ref.read(exchangeFormStateProvider(rateType)).exchange =
ref.read(exchangeProvider);
ref.read(exchangeFormStateProvider(rateType)).updateExchange(
exchange: ref.read(exchangeProvider),
shouldUpdateData: true,
shouldNotifyListeners: true,
);
});
final isEstimated = ref.watch(prefsChangeNotifierProvider

View file

@ -5,7 +5,6 @@ import 'package:flutter_svg/svg.dart';
import 'package:stackwallet/models/exchange/response_objects/estimate.dart';
import 'package:stackwallet/providers/providers.dart';
import 'package:stackwallet/services/exchange/change_now/change_now_exchange.dart';
import 'package:stackwallet/services/exchange/exchange.dart';
import 'package:stackwallet/services/exchange/exchange_response.dart';
import 'package:stackwallet/services/exchange/majestic_bank/majestic_bank_exchange.dart';
import 'package:stackwallet/utilities/assets.dart';
@ -60,14 +59,6 @@ class ExchangeProviderOptions extends ConsumerWidget {
ChangeNowExchange.exchangeName) {
ref.read(currentExchangeNameStateProvider.state).state =
ChangeNowExchange.exchangeName;
ref
.read(exchangeFormStateProvider(ref
.read(prefsChangeNotifierProvider)
.exchangeRateType))
.exchange =
Exchange.fromName(ref
.read(currentExchangeNameStateProvider.state)
.state);
}
},
child: Container(
@ -94,21 +85,21 @@ class ExchangeProviderOptions extends ConsumerWidget {
.watch(currentExchangeNameStateProvider.state)
.state,
onChanged: (value) {
if (value is String) {
ref
.read(
currentExchangeNameStateProvider.state)
.state = value;
ref
.read(exchangeFormStateProvider(ref
.read(prefsChangeNotifierProvider)
.exchangeRateType))
.exchange =
Exchange.fromName(ref
.read(currentExchangeNameStateProvider
.state)
.state);
}
// if (value is String) {
// ref
// .read(
// currentExchangeNameStateProvider.state)
// .state = value;
// ref
// .read(exchangeFormStateProvider(ref
// .read(prefsChangeNotifierProvider)
// .exchangeRateType))
// .exchange =
// Exchange.fromName(ref
// .read(currentExchangeNameStateProvider
// .state)
// .state);
// }
},
),
),
@ -282,14 +273,6 @@ class ExchangeProviderOptions extends ConsumerWidget {
MajesticBankExchange.exchangeName) {
ref.read(currentExchangeNameStateProvider.state).state =
MajesticBankExchange.exchangeName;
ref
.read(exchangeFormStateProvider(ref
.read(prefsChangeNotifierProvider)
.exchangeRateType))
.exchange =
Exchange.fromName(ref
.read(currentExchangeNameStateProvider.state)
.state);
}
},
child: Container(
@ -316,21 +299,21 @@ class ExchangeProviderOptions extends ConsumerWidget {
.watch(currentExchangeNameStateProvider.state)
.state,
onChanged: (value) {
if (value is String) {
ref
.read(
currentExchangeNameStateProvider.state)
.state = value;
ref
.read(exchangeFormStateProvider(ref
.read(prefsChangeNotifierProvider)
.exchangeRateType))
.exchange =
Exchange.fromName(ref
.read(currentExchangeNameStateProvider
.state)
.state);
}
// if (value is String) {
// ref
// .read(
// currentExchangeNameStateProvider.state)
// .state = value;
// ref
// .read(exchangeFormStateProvider(ref
// .read(prefsChangeNotifierProvider)
// .exchangeRateType))
// .exchange =
// Exchange.fromName(ref
// .read(currentExchangeNameStateProvider
// .state)
// .state);
// }
},
),
),

View file

@ -5,14 +5,13 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:stackwallet/notifications/show_flush_bar.dart';
import 'package:stackwallet/providers/global/debug_service_provider.dart';
import 'package:stackwallet/providers/providers.dart';
import 'package:stackwallet/services/exchange/majestic_bank/majestic_bank_api.dart';
import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/utilities/theme/stack_colors.dart';
import 'package:stackwallet/widgets/background.dart';
import 'package:stackwallet/widgets/rounded_white_container.dart';
import '../../../services/exchange/majestic_bank/majestic_bank_api.dart';
class HiddenSettings extends StatelessWidget {
const HiddenSettings({Key? key}) : super(key: key);
@ -157,8 +156,8 @@ class HiddenSettings extends StatelessWidget {
Consumer(builder: (_, ref, __) {
return GestureDetector(
onTap: () async {
final x =
await MajesticBankAPI.instance.getLimits();
final x = await MajesticBankAPI.instance
.getLimit(fromCurrency: 'btc');
print(x);
},
child: RoundedWhiteContainer(

View file

@ -7,7 +7,6 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/svg.dart';
import 'package:stackwallet/notifications/show_flush_bar.dart';
import 'package:stackwallet/pages/buy_view/buy_in_wallet_view.dart';
import 'package:stackwallet/pages/exchange_view/sub_widgets/exchange_rate_sheet.dart';
import 'package:stackwallet/pages/exchange_view/wallet_initiated_exchange_view.dart';
import 'package:stackwallet/pages/home_view/home_view.dart';
import 'package:stackwallet/pages/notification_views/notifications_view.dart';
@ -30,7 +29,6 @@ import 'package:stackwallet/services/coins/manager.dart';
import 'package:stackwallet/services/event_bus/events/global/node_connection_status_changed_event.dart';
import 'package:stackwallet/services/event_bus/events/global/wallet_sync_status_changed_event.dart';
import 'package:stackwallet/services/event_bus/global_event_bus.dart';
import 'package:stackwallet/services/exchange/change_now/change_now_exchange.dart';
import 'package:stackwallet/services/exchange/exchange_data_loading_service.dart';
import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/constants.dart';
@ -248,36 +246,33 @@ class _WalletViewState extends ConsumerState<WalletView> {
),
);
} else {
ref.read(currentExchangeNameStateProvider.state).state =
ChangeNowExchange.exchangeName;
final walletId = ref.read(managerProvider).walletId;
ref.read(prefsChangeNotifierProvider).exchangeRateType =
ExchangeRateType.estimated;
ref.read(exchangeFormStateProvider(ExchangeRateType.estimated)).exchange =
ref.read(exchangeProvider);
final currencies = ref
.read(availableChangeNowCurrenciesProvider)
.currencies
.where((element) =>
element.ticker.toLowerCase() == coin.ticker.toLowerCase());
if (currencies.isNotEmpty) {
ref
.read(exchangeFormStateProvider(ExchangeRateType.estimated))
.setCurrencies(
currencies.first,
ref
.read(availableChangeNowCurrenciesProvider)
.currencies
.firstWhere(
(element) =>
element.ticker.toLowerCase() !=
coin.ticker.toLowerCase(),
),
);
}
// ref.read(currentExchangeNameStateProvider.state).state =
// ChangeNowExchange.exchangeName;
// final walletId = ref.read(managerProvider).walletId;
// ref.read(prefsChangeNotifierProvider).exchangeRateType =
// ExchangeRateType.estimated;
//
// final currencies = ref
// .read(availableChangeNowCurrenciesProvider)
// .currencies
// .where((element) =>
// element.ticker.toLowerCase() == coin.ticker.toLowerCase());
//
// if (currencies.isNotEmpty) {
// ref
// .read(exchangeFormStateProvider(ExchangeRateType.estimated))
// .setCurrencies(
// currencies.first,
// ref
// .read(availableChangeNowCurrenciesProvider)
// .currencies
// .firstWhere(
// (element) =>
// element.ticker.toLowerCase() !=
// coin.ticker.toLowerCase(),
// ),
// );
// }
if (mounted) {
unawaited(

View file

@ -1,6 +0,0 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:stackwallet/models/exchange/change_now/cn_available_currencies.dart';
final availableChangeNowCurrenciesProvider = Provider<CNAvailableCurrencies>(
(ref) => CNAvailableCurrencies(),
);

View file

@ -1,6 +0,0 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:stackwallet/models/exchange/majestic_bank/mb_available_currencies.dart';
final availableMajesticBankCurrenciesProvider = Provider<MBAvailableCurrencies>(
(ref) => MBAvailableCurrencies(),
);

View file

@ -1,6 +0,0 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:stackwallet/models/exchange/simpleswap/sp_available_currencies.dart';
final availableSimpleswapCurrenciesProvider = Provider<SPAvailableCurrencies>(
(ref) => SPAvailableCurrencies(),
);

View file

@ -1,8 +1,6 @@
export './buy/buy_form_state_provider.dart';
export './buy/simplex_initial_load_status.dart';
export './buy/simplex_provider.dart';
export './exchange/available_changenow_currencies_provider.dart';
export './exchange/available_simpleswap_currencies_provider.dart';
export './exchange/changenow_initial_load_status.dart';
export './exchange/current_exchange_name_state_provider.dart';
export './exchange/exchange_flow_is_active_state_provider.dart';

View file

@ -39,8 +39,9 @@ class ExchangeDataLoadingService {
final time = DateTime.now();
try {
await Future.wait([
_loadFixedRateMarkets(),
_loadChangeNowStandardCurrencies(),
_loadChangeNowCurrencies(),
_loadChangeNowFixedRatePairs(),
_loadChangeNowEstimatedRatePairs(),
// loadSimpleswapFixedRateCurrencies(ref),
// loadSimpleswapFloatingRateCurrencies(ref),
loadMajesticBankCurrencies(),
@ -57,78 +58,72 @@ class ExchangeDataLoadingService {
}
}
Future<void> _loadFixedRateMarkets() async {
Future<void> _loadChangeNowCurrencies() async {
final exchange = ChangeNowExchange.instance;
final responseCurrencies = await exchange.getAllCurrencies(true);
final responseCurrencies = await exchange.getAllCurrencies(false);
if (responseCurrencies.value != null) {
final responsePairs = await exchange.getAllPairs(true);
if (responsePairs.value != null) {
await isar.writeTxn(() async {
final idsToDelete = await isar.currencies
.where()
.exchangeNameEqualTo(ChangeNowExchange.exchangeName)
.idProperty()
.findAll();
await isar.currencies.deleteAll(idsToDelete);
await isar.currencies.putAll(responseCurrencies.value!);
final idsToDelete2 = await isar.pairs
.where()
.exchangeNameEqualTo(ChangeNowExchange.exchangeName)
.idProperty()
.findAll();
await isar.pairs.deleteAll(idsToDelete2);
await isar.pairs.putAll(responsePairs.value!);
});
} else {
Logging.instance.log(
"Failed to load changeNOW available fixed rate pairs: ${responsePairs.exception?.message}",
level: LogLevel.Error);
return;
}
await isar.writeTxn(() async {
final idsToDelete = await isar.currencies
.where()
.exchangeNameEqualTo(ChangeNowExchange.exchangeName)
.idProperty()
.findAll();
await isar.currencies.deleteAll(idsToDelete);
await isar.currencies.putAll(responseCurrencies.value!);
});
} else {
Logging.instance.log(
"Failed to load changeNOW fixed rate currencies: ${responseCurrencies.exception?.message}",
"Failed to load changeNOW currencies: ${responseCurrencies.exception?.message}",
level: LogLevel.Error);
return;
}
}
Future<void> _loadChangeNowStandardCurrencies() async {
Future<void> _loadChangeNowFixedRatePairs() async {
final exchange = ChangeNowExchange.instance;
final responseCurrencies = await exchange.getAllCurrencies(false);
if (responseCurrencies.value != null) {
final responsePairs = await exchange.getAllPairs(false);
final responsePairs = await exchange.getAllPairs(true);
if (responsePairs.value != null) {
await isar.writeTxn(() async {
final idsToDelete = await isar.currencies
.where()
.exchangeNameEqualTo(ChangeNowExchange.exchangeName)
.idProperty()
.findAll();
await isar.currencies.deleteAll(idsToDelete);
await isar.currencies.putAll(responseCurrencies.value!);
final idsToDelete2 = await isar.pairs
.where()
.exchangeNameEqualTo(ChangeNowExchange.exchangeName)
.idProperty()
.findAll();
await isar.pairs.deleteAll(idsToDelete2);
await isar.pairs.putAll(responsePairs.value!);
});
} else {
Logging.instance.log(
"Failed to load changeNOW available floating rate pairs: ${responsePairs.exception?.message}",
level: LogLevel.Error);
return;
}
if (responsePairs.value != null) {
await isar.writeTxn(() async {
final idsToDelete2 = await isar.pairs
.where()
.exchangeNameEqualTo(ChangeNowExchange.exchangeName)
.filter()
.fixedRateEqualTo(true)
.idProperty()
.findAll();
await isar.pairs.deleteAll(idsToDelete2);
await isar.pairs.putAll(responsePairs.value!);
});
} else {
Logging.instance.log(
"Failed to load changeNOW currencies: ${responseCurrencies.exception?.message}",
"Failed to load changeNOW available fixed rate pairs: ${responsePairs.exception?.message}",
level: LogLevel.Error);
return;
}
}
Future<void> _loadChangeNowEstimatedRatePairs() async {
final exchange = ChangeNowExchange.instance;
final responsePairs = await exchange.getAllPairs(false);
if (responsePairs.value != null) {
await isar.writeTxn(() async {
final idsToDelete = await isar.pairs
.where()
.exchangeNameEqualTo(ChangeNowExchange.exchangeName)
.filter()
.floatingRateEqualTo(true)
.idProperty()
.findAll();
await isar.pairs.deleteAll(idsToDelete);
await isar.pairs.putAll(responsePairs.value!);
});
} else {
Logging.instance.log(
"Failed to load changeNOW available floating rate pairs: ${responsePairs.exception?.message}",
level: LogLevel.Error);
return;
}

View file

@ -189,8 +189,9 @@ class MajesticBankAPI {
return ExchangeResponse(value: result);
} catch (e, s) {
Logging.instance
.log("calculateOrder exception: $e\n$s", level: LogLevel.Error);
Logging.instance.log(
"calculateOrder $fromCurrency-$receiveCurrency exception: $e\n$s",
level: LogLevel.Error);
return ExchangeResponse(
exception: ExchangeException(
e.toString(),