From 9627590ba571dd9979117015ab815a66847dbdad Mon Sep 17 00:00:00 2001 From: OleksandrSobol Date: Fri, 30 Apr 2021 11:08:25 +0300 Subject: [PATCH] CAKE-306 | fixed findOrderById() in the moonpay_buy_provider.dart; fixed saveOrder() in the buy_view_model.dart --- lib/buy/moonpay/moonpay_buy_provider.dart | 27 ++++++++++++++----- lib/buy/wyre/wyre_buy_provider.dart | 2 +- lib/di.dart | 4 +-- lib/view_model/buy/buy_view_model.dart | 4 ++- .../dashboard/dashboard_view_model.dart | 3 --- lib/view_model/order_details_view_model.dart | 9 +++++-- 6 files changed, 33 insertions(+), 16 deletions(-) diff --git a/lib/buy/moonpay/moonpay_buy_provider.dart b/lib/buy/moonpay/moonpay_buy_provider.dart index 240d6389e..c53eb9c0e 100644 --- a/lib/buy/moonpay/moonpay_buy_provider.dart +++ b/lib/buy/moonpay/moonpay_buy_provider.dart @@ -1,5 +1,6 @@ import 'dart:convert'; import 'package:cake_wallet/buy/buy_exception.dart'; +import 'package:hive/hive.dart'; import 'package:http/http.dart'; import 'package:cake_wallet/buy/buy_amount.dart'; import 'package:cake_wallet/buy/buy_provider.dart'; @@ -11,9 +12,10 @@ import 'package:cake_wallet/exchange/trade_state.dart'; import 'package:cake_wallet/.secrets.g.dart' as secrets; class MoonPayBuyProvider extends BuyProvider { - MoonPayBuyProvider({WalletBase wallet, bool isTestEnvironment = false}) + MoonPayBuyProvider({WalletBase wallet, this.ordersSource, + bool isTestEnvironment = false}) : super(wallet: wallet, isTestEnvironment: isTestEnvironment) { - baseApiUrl = isTestEnvironment + baseApiUrl = isTestEnvironment ? _baseTestApiUrl : _baseProductApiUrl; } @@ -23,7 +25,6 @@ class MoonPayBuyProvider extends BuyProvider { static const _currenciesSuffix = '/v3/currencies'; static const _quoteSuffix = '/buy_quote'; static const _transactionsSuffix = '/v1/transactions'; - static const _fiatCurrency = 'USD'; static const _apiKey = secrets.moonPayApiKey; @override @@ -38,6 +39,7 @@ class MoonPayBuyProvider extends BuyProvider { @override String get trackUrl => baseApiUrl + '/transaction_receipt?transactionId='; + final Box ordersSource; String baseApiUrl; @override @@ -58,7 +60,7 @@ class MoonPayBuyProvider extends BuyProvider { @override Future calculateAmount(String amount, String sourceCurrency) async { - final url = baseApiUrl + _currenciesSuffix + '/$currencyCode' + + final url = _baseProductApiUrl + _currenciesSuffix + '/$currencyCode' + _quoteSuffix + '/?apiKey=' + _apiKey + '&baseCurrencyAmount=' + amount + '&baseCurrencyCode' + sourceCurrency.toLowerCase(); @@ -80,7 +82,7 @@ class MoonPayBuyProvider extends BuyProvider { @override Future findOrderById(String id) async { - final url = baseApiUrl + _transactionsSuffix + '/$id' + + final url = _baseProductApiUrl + _transactionsSuffix + '/$id' + '?apiKey=' + _apiKey; final response = await get(url); @@ -98,12 +100,23 @@ class MoonPayBuyProvider extends BuyProvider { final createdAt = DateTime.parse(createdAtRaw).toLocal(); final amount = responseJSON['quoteCurrencyAmount'] as double; + var from = ''; + var to = ''; + + for (final order in ordersSource.values) { + if (order.id == id) { + from = order.from; + to = order.to; + break; + } + } + return Order( id: id, provider: description, transferId: id, - from: _fiatCurrency, - to: currencyCode.toUpperCase(), + from: from, + to: to, state: state, createdAt: createdAt, amount: amount.toString(), diff --git a/lib/buy/wyre/wyre_buy_provider.dart b/lib/buy/wyre/wyre_buy_provider.dart index 77055c325..f1100d130 100644 --- a/lib/buy/wyre/wyre_buy_provider.dart +++ b/lib/buy/wyre/wyre_buy_provider.dart @@ -80,7 +80,7 @@ class WyreBuyProvider extends BuyProvider { @override Future calculateAmount(String amount, String sourceCurrency) async { - final quoteUrl = baseApiUrl + _ordersSuffix + _quoteSuffix; + final quoteUrl = _baseProductApiUrl + _ordersSuffix + _quoteSuffix; final body = { 'amount': amount, 'sourceCurrency': sourceCurrency, diff --git a/lib/di.dart b/lib/di.dart index 2a33e6b11..60a20e52a 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -238,7 +238,6 @@ Future setup( tradeFilterStore: getIt.get(), transactionFilterStore: getIt.get(), settingsStore: settingsStore, - ordersSource: _ordersSource, ordersStore: getIt.get())); getIt.registerFactory(() => AuthService( @@ -536,7 +535,7 @@ Future setup( getIt.registerFactory(() { final wallet = getIt.get().wallet; - return BuyViewModel(ordersSource, getIt.get(), + return BuyViewModel(_ordersSource, getIt.get(), getIt.get(), wallet: wallet); }); @@ -559,6 +558,7 @@ Future setup( return OrderDetailsViewModel( wallet: wallet, + ordersSource: _ordersSource, orderForDetails: order); }); diff --git a/lib/view_model/buy/buy_view_model.dart b/lib/view_model/buy/buy_view_model.dart index 70d1cc556..d0f2c2f6e 100644 --- a/lib/view_model/buy/buy_view_model.dart +++ b/lib/view_model/buy/buy_view_model.dart @@ -22,7 +22,7 @@ abstract class BuyViewModelBase with Store { {@required this.wallet}) { providerList = [ WyreBuyProvider(wallet: wallet), - MoonPayBuyProvider(wallet: wallet) + MoonPayBuyProvider(wallet: wallet, ordersSource: ordersSource) ]; items = providerList.map((provider) => BuyItem(provider: provider, buyAmountViewModel: buyAmountViewModel)) @@ -79,6 +79,8 @@ abstract class BuyViewModelBase with Store { Future saveOrder(String orderId) async { try { final order = await selectedProvider?.findOrderById(orderId); + order.from = fiatCurrency.title; + order.to = cryptoCurrency.title; await ordersSource.add(order); ordersStore.setOrder(order); } catch (e) { diff --git a/lib/view_model/dashboard/dashboard_view_model.dart b/lib/view_model/dashboard/dashboard_view_model.dart index 10e6a5afd..ff1d2411d 100644 --- a/lib/view_model/dashboard/dashboard_view_model.dart +++ b/lib/view_model/dashboard/dashboard_view_model.dart @@ -57,7 +57,6 @@ abstract class DashboardViewModelBase with Store { this.tradeFilterStore, this.transactionFilterStore, this.settingsStore, - this.ordersSource, this.ordersStore}) { filterItems = { S.current.transactions: [ @@ -212,8 +211,6 @@ abstract class DashboardViewModelBase with Store { bool get hasRescan => wallet.type == WalletType.monero; - Box ordersSource; - BalanceViewModel balanceViewModel; AppStore appStore; diff --git a/lib/view_model/order_details_view_model.dart b/lib/view_model/order_details_view_model.dart index 328b0c93c..0fb92a761 100644 --- a/lib/view_model/order_details_view_model.dart +++ b/lib/view_model/order_details_view_model.dart @@ -3,6 +3,7 @@ import 'package:cake_wallet/buy/buy_provider.dart'; import 'package:cake_wallet/buy/buy_provider_description.dart'; import 'package:cake_wallet/buy/order.dart'; import 'package:cake_wallet/utils/date_formatter.dart'; +import 'package:hive/hive.dart'; import 'package:mobx/mobx.dart'; import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/src/screens/transaction_details/standart_list_item.dart'; @@ -18,7 +19,8 @@ class OrderDetailsViewModel = OrderDetailsViewModelBase with _$OrderDetailsViewModel; abstract class OrderDetailsViewModelBase with Store { - OrderDetailsViewModelBase({WalletBase wallet, Order orderForDetails}) { + OrderDetailsViewModelBase({WalletBase wallet, this.ordersSource, + Order orderForDetails}) { order = orderForDetails; if (order.provider != null) { @@ -27,7 +29,8 @@ abstract class OrderDetailsViewModelBase with Store { _provider = WyreBuyProvider(wallet: wallet); break; case BuyProviderDescription.moonPay: - _provider = MoonPayBuyProvider(wallet: wallet); + _provider = + MoonPayBuyProvider(wallet: wallet, ordersSource: ordersSource); break; } } @@ -41,6 +44,8 @@ abstract class OrderDetailsViewModelBase with Store { timer = Timer.periodic(Duration(seconds: 20), (_) async => _updateOrder()); } + final Box ordersSource; + @observable Order order;