add caching for supported assets (#2184)

This commit is contained in:
Serhii 2025-04-11 04:23:31 +03:00 committed by GitHub
parent db051232ce
commit 6ed07a504e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -35,9 +35,16 @@ class RobinhoodBuyProvider extends BuyProvider {
static const _baseUrl = 'applink.robinhood.com'; static const _baseUrl = 'applink.robinhood.com';
static const _cIdBaseUrl = 'exchange-helper.cakewallet.com'; static const _cIdBaseUrl = 'exchange-helper.cakewallet.com';
static const _apiBaseUrl = 'api.robinhood.com';
static const _assetsPath = '/catpay/v1/supported_currencies/';
static const List<CryptoCurrency> _notSupportedCrypto = []; static List<CryptoCurrency> _supportedCrypto = [];
static const List<FiatCurrency> _notSupportedFiat = []; static final List<FiatCurrency> _supportedFiat = [FiatCurrency.usd];
static final List<CryptoCurrency> _notSupportedCrypto = [];
static final List<FiatCurrency> _notSupportedFiat =
FiatCurrency.all.where((fiat) => !_supportedFiat.contains(fiat)).toList();
@override @override
String get title => 'Robinhood Connect'; String get title => 'Robinhood Connect';
@ -58,6 +65,38 @@ class RobinhoodBuyProvider extends BuyProvider {
String get _apiSecret => secrets.exchangeHelperApiKey; String get _apiSecret => secrets.exchangeHelperApiKey;
Future<List<CryptoCurrency>> 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<String, dynamic>;
final pairs = responseData['cryptoCurrencyPairs'] as List<dynamic>;
final supportedAssets = <CryptoCurrency>[];
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<String> getSignature(String message) async { Future<String> getSignature(String message) async {
switch (wallet.type) { switch (wallet.type) {
case WalletType.ethereum: case WalletType.ethereum:
@ -156,6 +195,9 @@ class RobinhoodBuyProvider extends BuyProvider {
String? countryCode}) async { String? countryCode}) async {
String? paymentMethod; String? paymentMethod;
if (_supportedCrypto.isEmpty) _supportedCrypto = await getSupportedAssets();
if (_supportedCrypto.isNotEmpty && !(_supportedCrypto.contains(cryptoCurrency))) return null;
if (paymentType != null && paymentType != PaymentType.all) { if (paymentType != null && paymentType != PaymentType.all) {
paymentMethod = normalizePaymentMethod(paymentType); paymentMethod = normalizePaymentMethod(paymentType);
if (paymentMethod == null) return null; if (paymentMethod == null) return null;