|
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 | + }; |
3 | 143 | } |
0 commit comments