getWallets method

Future<List<Map<String, dynamic>>> getWallets()

Implementation

Future<List<Map<String, dynamic>>> getWallets() async {
  final currentTimestamp = DateTime.now().millisecondsSinceEpoch ~/ 1000;
  if (_cacheTtl != null &&
      _walletsListCacheCreationTimestamp != null &&
      currentTimestamp > _walletsListCacheCreationTimestamp! + _cacheTtl!) {
    _dynamicWalletsListCache = null;
  }

  if (_dynamicWalletsListCache == null) {
    List<Map<String, dynamic>> walletsList = [];
    try {
      final response = await http.get(Uri.parse(_walletsListSource));
      if (response.statusCode == 200) {
        final responseBody = jsonDecode(response.body);
        if (responseBody is List) {
          walletsList = responseBody.cast<Map<String, dynamic>>();
        } else {
          throw FetchWalletsError(
              'Wrong wallets list format, wallets list must be an array.');
        }
      } else {
        throw FetchWalletsError('Failed to fetch wallets list.');
      }
    } catch (e) {
      walletsList = fallbackWalletsList;
    }

    walletsListCache = [];
    for (final wallet in walletsList) {
      final supportedWallet = _getSupportedWalletConfig(wallet);
      if (supportedWallet != null) {
        walletsListCache.add(supportedWallet);
      }
    }

    _walletsListCacheCreationTimestamp = currentTimestamp;
  }

  return walletsListCache;
}