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

Function currencyconvert

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

Source from the content-addressed store, hash-verified

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