CAKE-185 | removed isValidAmount() from exchange_view_model.dart and exchange_page.dart; throw exception in the createTrade() (xmrto_exchange_provider.dart) if number of fractional digits of amount more than 8
OleksandrSobol committed
Dec 3, 2020 at 23:22 UTC
df01fa0314b8dab44893fcda1f43cca918ed58de
3 files changed
+20
-31
lib/exchange/xmrto/xmrto_exchange_provider.dart
+19
-5
@@ -12,6 +12,7 @@ import 'package:cake_wallet/exchange/xmrto/xmrto_trade_request.dart';
12
import 'package:cake_wallet/exchange/trade_not_created_exeption.dart';
13
import 'package:cake_wallet/exchange/exchange_provider_description.dart';
14
import 'package:cake_wallet/exchange/trade_not_found_exeption.dart';
15
+import 'package:cake_wallet/generated/i18n.dart';
16
17
class XMRTOExchangeProvider extends ExchangeProvider {
18
XMRTOExchangeProvider()
@@ -90,12 +91,25 @@ class XMRTOExchangeProvider extends ExchangeProvider {
91
Future<Trade> createTrade({TradeRequest request}) async {
92
final _request = request as XMRTOTradeRequest;
93
final url = originalApiUri + _orderCreateUriSuffix;
94
+ final _amount = _request.isBTCRequest
95
+ ? _request.receiveAmount
96
+ : _request.amount;
97
+
98
+ final _amountCurrency = _request.isBTCRequest
99
+ ? _request.to.toString()
100
+ : _request.from.toString();
101
+
102
+ final pattern = '^([0-9]+([.\,][0-9]{0,8})?|[.\,][0-9]{1,8})\$';
103
+ final isValid = RegExp(pattern).hasMatch(_amount);
104
+
105
+ if (!isValid) {
106
+ throw TradeNotCreatedException(description,
107
+ description: S.current.xmr_to_error_description);
108
+ }
109
+
110
final body = {
94
- 'amount':
95
- _request.isBTCRequest ? _request.receiveAmount : _request.amount,
96
- 'amount_currency': _request.isBTCRequest
97
- ? _request.to.toString()
98
- : _request.from.toString(),
111
+ 'amount': _amount,
112
+ 'amount_currency': _amountCurrency,
113
'btc_dest_address': _request.address
114
};
115
final response = await post(url,
lib/src/screens/exchange/exchange_page.dart
+1
-12
@@ -411,18 +411,7 @@ class ExchangePage extends BasePage {
411
buttonAction: () => Navigator.of(context).pop());
412
});
413
} else {
414
- exchangeViewModel.isValidAmount()
415
- ? exchangeViewModel.createTrade()
416
- : showPopUp<void>(
417
- context: context,
418
- builder: (BuildContext context) {
419
- return AlertWithOneAction(
420
- alertTitle: S.of(context).xmr_to_error,
421
- alertContent: S.of(context).xmr_to_error_description,
422
- buttonText: S.of(context).ok,
423
- buttonAction: () =>
424
- Navigator.of(context).pop());
425
- });
414
+ exchangeViewModel.createTrade();
415
}
416
}
417
},
lib/view_model/exchange/exchange_view_model.dart
-14
@@ -311,20 +311,6 @@ abstract class ExchangeViewModelBase with Store {
311
void removeTemplate({ExchangeTemplate template}) =>
312
_exchangeTemplateStore.remove(template: template);
313
314
- bool isValidAmount() {
315
- bool isValid = true;
316
-
317
- if (provider is XMRTOExchangeProvider) {
318
- final amount = isReceiveAmountEntered
319
- ? receiveAmount
320
- : depositAmount;
321
- final pattern = '^([0-9]+([.\,][0-9]{0,8})?|[.\,][0-9]{1,8})\$';
322
- isValid = RegExp(pattern).hasMatch(amount);
323
- }
324
-
325
- return isValid;
326
- }
327
-
314
List<ExchangeProvider> providersForCurrentPair() {
315
return _providersForPair(from: depositCurrency, to: receiveCurrency);
316
}