From 76e269471df7e09e4f17c74fac91968d0e5801c1 Mon Sep 17 00:00:00 2001 From: Philipp Hoenisch Date: Sun, 29 Oct 2023 10:36:48 +0100 Subject: [PATCH 1/2] chore: make clippy happy --- crates/tests-e2e/tests/collaborative_revert_position.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/tests-e2e/tests/collaborative_revert_position.rs b/crates/tests-e2e/tests/collaborative_revert_position.rs index e6d199892..f7753df6d 100644 --- a/crates/tests-e2e/tests/collaborative_revert_position.rs +++ b/crates/tests-e2e/tests/collaborative_revert_position.rs @@ -27,9 +27,9 @@ async fn can_revert_channel() { let wallet_info = app.rx.wallet_info().expect("To be able to get wallet info"); assert_eq!(wallet_info.balances.on_chain, 0); - let split: Vec<_> = channel.original_funding_txo.split(":").collect(); - let txid = Txid::from_str(&split[0]).expect("To have a channel id"); - let vout = u32::from_str(&split[1]).expect("To have valid vout"); + let split: Vec<_> = channel.original_funding_txo.split(':').collect(); + let txid = Txid::from_str(split[0]).expect("To have a channel id"); + let vout = u32::from_str(split[1]).expect("To have valid vout"); coordinator .sync_wallet() From 900bf0befb2ab2e4481ad7aad694cb70adcf0b11 Mon Sep 17 00:00:00 2001 From: Philipp Hoenisch Date: Sun, 29 Oct 2023 10:43:25 +0100 Subject: [PATCH 2/2] feat: allow to drain on-chain wallet --- CHANGELOG.md | 2 ++ crates/ln-dlc-node/src/ldk_node_wallet.rs | 12 ++++++------ crates/ln-dlc-node/src/node/mod.rs | 2 +- .../lib/features/wallet/send/send_screen.dart | 19 ++++++++++++------- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bf2a72d0..9246d85b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- Allow to drain on-chain wallet by sending amount `0`. + ## [1.4.4] - 2023-10-28 - Improve collab revert diff --git a/crates/ln-dlc-node/src/ldk_node_wallet.rs b/crates/ln-dlc-node/src/ldk_node_wallet.rs index 307c48d74..f58037b89 100644 --- a/crates/ln-dlc-node/src/ldk_node_wallet.rs +++ b/crates/ln-dlc-node/src/ldk_node_wallet.rs @@ -183,12 +183,12 @@ where /// Send funds to the given address. /// - /// If `amount_sat_or_drain` is `None` the wallet will be drained, i.e., all available funds + /// If `amount_sat_or_drain` is `0` the wallet will be drained, i.e., all available funds /// will be spent. pub(crate) fn send_to_address( &self, address: &bitcoin::Address, - amount_sat_or_drain: Option, + amount_sat_or_drain: u64, ) -> Result { let fee_rate = self.fee_rate_estimator.estimate(ConfirmationTarget::Normal); @@ -196,9 +196,9 @@ where let locked_wallet = self.bdk_lock(); let mut tx_builder = locked_wallet.build_tx(); - if let Some(amount_sats) = amount_sat_or_drain { + if amount_sat_or_drain > 0 { tx_builder - .add_recipient(address.script_pubkey(), amount_sats) + .add_recipient(address.script_pubkey(), amount_sat_or_drain) .fee_rate(fee_rate) .enable_rbf(); } else { @@ -234,11 +234,11 @@ where let txid = self.broadcast_transaction(&tx)?; - if let Some(amount_sats) = amount_sat_or_drain { + if amount_sat_or_drain > 0 { tracing::info!( "Created new transaction {} sending {}sats on-chain to address {}", txid, - amount_sats, + amount_sat_or_drain, address ); } else { diff --git a/crates/ln-dlc-node/src/node/mod.rs b/crates/ln-dlc-node/src/node/mod.rs index fe91c905d..f37457127 100644 --- a/crates/ln-dlc-node/src/node/mod.rs +++ b/crates/ln-dlc-node/src/node/mod.rs @@ -560,7 +560,7 @@ where pub fn send_to_address(&self, address: &bitcoin::Address, amount_sats: u64) -> Result { self.wallet .ldk_wallet() - .send_to_address(address, Some(amount_sats)) + .send_to_address(address, amount_sats) } } diff --git a/mobile/lib/features/wallet/send/send_screen.dart b/mobile/lib/features/wallet/send/send_screen.dart index c506a073e..06059b807 100644 --- a/mobile/lib/features/wallet/send/send_screen.dart +++ b/mobile/lib/features/wallet/send/send_screen.dart @@ -38,7 +38,7 @@ class _SendScreenState extends State { ChannelInfo? channelInfo; Destination? _destination; - Amount? _amount; + Amount _amount = Amount.zero(); final TextEditingController _controller = TextEditingController(); @@ -64,7 +64,7 @@ class _SendScreenState extends State { if (destination != null) { _destination = destination; _amount = destination.amount; - _controller.text = _amount!.formatted(); + _controller.text = _amount.formatted(); _invalidDestination = false; _valid = _formKey.currentState?.validate() ?? false; @@ -103,7 +103,7 @@ class _SendScreenState extends State { setState(() { _destination = destination; _amount = destination.amount; - _controller.text = _amount!.formatted(); + _controller.text = _amount.formatted(); _invalidDestination = false; _valid = _formKey.currentState?.validate() ?? false; @@ -176,12 +176,13 @@ class _SendScreenState extends State { ), )), const SizedBox(height: 20), - const Text("Amount in sats", style: TextStyle(fontWeight: FontWeight.bold)), + const Text("Amount in sats (0 to drain the wallet)", + style: TextStyle(fontWeight: FontWeight.bold)), const SizedBox(height: 2), AmountInputField( controller: _controller, label: "", - value: _amount ?? Amount.zero(), + value: _amount, enabled: _destination != null && _destination!.amount.sats == 0, onChanged: (value) { setState(() { @@ -196,8 +197,12 @@ class _SendScreenState extends State { final amount = Amount.parseAmount(value); - if (amount.sats <= 0) { - return "Amount is mandatory"; + if (amount.sats <= 0 && _destination!.getWalletType() == WalletType.lightning) { + return "Amount cannot be 0"; + } + + if (amount.sats < 0) { + return "Amount cannot be negative"; } if (_destination == null) {