Skip to content

Commit 10d379a

Browse files
committed
Add estimate fee cmd to cli
1 parent adb6e28 commit 10d379a

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

examples/cli/src/main.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ enum Commands {
4949
Transactions,
5050
/// List our lightning Channels
5151
Channels,
52+
/// Estimate the fee for a payment
53+
EstimateFee {
54+
/// Destination address or invoice
55+
destination: String,
56+
/// Amount in sats (optional)
57+
amount: Option<u64>,
58+
},
5259
/// Clear the screen
5360
Clear,
5461
/// Exit the application
@@ -346,6 +353,20 @@ fn parse_command(input: &str) -> Result<Commands> {
346353
"status" => Ok(Commands::Status),
347354
"transactions" | "txs" | "tx" => Ok(Commands::Transactions),
348355
"channels" | "chan" => Ok(Commands::Channels),
356+
"estimate-fee" | "estimatefee" | "fee" => {
357+
if parts.len() < 2 {
358+
return Err(anyhow::anyhow!("Usage: estimate-fee <destination> [amount]"));
359+
}
360+
let destination = parts[1].to_string();
361+
362+
let amount = if parts.len() > 2 {
363+
Some(parts[2].parse::<u64>().context("Amount must be a valid number")?)
364+
} else {
365+
None
366+
};
367+
368+
Ok(Commands::EstimateFee { destination, amount })
369+
},
349370
"clear" | "cls" => Ok(Commands::Clear),
350371
"exit" | "quit" | "q" => Ok(Commands::Exit),
351372
"help" => {
@@ -542,6 +563,33 @@ async fn execute_command(command: Commands, state: &mut WalletState) -> Result<(
542563
}
543564
}
544565
},
566+
Commands::EstimateFee { destination, amount } => {
567+
let wallet = state.wallet();
568+
569+
println!("{} Estimating fee...", "🧮".bright_yellow());
570+
println!("Destination: {}", destination.bright_cyan());
571+
if let Some(amount) = amount {
572+
println!("Amount: {} sats", amount.to_string().bright_green().bold());
573+
}
574+
575+
let _amount = amount
576+
.map(|a| Amount::from_sats(a).map_err(|_| anyhow::anyhow!("Invalid amount")))
577+
.transpose()?;
578+
579+
match wallet.parse_payment_instructions(&destination).await {
580+
Ok(instructions) => {
581+
let estimated_fee = wallet.estimate_fee(&instructions).await;
582+
println!(
583+
"{} Estimated fee: {} sats",
584+
"✅".bright_green(),
585+
estimated_fee.sats_rounding_up()
586+
);
587+
},
588+
Err(e) => {
589+
return Err(anyhow::anyhow!("Failed to parse payment instructions: {e:?}"));
590+
},
591+
}
592+
},
545593
Commands::Clear => {
546594
print!("\x1B[2J\x1B[1;1H");
547595
std::io::stdout().flush().unwrap();
@@ -577,6 +625,9 @@ fn print_help() {
577625
println!(" {}", "channels".bright_green().bold());
578626
println!(" List channels");
579627
println!();
628+
println!(" {} <destination> [amount]", "estimate-fee".bright_green().bold());
629+
println!(" Estimate the fee for a payment");
630+
println!();
580631
println!(" {}", "clear".bright_green().bold());
581632
println!(" Clear the terminal screen");
582633
println!();
@@ -587,6 +638,7 @@ fn print_help() {
587638
println!(" balance");
588639
println!(" send lnbc1... 10000");
589640
println!(" receive 25000");
641+
println!(" estimate-fee lnbc1... 10000");
590642
println!(" events");
591643
println!();
592644
println!("{}", "Note:".bright_yellow().bold());

0 commit comments

Comments
 (0)