- Recalculate best rate if isFixedRate changed

- check limits against the receive amount if isFixedRate
This commit is contained in:
OmarHatem 2022-12-06 13:30:11 +02:00
parent 945dc8e859
commit 36c2702688

View file

@ -44,7 +44,6 @@ abstract class ExchangeViewModelBase with Store {
ExchangeViewModelBase(this.wallet, this.trades, this._exchangeTemplateStore,
this.tradesStore, this._settingsStore, this.sharedPreferences)
: _cryptoNumberFormat = NumberFormat(),
isReverse = false,
isFixedRateMode = false,
isReceiveAmountEntered = false,
depositAmount = '',
@ -112,7 +111,11 @@ abstract class ExchangeViewModelBase with Store {
loadLimits();
reaction(
(_) => isFixedRateMode,
(Object _) => loadLimits());
(Object _) {
loadLimits();
_bestRate = 0;
_calculateBestRate();
});
}
final WalletBase wallet;
@ -227,8 +230,6 @@ abstract class ExchangeViewModelBase with Store {
Limits limits;
bool isReverse;
NumberFormat _cryptoNumberFormat;
final SettingsStore _settingsStore;
@ -258,7 +259,6 @@ abstract class ExchangeViewModelBase with Store {
@action
Future<void> changeReceiveAmount({required String amount}) async {
receiveAmount = amount;
isReverse = true;
if (amount.isEmpty) {
depositAmount = '';
@ -283,7 +283,6 @@ abstract class ExchangeViewModelBase with Store {
@action
Future<void> changeDepositAmount({required String amount}) async {
depositAmount = amount;
isReverse = false;
if (amount.isEmpty) {
depositAmount = '';
@ -307,7 +306,7 @@ abstract class ExchangeViewModelBase with Store {
}
Future<void> _calculateBestRate() async {
final amount = double.tryParse(isFixedRateMode ? receiveAmount : depositAmount) ?? 1;
final amount = double.tryParse(depositAmount) ?? 1;
final result = await Future.wait<double>(
_tradeAvailableProviders
@ -401,7 +400,7 @@ abstract class ExchangeViewModelBase with Store {
settleAddress: receiveAddress,
refundAddress: depositAddress,
);
amount = depositAmount;
amount = isFixedRateMode ? receiveAmount : depositAmount;
}
if (provider is SimpleSwapExchangeProvider) {
@ -412,7 +411,7 @@ abstract class ExchangeViewModelBase with Store {
address: receiveAddress,
refundAddress: depositAddress,
);
amount = depositAmount;
amount = isFixedRateMode ? receiveAmount : depositAmount;
}
if (provider is XMRTOExchangeProvider) {
@ -424,7 +423,7 @@ abstract class ExchangeViewModelBase with Store {
address: receiveAddress,
refundAddress: depositAddress,
isBTCRequest: isReceiveAmountEntered);
amount = depositAmount;
amount = isFixedRateMode ? receiveAmount : depositAmount;
}
if (provider is ChangeNowExchangeProvider) {
@ -435,8 +434,8 @@ abstract class ExchangeViewModelBase with Store {
toAmount: receiveAmount.replaceAll(',', '.'),
refundAddress: depositAddress,
address: receiveAddress,
isReverse: isReverse);
amount = isReverse ? receiveAmount : depositAmount;
isReverse: isFixedRateMode);
amount = isFixedRateMode ? receiveAmount : depositAmount;
}
if (provider is MorphTokenExchangeProvider) {
@ -446,13 +445,13 @@ abstract class ExchangeViewModelBase with Store {
amount: depositAmount.replaceAll(',', '.'),
refundAddress: depositAddress,
address: receiveAddress);
amount = depositAmount;
amount = isFixedRateMode ? receiveAmount : depositAmount;
}
amount = amount.replaceAll(',', '.');
if (limitsState is LimitsLoadedSuccessfully) {
if (double.parse(amount) < limits.min!) {
if (limits.max != null && double.parse(amount) < limits.min!) {
continue;
} else if (limits.max != null && double.parse(amount) > limits.max!) {
continue;