From 7a19d08eccc5463b83e40247c790310f144a64f3 Mon Sep 17 00:00:00 2001 From: dzdidi Date: Mon, 9 Dec 2024 08:27:29 +0100 Subject: [PATCH] Add cli missing commands Fix typo in readme Add cli command for GetNodeInfo Add cli command for GetBalance Add cli command for ListChannels Add cli command for ListPayments Co-authored-by: Gursharan Singh <3442979+G8XSU@users.noreply.github.com> --- README.md | 2 +- ldk-server-cli/src/main.rs | 17 +++++++++++++++ ldk-server-client/src/client.rs | 37 ++++++++++++++++++++++++++++++--- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 39b764f..1d37ee6 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ We welcome your feedback and contributions to help shape the future of LDK Serve ### Configuration -Refer `./ldk-server/ldk-server.config to see available configuration options. +Refer `./ldk-server/ldk-server.config` to see available configuration options. ### Building ``` diff --git a/ldk-server-cli/src/main.rs b/ldk-server-cli/src/main.rs index 4255735..140df23 100644 --- a/ldk-server-cli/src/main.rs +++ b/ldk-server-cli/src/main.rs @@ -3,6 +3,7 @@ use ldk_server_client::client::LdkServerClient; use ldk_server_client::error::LdkServerError; use ldk_server_client::ldk_server_protos::api::{ Bolt11ReceiveRequest, Bolt11SendRequest, Bolt12ReceiveRequest, Bolt12SendRequest, + GetBalancesRequest, GetNodeInfoRequest, ListChannelsRequest, ListPaymentsRequest, OnchainReceiveRequest, OnchainSendRequest, OpenChannelRequest, }; @@ -18,6 +19,8 @@ struct Cli { #[derive(Subcommand, Debug)] enum Commands { + GetNodeInfo, + GetBalances, OnchainReceive, OnchainSend { #[arg(short, long)] @@ -73,6 +76,8 @@ enum Commands { #[arg(long)] announce_channel: bool, }, + ListChannels, + ListPayments, } #[tokio::main] @@ -81,6 +86,12 @@ async fn main() { let client = LdkServerClient::new(cli.base_url); match cli.command { + Commands::GetNodeInfo => { + handle_response(client.get_node_info(GetNodeInfoRequest {}).await); + }, + Commands::GetBalances => { + handle_response(client.get_balances(GetBalancesRequest {}).await); + }, Commands::OnchainReceive => { handle_response(client.onchain_receive(OnchainReceiveRequest {}).await); }, @@ -138,6 +149,12 @@ async fn main() { .await, ); }, + Commands::ListChannels => { + handle_response(client.list_channels(ListChannelsRequest {}).await); + }, + Commands::ListPayments => { + handle_response(client.list_payments(ListPaymentsRequest {}).await); + }, } } diff --git a/ldk-server-client/src/client.rs b/ldk-server-client/src/client.rs index be9b15a..c8f8872 100644 --- a/ldk-server-client/src/client.rs +++ b/ldk-server-client/src/client.rs @@ -4,15 +4,18 @@ use crate::error::LdkServerError; use ldk_server_protos::api::{ Bolt11ReceiveRequest, Bolt11ReceiveResponse, Bolt11SendRequest, Bolt11SendResponse, Bolt12ReceiveRequest, Bolt12ReceiveResponse, Bolt12SendRequest, Bolt12SendResponse, - CloseChannelRequest, CloseChannelResponse, ListChannelsRequest, ListChannelsResponse, - OnchainReceiveRequest, OnchainReceiveResponse, OnchainSendRequest, OnchainSendResponse, - OpenChannelRequest, OpenChannelResponse, + CloseChannelRequest, CloseChannelResponse, GetBalancesRequest, GetBalancesResponse, + GetNodeInfoRequest, GetNodeInfoResponse, ListChannelsRequest, ListChannelsResponse, + ListPaymentsRequest, ListPaymentsResponse, OnchainReceiveRequest, OnchainReceiveResponse, + OnchainSendRequest, OnchainSendResponse, OpenChannelRequest, OpenChannelResponse, }; use reqwest::header::CONTENT_TYPE; use reqwest::Client; const APPLICATION_OCTET_STREAM: &str = "application/octet-stream"; +const GET_NODE_INFO_PATH: &str = "GetNodeInfo"; +const GET_BALANCES_PATH: &str = "GetBalances"; const ONCHAIN_RECEIVE_PATH: &str = "OnchainReceive"; const ONCHAIN_SEND_PATH: &str = "OnchainSend"; const BOLT11_RECEIVE_PATH: &str = "Bolt11Receive"; @@ -22,6 +25,7 @@ const BOLT12_SEND_PATH: &str = "Bolt12Send"; const OPEN_CHANNEL_PATH: &str = "OpenChannel"; const CLOSE_CHANNEL_PATH: &str = "CloseChannel"; const LIST_CHANNELS_PATH: &str = "ListChannels"; +const LIST_PAYMENTS_PATH: &str = "ListPayments"; /// Client to access a hosted instance of LDK Server. #[derive(Clone)] @@ -36,6 +40,24 @@ impl LdkServerClient { Self { base_url, client: Client::new() } } + /// Retrieve the latest node info like `node_id`, `current_best_block` etc. + /// For API contract/usage, refer to docs for [`GetNodeInfoRequest`] and [`GetNodeInfoResponse`]. + pub async fn get_node_info( + &self, request: GetNodeInfoRequest, + ) -> Result { + let url = format!("http://{}/{GET_NODE_INFO_PATH}", self.base_url); + self.post_request(&request, &url).await + } + + /// Retrieves an overview of all known balances. + /// For API contract/usage, refer to docs for [`GetBalancesRequest`] and [`GetBalancesResponse`]. + pub async fn get_balances( + &self, request: GetBalancesRequest, + ) -> Result { + let url = format!("http://{}/{GET_BALANCES_PATH}", self.base_url); + self.post_request(&request, &url).await + } + /// Retrieve a new on-chain funding address. /// For API contract/usage, refer to docs for [`OnchainReceiveRequest`] and [`OnchainReceiveResponse`]. pub async fn onchain_receive( @@ -117,6 +139,15 @@ impl LdkServerClient { self.post_request(&request, &url).await } + /// Retrieves list of all payments sent or received by us. + /// For API contract/usage, refer to docs for [`ListPaymentsRequest`] and [`ListPaymentsResponse`]. + pub async fn list_payments( + &self, request: ListPaymentsRequest, + ) -> Result { + let url = format!("http://{}/{LIST_PAYMENTS_PATH}", self.base_url); + self.post_request(&request, &url).await + } + async fn post_request( &self, request: &Rq, url: &str, ) -> Result {