2021-04-12 18:22:22 +00:00
|
|
|
import 'dart:convert';
|
2024-03-25 18:16:57 +00:00
|
|
|
|
|
|
|
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
|
|
|
import 'package:cake_wallet/buy/buy_amount.dart';
|
|
|
|
import 'package:cake_wallet/buy/buy_exception.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/exchange/trade_state.dart';
|
|
|
|
import 'package:cake_wallet/generated/i18n.dart';
|
2023-06-13 23:15:10 +00:00
|
|
|
import 'package:cake_wallet/palette.dart';
|
2023-12-28 19:20:59 +00:00
|
|
|
import 'package:cake_wallet/routes.dart';
|
|
|
|
import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
|
2023-06-13 23:15:10 +00:00
|
|
|
import 'package:cake_wallet/store/settings_store.dart';
|
|
|
|
import 'package:cake_wallet/themes/theme_base.dart';
|
2023-12-28 19:20:59 +00:00
|
|
|
import 'package:cake_wallet/utils/device_info.dart';
|
2024-03-25 18:16:57 +00:00
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cw_core/wallet_base.dart';
|
|
|
|
import 'package:cw_core/wallet_type.dart';
|
2024-03-25 18:16:57 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:http/http.dart';
|
2023-12-28 19:20:59 +00:00
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2021-12-08 12:46:01 +00:00
|
|
|
|
2024-03-28 13:30:41 +00:00
|
|
|
class MoonPayProvider extends BuyProvider {
|
|
|
|
MoonPayProvider({
|
2024-01-01 13:05:37 +00:00
|
|
|
required SettingsStore settingsStore,
|
|
|
|
required WalletBase wallet,
|
|
|
|
bool isTestEnvironment = false,
|
2024-03-28 13:30:41 +00:00
|
|
|
}) : baseSellUrl = isTestEnvironment ? _baseSellTestUrl : _baseSellProductUrl,
|
|
|
|
baseBuyUrl = isTestEnvironment ? _baseBuyTestUrl : _baseBuyProductUrl,
|
2023-12-28 19:20:59 +00:00
|
|
|
this._settingsStore = settingsStore,
|
2024-05-05 01:44:50 +00:00
|
|
|
super(wallet: wallet, isTestEnvironment: isTestEnvironment, ledgerVM: null);
|
2023-12-28 19:20:59 +00:00
|
|
|
|
|
|
|
final SettingsStore _settingsStore;
|
2021-12-08 12:46:01 +00:00
|
|
|
|
2024-03-28 13:30:41 +00:00
|
|
|
static const _baseSellTestUrl = 'sell-sandbox.moonpay.com';
|
|
|
|
static const _baseSellProductUrl = 'sell.moonpay.com';
|
|
|
|
static const _baseBuyTestUrl = 'buy-staging.moonpay.com';
|
|
|
|
static const _baseBuyProductUrl = 'buy.moonpay.com';
|
2024-03-25 18:16:57 +00:00
|
|
|
static const _cIdBaseUrl = 'exchange-helper.cakewallet.com';
|
2024-03-28 13:30:41 +00:00
|
|
|
static const _apiUrl = 'https://api.moonpay.com';
|
2023-12-28 19:20:59 +00:00
|
|
|
|
2024-01-01 13:05:37 +00:00
|
|
|
@override
|
|
|
|
String get providerDescription =>
|
|
|
|
'MoonPay offers a fast and simple way to buy and sell cryptocurrencies';
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get title => 'MoonPay';
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get lightIcon => 'assets/images/moonpay_light.png';
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get darkIcon => 'assets/images/moonpay_dark.png';
|
|
|
|
|
2023-06-13 23:15:10 +00:00
|
|
|
static String themeToMoonPayTheme(ThemeBase theme) {
|
|
|
|
switch (theme.type) {
|
|
|
|
case ThemeType.bright:
|
|
|
|
case ThemeType.light:
|
|
|
|
return 'light';
|
|
|
|
case ThemeType.dark:
|
|
|
|
return 'dark';
|
|
|
|
}
|
|
|
|
}
|
2023-12-28 19:20:59 +00:00
|
|
|
|
|
|
|
static String get _apiKey => secrets.moonPayApiKey;
|
2024-01-01 13:05:37 +00:00
|
|
|
|
2024-03-28 13:30:41 +00:00
|
|
|
final String baseBuyUrl;
|
|
|
|
final String baseSellUrl;
|
|
|
|
|
|
|
|
String get currencyCode => walletTypeToCryptoCurrency(wallet.type).title.toLowerCase();
|
|
|
|
|
|
|
|
String get trackUrl => baseBuyUrl + '/transaction_receipt?transactionId=';
|
|
|
|
|
2024-03-25 18:16:57 +00:00
|
|
|
static String get _exchangeHelperApiKey => secrets.exchangeHelperApiKey;
|
2021-12-08 12:46:01 +00:00
|
|
|
|
2024-03-25 18:16:57 +00:00
|
|
|
Future<String> getMoonpaySignature(String query) async {
|
|
|
|
final uri = Uri.https(_cIdBaseUrl, "/api/moonpay");
|
|
|
|
|
|
|
|
final response = await post(
|
|
|
|
uri,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'x-api-key': _exchangeHelperApiKey,
|
|
|
|
},
|
|
|
|
body: json.encode({'query': query}),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
return (jsonDecode(response.body) as Map<String, dynamic>)['signature'] as String;
|
|
|
|
} else {
|
|
|
|
throw Exception(
|
|
|
|
'Provider currently unavailable. Status: ${response.statusCode} ${response.body}');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-28 13:30:41 +00:00
|
|
|
Future<Uri> requestSellMoonPayUrl({
|
2023-12-28 19:20:59 +00:00
|
|
|
required CryptoCurrency currency,
|
|
|
|
required String refundWalletAddress,
|
|
|
|
required SettingsStore settingsStore,
|
|
|
|
}) async {
|
2024-03-28 13:30:41 +00:00
|
|
|
final params = {
|
2023-06-13 23:15:10 +00:00
|
|
|
'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)}',
|
2024-03-28 13:30:41 +00:00
|
|
|
'defaultCurrencyCode': _normalizeCurrency(currency),
|
|
|
|
'refundWalletAddress': refundWalletAddress,
|
2023-06-13 23:15:10 +00:00
|
|
|
};
|
|
|
|
|
2024-03-28 13:30:41 +00:00
|
|
|
if (_apiKey.isNotEmpty) {
|
|
|
|
params['apiKey'] = _apiKey;
|
|
|
|
}
|
|
|
|
|
2021-12-08 12:46:01 +00:00
|
|
|
final originalUri = Uri.https(
|
2024-03-28 13:30:41 +00:00
|
|
|
baseSellUrl,
|
2023-12-28 19:20:59 +00:00
|
|
|
'',
|
2024-03-28 13:30:41 +00:00
|
|
|
params,
|
2023-12-28 19:20:59 +00:00
|
|
|
);
|
|
|
|
|
2024-01-01 13:05:37 +00:00
|
|
|
if (isTestEnvironment) {
|
2023-04-14 04:39:08 +00:00
|
|
|
return originalUri;
|
2021-12-08 12:46:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-28 13:30:41 +00:00
|
|
|
final signature = await getMoonpaySignature('?${originalUri.query}');
|
|
|
|
|
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
|
|
|
}
|
2023-12-28 19:20:59 +00:00
|
|
|
|
2024-03-28 13:30:41 +00:00
|
|
|
// BUY:
|
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-13 18:40:44 +00:00
|
|
|
|
2024-03-28 13:30:41 +00:00
|
|
|
Future<Uri> requestBuyMoonPayUrl({
|
|
|
|
required CryptoCurrency currency,
|
|
|
|
required SettingsStore settingsStore,
|
|
|
|
required String walletAddress,
|
|
|
|
String? amount,
|
|
|
|
}) async {
|
|
|
|
final params = {
|
|
|
|
'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)}',
|
|
|
|
'defaultCurrencyCode': _normalizeCurrency(currency),
|
|
|
|
'baseCurrencyCode': _normalizeCurrency(currency),
|
|
|
|
'baseCurrencyAmount': amount ?? '0',
|
|
|
|
'currencyCode': currencyCode,
|
|
|
|
'walletAddress': walletAddress,
|
2024-04-08 17:42:27 +00:00
|
|
|
'lockAmount': 'false',
|
2024-03-28 13:30:41 +00:00
|
|
|
'showAllCurrencies': 'false',
|
|
|
|
'showWalletAddressForm': 'false',
|
|
|
|
'enabledPaymentMethods':
|
|
|
|
'credit_debit_card,apple_pay,google_pay,samsung_pay,sepa_bank_transfer,gbp_bank_transfer,gbp_open_banking_payment',
|
|
|
|
};
|
2021-04-12 18:22:22 +00:00
|
|
|
|
2024-03-28 13:30:41 +00:00
|
|
|
if (_apiKey.isNotEmpty) {
|
|
|
|
params['apiKey'] = _apiKey;
|
|
|
|
}
|
2021-04-12 18:22:22 +00:00
|
|
|
|
2024-03-28 13:30:41 +00:00
|
|
|
final originalUri = Uri.https(
|
|
|
|
baseBuyUrl,
|
|
|
|
'',
|
|
|
|
params,
|
|
|
|
);
|
2021-06-01 18:03:35 +00:00
|
|
|
|
2024-03-28 13:30:41 +00:00
|
|
|
if (isTestEnvironment) {
|
|
|
|
return originalUri;
|
|
|
|
}
|
2021-06-01 18:03:35 +00:00
|
|
|
|
2024-03-28 13:30:41 +00:00
|
|
|
final signature = await getMoonpaySignature('?${originalUri.query}');
|
|
|
|
final query = Map<String, dynamic>.from(originalUri.queryParameters);
|
|
|
|
query['signature'] = signature;
|
|
|
|
final signedUri = originalUri.replace(queryParameters: query);
|
|
|
|
return signedUri;
|
2021-04-12 18:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<BuyAmount> calculateAmount(String amount, String sourceCurrency) async {
|
2024-01-01 13:05:37 +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) {
|
2024-01-01 13:05:37 +00:00
|
|
|
throw BuyException(title: providerDescription, content: '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(
|
2024-01-01 13:05:37 +00:00
|
|
|
sourceAmount: sourceAmount, destAmount: destAmount, minAmount: minSourceAmount);
|
2021-04-12 18:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<Order> findOrderById(String id) async {
|
2024-01-01 13:05:37 +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) {
|
2024-01-01 13:05:37 +00:00
|
|
|
throw BuyException(title: providerDescription, content: '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,
|
2023-12-28 19:20:59 +00:00
|
|
|
provider: BuyProviderDescription.moonPay,
|
2021-04-12 18:22:22 +00:00
|
|
|
transferId: id,
|
|
|
|
state: state,
|
|
|
|
createdAt: createdAt,
|
|
|
|
amount: amount.toString(),
|
2023-12-28 19:20:59 +00:00
|
|
|
receiveAddress: wallet.walletAddresses.address,
|
2024-01-01 13:05:37 +00:00
|
|
|
walletId: wallet.id);
|
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-12-28 19:20:59 +00:00
|
|
|
|
|
|
|
@override
|
2024-03-28 13:30:41 +00:00
|
|
|
Future<void> launchProvider(BuildContext context, bool? isBuyAction) async {
|
2024-04-08 17:42:27 +00:00
|
|
|
try {
|
|
|
|
late final Uri uri;
|
|
|
|
if (isBuyAction ?? true) {
|
|
|
|
uri = await requestBuyMoonPayUrl(
|
|
|
|
currency: wallet.currency,
|
|
|
|
walletAddress: wallet.walletAddresses.address,
|
|
|
|
settingsStore: _settingsStore,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
uri = await requestSellMoonPayUrl(
|
|
|
|
currency: wallet.currency,
|
|
|
|
refundWalletAddress: wallet.walletAddresses.address,
|
|
|
|
settingsStore: _settingsStore,
|
|
|
|
);
|
|
|
|
}
|
2024-03-28 13:30:41 +00:00
|
|
|
|
2024-04-08 17:42:27 +00:00
|
|
|
if (await canLaunchUrl(uri)) {
|
|
|
|
if (DeviceInfo.instance.isMobile) {
|
|
|
|
Navigator.of(context).pushNamed(Routes.webViewPage, arguments: ['MoonPay', uri]);
|
|
|
|
} else {
|
|
|
|
await launchUrl(uri, mode: LaunchMode.externalApplication);
|
|
|
|
}
|
2024-03-28 13:30:41 +00:00
|
|
|
} else {
|
2024-04-08 17:42:27 +00:00
|
|
|
throw Exception('Could not launch URL');
|
2024-03-28 13:30:41 +00:00
|
|
|
}
|
2024-04-08 17:42:27 +00:00
|
|
|
} catch (e) {
|
|
|
|
await showDialog<void>(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return AlertWithOneAction(
|
|
|
|
alertTitle: 'MoonPay',
|
|
|
|
alertContent: 'The MoonPay service is currently unavailable: $e',
|
|
|
|
buttonText: S.of(context).ok,
|
|
|
|
buttonAction: () => Navigator.of(context).pop(),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2024-03-28 13:30:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String _normalizeCurrency(CryptoCurrency currency) {
|
|
|
|
if (currency == CryptoCurrency.maticpoly) {
|
|
|
|
return "MATIC_POLYGON";
|
|
|
|
}
|
|
|
|
|
|
|
|
return currency.toString().toLowerCase();
|
|
|
|
}
|
2024-01-01 13:05:37 +00:00
|
|
|
}
|