MCPcopy Create free account
hub / github.com/ElementsProject/lightning / get_source_rate

Method get_source_rate

plugins/currencyrate-plugin/src/oracle.rs:563–604  ·  view source on GitHub ↗
(
        &self,
        currency: &str,
        source_name: &str,
    )

Source from the content-addressed store, hash-verified

561 }
562
563 pub async fn get_source_rate(
564 &self,
565 currency: &str,
566 source_name: &str,
567 ) -> Result<f64, anyhow::Error> {
568 self.refresh_currency(currency).await?;
569
570 let inner = self.inner.lock().await;
571
572 let Some(source_health) = inner.sources.get(source_name) else {
573 // Give a helpful error if the source name is unknown entirely
574 let available = inner.sources.keys().cloned().collect::<Vec<_>>().join(", ");
575 return Err(anyhow!(
576 "Unknown source `{source_name}`. Available sources: {available}"
577 ));
578 };
579
580 let currency_cache = inner
581 .currencies
582 .get(currency)
583 .ok_or_else(|| anyhow!("No rates available for `{currency}`"))?;
584
585 let source_err = source_health
586 .last_error
587 .clone()
588 .unwrap_or_else(|| format!("{source_name} returned no result or error"));
589
590 let price_cache = currency_cache.prices.get(source_name).ok_or_else(|| {
591 anyhow!(
592 "Source `{source_name}` has no data for `{currency}`. \
593 The source may not support this currency: {source_err}"
594 )
595 })?;
596
597 if price_cache.timestamp + SERVE_TTL <= Instant::now() {
598 return Err(anyhow!(
599 "Cached rate from `{source_name}` is expired. Last source error: {source_err}"
600 ));
601 }
602
603 Ok(price_cache.price)
604 }
605}
606
607fn get_median(source_results: Vec<SourceResult>) -> f64 {

Callers 1

currencyrateFunction · 0.80

Calls 3

refresh_currencyMethod · 0.80
joinMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected