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

Function currencyconvert

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

Source from the content-addressed store, hash-verified

127}
128
129async fn currencyconvert(plugin: Plugin<PluginState>, args: Value) -> Result<Value, anyhow::Error> {
130 let (amount, currency) = match args {
131 Value::Array(values) => {
132 if values.len() > 2 {
133 return Err(anyhow!(
134 "Too many arguments: Expected 2 arguments, got {}",
135 values.len()
136 ));
137 }
138 let amount = values
139 .first()
140 .ok_or_else(|| anyhow!("Missing amount"))?
141 .as_f64()
142 .ok_or_else(|| anyhow!("amount must be a number"))?;
143 let currency = values
144 .get(1)
145 .ok_or_else(|| anyhow!("Missing currency"))?
146 .as_str()
147 .ok_or_else(|| anyhow!("currency must be a string"))?
148 .to_owned();
149 (amount, currency.to_uppercase())
150 }
151 Value::Object(map) => {
152 if map.len() > 2 {
153 return Err(anyhow!(
154 "Too many arguments: Expected 2 arguments, got {}",
155 map.len()
156 ));
157 }
158 let amount = map
159 .get("amount")
160 .ok_or_else(|| anyhow!("Missing amount"))?
161 .as_f64()
162 .ok_or_else(|| anyhow!("amount must be a number"))?;
163 let currency = map
164 .get("currency")
165 .ok_or_else(|| anyhow!("Missing currency"))?
166 .as_str()
167 .ok_or_else(|| anyhow!("currency must be a string"))?
168 .to_owned();
169 (amount, currency.to_uppercase())
170 }
171 _ => return Err(anyhow!("Arguments must be an array or dictionary")),
172 };
173
174 let oracle = plugin.state().oracle.lock().await;
175 oracle.currency_requested(&currency).await;
176
177 match oracle.get_median_rate(&currency).await {
178 Ok(rate) => Ok(json!({
179 "msat": (amount * MSAT_PER_BTC / rate).round() as u64,
180 })),
181 Err(e) => Err(anyhow!("Error converting currency: {e}")),
182 }
183}
184
185async fn currencyrate(plugin: Plugin<PluginState>, args: Value) -> Result<Value, anyhow::Error> {
186 let (currency, source) = match args {

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected