mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-06-28 20:39:51 +00:00
Cw 897 stable coins exchanges rate issue (#1939)
* update amount in UI when best rate changes * minor fixes * update rate when amount has changed
This commit is contained in:
parent
066eec2b26
commit
c88809133f
5 changed files with 36 additions and 21 deletions
|
@ -11,6 +11,7 @@ import 'package:cake_wallet/exchange/trade_request.dart';
|
|||
import 'package:cake_wallet/exchange/trade_state.dart';
|
||||
import 'package:cake_wallet/exchange/utils/currency_pairs_utils.dart';
|
||||
import 'package:cw_core/crypto_currency.dart';
|
||||
import 'package:cw_core/utils/print_verbose.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class LetsExchangeExchangeProvider extends ExchangeProvider {
|
||||
|
@ -101,7 +102,7 @@ class LetsExchangeExchangeProvider extends ExchangeProvider {
|
|||
|
||||
return isFixedRateMode ? amount / amountToGet : amountToGet / amount;
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
printV(e.toString());
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import 'package:cake_wallet/exchange/trade_request.dart';
|
|||
import 'package:cake_wallet/exchange/trade_state.dart';
|
||||
import 'package:cake_wallet/exchange/utils/currency_pairs_utils.dart';
|
||||
import 'package:cw_core/crypto_currency.dart';
|
||||
import 'package:cw_core/utils/print_verbose.dart';
|
||||
import 'package:http/http.dart';
|
||||
|
||||
class SideShiftExchangeProvider extends ExchangeProvider {
|
||||
|
@ -152,7 +153,7 @@ class SideShiftExchangeProvider extends ExchangeProvider {
|
|||
|
||||
return double.parse(responseJSON['rate'] as String);
|
||||
} catch (e) {
|
||||
log('Error fetching rate in SideShift Provider: ${e.toString()}');
|
||||
printV(e.toString());
|
||||
return 0.00;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -138,6 +138,9 @@ class TrocadorExchangeProvider extends ExchangeProvider {
|
|||
final response = await get(uri, headers: {'API-Key': apiKey});
|
||||
|
||||
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
||||
|
||||
if (responseJSON['error'] != null) throw Exception(responseJSON['error']);
|
||||
|
||||
final fromAmount = double.parse(responseJSON['amount_from'].toString());
|
||||
final toAmount = double.parse(responseJSON['amount_to'].toString());
|
||||
final rateId = responseJSON['trade_id'] as String? ?? '';
|
||||
|
|
|
@ -478,6 +478,14 @@ class ExchangePage extends BasePage {
|
|||
}
|
||||
});
|
||||
|
||||
reaction((_) => exchangeViewModel.bestRate, (double rate) {
|
||||
if (exchangeViewModel.isFixedRateMode) {
|
||||
exchangeViewModel.changeReceiveAmount(amount: receiveAmountController.text);
|
||||
} else {
|
||||
exchangeViewModel.changeDepositAmount(amount: depositAmountController.text);
|
||||
}
|
||||
});
|
||||
|
||||
depositAddressController
|
||||
.addListener(() => exchangeViewModel.depositAddress = depositAddressController.text);
|
||||
|
||||
|
@ -493,6 +501,7 @@ class ExchangePage extends BasePage {
|
|||
: Debounce(Duration(milliseconds: 500));
|
||||
|
||||
_depositAmountDebounce.run(() {
|
||||
exchangeViewModel.calculateBestRate();
|
||||
exchangeViewModel.changeDepositAmount(amount: depositAmountController.text);
|
||||
exchangeViewModel.isReceiveAmountEntered = false;
|
||||
});
|
||||
|
@ -505,6 +514,7 @@ class ExchangePage extends BasePage {
|
|||
receiveAmountController.addListener(() {
|
||||
if (receiveAmountController.text != exchangeViewModel.receiveAmount) {
|
||||
_receiveAmountDebounce.run(() {
|
||||
exchangeViewModel.calculateBestRate();
|
||||
exchangeViewModel.changeReceiveAmount(amount: receiveAmountController.text);
|
||||
exchangeViewModel.isReceiveAmountEntered = true;
|
||||
});
|
||||
|
|
|
@ -115,9 +115,9 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
|
|||
.toList());
|
||||
|
||||
_setAvailableProviders();
|
||||
_calculateBestRate();
|
||||
calculateBestRate();
|
||||
|
||||
bestRateSync = Timer.periodic(Duration(seconds: 10), (timer) => _calculateBestRate());
|
||||
bestRateSync = Timer.periodic(Duration(seconds: 10), (timer) => calculateBestRate());
|
||||
|
||||
isDepositAddressEnabled = !(depositCurrency == wallet.currency);
|
||||
depositAmount = '';
|
||||
|
@ -144,8 +144,8 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
|
|||
loadLimits();
|
||||
reaction((_) => isFixedRateMode, (Object _) {
|
||||
loadLimits();
|
||||
_bestRate = 0;
|
||||
_calculateBestRate();
|
||||
bestRate = 0;
|
||||
calculateBestRate();
|
||||
});
|
||||
|
||||
if (isElectrumWallet) {
|
||||
|
@ -334,7 +334,8 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
|
|||
|
||||
final ContactListViewModel contactListViewModel;
|
||||
|
||||
double _bestRate = 0.0;
|
||||
@observable
|
||||
double bestRate = 0.0;
|
||||
|
||||
late Timer bestRateSync;
|
||||
|
||||
|
@ -366,15 +367,15 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
|
|||
|
||||
final _enteredAmount = double.tryParse(amount.replaceAll(',', '.')) ?? 0;
|
||||
|
||||
if (_bestRate == 0) {
|
||||
if (bestRate == 0) {
|
||||
depositAmount = S.current.fetching;
|
||||
|
||||
await _calculateBestRate();
|
||||
await calculateBestRate();
|
||||
}
|
||||
_cryptoNumberFormat.maximumFractionDigits = depositMaxDigits;
|
||||
|
||||
depositAmount = _cryptoNumberFormat
|
||||
.format(_enteredAmount / _bestRate)
|
||||
.format(_enteredAmount / bestRate)
|
||||
.toString()
|
||||
.replaceAll(RegExp('\\,'), '');
|
||||
}
|
||||
|
@ -392,15 +393,15 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
|
|||
final _enteredAmount = double.tryParse(amount.replaceAll(',', '.')) ?? 0;
|
||||
|
||||
/// in case the best rate was not calculated yet
|
||||
if (_bestRate == 0) {
|
||||
if (bestRate == 0) {
|
||||
receiveAmount = S.current.fetching;
|
||||
|
||||
await _calculateBestRate();
|
||||
await calculateBestRate();
|
||||
}
|
||||
_cryptoNumberFormat.maximumFractionDigits = receiveMaxDigits;
|
||||
|
||||
receiveAmount = _cryptoNumberFormat
|
||||
.format(_bestRate * _enteredAmount)
|
||||
.format(bestRate * _enteredAmount)
|
||||
.toString()
|
||||
.replaceAll(RegExp('\\,'), '');
|
||||
}
|
||||
|
@ -416,8 +417,7 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<void> _calculateBestRate() async {
|
||||
Future<void> calculateBestRate() async {
|
||||
final amount = double.tryParse(isFixedRateMode ? receiveAmount : depositAmount) ?? 1;
|
||||
|
||||
final _providers = _tradeAvailableProviders
|
||||
|
@ -453,7 +453,7 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
|
|||
}
|
||||
}
|
||||
}
|
||||
if (_sortedAvailableProviders.isNotEmpty) _bestRate = _sortedAvailableProviders.keys.first;
|
||||
if (_sortedAvailableProviders.isNotEmpty) bestRate = _sortedAvailableProviders.keys.first;
|
||||
}
|
||||
|
||||
@action
|
||||
|
@ -533,7 +533,7 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
|
|||
|
||||
if (!(await provider.checkIsAvailable())) continue;
|
||||
|
||||
_bestRate = providerRate;
|
||||
bestRate = providerRate;
|
||||
await changeDepositAmount(amount: depositAmount);
|
||||
|
||||
final request = TradeRequest(
|
||||
|
@ -693,8 +693,8 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
|
|||
receiveAmount = '';
|
||||
loadLimits();
|
||||
_setAvailableProviders();
|
||||
_bestRate = 0;
|
||||
_calculateBestRate();
|
||||
bestRate = 0;
|
||||
calculateBestRate();
|
||||
}
|
||||
|
||||
void _initialPairBasedOnWallet() {
|
||||
|
@ -785,8 +785,8 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
|
|||
isFixedRateMode = false;
|
||||
_defineIsReceiveAmountEditable();
|
||||
loadLimits();
|
||||
_bestRate = 0;
|
||||
_calculateBestRate();
|
||||
bestRate = 0;
|
||||
calculateBestRate();
|
||||
|
||||
final Map<String, dynamic> exchangeProvidersSelection =
|
||||
json.decode(sharedPreferences.getString(PreferencesKey.exchangeProvidersSelection) ?? "{}")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue