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

Function currencyrate

plugins/currencyrate-plugin/src/main.rs:185–245  ·  view source on GitHub ↗
(plugin: Plugin<PluginState>, args: Value)

Source from the content-addressed store, hash-verified

183}
184
185async fn currencyrate(plugin: Plugin<PluginState>, args: Value) -> Result<Value, anyhow::Error> {
186 let (currency, source) = match args {
187 Value::Array(values) => {
188 if values.len() > 2 {
189 return Err(anyhow!(
190 "Too many arguments: Expected at most 2 arguments, got {}",
191 values.len()
192 ));
193 }
194 let currency = values
195 .first()
196 .ok_or_else(|| anyhow!("Missing currency"))?
197 .as_str()
198 .ok_or_else(|| anyhow!("currency must be a string"))?
199 .to_uppercase();
200 let source = values.get(1).and_then(|v| {
201 v.as_str()
202 .map(str::to_owned)
203 .or_else(|| v.as_number().map(std::string::ToString::to_string))
204 });
205 (currency, source)
206 }
207 Value::Object(map) => {
208 if map.len() > 2 {
209 return Err(anyhow!(
210 "Too many arguments: Expected at most 2 arguments, got {}",
211 map.len()
212 ));
213 }
214 let currency = map
215 .get("currency")
216 .ok_or_else(|| anyhow!("Missing currency"))?
217 .as_str()
218 .ok_or_else(|| anyhow!("currency must be a string"))?
219 .to_uppercase();
220 let source = map.get("source").and_then(|v| {
221 v.as_str()
222 .map(str::to_owned)
223 .or_else(|| v.as_number().map(std::string::ToString::to_string))
224 });
225 (currency, source)
226 }
227 _ => return Err(anyhow!("Arguments must be an array or dictionary")),
228 };
229
230 let oracle = plugin.state().oracle.lock().await;
231 oracle.currency_requested(&currency).await;
232
233 let rate = match source {
234 Some(source_name) => oracle
235 .get_source_rate(&currency, &source_name)
236 .await
237 .map_err(|e| anyhow!("Error getting rate from source: {e}"))?,
238 None => oracle
239 .get_median_rate(&currency)
240 .await
241 .map_err(|e| anyhow!("Error getting median rate: {e}"))?,
242 };

Callers

nothing calls this directly

Calls 7

as_strMethod · 0.80
mapMethod · 0.80
stateMethod · 0.80
currency_requestedMethod · 0.80
get_source_rateMethod · 0.80
get_median_rateMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected