Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit f772d50

Browse files
committed
node info endpoint
1 parent 1f0207c commit f772d50

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

mutiny-server/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ async fn main() -> anyhow::Result<()> {
7474
.route("/createinvoice", post(routes::create_invoice))
7575
.route("/payinvoice", post(routes::pay_invoice))
7676
.route("/balance", get(routes::get_balance))
77+
.route("/getinfo", get(routes::get_node_info))
7778
.fallback(fallback)
7879
.layer(Extension(state.clone()));
7980

mutiny-server/src/routes.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,81 @@ pub async fn get_balance(
244244
Ok(Json(json!(balance)))
245245
}
246246

247+
#[derive(Serialize)]
248+
#[serde(rename_all = "camelCase")]
249+
pub struct NodeInfoResponse {
250+
node_id: String,
251+
channels: Vec<Channel>,
252+
}
253+
254+
#[derive(Serialize)]
255+
#[serde(rename_all = "camelCase")]
256+
struct Channel {
257+
state: String,
258+
channel_id: String,
259+
balance_sat: u64,
260+
inbound_liquidity_sat: u64,
261+
capacity_sat: u64,
262+
funding_tx_id: Option<String>,
263+
}
264+
265+
pub async fn get_node_info(
266+
Extension(state): Extension<State>,
267+
) -> Result<Json<Value>, (StatusCode, Json<Value>)> {
268+
let nodes = state
269+
.mutiny_wallet
270+
.node_manager
271+
.list_nodes()
272+
.await
273+
.map_err(|e| handle_mutiny_err(e))?;
274+
let node_pubkey: PublicKey;
275+
if !nodes.is_empty() {
276+
node_pubkey = nodes[0];
277+
} else {
278+
return Err((
279+
StatusCode::INTERNAL_SERVER_ERROR,
280+
Json(json!({"error": "unable to get node info"})),
281+
));
282+
}
283+
284+
let channels = state
285+
.mutiny_wallet
286+
.node_manager
287+
.list_channels()
288+
.await
289+
.map_err(|e| handle_mutiny_err(e))?;
290+
let channels = channels
291+
.into_iter()
292+
.map(|channel| {
293+
let state = match channel.is_usable {
294+
true => "usable",
295+
false => "unusable",
296+
}
297+
.to_string();
298+
let funding_tx_id = match channel.outpoint {
299+
Some(outpoint) => Some(outpoint.txid.to_string()),
300+
None => None,
301+
};
302+
303+
Channel {
304+
state,
305+
channel_id: channel.user_chan_id,
306+
balance_sat: channel.balance,
307+
inbound_liquidity_sat: channel.inbound,
308+
capacity_sat: channel.size,
309+
funding_tx_id,
310+
}
311+
})
312+
.collect();
313+
314+
let node_info = NodeInfoResponse {
315+
node_id: node_pubkey.to_string(),
316+
channels,
317+
};
318+
319+
Ok(Json(json!(node_info)))
320+
}
321+
247322
fn handle_mutiny_err(err: MutinyError) -> (StatusCode, Json<Value>) {
248323
let err = json!({
249324
"error": format!("{err}"),

mutiny-server/src/sled.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub struct SledStorage {
1616
pub cipher: Option<Cipher>,
1717
db: sled::Db,
1818
delayed_keys: Arc<Mutex<HashMap<String, DelayedKeyValueItem>>>,
19+
activity_index: Arc<RwLock<BTreeSet<IndexItem>>>,
1920
}
2021

2122
impl SledStorage {
@@ -41,6 +42,7 @@ impl SledStorage {
4142
cipher,
4243
db,
4344
delayed_keys: Arc::new(Mutex::new(HashMap::new())),
45+
activity_index: Arc::new(RwLock::new(BTreeSet::new())),
4446
})
4547
}
4648
}
@@ -65,7 +67,7 @@ impl MutinyStorage for SledStorage {
6567
}
6668

6769
fn activity_index(&self) -> Arc<RwLock<BTreeSet<IndexItem>>> {
68-
Arc::new(RwLock::new(BTreeSet::new()))
70+
self.activity_index.clone()
6971
}
7072

7173
fn set(&self, items: Vec<(String, impl Serialize)>) -> Result<(), MutinyError> {

0 commit comments

Comments
 (0)