only fetch prices for enabled coins

This commit is contained in:
julian 2024-05-24 13:40:20 -06:00
parent a3a1ddeeaf
commit f1b0e4e119

View file

@ -13,16 +13,40 @@ import 'dart:convert';
import 'package:decimal/decimal.dart'; import 'package:decimal/decimal.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:tuple/tuple.dart';
import '../app_config.dart'; import '../app_config.dart';
import '../db/hive/db.dart'; import '../db/hive/db.dart';
import '../networking/http.dart'; import '../networking/http.dart';
import 'tor_service.dart';
import '../utilities/logger.dart'; import '../utilities/logger.dart';
import '../utilities/prefs.dart'; import '../utilities/prefs.dart';
import '../wallets/crypto_currency/crypto_currency.dart'; import '../wallets/crypto_currency/crypto_currency.dart';
import 'package:tuple/tuple.dart'; import 'tor_service.dart';
class PriceAPI { class PriceAPI {
// coingecko coin ids
static const Map<Type, String> _coinToIdMap = {
Bitcoin: "bitcoin",
BitcoinFrost: "bitcoin",
Litecoin: "litecoin",
Bitcoincash: "bitcoin-cash",
Dogecoin: "dogecoin",
Epiccash: "epic-cash",
Ecash: "ecash",
Ethereum: "ethereum",
Firo: "zcoin",
Monero: "monero",
Particl: "particl",
Peercoin: "peercoin",
Solana: "solana",
Stellar: "stellar",
Tezos: "tezos",
Wownero: "wownero",
Namecoin: "namecoin",
Nano: "nano",
Banano: "banano",
};
static const refreshInterval = 60; static const refreshInterval = 60;
// initialize to older than current time minus at least refreshInterval // initialize to older than current time minus at least refreshInterval
@ -83,6 +107,12 @@ class PriceAPI {
return result; return result;
} }
String get _coinIds => AppConfig.coins
.where((e) => e.network == CryptoCurrencyNetwork.main)
.map((e) => _coinToIdMap[e.runtimeType])
.where((e) => e != null)
.join(",");
Future<Map<CryptoCurrency, Tuple2<Decimal, double>>> getPricesAnd24hChange({ Future<Map<CryptoCurrency, Tuple2<Decimal, double>>> getPricesAnd24hChange({
required String baseCurrency, required String baseCurrency,
}) async { }) async {
@ -108,10 +138,9 @@ class PriceAPI {
try { try {
final uri = Uri.parse( final uri = Uri.parse(
"https://api.coingecko.com/api/v3/coins/markets?vs_currency" "https://api.coingecko.com/api/v3/coins/markets?vs_currency"
"=${baseCurrency.toLowerCase()}" "=${baseCurrency.toLowerCase()}&ids=$_coinIds&order=market_cap_desc"
"&ids=monero,bitcoin,litecoin,ecash,epic-cash,zcoin,dogecoin," "&per_page=50&page=1&sparkline=false",
"bitcoin-cash,namecoin,wownero,ethereum,particl,nano,banano,stellar,tezos,solana" );
"&order=market_cap_desc&per_page=50&page=1&sparkline=false");
final coinGeckoResponse = await client.get( final coinGeckoResponse = await client.get(
url: uri, url: uri,
@ -191,7 +220,8 @@ class PriceAPI {
}) async { }) async {
final Map<String, Tuple2<Decimal, double>> tokenPrices = {}; final Map<String, Tuple2<Decimal, double>> tokenPrices = {};
if (contractAddresses.isEmpty) return tokenPrices; if (AppConfig.coins.whereType<Ethereum>().isEmpty ||
contractAddresses.isEmpty) return tokenPrices;
final externalCalls = Prefs.instance.externalCalls; final externalCalls = Prefs.instance.externalCalls;
if ((!Logger.isTestEnv && !externalCalls) || if ((!Logger.isTestEnv && !externalCalls) ||