Skip to content

Commit

Permalink
Appy code review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
dleutenegger committed Oct 5, 2023
1 parent 11cd5d6 commit e1a06c3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
36 changes: 17 additions & 19 deletions examples/3l-node/cli.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use crate::hinter::{CommandHint, CommandHinter};

use uniffi_lipalightninglib::{
Amount, FiatValue, MaxRoutingFeeMode, OfferKind, PaymentState, TopupCurrency,
};

use bitcoin::hashes::hex::ToHex;
use chrono::offset::FixedOffset;
use chrono::{DateTime, Local, Utc};
use colored::Colorize;
Expand All @@ -15,6 +10,9 @@ use rustyline::Editor;
use std::collections::HashSet;
use std::path::Path;
use uniffi_lipalightninglib::LiquidityLimit;
use uniffi_lipalightninglib::{
Amount, FiatValue, MaxRoutingFeeMode, OfferKind, PaymentState, TopupCurrency,
};

use crate::LightningNode;
use crate::TzConfig;
Expand Down Expand Up @@ -133,22 +131,22 @@ pub(crate) fn poll_for_user_input(node: &LightningNode, log_file_path: &str) {
Ok(uuid) => println!("{uuid}"),
Err(message) => eprintln!("{}", message.red()),
},
"drain" => {
let vec: Vec<u8> = vec![0; 64];
println!("{}", vec.to_hex());
"sweep" => {
let address = words
.next()
.ok_or_else(|| "Address is required".to_string());
if let Err(e) = address {
println!("{}", e.red());
return;
}

let address = address.unwrap().to_string();
match drain(node, address.clone()) {
let address = match address {
Ok(a) => a.to_string(),
Err(e) => {
println!("{}", e.red());
return;
}
};
match sweep(node, address.clone()) {
Ok(txid) => {
println!();
println!("Transaction Id: {}", txid.to_hex());
println!("Transaction Id: {}", txid);
println!("Payout address: {}", address)
}
Err(e) => println!("{}", e.red()),
Expand Down Expand Up @@ -227,7 +225,7 @@ fn setup_editor(history_path: &Path) -> Editor<CommandHinter, DefaultHistory> {
"paymentuuid <payment hash>",
"paymentuuid",
));
hints.insert(CommandHint::new("drain <address>", "drain"));
hints.insert(CommandHint::new("sweep <address>", "sweep"));
hints.insert(CommandHint::new("foreground", "foreground"));
hints.insert(CommandHint::new("background", "background"));
hints.insert(CommandHint::new("stop", "stop"));
Expand Down Expand Up @@ -263,7 +261,7 @@ fn help() {
println!(" listpayments");
println!(" paymentuuid <payment hash>");
println!();
println!(" drain <address>");
println!(" sweep <address>");
println!();
println!(" foreground");
println!(" background");
Expand Down Expand Up @@ -706,10 +704,10 @@ fn payment_uuid(
};
}

fn drain(node: &LightningNode, address: String) -> Result<Vec<u8>, String> {
fn sweep(node: &LightningNode, address: String) -> Result<String, String> {
let fee_rate = node.query_onchain_fee().map_err(|e| e.to_string())?;

node.drain(address.to_string(), fee_rate)
node.sweep(address.to_string(), fee_rate)
.map_err(|e| e.to_string())
}

Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,13 +843,14 @@ impl LightningNode {
}

// TODO return txid when exposed by breez sdk https://github.com/breez/breez-sdk/issues/476
pub fn drain(&self, address: String, onchain_fee: u64) -> Result<Vec<u8>> {
pub fn sweep(&self, address: String, onchain_fee: u64) -> Result<String> {
self.rt
.handle()
.block_on(self.sdk.sweep(address, onchain_fee))
.map_to_runtime_error(NodeUnavailable, "Failed to drain funds")?;

Ok(vec![0; 64])
let txid: Vec<u8> = vec![0; 64];
Ok(txid.to_hex())
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/lipalightninglib.udl
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,12 @@ interface LightningNode {

// TODO: Currently doesn't return the correct txid until exposed by breez sdk https://github.com/breez/breez-sdk/issues/476
//
// Drain all onchain funds to a specified address
//
// Drains all available onchain funds on the specified onchain address.
// Sweeps all available onchain funds on the specified onchain address.
// - address - the funds will be drained to this address
// - onchain_fee - the fees that should be applied for the transaction
// Returns the txid of the draining transaction.
// Returns the txid of the sweeping transaction.
[Throws=LnError]
sequence<u8> drain(string address, u64 onchain_fee);
string sweep(string address, u64 onchain_fee);
};

// An object that holds all configuration needed to start a LightningNode instance.
Expand Down

0 comments on commit e1a06c3

Please sign in to comment.