Skip to content

Commit 06f986c

Browse files
authored
Merge pull request #6 from G8XSU/cli-base-setup
2 parents 6f56c2e + 2491cdf commit 06f986c

File tree

7 files changed

+407
-16
lines changed

7 files changed

+407
-16
lines changed

Cargo.lock

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
client = { path = "../client" }
8+
clap = { version = "4.0.5", default-features = false, features = ["derive", "std"] }
9+
tokio = { version = "1.38.0", default-features = false, features = ["rt-multi-thread", "macros"] }
10+
prost = { version = "0.11.6", default-features = false}

cli/src/main.rs

Lines changed: 142 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,143 @@
1-
fn main() {
2-
println!("Hello, world!");
1+
use clap::{Parser, Subcommand};
2+
use client::client::LdkNodeServerClient;
3+
use client::error::LdkNodeServerError;
4+
use client::protos::{
5+
Bolt11ReceiveRequest, Bolt11SendRequest, Bolt12ReceiveRequest, Bolt12SendRequest,
6+
OnchainReceiveRequest, OnchainSendRequest, OpenChannelRequest,
7+
};
8+
9+
#[derive(Parser, Debug)]
10+
#[command(version, about, long_about = None)]
11+
struct Cli {
12+
#[arg(short, long, default_value = "localhost:3000")]
13+
base_url: String,
14+
15+
#[command(subcommand)]
16+
command: Commands,
17+
}
18+
19+
#[derive(Subcommand, Debug)]
20+
enum Commands {
21+
OnchainReceive,
22+
OnchainSend {
23+
#[arg(short, long)]
24+
address: String,
25+
#[arg(long)]
26+
amount_sats: Option<u64>,
27+
#[arg(long)]
28+
send_all: Option<bool>,
29+
},
30+
Bolt11Receive {
31+
#[arg(short, long)]
32+
description: String,
33+
#[arg(short, long)]
34+
expiry_secs: u32,
35+
#[arg(long)]
36+
amount_msat: Option<u64>,
37+
},
38+
Bolt11Send {
39+
#[arg(short, long)]
40+
invoice: String,
41+
#[arg(long)]
42+
amount_msat: Option<u64>,
43+
},
44+
Bolt12Receive {
45+
#[arg(short, long)]
46+
description: String,
47+
#[arg(long)]
48+
amount_msat: Option<u64>,
49+
},
50+
Bolt12Send {
51+
#[arg(short, long)]
52+
offer: String,
53+
#[arg(long)]
54+
amount_msat: Option<u64>,
55+
#[arg(short, long)]
56+
quantity: Option<u64>,
57+
#[arg(short, long)]
58+
payer_note: Option<String>,
59+
},
60+
OpenChannel {
61+
#[arg(short, long)]
62+
node_pubkey: String,
63+
#[arg(short, long)]
64+
address: String,
65+
#[arg(long)]
66+
channel_amount_sats: u64,
67+
#[arg(long)]
68+
push_to_counterparty_msat: Option<u64>,
69+
#[arg(long)]
70+
announce_channel: bool,
71+
},
72+
}
73+
74+
#[tokio::main]
75+
async fn main() {
76+
let cli = Cli::parse();
77+
let client = LdkNodeServerClient::new(cli.base_url);
78+
79+
match cli.command {
80+
Commands::OnchainReceive => {
81+
handle_response(client.onchain_receive(OnchainReceiveRequest {}).await);
82+
},
83+
Commands::OnchainSend { address, amount_sats, send_all } => {
84+
handle_response(
85+
client.onchain_send(OnchainSendRequest { address, amount_sats, send_all }).await,
86+
);
87+
},
88+
Commands::Bolt11Receive { description, expiry_secs, amount_msat } => {
89+
handle_response(
90+
client
91+
.bolt11_receive(Bolt11ReceiveRequest { description, expiry_secs, amount_msat })
92+
.await,
93+
);
94+
},
95+
Commands::Bolt11Send { invoice, amount_msat } => {
96+
handle_response(client.bolt11_send(Bolt11SendRequest { invoice, amount_msat }).await);
97+
},
98+
Commands::Bolt12Receive { description, amount_msat } => {
99+
handle_response(
100+
client.bolt12_receive(Bolt12ReceiveRequest { description, amount_msat }).await,
101+
);
102+
},
103+
Commands::Bolt12Send { offer, amount_msat, quantity, payer_note } => {
104+
handle_response(
105+
client
106+
.bolt12_send(Bolt12SendRequest { offer, amount_msat, quantity, payer_note })
107+
.await,
108+
);
109+
},
110+
Commands::OpenChannel {
111+
node_pubkey,
112+
address,
113+
channel_amount_sats,
114+
push_to_counterparty_msat,
115+
announce_channel,
116+
} => {
117+
handle_response(
118+
client
119+
.open_channel(OpenChannelRequest {
120+
node_pubkey,
121+
address,
122+
channel_amount_sats,
123+
push_to_counterparty_msat,
124+
channel_config: None,
125+
announce_channel,
126+
})
127+
.await,
128+
);
129+
},
130+
}
131+
}
132+
133+
fn handle_response<Rs: ::prost::Message>(response: Result<Rs, LdkNodeServerError>) {
134+
match response {
135+
Ok(response) => {
136+
println!("{:?}", response);
137+
},
138+
Err(e) => {
139+
eprintln!("Error executing command: {:?}", e);
140+
std::process::exit(1); // Exit with status code 1 on error.
141+
},
142+
};
3143
}

client/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
protos = { path = "../protos" }
8+
reqwest = { version = "0.11.13", default-features = false, features = ["rustls-tls"] }
9+
tokio = { version = "1.38.0", default-features = false }
10+
prost = { version = "0.11.6", default-features = false, features = ["std", "prost-derive"] }

0 commit comments

Comments
 (0)