From ef750cb454604699b9fd7b0f9ebd16aa670e6fda Mon Sep 17 00:00:00 2001 From: Oleksandr Sobol Date: Mon, 13 Apr 2020 19:05:27 +0300 Subject: [PATCH 1/3] CWA-199 | changed possibility to enter amount in the deposit card only (exchange page); changed request for XMRTOExchangeProvider in the createTrade() (exchange_store); fixed calculation limits for XMR in the fetchLimits(), fixed post body in the createTrade() (xmrto_exchange_provider); created limits_format --- lib/src/domain/exchange/limits_format.dart | 7 +++ .../xmrto/xmrto_exchange_provider.dart | 27 +++++++++--- lib/src/screens/exchange/exchange_page.dart | 43 +++++-------------- lib/src/stores/exchange/exchange_store.dart | 8 ++-- 4 files changed, 43 insertions(+), 42 deletions(-) create mode 100644 lib/src/domain/exchange/limits_format.dart diff --git a/lib/src/domain/exchange/limits_format.dart b/lib/src/domain/exchange/limits_format.dart new file mode 100644 index 000000000..c25f03e9f --- /dev/null +++ b/lib/src/domain/exchange/limits_format.dart @@ -0,0 +1,7 @@ +import 'package:intl/intl.dart'; + +final amountFormat = NumberFormat() + ..maximumFractionDigits = 3 + ..minimumFractionDigits = 1; + +double limitsFormat(double limit) => double.parse(amountFormat.format(limit)); \ No newline at end of file diff --git a/lib/src/domain/exchange/xmrto/xmrto_exchange_provider.dart b/lib/src/domain/exchange/xmrto/xmrto_exchange_provider.dart index df9cf828f..47f5716d8 100644 --- a/lib/src/domain/exchange/xmrto/xmrto_exchange_provider.dart +++ b/lib/src/domain/exchange/xmrto/xmrto_exchange_provider.dart @@ -12,6 +12,7 @@ import 'package:cake_wallet/src/domain/exchange/xmrto/xmrto_trade_request.dart'; import 'package:cake_wallet/src/domain/exchange/trade_not_created_exeption.dart'; import 'package:cake_wallet/src/domain/exchange/exchange_provider_description.dart'; import 'package:cake_wallet/src/domain/exchange/trade_not_found_exeption.dart'; +import 'package:cake_wallet/src/domain/exchange/limits_format.dart'; class XMRTOExchangeProvider extends ExchangeProvider { XMRTOExchangeProvider() @@ -54,14 +55,31 @@ class XMRTOExchangeProvider extends ExchangeProvider { Future fetchLimits({CryptoCurrency from, CryptoCurrency to}) async { final url = await getApiUri() + _orderParameterUriSufix; final response = await get(url); + final correction = 0.001; if (response.statusCode != 200) { return Limits(min: 0, max: 0); } final responseJSON = json.decode(response.body) as Map; - final min = responseJSON['lower_limit'] as double; - final max = responseJSON['upper_limit'] as double; + double min = responseJSON['lower_limit'] as double; + double max = responseJSON['upper_limit'] as double; + final price = responseJSON['price'] as double; + + if (price > 0) { + try { + min /= price; + min = limitsFormat(min) + correction; + max /= price; + max = limitsFormat(max); + } catch (e) { + min = 0; + max = 0; + } + } else { + min = 0; + max = 0; + } return Limits(min: min, max: max); } @@ -71,7 +89,7 @@ class XMRTOExchangeProvider extends ExchangeProvider { final _request = request as XMRTOTradeRequest; final url = await getApiUri() + _orderCreateUriSufix; final body = { - 'btc_amount': _request.amount, + 'xmr_amount': _request.amount, 'btc_dest_address': _request.address }; final response = await post(url, @@ -168,8 +186,7 @@ class XMRTOExchangeProvider extends ExchangeProvider { final response = await get(url, headers: {'Content-Type': 'application/json'}); final responseJSON = json.decode(response.body) as Map; - final btcprice = responseJSON['price'] as double; - final price = 1 / btcprice; + final price = responseJSON['price'] as double; return price; } catch (e) { diff --git a/lib/src/screens/exchange/exchange_page.dart b/lib/src/screens/exchange/exchange_page.dart index ce533c3cd..5cf90b2f8 100644 --- a/lib/src/screens/exchange/exchange_page.dart +++ b/lib/src/screens/exchange/exchange_page.dart @@ -168,12 +168,9 @@ class ExchangeFormState extends State { exchangeStore.depositCurrency == walletStore.type ? walletStore.address : null, - initialIsAmountEditable: - !(exchangeStore.provider is XMRTOExchangeProvider), - initialIsAddressEditable: - !(exchangeStore.provider is XMRTOExchangeProvider), - isAmountEstimated: - exchangeStore.provider is XMRTOExchangeProvider, + initialIsAmountEditable: true, + initialIsAddressEditable: true, + isAmountEstimated: false, currencies: CryptoCurrency.all, onCurrencySelected: (currency) => exchangeStore.changeDepositCurrency(currency: currency), @@ -209,12 +206,9 @@ class ExchangeFormState extends State { exchangeStore.receiveCurrency == walletStore.type ? walletStore.address : null, - initialIsAmountEditable: - exchangeStore.provider is XMRTOExchangeProvider, - initialIsAddressEditable: - exchangeStore.provider is XMRTOExchangeProvider, - isAmountEstimated: !(exchangeStore.provider - is XMRTOExchangeProvider), + initialIsAmountEditable: false, + initialIsAddressEditable: true, + isAmountEstimated: true, currencies: CryptoCurrency.all, onCurrencySelected: (currency) => exchangeStore .changeReceiveCurrency(currency: currency), @@ -321,8 +315,7 @@ class ExchangeFormState extends State { final max = limitsState.limits.max != null ? limitsState.limits.max.toString() : null; - final key = - store.provider is XMRTOExchangeProvider ? receiveKey : depositKey; + final key = depositKey; key.currentState.changeLimits(min: min, max: max); } @@ -363,22 +356,12 @@ class ExchangeFormState extends State { }); reaction((_) => store.provider, (ExchangeProvider provider) { - final isReversed = provider is XMRTOExchangeProvider; - - if (isReversed) { - receiveKey.currentState.isAddressEditable(isEditable: true); - receiveKey.currentState.isAmountEditable(isEditable: true); - depositKey.currentState.isAddressEditable(isEditable: false); - depositKey.currentState.isAmountEditable(isEditable: false); - } else { receiveKey.currentState.isAddressEditable(isEditable: true); receiveKey.currentState.isAmountEditable(isEditable: false); depositKey.currentState.isAddressEditable(isEditable: true); depositKey.currentState.isAmountEditable(isEditable: true); - } - depositKey.currentState.changeIsAmountEstimated(isReversed); - receiveKey.currentState.changeIsAmountEstimated(!isReversed); + receiveKey.currentState.changeIsAmountEstimated(true); }); reaction((_) => store.tradeState, (ExchangeTradeState state) { @@ -406,7 +389,6 @@ class ExchangeFormState extends State { }); reaction((_) => store.limitsState, (LimitsState state) { - final isXMRTO = store.provider is XMRTOExchangeProvider; String min; String max; @@ -425,13 +407,8 @@ class ExchangeFormState extends State { max = '...'; } - final depositMin = isXMRTO ? null : min; - final depositMax = isXMRTO ? null : max; - final receiveMin = isXMRTO ? min : null; - final receiveMax = isXMRTO ? max : null; - - depositKey.currentState.changeLimits(min: depositMin, max: depositMax); - receiveKey.currentState.changeLimits(min: receiveMin, max: receiveMax); + depositKey.currentState.changeLimits(min: min, max: max); + receiveKey.currentState.changeLimits(min: null, max: null); }); depositAddressController.addListener( diff --git a/lib/src/stores/exchange/exchange_store.dart b/lib/src/stores/exchange/exchange_store.dart index 189bbea8a..df8f9b618 100644 --- a/lib/src/stores/exchange/exchange_store.dart +++ b/lib/src/stores/exchange/exchange_store.dart @@ -157,13 +157,13 @@ abstract class ExchangeStoreBase with Store { if (provider is XMRTOExchangeProvider) { request = XMRTOTradeRequest( - to: receiveCurrency, from: depositCurrency, - amount: receiveAmount, + to: receiveCurrency, + amount: depositAmount, address: receiveAddress, refundAddress: depositAddress); - amount = receiveAmount; - currency = receiveCurrency; + amount = depositAmount; + currency = depositCurrency; } if (provider is ChangeNowExchangeProvider) { From 99c732aab0d2a5ec5fe28dabeba1293a72c23bc8 Mon Sep 17 00:00:00 2001 From: Oleksandr Sobol Date: Mon, 13 Apr 2020 20:07:26 +0300 Subject: [PATCH 2/3] CWA-199 | fixed receive amount for empty deposit amount; added correction to xmr limits --- lib/src/domain/exchange/xmrto/xmrto_exchange_provider.dart | 6 +++--- lib/src/stores/exchange/exchange_store.dart | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/src/domain/exchange/xmrto/xmrto_exchange_provider.dart b/lib/src/domain/exchange/xmrto/xmrto_exchange_provider.dart index 47f5716d8..ee58771d1 100644 --- a/lib/src/domain/exchange/xmrto/xmrto_exchange_provider.dart +++ b/lib/src/domain/exchange/xmrto/xmrto_exchange_provider.dart @@ -68,9 +68,9 @@ class XMRTOExchangeProvider extends ExchangeProvider { if (price > 0) { try { - min /= price; - min = limitsFormat(min) + correction; - max /= price; + min = min/price + correction; + min = limitsFormat(min); + max = max/price - correction; max = limitsFormat(max); } catch (e) { min = 0; diff --git a/lib/src/stores/exchange/exchange_store.dart b/lib/src/stores/exchange/exchange_store.dart index df8f9b618..a5f41444b 100644 --- a/lib/src/stores/exchange/exchange_store.dart +++ b/lib/src/stores/exchange/exchange_store.dart @@ -107,6 +107,7 @@ abstract class ExchangeStoreBase with Store { if (amount == null || amount.isEmpty) { depositAmount = ''; + receiveAmount = ''; return; } @@ -125,6 +126,7 @@ abstract class ExchangeStoreBase with Store { if (amount == null || amount.isEmpty) { depositAmount = ''; + receiveAmount = ''; return; } From 877522d12e3a0027d40f4066e1e6a83494b17b44 Mon Sep 17 00:00:00 2001 From: Oleksandr Sobol Date: Mon, 13 Apr 2020 20:31:00 +0300 Subject: [PATCH 3/3] CWA-199 | moved limitsFormat() to xmrto_exchange_provider --- lib/src/domain/exchange/limits_format.dart | 7 ------- lib/src/domain/exchange/xmrto/xmrto_exchange_provider.dart | 7 ++++--- 2 files changed, 4 insertions(+), 10 deletions(-) delete mode 100644 lib/src/domain/exchange/limits_format.dart diff --git a/lib/src/domain/exchange/limits_format.dart b/lib/src/domain/exchange/limits_format.dart deleted file mode 100644 index c25f03e9f..000000000 --- a/lib/src/domain/exchange/limits_format.dart +++ /dev/null @@ -1,7 +0,0 @@ -import 'package:intl/intl.dart'; - -final amountFormat = NumberFormat() - ..maximumFractionDigits = 3 - ..minimumFractionDigits = 1; - -double limitsFormat(double limit) => double.parse(amountFormat.format(limit)); \ No newline at end of file diff --git a/lib/src/domain/exchange/xmrto/xmrto_exchange_provider.dart b/lib/src/domain/exchange/xmrto/xmrto_exchange_provider.dart index ee58771d1..d65f23db3 100644 --- a/lib/src/domain/exchange/xmrto/xmrto_exchange_provider.dart +++ b/lib/src/domain/exchange/xmrto/xmrto_exchange_provider.dart @@ -12,7 +12,6 @@ import 'package:cake_wallet/src/domain/exchange/xmrto/xmrto_trade_request.dart'; import 'package:cake_wallet/src/domain/exchange/trade_not_created_exeption.dart'; import 'package:cake_wallet/src/domain/exchange/exchange_provider_description.dart'; import 'package:cake_wallet/src/domain/exchange/trade_not_found_exeption.dart'; -import 'package:cake_wallet/src/domain/exchange/limits_format.dart'; class XMRTOExchangeProvider extends ExchangeProvider { XMRTOExchangeProvider() @@ -69,9 +68,9 @@ class XMRTOExchangeProvider extends ExchangeProvider { if (price > 0) { try { min = min/price + correction; - min = limitsFormat(min); + min = _limitsFormat(min); max = max/price - correction; - max = limitsFormat(max); + max = _limitsFormat(max); } catch (e) { min = 0; max = 0; @@ -194,4 +193,6 @@ class XMRTOExchangeProvider extends ExchangeProvider { return 0.0; } } + + double _limitsFormat(double limit) => double.parse(limit.toStringAsFixed(3)); }