mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-01-09 12:29:31 +00:00
171 lines
5.4 KiB
Dart
171 lines
5.4 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
|
import 'package:cake_wallet/buy/buy_provider.dart';
|
|
import 'package:cake_wallet/routes.dart';
|
|
import 'package:cake_wallet/store/settings_store.dart';
|
|
import 'package:cake_wallet/themes/theme_base.dart';
|
|
import 'package:cake_wallet/utils/device_info.dart';
|
|
import 'package:cw_core/crypto_currency.dart';
|
|
import 'package:cw_core/wallet_base.dart';
|
|
import 'package:cw_core/wallet_type.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
class MeldProvider extends BuyProvider {
|
|
MeldProvider({
|
|
required SettingsStore settingsStore,
|
|
required WalletBase wallet,
|
|
bool isTestEnvironment = false,
|
|
}) : baseSellUrl = isTestEnvironment ? _baseSellTestUrl : _baseSellProductUrl,
|
|
baseBuyUrl = isTestEnvironment ? _baseBuyTestUrl : _baseBuyProductUrl,
|
|
this._settingsStore = settingsStore,
|
|
super(wallet: wallet, isTestEnvironment: isTestEnvironment);
|
|
|
|
final SettingsStore _settingsStore;
|
|
|
|
static const _baseSellTestUrl = 'sb.fluidmoney.xyz';
|
|
static const _baseSellProductUrl = 'fluidmoney.xyz';
|
|
static const _baseBuyTestUrl = 'sb.fluidmoney.xyz';
|
|
static const _baseBuyProductUrl = 'fluidmoney.xyz';
|
|
|
|
final String baseBuyUrl;
|
|
final String baseSellUrl;
|
|
|
|
@override
|
|
String get providerDescription =>
|
|
'Buy crypto with many payment providers like Stripe and PayPal. Available in most countries. Spreads and feed vary.';
|
|
|
|
@override
|
|
String get providerSellDescription =>
|
|
'Sell crypto with many payment providers like Stripe and PayPal. Available in most countries. Spreads and feed vary.';
|
|
|
|
@override
|
|
String get title => 'Meld';
|
|
|
|
@override
|
|
String get lightIcon => 'assets/images/meld_dark.png';
|
|
|
|
@override
|
|
String get darkIcon => 'assets/images/meld_dark.png';
|
|
|
|
String get currencyCode => walletTypeToCryptoCurrency(wallet.type).title.toLowerCase();
|
|
|
|
static String get _exchangeHelperApiKey => secrets.exchangeHelperApiKey;
|
|
|
|
static String convertTheme(ThemeBase theme) {
|
|
switch (theme.type) {
|
|
case ThemeType.bright:
|
|
case ThemeType.light:
|
|
return 'lightMode';
|
|
case ThemeType.dark:
|
|
return 'darkMode';
|
|
}
|
|
}
|
|
|
|
Future<Uri> requestSellUrl({
|
|
required CryptoCurrency currency,
|
|
required String walletAddress,
|
|
required SettingsStore settingsStore,
|
|
String? amount,
|
|
}) async {
|
|
// try {
|
|
// final headers = {
|
|
// 'Meld-Version': '2023-12-19',
|
|
// 'Content-Type': 'application/json',
|
|
// 'Authorization': 'BASIC ${secrets.meldApiKey}',
|
|
// };
|
|
// final body = {
|
|
// "sessionData": {
|
|
// "walletAddress": walletAddress,
|
|
// "countryCode": _normalizeCountryCode(settingsStore.fiatCurrency.countryCode),
|
|
// "destinationCurrencyCode": settingsStore.fiatCurrency.raw,
|
|
// "sourceAmount": amount ?? '60',
|
|
// "sourceCurrencyCode": currencyCode.toUpperCase(),
|
|
// "serviceProvider": "STRIPE"
|
|
// },
|
|
// "sessionType": "SELL",
|
|
// "externalCustomerId": "testcustomer",
|
|
// };
|
|
// final response = await http.post(
|
|
// Uri.https(baseBuyUrl, _buyWidgetSuffix),
|
|
// headers: headers,
|
|
// body: json.encode(body),
|
|
// );
|
|
// print(response.body);
|
|
// return Uri.parse(json.decode(response.body)["widgetUrl"] as String);
|
|
// } catch (e) {
|
|
// print(e);
|
|
// rethrow;
|
|
// }
|
|
final params = {
|
|
"publicKey": secrets.meldApiKey,
|
|
"theme": convertTheme(settingsStore.currentTheme),
|
|
"sourceCurrencyCode": currencyCode.toUpperCase(),
|
|
"destinationCurrencyCode": settingsStore.fiatCurrency.raw,
|
|
"sourceAmount": amount ?? '100',
|
|
"walletAddress": walletAddress,
|
|
};
|
|
|
|
final uri = Uri.https(baseBuyUrl, '', params);
|
|
return uri;
|
|
}
|
|
|
|
Future<Uri> requestBuyUrl({
|
|
required CryptoCurrency currency,
|
|
required SettingsStore settingsStore,
|
|
required String walletAddress,
|
|
String? amount,
|
|
}) async {
|
|
final params = {
|
|
"publicKey": secrets.meldApiKey,
|
|
"theme": convertTheme(settingsStore.currentTheme),
|
|
"destinationCurrencyCode": currencyCode.toUpperCase(),
|
|
"sourceCurrencyCode": settingsStore.fiatCurrency.raw,
|
|
"sourceAmount": amount ?? '100',
|
|
"walletAddress": walletAddress,
|
|
};
|
|
|
|
final uri = Uri.https(baseBuyUrl, '', params);
|
|
return uri;
|
|
}
|
|
|
|
@override
|
|
Future<void> launchProvider(BuildContext context, bool? isBuyAction) async {
|
|
late final Uri uri;
|
|
if (isBuyAction ?? true) {
|
|
uri = await requestBuyUrl(
|
|
currency: wallet.currency,
|
|
walletAddress: wallet.walletAddresses.address,
|
|
settingsStore: _settingsStore,
|
|
);
|
|
} else {
|
|
uri = await requestSellUrl(
|
|
currency: wallet.currency,
|
|
walletAddress: wallet.walletAddresses.address,
|
|
settingsStore: _settingsStore,
|
|
);
|
|
}
|
|
|
|
if (await canLaunchUrl(uri)) {
|
|
if (DeviceInfo.instance.isMobile) {
|
|
Navigator.of(context).pushNamed(Routes.webViewPage, arguments: ['Meld', uri]);
|
|
} else {
|
|
await launchUrl(uri, mode: LaunchMode.externalApplication);
|
|
}
|
|
} else {
|
|
throw Exception('Could not launch URL');
|
|
}
|
|
}
|
|
|
|
// normalize country codes to ISO-3166:
|
|
String _normalizeCountryCode(String countryCode) {
|
|
countryCode = countryCode.toLowerCase();
|
|
switch (countryCode) {
|
|
case "usa":
|
|
default:
|
|
return "US";
|
|
}
|
|
}
|
|
}
|