From 08efc52f71ed49b0cf058131d43280f736051cbc Mon Sep 17 00:00:00 2001 From: OleksandrSobol Date: Tue, 23 Mar 2021 19:19:01 +0200 Subject: [PATCH] CAKE-279 | removed find_order_by_id.dart; added and applied wyre_service.dart to the app --- lib/di.dart | 14 ++- lib/entities/find_order_by_id.dart | 40 ------ lib/entities/wyre_service.dart | 115 ++++++++++++++++++ lib/src/screens/dashboard/dashboard_page.dart | 9 +- .../dashboard/dashboard_view_model.dart | 20 --- lib/view_model/order_details_view_model.dart | 11 +- lib/view_model/wyre_view_model.dart | 68 ++++------- 7 files changed, 160 insertions(+), 117 deletions(-) delete mode 100644 lib/entities/find_order_by_id.dart create mode 100644 lib/entities/wyre_service.dart diff --git a/lib/di.dart b/lib/di.dart index 87d1c9908..db0b70283 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -496,9 +496,6 @@ Future setup( getIt.registerFactoryParam((trade, _) => TradeDetailsViewModel(tradeForDetails: trade, trades: _tradesSource)); - getIt.registerFactoryParam( - (order, _) => OrderDetailsViewModel(orderForDetails: order)); - getIt.registerFactory(() => BackupService( getIt.get(), _walletInfoSource, @@ -528,9 +525,6 @@ Future setup( getIt.registerFactoryParam((Trade trade, _) => TradeDetailsPage(getIt.get(param1: trade))); - getIt.registerFactoryParam((Order order, _) => - OrderDetailsPage(getIt.get(param1: order))); - getIt.registerFactory(() { final wallet = getIt.get().wallet; return WyreViewModel(ordersSource, getIt.get(), @@ -541,6 +535,14 @@ Future setup( WyrePage(getIt.get(), ordersStore: getIt.get(), url: url)); + getIt.registerFactoryParam( + (order, _) => OrderDetailsViewModel( + wyreViewModel: getIt.get(), + orderForDetails: order)); + + getIt.registerFactoryParam((Order order, _) => + OrderDetailsPage(getIt.get(param1: order))); + getIt.registerFactory(() => SupportViewModel()); getIt.registerFactory(() => SupportPage(getIt.get())); diff --git a/lib/entities/find_order_by_id.dart b/lib/entities/find_order_by_id.dart deleted file mode 100644 index d97c30593..000000000 --- a/lib/entities/find_order_by_id.dart +++ /dev/null @@ -1,40 +0,0 @@ -import 'dart:convert'; -import 'package:cake_wallet/entities/order.dart'; -import 'package:cake_wallet/exchange/trade_state.dart'; -import 'package:http/http.dart'; - -Future findOrderById(String id) async { - final orderUrl = 'https://api.sendwyre.com/v3/orders/' + id; - - final orderResponse = await get(orderUrl); - - final orderResponseJSON = - json.decode(orderResponse.body) as Map; - final transferId = orderResponseJSON['transferId'] as String; - final from = orderResponseJSON['sourceCurrency'] as String; - final to = orderResponseJSON['destCurrency'] as String; - final status = orderResponseJSON['status'] as String; - final state = TradeState.deserialize(raw: status.toLowerCase()); - final createdAtRaw = orderResponseJSON['createdAt'] as int; - final createdAt = - DateTime.fromMillisecondsSinceEpoch(createdAtRaw).toLocal(); - - final transferUrl = - 'https://api.sendwyre.com/v2/transfer/' + transferId + '/track'; - - final transferResponse = await get(transferUrl); - - final transferResponseJSON = - json.decode(transferResponse.body) as Map; - final amount = transferResponseJSON['destAmount'] as double; - - return Order( - id: id, - transferId: transferId, - from: from, - to: to, - state: state, - createdAt: createdAt, - amount: amount.toString() - ); -} \ No newline at end of file diff --git a/lib/entities/wyre_service.dart b/lib/entities/wyre_service.dart new file mode 100644 index 000000000..eb160af50 --- /dev/null +++ b/lib/entities/wyre_service.dart @@ -0,0 +1,115 @@ +import 'dart:convert'; +import 'package:cake_wallet/exchange/trade_state.dart'; +import 'package:flutter/foundation.dart'; +import 'package:http/http.dart'; +import 'package:cake_wallet/.secrets.g.dart' as secrets; +import 'package:cake_wallet/entities/order.dart'; +import 'package:cake_wallet/entities/wallet_type.dart'; + +class WyreService { + WyreService({ + @required this.walletType, + @required this.walletAddress, + this.isTestEnvironment = false}) { + baseApiUrl = isTestEnvironment + ? 'https://api.testwyre.com' + : 'https://api.sendwyre.com'; + trackUrl = isTestEnvironment + ? 'https://dash.testwyre.com/track/' + : 'https://dash.sendwyre.com/track/'; + } + + static const _ordersSuffix = '/v3/orders'; + static const _reserveSuffix = '/reserve'; + static const _timeStampSuffix = '?timestamp='; + static const _transferSuffix = '/v2/transfer/'; + static const _trackSuffix = '/track'; + + final bool isTestEnvironment; + final WalletType walletType; + final String walletAddress; + + String baseApiUrl; + String trackUrl; + + Future getWyreUrl() async { + final timestamp = DateTime.now().millisecondsSinceEpoch.toString(); + final url = baseApiUrl + _ordersSuffix + _reserveSuffix + + _timeStampSuffix + timestamp; + final secretKey = secrets.wyreSecretKey; + final accountId = secrets.wyreAccountId; + final body = { + 'destCurrency': walletTypeToCryptoCurrency(walletType).title, + 'dest': walletTypeToString(walletType).toLowerCase() + ':' + walletAddress, + 'referrerAccountId': accountId, + 'lockFields': ['destCurrency', 'dest'] + }; + + final response = await post(url, + headers: { + 'Authorization': 'Bearer $secretKey', + 'Content-Type': 'application/json', + 'cache-control': 'no-cache' + }, + body: json.encode(body)); + + if (response.statusCode != 200) { + throw WyreException('Url $url is not found!'); + } + + final responseJSON = json.decode(response.body) as Map; + final urlFromResponse = responseJSON['url'] as String; + return urlFromResponse; + } + + Future findOrderById(String id) async { + final orderUrl = baseApiUrl + _ordersSuffix + id; + final orderResponse = await get(orderUrl); + + if (orderResponse.statusCode != 200) { + throw WyreException('Order $id is not found!'); + } + + final orderResponseJSON = + json.decode(orderResponse.body) as Map; + final transferId = orderResponseJSON['transferId'] as String; + final from = orderResponseJSON['sourceCurrency'] as String; + final to = orderResponseJSON['destCurrency'] as String; + final status = orderResponseJSON['status'] as String; + final state = TradeState.deserialize(raw: status.toLowerCase()); + final createdAtRaw = orderResponseJSON['createdAt'] as int; + final createdAt = + DateTime.fromMillisecondsSinceEpoch(createdAtRaw).toLocal(); + + final transferUrl = + baseApiUrl + _transferSuffix + transferId + _trackSuffix; + final transferResponse = await get(transferUrl); + + if (transferResponse.statusCode != 200) { + throw WyreException('Transfer $transferId is not found!'); + } + + final transferResponseJSON = + json.decode(transferResponse.body) as Map; + final amount = transferResponseJSON['destAmount'] as double; + + return Order( + id: id, + transferId: transferId, + from: from, + to: to, + state: state, + createdAt: createdAt, + amount: amount.toString() + ); + } +} + +class WyreException implements Exception { + WyreException(this.description); + + String description; + + @override + String toString() => description; +} \ No newline at end of file diff --git a/lib/src/screens/dashboard/dashboard_page.dart b/lib/src/screens/dashboard/dashboard_page.dart index 39e52b853..172483ed0 100644 --- a/lib/src/screens/dashboard/dashboard_page.dart +++ b/lib/src/screens/dashboard/dashboard_page.dart @@ -146,11 +146,10 @@ class DashboardPage extends BasePage { : () async { try { walletViewModel.isRunningWebView = true; - final url = await walletViewModel.wyreViewModel.getWyreUrl(); - if (url.isNotEmpty) { - await Navigator.of(context) - .pushNamed(Routes.wyre, arguments: url); - } + final url = + await walletViewModel.wyreViewModel.wyreUrl; + await Navigator.of(context) + .pushNamed(Routes.wyre, arguments: url); walletViewModel.isRunningWebView = false; } catch(e) { print(e.toString()); diff --git a/lib/view_model/dashboard/dashboard_view_model.dart b/lib/view_model/dashboard/dashboard_view_model.dart index 5b6422b40..02ffe4b8f 100644 --- a/lib/view_model/dashboard/dashboard_view_model.dart +++ b/lib/view_model/dashboard/dashboard_view_model.dart @@ -4,7 +4,6 @@ import 'dart:io'; import 'package:cake_wallet/bitcoin/bitcoin_transaction_info.dart'; import 'package:cake_wallet/bitcoin/bitcoin_wallet.dart'; import 'package:cake_wallet/entities/balance.dart'; -import 'package:cake_wallet/entities/find_order_by_id.dart'; import 'package:cake_wallet/entities/order.dart'; import 'package:cake_wallet/entities/transaction_history.dart'; import 'package:cake_wallet/exchange/trade_state.dart'; @@ -147,27 +146,8 @@ abstract class DashboardViewModelBase with Store { return true; }); - - dataChannel.setMessageHandler((ByteData message) async { - final type = ByteData.view(message.buffer, 0, 4).getInt32(0); - - switch (type) { - case _dataExists: - print('Wyre: Data exists'); - break; - case _dataNotExists: - print('Wyre: Data not exists'); - break; - } - - return ByteData(0); - }); } - static const dataChannel = BasicMessageChannel('data_change', BinaryCodec()); - static const _dataExists = 1; - static const _dataNotExists = 0; - @observable WalletType type; diff --git a/lib/view_model/order_details_view_model.dart b/lib/view_model/order_details_view_model.dart index dd34dd489..44367a19d 100644 --- a/lib/view_model/order_details_view_model.dart +++ b/lib/view_model/order_details_view_model.dart @@ -1,7 +1,7 @@ import 'dart:async'; -import 'package:cake_wallet/entities/find_order_by_id.dart'; import 'package:cake_wallet/entities/order.dart'; import 'package:cake_wallet/utils/date_formatter.dart'; +import 'package:cake_wallet/view_model/wyre_view_model.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'; @@ -14,7 +14,7 @@ class OrderDetailsViewModel = OrderDetailsViewModelBase with _$OrderDetailsViewModel; abstract class OrderDetailsViewModelBase with Store { - OrderDetailsViewModelBase({Order orderForDetails}) { + OrderDetailsViewModelBase({this.wyreViewModel, Order orderForDetails}) { order = orderForDetails; items = ObservableList(); @@ -32,12 +32,15 @@ abstract class OrderDetailsViewModelBase with Store { @observable ObservableList items; + WyreViewModel wyreViewModel; + Timer _timer; @action Future _updateOrder() async { try { - final updatedOrder = await findOrderById(order.id); + final updatedOrder = + await wyreViewModel.wyreService.findOrderById(order.id); updatedOrder.receiveAddress = order.receiveAddress; updatedOrder.walletId = order.walletId; @@ -52,7 +55,7 @@ abstract class OrderDetailsViewModelBase with Store { void _updateItems() { final dateFormat = DateFormatter.withCurrentLocal(); final buildURL = - 'https://dash.sendwyre.com/track/${order.transferId}'; + wyreViewModel.trackUrl + '${order.transferId}'; items?.clear(); diff --git a/lib/view_model/wyre_view_model.dart b/lib/view_model/wyre_view_model.dart index 9b55136cc..7862557be 100644 --- a/lib/view_model/wyre_view_model.dart +++ b/lib/view_model/wyre_view_model.dart @@ -1,16 +1,23 @@ -import 'dart:convert'; +import 'package:cake_wallet/entities/wyre_service.dart'; import 'package:flutter/foundation.dart'; import 'package:hive/hive.dart'; -import 'package:http/http.dart'; -import 'package:cake_wallet/.secrets.g.dart' as secrets; -import 'package:cake_wallet/entities/find_order_by_id.dart'; import 'package:cake_wallet/entities/order.dart'; import 'package:cake_wallet/entities/wallet_type.dart'; import 'package:cake_wallet/store/dashboard/orders_store.dart'; +import 'package:mobx/mobx.dart'; -class WyreViewModel { - WyreViewModel(this.ordersSource, this.ordersStore, - {@required this.walletId, @required this.address, @required this.type}); +part 'wyre_view_model.g.dart'; + +class WyreViewModel = WyreViewModelBase with _$WyreViewModel; + +abstract class WyreViewModelBase with Store { + WyreViewModelBase(this.ordersSource, this.ordersStore, + {@required this.walletId, @required this.address, @required this.type}) + : wyreService = WyreService(walletType: type, walletAddress: address); + + Future get wyreUrl => wyreService.getWyreUrl(); + + String get trackUrl => wyreService.trackUrl; final Box ordersSource; final OrdersStore ordersStore; @@ -19,42 +26,19 @@ class WyreViewModel { final WalletType type; final String address; + WyreService wyreService; + Future saveOrder(String orderId) async { - final order = await findOrderById(orderId); - order.receiveAddress = address; - order.walletId = walletId; - await ordersSource.add(order); - ordersStore.setOrder(order); - } - - Future getWyreUrl() async { - final timestamp = DateTime.now().millisecondsSinceEpoch.toString(); - final url = 'https://api.sendwyre.com/v3/orders/reserve' + - '?timestamp=' + - timestamp; - final secretKey = secrets.wyreSecretKey; - final accountId = secrets.wyreAccountId; - final body = { - 'destCurrency': walletTypeToCryptoCurrency(type).title, - 'dest': walletTypeToString(type).toLowerCase() + ':' + address, - 'referrerAccountId': accountId, - 'lockFields': ['destCurrency', 'dest'] - }; - - final response = await post(url, - headers: { - 'Authorization': 'Bearer $secretKey', - 'Content-Type': 'application/json', - 'cache-control': 'no-cache' - }, - body: json.encode(body)); - - if (response.statusCode == 200) { - final responseJSON = json.decode(response.body) as Map; - final urlFromResponse = responseJSON['url'] as String; - return urlFromResponse; - } else { - return ''; + try { + final order = await wyreService.findOrderById(orderId); + order.receiveAddress = address; + order.walletId = walletId; + await ordersSource.add(order); + ordersStore.setOrder(order); + } catch (e) { + print(e.toString()); } } + + }