2021-04-12 18:22:22 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'package:cake_wallet/buy/buy_exception.dart';
|
2021-04-30 08:08:25 +00:00
|
|
|
import 'package:hive/hive.dart';
|
2021-04-12 18:22:22 +00:00
|
|
|
import 'package:http/http.dart';
|
|
|
|
import 'package:cake_wallet/buy/buy_amount.dart';
|
|
|
|
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/core/wallet_base.dart';
|
|
|
|
import 'package:cake_wallet/entities/wallet_type.dart';
|
|
|
|
import 'package:cake_wallet/exchange/trade_state.dart';
|
|
|
|
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
|
|
|
|
|
|
|
class MoonPayBuyProvider extends BuyProvider {
|
2021-04-30 08:08:25 +00:00
|
|
|
MoonPayBuyProvider({WalletBase wallet, this.ordersSource,
|
|
|
|
bool isTestEnvironment = false})
|
2021-04-12 18:22:22 +00:00
|
|
|
: super(wallet: wallet, isTestEnvironment: isTestEnvironment) {
|
2021-04-30 08:08:25 +00:00
|
|
|
baseApiUrl = isTestEnvironment
|
2021-04-12 18:22:22 +00:00
|
|
|
? _baseTestApiUrl
|
|
|
|
: _baseProductApiUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const _baseTestApiUrl = 'https://buy-staging.moonpay.com';
|
|
|
|
static const _baseProductApiUrl = 'https://api.moonpay.com';
|
|
|
|
static const _currenciesSuffix = '/v3/currencies';
|
|
|
|
static const _quoteSuffix = '/buy_quote';
|
|
|
|
static const _transactionsSuffix = '/v1/transactions';
|
|
|
|
static const _apiKey = secrets.moonPayApiKey;
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get title => 'MoonPay';
|
|
|
|
|
|
|
|
@override
|
|
|
|
BuyProviderDescription get description => BuyProviderDescription.moonPay;
|
|
|
|
|
|
|
|
String get currencyCode =>
|
|
|
|
walletTypeToCryptoCurrency(walletType).title.toLowerCase();
|
|
|
|
|
2021-04-13 18:40:44 +00:00
|
|
|
@override
|
2021-04-15 17:10:23 +00:00
|
|
|
String get trackUrl => baseApiUrl + '/transaction_receipt?transactionId=';
|
2021-04-13 18:40:44 +00:00
|
|
|
|
2021-04-30 08:08:25 +00:00
|
|
|
final Box<Order> ordersSource;
|
2021-04-12 18:22:22 +00:00
|
|
|
String baseApiUrl;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<String> requestUrl(String amount, String sourceCurrency) async {
|
|
|
|
final enabledPaymentMethods =
|
|
|
|
'credit_debit_card%2Capple_pay%2Cgoogle_pay%2Csamsung_pay'
|
|
|
|
'%2Csepa_bank_transfer%2Cgbp_bank_transfer%2Cgbp_open_banking_payment';
|
|
|
|
|
|
|
|
final originalUrl = baseApiUrl + '?apiKey=' + _apiKey + '¤cyCode=' +
|
|
|
|
currencyCode + '&enabledPaymentMethods=' + enabledPaymentMethods +
|
|
|
|
'&walletAddress=' + walletAddress +
|
|
|
|
'&baseCurrencyCode=' + sourceCurrency.toLowerCase() +
|
|
|
|
'&baseCurrencyAmount=' + amount + '&lockAmount=true' +
|
|
|
|
'&showAllCurrencies=false' + '&showWalletAddressForm=false';
|
|
|
|
|
|
|
|
return originalUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<BuyAmount> calculateAmount(String amount, String sourceCurrency) async {
|
2021-04-30 08:08:25 +00:00
|
|
|
final url = _baseProductApiUrl + _currenciesSuffix + '/$currencyCode' +
|
2021-04-12 18:22:22 +00:00
|
|
|
_quoteSuffix + '/?apiKey=' + _apiKey +
|
|
|
|
'&baseCurrencyAmount=' + amount +
|
|
|
|
'&baseCurrencyCode' + sourceCurrency.toLowerCase();
|
|
|
|
|
|
|
|
final response = await get(url);
|
|
|
|
|
|
|
|
if (response.statusCode != 200) {
|
|
|
|
throw BuyException(
|
|
|
|
description: description,
|
|
|
|
text: 'Quote is not found!');
|
|
|
|
}
|
|
|
|
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final sourceAmount = responseJSON['totalAmount'] as double;
|
|
|
|
final destAmount = responseJSON['quoteCurrencyAmount'] as double;
|
|
|
|
|
|
|
|
return BuyAmount(sourceAmount: sourceAmount, destAmount: destAmount);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Order> findOrderById(String id) async {
|
2021-04-30 08:08:25 +00:00
|
|
|
final url = _baseProductApiUrl + _transactionsSuffix + '/$id' +
|
2021-04-12 18:22:22 +00:00
|
|
|
'?apiKey=' + _apiKey;
|
|
|
|
|
|
|
|
final response = await get(url);
|
|
|
|
|
|
|
|
if (response.statusCode != 200) {
|
|
|
|
throw BuyException(
|
|
|
|
description: description,
|
|
|
|
text: 'Transaction $id is not found!');
|
|
|
|
}
|
|
|
|
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final status = responseJSON['status'] as String;
|
2021-04-14 18:23:10 +00:00
|
|
|
final state = TradeState.deserialize(raw: status);
|
|
|
|
final createdAtRaw = responseJSON['createdAt'] as String;
|
|
|
|
final createdAt = DateTime.parse(createdAtRaw).toLocal();
|
2021-04-12 18:22:22 +00:00
|
|
|
final amount = responseJSON['quoteCurrencyAmount'] as double;
|
|
|
|
|
2021-04-30 08:08:25 +00:00
|
|
|
var from = '';
|
|
|
|
var to = '';
|
|
|
|
|
|
|
|
for (final order in ordersSource.values) {
|
|
|
|
if (order.id == id) {
|
|
|
|
from = order.from;
|
|
|
|
to = order.to;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-12 18:22:22 +00:00
|
|
|
return Order(
|
|
|
|
id: id,
|
|
|
|
provider: description,
|
|
|
|
transferId: id,
|
2021-04-30 08:08:25 +00:00
|
|
|
from: from,
|
|
|
|
to: to,
|
2021-04-12 18:22:22 +00:00
|
|
|
state: state,
|
|
|
|
createdAt: createdAt,
|
|
|
|
amount: amount.toString(),
|
|
|
|
receiveAddress: walletAddress,
|
|
|
|
walletId: walletId
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|