Skip to content

Commit

Permalink
node info endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
elnosh committed Apr 11, 2024
1 parent 3223464 commit f2e73b0
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
1 change: 1 addition & 0 deletions mutiny-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ async fn main() -> anyhow::Result<()> {
.route("/createinvoice", post(routes::create_invoice))
.route("/payinvoice", post(routes::pay_invoice))
.route("/balance", get(routes::get_balance))
.route("/getinfo", get(routes::get_node_info))
.fallback(fallback)
.layer(Extension(state.clone()));

Expand Down
75 changes: 75 additions & 0 deletions mutiny-server/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,81 @@ pub async fn get_balance(
Ok(Json(json!(balance)))
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NodeInfoResponse {
node_id: String,
channels: Vec<Channel>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct Channel {
state: String,
channel_id: String,
balance_sat: u64,
inbound_liquidity_sat: u64,
capacity_sat: u64,
funding_tx_id: Option<String>,
}

pub async fn get_node_info(
Extension(state): Extension<State>,
) -> Result<Json<Value>, (StatusCode, Json<Value>)> {
let nodes = state
.mutiny_wallet
.node_manager
.list_nodes()
.await
.map_err(|e| handle_mutiny_err(e))?;
let node_pubkey: PublicKey;
if !nodes.is_empty() {
node_pubkey = nodes[0];
} else {
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"error": "unable to get node info"})),
));
}

let channels = state
.mutiny_wallet
.node_manager
.list_channels()
.await
.map_err(|e| handle_mutiny_err(e))?;
let channels = channels
.into_iter()
.map(|channel| {
let state = match channel.is_usable {
true => "usable",
false => "unusable",
}
.to_string();
let funding_tx_id = match channel.outpoint {
Some(outpoint) => Some(outpoint.txid.to_string()),
None => None,
};

Channel {
state,
channel_id: channel.user_chan_id,
balance_sat: channel.balance,
inbound_liquidity_sat: channel.inbound,
capacity_sat: channel.size,
funding_tx_id,
}
})
.collect();

let node_info = NodeInfoResponse {
node_id: node_pubkey.to_string(),
channels,
};

Ok(Json(json!(node_info)))
}

fn handle_mutiny_err(err: MutinyError) -> (StatusCode, Json<Value>) {
let err = json!({
"error": format!("{err}"),
Expand Down
4 changes: 3 additions & 1 deletion mutiny-server/src/sled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct SledStorage {
pub cipher: Option<Cipher>,
db: sled::Db,
delayed_keys: Arc<Mutex<HashMap<String, DelayedKeyValueItem>>>,
activity_index: Arc<RwLock<BTreeSet<IndexItem>>>,
}

impl SledStorage {
Expand All @@ -41,6 +42,7 @@ impl SledStorage {
cipher,
db,
delayed_keys: Arc::new(Mutex::new(HashMap::new())),
activity_index: Arc::new(RwLock::new(BTreeSet::new())),
})
}
}
Expand All @@ -65,7 +67,7 @@ impl MutinyStorage for SledStorage {
}

fn activity_index(&self) -> Arc<RwLock<BTreeSet<IndexItem>>> {
Arc::new(RwLock::new(BTreeSet::new()))
self.activity_index.clone()
}

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

0 comments on commit f2e73b0

Please sign in to comment.