From 6ed07a504e5e0010bf69135bbf51198789cbe5ad Mon Sep 17 00:00:00 2001 From: Serhii Date: Fri, 11 Apr 2025 04:23:31 +0300 Subject: [PATCH] add caching for supported assets (#2184) --- lib/buy/robinhood/robinhood_buy_provider.dart | 46 ++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/lib/buy/robinhood/robinhood_buy_provider.dart b/lib/buy/robinhood/robinhood_buy_provider.dart index 90177ec61..93efd5642 100644 --- a/lib/buy/robinhood/robinhood_buy_provider.dart +++ b/lib/buy/robinhood/robinhood_buy_provider.dart @@ -35,9 +35,16 @@ class RobinhoodBuyProvider extends BuyProvider { static const _baseUrl = 'applink.robinhood.com'; static const _cIdBaseUrl = 'exchange-helper.cakewallet.com'; + static const _apiBaseUrl = 'api.robinhood.com'; + static const _assetsPath = '/catpay/v1/supported_currencies/'; - static const List _notSupportedCrypto = []; - static const List _notSupportedFiat = []; + static List _supportedCrypto = []; + static final List _supportedFiat = [FiatCurrency.usd]; + + static final List _notSupportedCrypto = []; + + static final List _notSupportedFiat = + FiatCurrency.all.where((fiat) => !_supportedFiat.contains(fiat)).toList(); @override String get title => 'Robinhood Connect'; @@ -58,6 +65,38 @@ class RobinhoodBuyProvider extends BuyProvider { String get _apiSecret => secrets.exchangeHelperApiKey; + Future> getSupportedAssets() async { + final uri = Uri.https(_apiBaseUrl, '$_assetsPath', {'applicationId': _applicationId}); + + try { + final response = await http.get(uri, headers: {'accept': 'application/json'}); + + if (response.statusCode == 200) { + final responseData = jsonDecode(response.body) as Map; + final pairs = responseData['cryptoCurrencyPairs'] as List; + + final supportedAssets = []; + + for (final item in pairs) { + String code = item['assetCurrency']['code'] as String; + if (code == 'AVAX') code = 'AVAXC'; + try { + final currency = CryptoCurrency.fromString(code); + supportedAssets.add(currency); + } catch (e) { + log('Robinhood: Unknown asset code "$code" - skipped'); + } + } + return supportedAssets; + } else { + return []; + } + } catch (e) { + log('Robinhood: Failed to fetch supported assets: $e'); + return []; + } + } + Future getSignature(String message) async { switch (wallet.type) { case WalletType.ethereum: @@ -156,6 +195,9 @@ class RobinhoodBuyProvider extends BuyProvider { String? countryCode}) async { String? paymentMethod; + if (_supportedCrypto.isEmpty) _supportedCrypto = await getSupportedAssets(); + if (_supportedCrypto.isNotEmpty && !(_supportedCrypto.contains(cryptoCurrency))) return null; + if (paymentType != null && paymentType != PaymentType.all) { paymentMethod = normalizePaymentMethod(paymentType); if (paymentMethod == null) return null;