2023-09-14 19:14:49 +00:00
|
|
|
import 'dart:convert';
|
2024-11-09 19:00:56 +00:00
|
|
|
import 'dart:developer';
|
2023-09-14 19:14:49 +00:00
|
|
|
|
|
|
|
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
2023-12-28 19:20:59 +00:00
|
|
|
import 'package:cake_wallet/buy/buy_provider.dart';
|
2024-11-09 19:00:56 +00:00
|
|
|
import 'package:cake_wallet/buy/buy_quote.dart';
|
|
|
|
import 'package:cake_wallet/buy/payment_method.dart';
|
|
|
|
import 'package:cake_wallet/entities/fiat_currency.dart';
|
2023-09-14 19:14:49 +00:00
|
|
|
import 'package:cake_wallet/generated/i18n.dart';
|
2024-05-05 01:44:50 +00:00
|
|
|
import 'package:cake_wallet/routes.dart';
|
|
|
|
import 'package:cake_wallet/src/screens/connect_device/connect_device_page.dart';
|
2023-09-14 19:14:49 +00:00
|
|
|
import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
|
|
|
|
import 'package:cake_wallet/utils/show_pop_up.dart';
|
2024-05-05 01:44:50 +00:00
|
|
|
import 'package:cake_wallet/view_model/hardware_wallet/ledger_view_model.dart';
|
2024-11-09 19:00:56 +00:00
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
2024-12-09 18:23:59 +00:00
|
|
|
import 'package:cw_core/utils/print_verbose.dart';
|
2023-09-14 19:14:49 +00:00
|
|
|
import 'package:cw_core/wallet_base.dart';
|
|
|
|
import 'package:cw_core/wallet_type.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
|
2024-01-01 13:05:37 +00:00
|
|
|
class RobinhoodBuyProvider extends BuyProvider {
|
2024-11-09 19:00:56 +00:00
|
|
|
RobinhoodBuyProvider(
|
|
|
|
{required WalletBase wallet, bool isTestEnvironment = false, LedgerViewModel? ledgerVM})
|
2024-05-05 01:44:50 +00:00
|
|
|
: super(wallet: wallet, isTestEnvironment: isTestEnvironment, ledgerVM: ledgerVM);
|
2023-09-14 19:14:49 +00:00
|
|
|
|
|
|
|
static const _baseUrl = 'applink.robinhood.com';
|
|
|
|
static const _cIdBaseUrl = 'exchange-helper.cakewallet.com';
|
|
|
|
|
2023-12-28 19:20:59 +00:00
|
|
|
@override
|
|
|
|
String get title => 'Robinhood Connect';
|
|
|
|
|
|
|
|
@override
|
2024-01-01 13:05:37 +00:00
|
|
|
String get providerDescription => S.current.robinhood_option_description;
|
2023-12-28 19:20:59 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
String get lightIcon => 'assets/images/robinhood_light.png';
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get darkIcon => 'assets/images/robinhood_dark.png';
|
|
|
|
|
2024-11-09 19:00:56 +00:00
|
|
|
@override
|
|
|
|
bool get isAggregator => false;
|
|
|
|
|
2023-09-14 19:14:49 +00:00
|
|
|
String get _applicationId => secrets.robinhoodApplicationId;
|
2023-11-17 18:37:32 +00:00
|
|
|
|
2024-03-25 18:16:57 +00:00
|
|
|
String get _apiSecret => secrets.exchangeHelperApiKey;
|
2023-09-14 19:14:49 +00:00
|
|
|
|
2024-08-17 23:10:27 +00:00
|
|
|
Future<String> getSignature(String message) async {
|
2023-12-28 19:20:59 +00:00
|
|
|
switch (wallet.type) {
|
2023-09-14 19:14:49 +00:00
|
|
|
case WalletType.ethereum:
|
2024-03-25 18:16:57 +00:00
|
|
|
case WalletType.polygon:
|
2024-08-17 23:10:27 +00:00
|
|
|
return await wallet.signMessage(message);
|
2023-09-14 19:14:49 +00:00
|
|
|
case WalletType.litecoin:
|
|
|
|
case WalletType.bitcoin:
|
2023-11-17 18:37:32 +00:00
|
|
|
case WalletType.bitcoinCash:
|
2024-08-17 23:10:27 +00:00
|
|
|
return await wallet.signMessage(message, address: wallet.walletAddresses.address);
|
2023-09-14 19:14:49 +00:00
|
|
|
default:
|
2023-12-28 19:20:59 +00:00
|
|
|
throw Exception("WalletType is not available for Robinhood ${wallet.type}");
|
2023-09-14 19:14:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> getConnectId() async {
|
2023-12-28 19:20:59 +00:00
|
|
|
final walletAddress = wallet.walletAddresses.address;
|
2023-09-14 19:14:49 +00:00
|
|
|
final valid_until = (DateTime.now().millisecondsSinceEpoch / 1000).round() + 10;
|
|
|
|
final message = "$_apiSecret:${valid_until}";
|
|
|
|
|
2024-05-05 01:44:50 +00:00
|
|
|
final signature = await getSignature(message);
|
2023-09-14 19:14:49 +00:00
|
|
|
|
|
|
|
final uri = Uri.https(_cIdBaseUrl, "/api/robinhood");
|
|
|
|
|
|
|
|
var response = await http.post(uri,
|
|
|
|
headers: {'Content-Type': 'application/json'},
|
|
|
|
body: json
|
|
|
|
.encode({'valid_until': valid_until, 'wallet': walletAddress, 'signature': signature}));
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
return (jsonDecode(response.body) as Map<String, dynamic>)['connectId'] as String;
|
|
|
|
} else {
|
2023-11-17 18:37:32 +00:00
|
|
|
throw Exception(
|
|
|
|
'Provider currently unavailable. Status: ${response.statusCode} ${response.body}');
|
2023-09-14 19:14:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 19:20:59 +00:00
|
|
|
Future<Uri> requestProviderUrl() async {
|
2023-09-14 19:14:49 +00:00
|
|
|
final connectId = await getConnectId();
|
2023-12-28 19:20:59 +00:00
|
|
|
final networkName = wallet.currency.fullName?.toUpperCase().replaceAll(" ", "_");
|
2023-09-14 19:14:49 +00:00
|
|
|
|
|
|
|
return Uri.https(_baseUrl, '/u/connect', <String, dynamic>{
|
|
|
|
'applicationId': _applicationId,
|
|
|
|
'connectId': connectId,
|
2023-12-28 19:20:59 +00:00
|
|
|
'walletAddress': wallet.walletAddresses.address,
|
|
|
|
'userIdentifier': wallet.walletAddresses.address,
|
2023-09-14 19:14:49 +00:00
|
|
|
'supportedNetworks': networkName
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-11-09 19:00:56 +00:00
|
|
|
Future<void>? launchProvider(
|
|
|
|
{required BuildContext context,
|
|
|
|
required Quote quote,
|
|
|
|
required double amount,
|
|
|
|
required bool isBuyAction,
|
|
|
|
required String cryptoCurrencyAddress,
|
|
|
|
String? countryCode}) async {
|
2024-05-05 01:44:50 +00:00
|
|
|
if (wallet.isHardwareWallet) {
|
|
|
|
if (!ledgerVM!.isConnected) {
|
|
|
|
await Navigator.of(context).pushNamed(Routes.connectDevices,
|
|
|
|
arguments: ConnectDevicePageParams(
|
|
|
|
walletType: wallet.walletInfo.type,
|
|
|
|
onConnectDevice: (BuildContext context, LedgerViewModel ledgerVM) {
|
|
|
|
ledgerVM.setLedger(wallet);
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
ledgerVM!.setLedger(wallet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 19:14:49 +00:00
|
|
|
try {
|
2023-12-28 19:20:59 +00:00
|
|
|
final uri = await requestProviderUrl();
|
2023-09-14 19:14:49 +00:00
|
|
|
await launchUrl(uri, mode: LaunchMode.externalApplication);
|
2023-11-17 18:37:32 +00:00
|
|
|
} catch (_) {
|
2023-09-14 19:14:49 +00:00
|
|
|
await showPopUp<void>(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return AlertWithOneAction(
|
|
|
|
alertTitle: "Robinhood Connect",
|
|
|
|
alertContent: S.of(context).buy_provider_unavailable,
|
|
|
|
buttonText: S.of(context).ok,
|
|
|
|
buttonAction: () => Navigator.of(context).pop());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2024-11-09 19:00:56 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<List<Quote>?> fetchQuote(
|
|
|
|
{required CryptoCurrency cryptoCurrency,
|
|
|
|
required FiatCurrency fiatCurrency,
|
|
|
|
required double amount,
|
|
|
|
required bool isBuyAction,
|
|
|
|
required String walletAddress,
|
|
|
|
PaymentType? paymentType,
|
|
|
|
String? countryCode}) async {
|
|
|
|
String? paymentMethod;
|
|
|
|
|
|
|
|
if (paymentType != null && paymentType != PaymentType.all) {
|
|
|
|
paymentMethod = normalizePaymentMethod(paymentType);
|
2024-11-28 19:02:10 +00:00
|
|
|
if (paymentMethod == null) return null;
|
2024-11-09 19:00:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final action = isBuyAction ? 'buy' : 'sell';
|
|
|
|
log('Robinhood: Fetching $action quote: ${isBuyAction ? cryptoCurrency.title : fiatCurrency.name.toUpperCase()} -> ${isBuyAction ? fiatCurrency.name.toUpperCase() : cryptoCurrency.title}, amount: $amount paymentMethod: $paymentMethod');
|
|
|
|
|
|
|
|
final queryParams = {
|
|
|
|
'applicationId': _applicationId,
|
|
|
|
'fiatCode': fiatCurrency.name,
|
|
|
|
'assetCode': cryptoCurrency.title,
|
|
|
|
'fiatAmount': amount.toString(),
|
|
|
|
if (paymentMethod != null) 'paymentMethod': paymentMethod,
|
|
|
|
};
|
|
|
|
|
|
|
|
final uri =
|
|
|
|
Uri.https('api.robinhood.com', '/catpay/v1/${cryptoCurrency.title}/quote/', queryParams);
|
|
|
|
|
|
|
|
try {
|
|
|
|
final response = await http.get(uri, headers: {'accept': 'application/json'});
|
|
|
|
final responseData = jsonDecode(response.body) as Map<String, dynamic>;
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
final paymentType = _getPaymentTypeByString(responseData['paymentMethod'] as String?);
|
|
|
|
final quote = Quote.fromRobinhoodJson(responseData, isBuyAction, paymentType);
|
|
|
|
quote.setFiatCurrency = fiatCurrency;
|
|
|
|
quote.setCryptoCurrency = cryptoCurrency;
|
|
|
|
return [quote];
|
|
|
|
} else {
|
|
|
|
if (responseData.containsKey('message')) {
|
|
|
|
log('Robinhood Error: ${responseData['message']}');
|
|
|
|
} else {
|
2024-12-09 18:23:59 +00:00
|
|
|
printV('Robinhood Failed to fetch $action quote: ${response.statusCode}');
|
2024-11-09 19:00:56 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log('Robinhood: Failed to fetch $action quote: $e');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-11-28 19:02:10 +00:00
|
|
|
// Supported payment methods:
|
2024-11-09 19:00:56 +00:00
|
|
|
// ● buying_power
|
|
|
|
// ● crypto_balance
|
|
|
|
// ● debit_card
|
|
|
|
// ● bank_transfer
|
|
|
|
}
|
|
|
|
|
|
|
|
String? normalizePaymentMethod(PaymentType paymentMethod) {
|
|
|
|
switch (paymentMethod) {
|
|
|
|
case PaymentType.creditCard:
|
|
|
|
return 'debit_card';
|
|
|
|
case PaymentType.debitCard:
|
|
|
|
return 'debit_card';
|
|
|
|
case PaymentType.bankTransfer:
|
|
|
|
return 'bank_transfer';
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PaymentType _getPaymentTypeByString(String? paymentMethod) {
|
|
|
|
switch (paymentMethod) {
|
|
|
|
case 'debit_card':
|
|
|
|
return PaymentType.debitCard;
|
|
|
|
case 'bank_transfer':
|
|
|
|
return PaymentType.bankTransfer;
|
|
|
|
default:
|
|
|
|
return PaymentType.all;
|
|
|
|
}
|
|
|
|
}
|
2023-09-14 19:14:49 +00:00
|
|
|
}
|