Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cw 897 stable coins exchanges rate issue #1939

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/exchange/provider/letsexchange_exchange_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/exchange/provider/sideshift_exchange_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/exchange/provider/trocador_exchange_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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? ?? '';
Expand Down
10 changes: 10 additions & 0 deletions lib/src/screens/exchange/exchange_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -493,6 +501,7 @@ class ExchangePage extends BasePage {
: Debounce(Duration(milliseconds: 500));

_depositAmountDebounce.run(() {
exchangeViewModel.calculateBestRate();
exchangeViewModel.changeDepositAmount(amount: depositAmountController.text);
exchangeViewModel.isReceiveAmountEntered = false;
});
Expand All @@ -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;
});
Expand Down
38 changes: 19 additions & 19 deletions lib/view_model/exchange/exchange_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand All @@ -144,8 +144,8 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
loadLimits();
reaction((_) => isFixedRateMode, (Object _) {
loadLimits();
_bestRate = 0;
_calculateBestRate();
bestRate = 0;
calculateBestRate();
});

if (isElectrumWallet) {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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('\\,'), '');
}
Expand All @@ -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('\\,'), '');
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -693,8 +693,8 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
receiveAmount = '';
loadLimits();
_setAvailableProviders();
_bestRate = 0;
_calculateBestRate();
bestRate = 0;
calculateBestRate();
}

void _initialPairBasedOnWallet() {
Expand Down Expand Up @@ -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) ?? "{}")
Expand Down
Loading