add caching for supported assets (#2184)

Serhii committed Apr 11, 2025 at 04:23 UTC 6ed07a504e5e0010bf69135bbf51198789cbe5ad
1 file changed +44 -2
lib/buy/robinhood/robinhood_buy_provider.dart
+44 -2
@@ -35,9 +35,16 @@ class RobinhoodBuyProvider extends BuyProvider {
35
36 static const _baseUrl = 'applink.robinhood.com';
37 static const _cIdBaseUrl = 'exchange-helper.cakewallet.com';
38 + static const _apiBaseUrl = 'api.robinhood.com';
39 + static const _assetsPath = '/catpay/v1/supported_currencies/';
40
39 - static const List<CryptoCurrency> _notSupportedCrypto = [];
40 - static const List<FiatCurrency> _notSupportedFiat = [];
41 + static List<CryptoCurrency> _supportedCrypto = [];
42 + static final List<FiatCurrency> _supportedFiat = [FiatCurrency.usd];
43 +
44 + static final List<CryptoCurrency> _notSupportedCrypto = [];
45 +
46 + static final List<FiatCurrency> _notSupportedFiat =
47 + FiatCurrency.all.where((fiat) => !_supportedFiat.contains(fiat)).toList();
48
49 @override
50 String get title => 'Robinhood Connect';
@@ -58,6 +65,38 @@ class RobinhoodBuyProvider extends BuyProvider {
65
66 String get _apiSecret => secrets.exchangeHelperApiKey;
67
68 + Future<List<CryptoCurrency>> getSupportedAssets() async {
69 + final uri = Uri.https(_apiBaseUrl, '$_assetsPath', {'applicationId': _applicationId});
70 +
71 + try {
72 + final response = await http.get(uri, headers: {'accept': 'application/json'});
73 +
74 + if (response.statusCode == 200) {
75 + final responseData = jsonDecode(response.body) as Map<String, dynamic>;
76 + final pairs = responseData['cryptoCurrencyPairs'] as List<dynamic>;
77 +
78 + final supportedAssets = <CryptoCurrency>[];
79 +
80 + for (final item in pairs) {
81 + String code = item['assetCurrency']['code'] as String;
82 + if (code == 'AVAX') code = 'AVAXC';
83 + try {
84 + final currency = CryptoCurrency.fromString(code);
85 + supportedAssets.add(currency);
86 + } catch (e) {
87 + log('Robinhood: Unknown asset code "$code" - skipped');
88 + }
89 + }
90 + return supportedAssets;
91 + } else {
92 + return [];
93 + }
94 + } catch (e) {
95 + log('Robinhood: Failed to fetch supported assets: $e');
96 + return [];
97 + }
98 + }
99 +
100 Future<String> getSignature(String message) async {
101 switch (wallet.type) {
102 case WalletType.ethereum:
@@ -156,6 +195,9 @@ class RobinhoodBuyProvider extends BuyProvider {
195 String? countryCode}) async {
196 String? paymentMethod;
197
198 + if (_supportedCrypto.isEmpty) _supportedCrypto = await getSupportedAssets();
199 + if (_supportedCrypto.isNotEmpty && !(_supportedCrypto.contains(cryptoCurrency))) return null;
200 +
201 if (paymentType != null && paymentType != PaymentType.all) {
202 paymentMethod = normalizePaymentMethod(paymentType);
203 if (paymentMethod == null) return null;