2021-03-23 17:19:01 +00:00
|
|
|
import 'dart:convert';
|
2021-03-23 18:57:20 +00:00
|
|
|
import 'package:cake_wallet/entities/wyre_exception.dart';
|
2021-03-23 17:19:01 +00:00
|
|
|
import 'package:cake_wallet/exchange/trade_state.dart';
|
2021-05-11 15:37:26 +00:00
|
|
|
import 'package:cake_wallet/store/app_store.dart';
|
2021-03-23 17:19:01 +00:00
|
|
|
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 {
|
2021-05-11 15:37:26 +00:00
|
|
|
WyreService({@required this.appStore, this.isTestEnvironment = false}) {
|
|
|
|
baseApiUrl = isTestEnvironment ? _baseTestApiUrl : _baseProductApiUrl;
|
|
|
|
trackUrl = isTestEnvironment ? _trackTestUrl : _trackProductUrl;
|
2021-03-23 17:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 18:57:20 +00:00
|
|
|
static const _baseTestApiUrl = 'https://api.testwyre.com';
|
|
|
|
static const _baseProductApiUrl = 'https://api.sendwyre.com';
|
|
|
|
static const _trackTestUrl = 'https://dash.testwyre.com/track/';
|
|
|
|
static const _trackProductUrl = 'https://dash.sendwyre.com/track/';
|
2021-03-23 17:19:01 +00:00
|
|
|
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;
|
2021-05-11 15:37:26 +00:00
|
|
|
final AppStore appStore;
|
2021-03-31 17:02:44 +00:00
|
|
|
|
2021-05-11 15:37:26 +00:00
|
|
|
WalletType get walletType => appStore.wallet.type;
|
|
|
|
String get walletAddress => appStore.wallet.address;
|
|
|
|
String get walletId => appStore.wallet.id;
|
2021-03-23 17:19:01 +00:00
|
|
|
|
|
|
|
String baseApiUrl;
|
|
|
|
String trackUrl;
|
|
|
|
|
|
|
|
Future<String> getWyreUrl() async {
|
|
|
|
final timestamp = DateTime.now().millisecondsSinceEpoch.toString();
|
2021-05-11 15:37:26 +00:00
|
|
|
final url = baseApiUrl +
|
|
|
|
_ordersSuffix +
|
|
|
|
_reserveSuffix +
|
|
|
|
_timeStampSuffix +
|
|
|
|
timestamp;
|
2021-03-23 17:19:01 +00:00
|
|
|
final secretKey = secrets.wyreSecretKey;
|
|
|
|
final accountId = secrets.wyreAccountId;
|
|
|
|
final body = {
|
|
|
|
'destCurrency': walletTypeToCryptoCurrency(walletType).title,
|
2021-05-11 15:37:26 +00:00
|
|
|
'dest':
|
|
|
|
walletTypeToString(walletType).toLowerCase() + ':' + walletAddress,
|
2021-03-23 17:19:01 +00:00
|
|
|
'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<String, dynamic>;
|
|
|
|
final urlFromResponse = responseJSON['url'] as String;
|
|
|
|
return urlFromResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Order> findOrderById(String id) async {
|
2021-03-24 09:50:51 +00:00
|
|
|
final orderUrl = baseApiUrl + _ordersSuffix + '/$id';
|
2021-03-23 17:19:01 +00:00
|
|
|
final orderResponse = await get(orderUrl);
|
|
|
|
|
|
|
|
if (orderResponse.statusCode != 200) {
|
|
|
|
throw WyreException('Order $id is not found!');
|
|
|
|
}
|
|
|
|
|
|
|
|
final orderResponseJSON =
|
2021-05-11 15:37:26 +00:00
|
|
|
json.decode(orderResponse.body) as Map<String, dynamic>;
|
2021-03-23 17:19:01 +00:00
|
|
|
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 =
|
2021-05-11 15:37:26 +00:00
|
|
|
DateTime.fromMillisecondsSinceEpoch(createdAtRaw).toLocal();
|
2021-03-23 17:19:01 +00:00
|
|
|
|
|
|
|
final transferUrl =
|
|
|
|
baseApiUrl + _transferSuffix + transferId + _trackSuffix;
|
|
|
|
final transferResponse = await get(transferUrl);
|
|
|
|
|
|
|
|
if (transferResponse.statusCode != 200) {
|
|
|
|
throw WyreException('Transfer $transferId is not found!');
|
|
|
|
}
|
|
|
|
|
|
|
|
final transferResponseJSON =
|
2021-05-11 15:37:26 +00:00
|
|
|
json.decode(transferResponse.body) as Map<String, dynamic>;
|
2021-03-23 17:19:01 +00:00
|
|
|
final amount = transferResponseJSON['destAmount'] as double;
|
|
|
|
|
|
|
|
return Order(
|
|
|
|
id: id,
|
|
|
|
transferId: transferId,
|
|
|
|
from: from,
|
|
|
|
to: to,
|
|
|
|
state: state,
|
|
|
|
createdAt: createdAt,
|
2021-03-31 17:02:44 +00:00
|
|
|
amount: amount.toString(),
|
|
|
|
receiveAddress: walletAddress,
|
2021-05-11 15:37:26 +00:00
|
|
|
walletId: walletId);
|
2021-03-23 17:19:01 +00:00
|
|
|
}
|
2021-05-11 15:37:26 +00:00
|
|
|
}
|