2021-04-12 18:22:22 +00:00
|
|
|
import 'dart:convert';
|
2023-07-13 22:48:47 +00:00
|
|
|
import 'package:cake_wallet/palette.dart';
|
|
|
|
import 'package:cake_wallet/store/settings_store.dart';
|
|
|
|
import 'package:cake_wallet/themes/theme_base.dart';
|
2021-06-01 18:03:35 +00:00
|
|
|
import 'package:crypto/crypto.dart';
|
2021-04-12 18:22:22 +00:00
|
|
|
import 'package:cake_wallet/buy/buy_exception.dart';
|
|
|
|
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';
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cw_core/wallet_base.dart';
|
|
|
|
import 'package:cw_core/wallet_type.dart';
|
2021-04-12 18:22:22 +00:00
|
|
|
import 'package:cake_wallet/exchange/trade_state.dart';
|
|
|
|
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
2021-12-08 12:46:01 +00:00
|
|
|
|
|
|
|
class MoonPaySellProvider {
|
2023-07-13 22:48:47 +00:00
|
|
|
MoonPaySellProvider({this.isTest = false}) : baseUrl = isTest ? _baseTestUrl : _baseProductUrl;
|
2021-12-08 12:46:01 +00:00
|
|
|
|
2023-07-13 22:48:47 +00:00
|
|
|
static const _baseTestUrl = 'sell-sandbox.moonpay.com';
|
2021-12-08 12:46:01 +00:00
|
|
|
static const _baseProductUrl = 'sell.moonpay.com';
|
2023-07-13 22:48:47 +00:00
|
|
|
|
|
|
|
static String themeToMoonPayTheme(ThemeBase theme) {
|
|
|
|
switch (theme.type) {
|
|
|
|
case ThemeType.bright:
|
|
|
|
return 'light';
|
|
|
|
case ThemeType.light:
|
|
|
|
return 'light';
|
|
|
|
case ThemeType.dark:
|
|
|
|
return 'dark';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static String get _apiKey => secrets.moonPayApiKey;
|
|
|
|
|
|
|
|
static String get _secretKey => secrets.moonPaySecretKey;
|
2021-12-08 12:46:01 +00:00
|
|
|
final bool isTest;
|
|
|
|
final String baseUrl;
|
|
|
|
|
2023-07-13 22:48:47 +00:00
|
|
|
Future<Uri> requestUrl(
|
|
|
|
{required CryptoCurrency currency,
|
|
|
|
required String refundWalletAddress,
|
|
|
|
required SettingsStore settingsStore}) async {
|
|
|
|
final customParams = {
|
|
|
|
'theme': themeToMoonPayTheme(settingsStore.currentTheme),
|
|
|
|
'language': settingsStore.languageCode,
|
|
|
|
'colorCode': settingsStore.currentTheme.type == ThemeType.dark
|
|
|
|
? '#${Palette.blueCraiola.value.toRadixString(16).substring(2, 8)}'
|
|
|
|
: '#${Palette.moderateSlateBlue.value.toRadixString(16).substring(2, 8)}',
|
|
|
|
};
|
|
|
|
|
2021-12-08 12:46:01 +00:00
|
|
|
final originalUri = Uri.https(
|
2023-07-13 22:48:47 +00:00
|
|
|
baseUrl,
|
|
|
|
'',
|
|
|
|
<String, dynamic>{
|
|
|
|
'apiKey': _apiKey,
|
|
|
|
'defaultBaseCurrencyCode': currency.toString().toLowerCase(),
|
|
|
|
'refundWalletAddress': refundWalletAddress
|
|
|
|
}..addAll(customParams));
|
2021-12-08 12:46:01 +00:00
|
|
|
final messageBytes = utf8.encode('?${originalUri.query}');
|
|
|
|
final key = utf8.encode(_secretKey);
|
|
|
|
final hmac = Hmac(sha256, key);
|
|
|
|
final digest = hmac.convert(messageBytes);
|
|
|
|
final signature = base64.encode(digest.bytes);
|
|
|
|
|
|
|
|
if (isTest) {
|
2023-04-14 04:39:08 +00:00
|
|
|
return originalUri;
|
2021-12-08 12:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final query = Map<String, dynamic>.from(originalUri.queryParameters);
|
|
|
|
query['signature'] = signature;
|
|
|
|
final signedUri = originalUri.replace(queryParameters: query);
|
2023-04-14 04:39:08 +00:00
|
|
|
return signedUri;
|
2021-12-08 12:46:01 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-12 18:22:22 +00:00
|
|
|
|
|
|
|
class MoonPayBuyProvider extends BuyProvider {
|
2022-10-12 17:09:57 +00:00
|
|
|
MoonPayBuyProvider({required WalletBase wallet, bool isTestEnvironment = false})
|
|
|
|
: baseUrl = isTestEnvironment ? _baseTestUrl : _baseProductUrl,
|
|
|
|
super(wallet: wallet, isTestEnvironment: isTestEnvironment);
|
2021-04-12 18:22:22 +00:00
|
|
|
|
2021-06-01 18:03:35 +00:00
|
|
|
static const _baseTestUrl = 'https://buy-staging.moonpay.com';
|
|
|
|
static const _baseProductUrl = 'https://buy.moonpay.com';
|
|
|
|
static const _apiUrl = 'https://api.moonpay.com';
|
2021-04-12 18:22:22 +00:00
|
|
|
static const _currenciesSuffix = '/v3/currencies';
|
|
|
|
static const _quoteSuffix = '/buy_quote';
|
|
|
|
static const _transactionsSuffix = '/v1/transactions';
|
2021-06-07 11:28:53 +00:00
|
|
|
static const _ipAddressSuffix = '/v4/ip_address';
|
2021-04-12 18:22:22 +00:00
|
|
|
static const _apiKey = secrets.moonPayApiKey;
|
2021-06-01 18:03:35 +00:00
|
|
|
static const _secretKey = secrets.moonPaySecretKey;
|
2021-04-12 18:22:22 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
String get title => 'MoonPay';
|
|
|
|
|
|
|
|
@override
|
|
|
|
BuyProviderDescription get description => BuyProviderDescription.moonPay;
|
|
|
|
|
2023-07-13 22:48:47 +00:00
|
|
|
String get currencyCode => walletTypeToCryptoCurrency(walletType).title.toLowerCase();
|
2021-04-12 18:22:22 +00:00
|
|
|
|
2021-04-13 18:40:44 +00:00
|
|
|
@override
|
2021-06-01 18:03:35 +00:00
|
|
|
String get trackUrl => baseUrl + '/transaction_receipt?transactionId=';
|
2021-04-13 18:40:44 +00:00
|
|
|
|
2021-06-01 18:03:35 +00:00
|
|
|
String baseUrl;
|
2021-04-12 18:22:22 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<String> requestUrl(String amount, String sourceCurrency) async {
|
2023-07-13 22:48:47 +00:00
|
|
|
final enabledPaymentMethods = 'credit_debit_card%2Capple_pay%2Cgoogle_pay%2Csamsung_pay'
|
2021-04-12 18:22:22 +00:00
|
|
|
'%2Csepa_bank_transfer%2Cgbp_bank_transfer%2Cgbp_open_banking_payment';
|
|
|
|
|
2023-07-13 22:48:47 +00:00
|
|
|
final suffix = '?apiKey=' +
|
|
|
|
_apiKey +
|
|
|
|
'¤cyCode=' +
|
|
|
|
currencyCode +
|
|
|
|
'&enabledPaymentMethods=' +
|
|
|
|
enabledPaymentMethods +
|
|
|
|
'&walletAddress=' +
|
|
|
|
walletAddress +
|
|
|
|
'&baseCurrencyCode=' +
|
|
|
|
sourceCurrency.toLowerCase() +
|
|
|
|
'&baseCurrencyAmount=' +
|
|
|
|
amount +
|
|
|
|
'&lockAmount=true' +
|
|
|
|
'&showAllCurrencies=false' +
|
|
|
|
'&showWalletAddressForm=false';
|
2021-04-12 18:22:22 +00:00
|
|
|
|
2021-06-01 18:03:35 +00:00
|
|
|
final originalUrl = baseUrl + suffix;
|
|
|
|
|
|
|
|
final messageBytes = utf8.encode(suffix);
|
|
|
|
final key = utf8.encode(_secretKey);
|
|
|
|
final hmac = Hmac(sha256, key);
|
|
|
|
final digest = hmac.convert(messageBytes);
|
|
|
|
final signature = base64.encode(digest.bytes);
|
2023-07-13 22:48:47 +00:00
|
|
|
final urlWithSignature = originalUrl + '&signature=${Uri.encodeComponent(signature)}';
|
2021-06-01 18:03:35 +00:00
|
|
|
|
|
|
|
return isTestEnvironment ? originalUrl : urlWithSignature;
|
2021-04-12 18:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<BuyAmount> calculateAmount(String amount, String sourceCurrency) async {
|
2023-07-13 22:48:47 +00:00
|
|
|
final url = _apiUrl +
|
|
|
|
_currenciesSuffix +
|
|
|
|
'/$currencyCode' +
|
|
|
|
_quoteSuffix +
|
|
|
|
'/?apiKey=' +
|
|
|
|
_apiKey +
|
|
|
|
'&baseCurrencyAmount=' +
|
|
|
|
amount +
|
|
|
|
'&baseCurrencyCode=' +
|
|
|
|
sourceCurrency.toLowerCase();
|
2022-10-12 17:09:57 +00:00
|
|
|
final uri = Uri.parse(url);
|
|
|
|
final response = await get(uri);
|
2021-04-12 18:22:22 +00:00
|
|
|
|
|
|
|
if (response.statusCode != 200) {
|
2023-07-13 22:48:47 +00:00
|
|
|
throw BuyException(description: description, text: 'Quote is not found!');
|
2021-04-12 18:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final sourceAmount = responseJSON['totalAmount'] as double;
|
|
|
|
final destAmount = responseJSON['quoteCurrencyAmount'] as double;
|
2021-06-01 18:03:35 +00:00
|
|
|
final minSourceAmount = responseJSON['baseCurrency']['minAmount'] as int;
|
2021-04-12 18:22:22 +00:00
|
|
|
|
2021-06-01 18:03:35 +00:00
|
|
|
return BuyAmount(
|
2023-07-13 22:48:47 +00:00
|
|
|
sourceAmount: sourceAmount, destAmount: destAmount, minAmount: minSourceAmount);
|
2021-04-12 18:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Order> findOrderById(String id) async {
|
2023-07-13 22:48:47 +00:00
|
|
|
final url = _apiUrl + _transactionsSuffix + '/$id' + '?apiKey=' + _apiKey;
|
2022-10-12 17:09:57 +00:00
|
|
|
final uri = Uri.parse(url);
|
|
|
|
final response = await get(uri);
|
2021-04-12 18:22:22 +00:00
|
|
|
|
|
|
|
if (response.statusCode != 200) {
|
2023-07-13 22:48:47 +00:00
|
|
|
throw BuyException(description: description, text: 'Transaction $id is not found!');
|
2021-04-12 18:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
return Order(
|
|
|
|
id: id,
|
|
|
|
provider: description,
|
|
|
|
transferId: id,
|
|
|
|
state: state,
|
|
|
|
createdAt: createdAt,
|
|
|
|
amount: amount.toString(),
|
|
|
|
receiveAddress: walletAddress,
|
2023-07-13 22:48:47 +00:00
|
|
|
walletId: walletId);
|
2021-04-12 18:22:22 +00:00
|
|
|
}
|
2021-06-01 18:03:35 +00:00
|
|
|
|
2021-06-07 11:28:53 +00:00
|
|
|
static Future<bool> onEnabled() async {
|
|
|
|
final url = _apiUrl + _ipAddressSuffix + '?apiKey=' + _apiKey;
|
2021-06-01 18:03:35 +00:00
|
|
|
var isBuyEnable = false;
|
2022-10-12 17:09:57 +00:00
|
|
|
final uri = Uri.parse(url);
|
|
|
|
final response = await get(uri);
|
2021-06-01 18:03:35 +00:00
|
|
|
|
|
|
|
try {
|
2021-06-07 11:28:53 +00:00
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
isBuyEnable = responseJSON['isBuyAllowed'] as bool;
|
2021-06-01 18:03:35 +00:00
|
|
|
} catch (e) {
|
|
|
|
isBuyEnable = false;
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
return isBuyEnable;
|
|
|
|
}
|
2023-07-13 22:48:47 +00:00
|
|
|
}
|