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

Method get_rate

plugins/currencyrate-plugin/src/oracle.rs:63–116  ·  view source on GitHub ↗
(
        &self,
        proxy: Option<bitreq::Proxy>,
        client: &Client,
        currency: &str,
    )

Source from the content-addressed store, hash-verified

61 }
62
63 async fn get_rate(
64 &self,
65 proxy: Option<bitreq::Proxy>,
66 client: &Client,
67 currency: &str,
68 ) -> Result<f64, anyhow::Error> {
69 let now = Instant::now();
70
71 let currency_lc = currency.to_lowercase();
72 let currency = currency.to_uppercase();
73 let url = self.url(&currency_lc, &currency);
74
75 let mut request = bitreq::get(&url)
76 .with_header("User-Agent", "cln-currencyrate")
77 .with_timeout(SOURCE_TIMEOUT_SECS)
78 .with_max_redirects(10);
79
80 if let Some(tp) = proxy {
81 request = request.with_proxy(tp);
82 }
83
84 let raw_resp: bitreq::Response = client.send_async(request).await?;
85
86 if raw_resp.status_code < 200 || raw_resp.status_code > 299 {
87 return Err(anyhow!(
88 "HTTP error {} from {url}: body={}",
89 raw_resp.status_code,
90 body_str(&raw_resp)
91 ));
92 }
93
94 let resp: Value = match raw_resp.json() {
95 Ok(resp) => resp,
96 Err(e) => {
97 return Err(anyhow!(
98 "Could not decode: {} as json: {e}",
99 body_str(&raw_resp)
100 ));
101 }
102 };
103
104 let reply_members = self.reply_members(&currency_lc, &currency);
105
106 let price = extract_price_from_response(&resp, &reply_members, self.name(), &currency)?;
107
108 log::info!(
109 "Fetched price in {}ms from {}: {:.2} {currency}/BTC",
110 now.elapsed().as_millis(),
111 self.name,
112 price
113 );
114
115 Ok(price)
116 }
117}
118
119fn body_str(body: &bitreq::Response) -> String {

Callers 2

refresh_currencyMethod · 0.80
background_refreshMethod · 0.80

Calls 5

urlMethod · 0.80
jsonMethod · 0.80
reply_membersMethod · 0.80
nameMethod · 0.45

Tested by

no test coverage detected