From 92ff78750e630bcf41c75367bd1394df9a510a45 Mon Sep 17 00:00:00 2001 From: Peter Neuroth Date: Mon, 30 Sep 2024 17:11:27 +0200 Subject: [PATCH] glcli: Add listpays method Adding listpays requires us to decode the input hex string. Signed-off-by: Peter Neuroth --- Cargo.lock | 1 + libs/gl-cli/Cargo.toml | 1 + libs/gl-cli/src/node.rs | 78 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 7184f5046..c5f431747 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1454,6 +1454,7 @@ dependencies = [ "env_logger 0.11.5", "futures", "gl-client", + "hex", "thiserror", "tokio 1.37.0", "vls-core", diff --git a/libs/gl-cli/Cargo.toml b/libs/gl-cli/Cargo.toml index a6a1dd37d..8f2e6e55d 100644 --- a/libs/gl-cli/Cargo.toml +++ b/libs/gl-cli/Cargo.toml @@ -13,6 +13,7 @@ dirs = "5.0.1" env_logger = "0.11.3" futures = "0.3" gl-client = { path = "../gl-client" } +hex = "0.4" thiserror = "1.0.59" tokio = "1.37.0" vls-core.workspace = true diff --git a/libs/gl-cli/src/node.rs b/libs/gl-cli/src/node.rs index 6df20ec23..2949f15b6 100644 --- a/libs/gl-cli/src/node.rs +++ b/libs/gl-cli/src/node.rs @@ -77,6 +77,17 @@ pub enum Command { #[arg(help = "The peer's port number defaults to the networks default ports if missing.")] port: Option, }, + /// List attempted payments + Listpays { + #[arg(help = "A Bolt11 string to get the payment details")] + bolt11: Option, + #[arg(help = "A payment_hash to get the payment details")] + payment_hash: Option, + #[arg( + help = "Can be one of \"pending\", \"completed\", \"failed\", filters the payments that are returned" + )] + status: Option, + }, /// Stop the node Stop, } @@ -140,6 +151,38 @@ pub async fn command_handler>(cmd: Command, config: Config

) -> .await } Command::Connect { id, host, port } => connect_handler(config, id, host, port).await, + Command::Listpays { + bolt11, + payment_hash, + status, + } => { + let payment_hash = if let Some(v) = payment_hash { + match hex::decode(v) { + Ok(decoded) => Some(decoded), + Err(e) => { + println!("Payment hash is not a valid hex string: {}", e); + return Ok(()); // Exit the function early if hex decoding fails + } + } + } else { + None + }; + let status = if let Some(status_str) = status { + match status_str.as_str() { + "pending" => Some(0), + "complete" => Some(1), + "failed" => Some(2), + _ => { + println!("Invalid status: {}, expected one of \"pending\", \"completed\", \"failed\"", status_str); + return Ok(()); // Exit the function early if the status is invalid + } + } + } else { + None + }; + + listpays_handler(config, bolt11, payment_hash, status).await + } Command::Stop => stop(config).await, } } @@ -316,6 +359,41 @@ async fn connect_handler>( Ok(()) } +async fn listpays_handler>( + config: Config

, + bolt11: Option, + payment_hash: Option>, + status: Option, +) -> Result<()> { + let creds_path = config.data_dir.as_ref().join(CREDENTIALS_FILE_NAME); + let creds = match util::read_credentials(&creds_path) { + Some(c) => c, + None => { + return Err(Error::CredentialsNotFoundError(format!( + "could not read from {}", + creds_path.display() + ))) + } + }; + + let scheduler = gl_client::scheduler::Scheduler::new(config.network, creds) + .await + .map_err(Error::custom)?; + + let mut node: gl_client::node::ClnClient = scheduler.node().await.map_err(Error::custom)?; + let res = node + .list_pays(cln::ListpaysRequest { + bolt11, + payment_hash, + status, + }) + .await + .map_err(|e| Error::custom(e.message()))? + .into_inner(); + println!("{:?}", res); + Ok(()) +} + async fn pay_handler>( config: Config

, bolt11: String,