Skip to content

Commit

Permalink
Merge pull request #34 from dzdidi/main
Browse files Browse the repository at this point in the history
Adds missing commands to CLI
  • Loading branch information
G8XSU authored Dec 9, 2024
2 parents 8a3a55a + 7a19d08 commit 9e24e8d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
17 changes: 17 additions & 0 deletions ldk-server-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -18,6 +19,8 @@ struct Cli {

#[derive(Subcommand, Debug)]
enum Commands {
GetNodeInfo,
GetBalances,
OnchainReceive,
OnchainSend {
#[arg(short, long)]
Expand Down Expand Up @@ -73,6 +76,8 @@ enum Commands {
#[arg(long)]
announce_channel: bool,
},
ListChannels,
ListPayments,
}

#[tokio::main]
Expand All @@ -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);
},
Expand Down Expand Up @@ -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);
},
}
}

Expand Down
37 changes: 34 additions & 3 deletions ldk-server-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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)]
Expand All @@ -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<GetNodeInfoResponse, LdkServerError> {
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<GetBalancesResponse, LdkServerError> {
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(
Expand Down Expand Up @@ -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<ListPaymentsResponse, LdkServerError> {
let url = format!("http://{}/{LIST_PAYMENTS_PATH}", self.base_url);
self.post_request(&request, &url).await
}

async fn post_request<Rq: Message, Rs: Message + Default>(
&self, request: &Rq, url: &str,
) -> Result<Rs, LdkServerError> {
Expand Down

0 comments on commit 9e24e8d

Please sign in to comment.