Skip to content

Commit

Permalink
glcli: Add listpays method
Browse files Browse the repository at this point in the history
Adding listpays requires us to decode the input hex string.

Signed-off-by: Peter Neuroth <[email protected]>
  • Loading branch information
nepet committed Oct 1, 2024
1 parent 4c8edc5 commit 92ff787
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions libs/gl-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
78 changes: 78 additions & 0 deletions libs/gl-cli/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ pub enum Command {
#[arg(help = "The peer's port number defaults to the networks default ports if missing.")]
port: Option<u32>,
},
/// List attempted payments
Listpays {
#[arg(help = "A Bolt11 string to get the payment details")]
bolt11: Option<String>,
#[arg(help = "A payment_hash to get the payment details")]
payment_hash: Option<String>,
#[arg(
help = "Can be one of \"pending\", \"completed\", \"failed\", filters the payments that are returned"
)]
status: Option<String>,
},
/// Stop the node
Stop,
}
Expand Down Expand Up @@ -140,6 +151,38 @@ pub async fn command_handler<P: AsRef<Path>>(cmd: Command, config: Config<P>) ->
.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,
}
}
Expand Down Expand Up @@ -316,6 +359,41 @@ async fn connect_handler<P: AsRef<Path>>(
Ok(())
}

async fn listpays_handler<P: AsRef<Path>>(
config: Config<P>,
bolt11: Option<String>,
payment_hash: Option<Vec<u8>>,
status: Option<i32>,
) -> 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<P: AsRef<Path>>(
config: Config<P>,
bolt11: String,
Expand Down

0 comments on commit 92ff787

Please sign in to comment.