From 23b34654726d096299f165b174ffbc0a81aac8f8 Mon Sep 17 00:00:00 2001 From: aglkm <39521015+aglkm@users.noreply.github.com> Date: Mon, 19 Jan 2026 23:33:25 +0300 Subject: [PATCH] reduce the number of coingecko api calls --- src/requests.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/requests.rs b/src/requests.rs index e3548aa..119d4ab 100644 --- a/src/requests.rs +++ b/src/requests.rs @@ -1,11 +1,12 @@ +use chrono::{Utc, DateTime}; +use fs_extra::dir::get_size; +use humantime::format_duration; +use num_format::{Locale, ToFormattedString}; use reqwest::Error; use serde_json::Value; use std::sync::{Arc, Mutex}; -use num_format::{Locale, ToFormattedString}; -use fs_extra::dir::get_size; -use humantime::format_duration; +use std::sync::atomic::{AtomicU32, Ordering}; use std::time::Duration; -use chrono::{Utc, DateTime}; use std::collections::HashMap; use crate::data::{Block, Dashboard, Kernel, Output, Statistics, Transactions}; @@ -194,7 +195,12 @@ pub async fn get_market(dashboard: Arc>) -> Result<(), anyhow:: let result; let mut val = Value::Null; - if CONFIG.coingecko_api == "enabled" { + static COINGECKO_COUNT: AtomicU32 = AtomicU32::new(0); + + let count = COINGECKO_COUNT.fetch_add(1, Ordering::Relaxed); + + // Call CG API only once every 10 calls (15sec * 10) + if CONFIG.coingecko_api == "enabled" && count % 10 == 0 { client = reqwest::Client::new(); result = client.get("https://api.coingecko.com/api/v3/simple/price?ids=grin&vs_currencies=usd%2Cbtc&include_24hr_vol=true").send().await?; val = serde_json::from_str(&result.text().await?)?;