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

Function currencyrate

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

Source from the content-addressed store, hash-verified

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

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