From bc85058ad8468dd6c009cbe7e82ca9ec989fb3fd Mon Sep 17 00:00:00 2001 From: OleksandrSobol Date: Fri, 25 Sep 2020 22:55:41 +0300 Subject: [PATCH 1/3] CAKE-43 | added the ability to enter an amount for xmr.to exchange in both currencies xmr and btc; added isReceiveAmountEntered to ExchangeViewModel; added receiveAmount and isBTCRequest to XMRTOTradeRequest; added isReceiveAmount to calculateAmount method of ExchangeProvider; fixed XMRTOExchangeProvider --- .../changenow_exchange_provider.dart | 3 ++- lib/exchange/exchange_provider.dart | 2 +- .../morphtoken_exchange_provider.dart | 3 ++- .../xmrto/xmrto_exchange_provider.dart | 26 ++++++++++++------- lib/exchange/xmrto/xmrto_trade_request.dart | 6 ++++- lib/src/screens/dashboard/dashboard_page.dart | 6 ++--- lib/src/screens/exchange/exchange_page.dart | 11 +++++++- .../exchange/exchange_view_model.dart | 13 +++++++--- 8 files changed, 50 insertions(+), 20 deletions(-) diff --git a/lib/exchange/changenow/changenow_exchange_provider.dart b/lib/exchange/changenow/changenow_exchange_provider.dart index 94f8ea6ac..2a9b38c64 100644 --- a/lib/exchange/changenow/changenow_exchange_provider.dart +++ b/lib/exchange/changenow/changenow_exchange_provider.dart @@ -146,7 +146,8 @@ class ChangeNowExchangeProvider extends ExchangeProvider { @override Future calculateAmount( - {CryptoCurrency from, CryptoCurrency to, double amount}) async { + {CryptoCurrency from, CryptoCurrency to, double amount, + bool isReceiveAmount}) async { final url = apiUri + _exchangeAmountUriSufix + amount.toString() + diff --git a/lib/exchange/exchange_provider.dart b/lib/exchange/exchange_provider.dart index 418d20b8c..65f7ad8ba 100644 --- a/lib/exchange/exchange_provider.dart +++ b/lib/exchange/exchange_provider.dart @@ -20,5 +20,5 @@ abstract class ExchangeProvider { Future createTrade({TradeRequest request}); Future findTradeById({@required String id}); Future calculateAmount( - {CryptoCurrency from, CryptoCurrency to, double amount}); + {CryptoCurrency from, CryptoCurrency to, double amount, bool isReceiveAmount}); } diff --git a/lib/exchange/morphtoken/morphtoken_exchange_provider.dart b/lib/exchange/morphtoken/morphtoken_exchange_provider.dart index ec632cccb..3d9ba9587 100644 --- a/lib/exchange/morphtoken/morphtoken_exchange_provider.dart +++ b/lib/exchange/morphtoken/morphtoken_exchange_provider.dart @@ -186,7 +186,8 @@ class MorphTokenExchangeProvider extends ExchangeProvider { @override Future calculateAmount( - {CryptoCurrency from, CryptoCurrency to, double amount}) async { + {CryptoCurrency from, CryptoCurrency to, double amount, + bool isReceiveAmount}) async { final url = apiUri + _ratesURISuffix; final response = await get(url); final responseJSON = json.decode(response.body) as Map; diff --git a/lib/exchange/xmrto/xmrto_exchange_provider.dart b/lib/exchange/xmrto/xmrto_exchange_provider.dart index ca1253310..25232db65 100644 --- a/lib/exchange/xmrto/xmrto_exchange_provider.dart +++ b/lib/exchange/xmrto/xmrto_exchange_provider.dart @@ -21,8 +21,8 @@ class XMRTOExchangeProvider extends ExchangeProvider { ]); static const userAgent = 'CakeWallet/XMR iOS'; - static const originalApiUri = 'https://xmr.to/api/v2/xmr2btc'; - static const proxyApiUri = 'https://xmrproxy.net/api/v2/xmr2btc'; + static const originalApiUri = 'https://xmr.to/api/v3/xmr2btc'; + static const proxyApiUri = 'https://xmrproxy.net/api/v3/xmr2btc'; static const _orderParameterUriSufix = '/order_parameter_query'; static const _orderStatusUriSufix = '/order_status_query/'; static const _orderCreateUriSufix = '/order_create/'; @@ -61,9 +61,9 @@ class XMRTOExchangeProvider extends ExchangeProvider { } final responseJSON = json.decode(response.body) as Map; - double min = responseJSON['lower_limit'] as double; - double max = responseJSON['upper_limit'] as double; - final price = responseJSON['price'] as double; + double min = double.parse(responseJSON['lower_limit'] as String); + double max = double.parse(responseJSON['upper_limit'] as String); + final price = double.parse(responseJSON['price'] as String); if (price > 0) { try { @@ -88,7 +88,12 @@ class XMRTOExchangeProvider extends ExchangeProvider { final _request = request as XMRTOTradeRequest; final url = await getApiUri() + _orderCreateUriSufix; final body = { - 'xmr_amount': _request.amount, + 'amount': _request.isBTCRequest + ? _request.receiveAmount + : _request.amount, + 'amount_currency': _request.isBTCRequest + ? _request.to.toString() + : _request.from.toString(), 'btc_dest_address': _request.address }; final response = await post(url, @@ -165,7 +170,8 @@ class XMRTOExchangeProvider extends ExchangeProvider { @override Future calculateAmount( - {CryptoCurrency from, CryptoCurrency to, double amount}) async { + {CryptoCurrency from, CryptoCurrency to, double amount, + bool isReceiveAmount}) async { if (from != CryptoCurrency.xmr && to != CryptoCurrency.btc) { return 0; } @@ -174,7 +180,9 @@ class XMRTOExchangeProvider extends ExchangeProvider { _rate = await _fetchRates(); } - final double result = _rate * amount; + final double result = isReceiveAmount + ? _rate == 0 ? 0 : amount / _rate + : _rate * amount; return double.parse(result.toStringAsFixed(12)); } @@ -185,7 +193,7 @@ class XMRTOExchangeProvider extends ExchangeProvider { final response = await get(url, headers: {'Content-Type': 'application/json'}); final responseJSON = json.decode(response.body) as Map; - final price = responseJSON['price'] as double; + final price = double.parse(responseJSON['price'] as String); return price; } catch (e) { diff --git a/lib/exchange/xmrto/xmrto_trade_request.dart b/lib/exchange/xmrto/xmrto_trade_request.dart index a456b572a..e88db176d 100644 --- a/lib/exchange/xmrto/xmrto_trade_request.dart +++ b/lib/exchange/xmrto/xmrto_trade_request.dart @@ -7,12 +7,16 @@ class XMRTOTradeRequest extends TradeRequest { {@required this.from, @required this.to, @required this.amount, + @required this.receiveAmount, @required this.address, - @required this.refundAddress}); + @required this.refundAddress, + @required this.isBTCRequest}); final CryptoCurrency from; final CryptoCurrency to; final String amount; + final String receiveAmount; final String address; final String refundAddress; + final bool isBTCRequest; } diff --git a/lib/src/screens/dashboard/dashboard_page.dart b/lib/src/screens/dashboard/dashboard_page.dart index 16cce7518..6458b5f2a 100644 --- a/lib/src/screens/dashboard/dashboard_page.dart +++ b/lib/src/screens/dashboard/dashboard_page.dart @@ -114,9 +114,9 @@ class DashboardPage extends BasePage { alignment: Alignment.centerLeft, ), ActionButton( - image: sendImage, - title: S.of(context).send, - route: Routes.send, + image: exchangeImage, + title: S.of(context).exchange, + route: Routes.exchange, alignment: Alignment.centerLeft, ), ], diff --git a/lib/src/screens/exchange/exchange_page.dart b/lib/src/screens/exchange/exchange_page.dart index 1cabcc448..23210deb8 100644 --- a/lib/src/screens/exchange/exchange_page.dart +++ b/lib/src/screens/exchange/exchange_page.dart @@ -1,4 +1,5 @@ import 'dart:ui'; +import 'package:cake_wallet/exchange/exchange_provider.dart'; import 'package:cake_wallet/exchange/exchange_template.dart'; import 'package:cake_wallet/src/screens/base_page.dart'; import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart'; @@ -181,7 +182,7 @@ class ExchangePage extends BasePage { exchangeViewModel.wallet.currency ? exchangeViewModel.wallet.address : exchangeViewModel.receiveAddress, - initialIsAmountEditable: false, + initialIsAmountEditable: exchangeViewModel.provider is XMRTOExchangeProvider ? true : false, initialIsAddressEditable: exchangeViewModel.isReceiveAddressEnabled, isAmountEstimated: true, @@ -487,6 +488,12 @@ class ExchangePage extends BasePage { receiveKey.currentState.isAddressEditable(isEditable: isEnabled); }); + reaction((_) => exchangeViewModel.provider, (ExchangeProvider provider) { + provider is XMRTOExchangeProvider + ? receiveKey.currentState.isAmountEditable(isEditable: true) + : receiveKey.currentState.isAmountEditable(isEditable: false); + }); + // FIXME: FIXME // reaction((_) => exchangeViewModel.tradeState, (ExchangeTradeState state) { @@ -540,6 +547,7 @@ class ExchangePage extends BasePage { if (depositAmountController.text != exchangeViewModel.depositAmount) { exchangeViewModel.changeDepositAmount( amount: depositAmountController.text); + exchangeViewModel.isReceiveAmountEntered = false; } }); @@ -550,6 +558,7 @@ class ExchangePage extends BasePage { if (receiveAmountController.text != exchangeViewModel.receiveAmount) { exchangeViewModel.changeReceiveAmount( amount: receiveAmountController.text); + exchangeViewModel.isReceiveAmountEntered = true; } }); diff --git a/lib/view_model/exchange/exchange_view_model.dart b/lib/view_model/exchange/exchange_view_model.dart index d1a29c731..caca84b83 100644 --- a/lib/view_model/exchange/exchange_view_model.dart +++ b/lib/view_model/exchange/exchange_view_model.dart @@ -48,6 +48,7 @@ abstract class ExchangeViewModelBase with Store { tradeState = ExchangeTradeStateInitial(); _cryptoNumberFormat = NumberFormat()..maximumFractionDigits = 12; provider = providersForCurrentPair().first; + isReceiveAmountEntered = false; loadLimits(); } @@ -92,6 +93,8 @@ abstract class ExchangeViewModelBase with Store { @observable bool isReceiveAddressEnabled; + bool isReceiveAmountEntered; + Limits limits; NumberFormat _cryptoNumberFormat; @@ -138,7 +141,8 @@ abstract class ExchangeViewModelBase with Store { provider .calculateAmount( - from: depositCurrency, to: receiveCurrency, amount: _amount) + from: depositCurrency, to: receiveCurrency, amount: _amount, + isReceiveAmount: true) .then((amount) => _cryptoNumberFormat .format(amount) .toString() @@ -159,7 +163,8 @@ abstract class ExchangeViewModelBase with Store { final _amount = double.parse(amount); provider .calculateAmount( - from: depositCurrency, to: receiveCurrency, amount: _amount) + from: depositCurrency, to: receiveCurrency, amount: _amount, + isReceiveAmount: false) .then((amount) => _cryptoNumberFormat .format(amount) .toString() @@ -191,8 +196,10 @@ abstract class ExchangeViewModelBase with Store { from: depositCurrency, to: receiveCurrency, amount: depositAmount, + receiveAmount: receiveAmount, address: receiveAddress, - refundAddress: depositAddress); + refundAddress: depositAddress, + isBTCRequest: isReceiveAmountEntered); amount = depositAmount; currency = depositCurrency; } From e81c75be3525eee84c1521175acd9dff90bdab9a Mon Sep 17 00:00:00 2001 From: OleksandrSobol Date: Mon, 28 Sep 2020 21:39:24 +0300 Subject: [PATCH 2/3] CAKE-43 | merged new world branch into current --- lib/utils/show_pop_up.dart | 4 ++-- pubspec.lock | 31 ++++++++++++------------------- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/lib/utils/show_pop_up.dart b/lib/utils/show_pop_up.dart index a97fb3762..280bcff3c 100644 --- a/lib/utils/show_pop_up.dart +++ b/lib/utils/show_pop_up.dart @@ -14,8 +14,8 @@ Future showPopUp({ context: context, builder: builder, barrierDismissible: barrierDismissible, - barrierColor: barrierColor, - useSafeArea: useSafeArea, + //barrierColor: barrierColor, + //useSafeArea: useSafeArea, useRootNavigator: useRootNavigator, routeSettings: routeSettings, child: child); diff --git a/pubspec.lock b/pubspec.lock index f9bbbbd26..7d3081850 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -42,7 +42,7 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.4.2" + version: "2.4.1" auto_size_text: dependency: "direct main" description: @@ -210,7 +210,7 @@ packages: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.14.13" + version: "1.14.12" connectivity: dependency: "direct main" description: @@ -252,7 +252,7 @@ packages: name: crypto url: "https://pub.dartlang.org" source: hosted - version: "2.1.5" + version: "2.1.4" csslib: dependency: transitive description: @@ -330,13 +330,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.2" - fake_async: - dependency: transitive - description: - name: fake_async - url: "https://pub.dartlang.org" - source: hosted - version: "1.1.0" ffi: dependency: transitive description: @@ -505,7 +498,7 @@ packages: name: image url: "https://pub.dartlang.org" source: hosted - version: "2.1.18" + version: "2.1.12" intl: dependency: "direct main" description: @@ -554,7 +547,7 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.8" + version: "0.12.6" meta: dependency: transitive description: @@ -624,7 +617,7 @@ packages: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.6.4" path_drawing: dependency: transitive description: @@ -687,7 +680,7 @@ packages: name: petitparser url: "https://pub.dartlang.org" source: hosted - version: "3.0.4" + version: "2.4.0" platform: dependency: transitive description: @@ -867,7 +860,7 @@ packages: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.5" + version: "1.9.3" stream_channel: dependency: transitive description: @@ -902,7 +895,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.17" + version: "0.2.15" time: dependency: transitive description: @@ -923,7 +916,7 @@ packages: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.1.6" url_launcher: dependency: "direct main" description: @@ -1014,7 +1007,7 @@ packages: name: xml url: "https://pub.dartlang.org" source: hosted - version: "4.5.1" + version: "3.6.1" yaml: dependency: "direct main" description: @@ -1023,5 +1016,5 @@ packages: source: hosted version: "2.2.1" sdks: - dart: ">=2.9.0-14.0.dev <3.0.0" + dart: ">=2.7.0 <3.0.0" flutter: ">=1.12.13+hotfix.5 <2.0.0" From 97226ad5eb57abe3597a7dc0fe10496ab252a4d8 Mon Sep 17 00:00:00 2001 From: OleksandrSobol Date: Wed, 30 Sep 2020 20:34:18 +0300 Subject: [PATCH 3/3] CAKE-43 | merged new world branch into current --- lib/src/screens/exchange/exchange_page.dart | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/src/screens/exchange/exchange_page.dart b/lib/src/screens/exchange/exchange_page.dart index a99f66eae..694f74129 100644 --- a/lib/src/screens/exchange/exchange_page.dart +++ b/lib/src/screens/exchange/exchange_page.dart @@ -208,7 +208,9 @@ class ExchangePage extends BasePage { exchangeViewModel.wallet.currency ? exchangeViewModel.wallet.address : exchangeViewModel.receiveAddress, - initialIsAmountEditable: false, + initialIsAmountEditable: + exchangeViewModel.provider is + XMRTOExchangeProvider ? true : false, initialIsAddressEditable: exchangeViewModel .isReceiveAddressEnabled,