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

Method get_rate

plugins/currencyrate-plugin/src/oracle.rs:66–134  ·  view source on GitHub ↗
(&self, client: &Client, currency: &str)

Source from the content-addressed store, hash-verified

64 }
65
66 async fn get_rate(&self, client: &Client, currency: &str) -> Result<f64, anyhow::Error> {
67 let now = Instant::now();
68
69 let currency_lc = currency.to_lowercase();
70 let currency = currency.to_uppercase();
71 let url = self.url(&currency_lc, &currency);
72
73 let resp: Value = client
74 .get(&url)
75 .send()
76 .await
77 .map_err(|e| anyhow!("Failed to request url {url} caused by: {:?}", e.source()))?
78 .json()
79 .await
80 .map_err(|e| {
81 anyhow!(
82 "Failed to decode response body from {url}, caused by: {:?}",
83 e.source()
84 )
85 })?;
86
87 let reply_members = self.reply_members(&currency_lc, &currency);
88
89 let mut current = &mut resp.clone();
90 for member in reply_members {
91 if let Ok(pos) = member.parse::<usize>() {
92 current = current.get_mut(pos).ok_or(anyhow!(
93 "Positional member `{}` not found in {} response: {}",
94 member,
95 self.name(),
96 resp
97 ))?;
98 } else {
99 current = current.get_mut(&member).ok_or(anyhow!(
100 "Member `{}` not found in {} response: {}",
101 member,
102 self.name(),
103 resp
104 ))?;
105 }
106 }
107 let price = match current {
108 Value::Number(number) => number
109 .as_f64()
110 .ok_or(anyhow!("Json number price could not be converted to float"))?,
111 Value::String(string) => string
112 .parse::<f64>()
113 .map_err(|e| anyhow!("Price string could not be converted to float: {e}"))?,
114 _ => return Err(anyhow!("Price is invalid json type")),
115 };
116
117 if price == 0.0 {
118 log::warn!("{} returned 0.0 as price for {}", self.name, currency);
119 return Err(anyhow!(
120 "{} returned 0.0 as price for {}",
121 self.name,
122 currency
123 ));

Callers 2

refresh_currencyMethod · 0.80
background_refreshMethod · 0.80

Calls 5

urlMethod · 0.80
jsonMethod · 0.80
reply_membersMethod · 0.80
sendMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected