Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: drain on-chain wallet #1541

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions crates/ln-dlc-node/src/ldk_node_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,22 +183,22 @@ 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<u64>,
amount_sat_or_drain: u64,
) -> Result<Txid> {
let fee_rate = self.fee_rate_estimator.estimate(ConfirmationTarget::Normal);

let tx = {
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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/ln-dlc-node/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ where
pub fn send_to_address(&self, address: &bitcoin::Address, amount_sats: u64) -> Result<Txid> {
self.wallet
.ldk_wallet()
.send_to_address(address, Some(amount_sats))
.send_to_address(address, amount_sats)
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/tests-e2e/tests/collaborative_revert_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
19 changes: 12 additions & 7 deletions mobile/lib/features/wallet/send/send_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class _SendScreenState extends State<SendScreen> {
ChannelInfo? channelInfo;

Destination? _destination;
Amount? _amount;
Amount _amount = Amount.zero();

final TextEditingController _controller = TextEditingController();

Expand All @@ -64,7 +64,7 @@ class _SendScreenState extends State<SendScreen> {
if (destination != null) {
_destination = destination;
_amount = destination.amount;
_controller.text = _amount!.formatted();
_controller.text = _amount.formatted();

_invalidDestination = false;
_valid = _formKey.currentState?.validate() ?? false;
Expand Down Expand Up @@ -103,7 +103,7 @@ class _SendScreenState extends State<SendScreen> {
setState(() {
_destination = destination;
_amount = destination.amount;
_controller.text = _amount!.formatted();
_controller.text = _amount.formatted();

_invalidDestination = false;
_valid = _formKey.currentState?.validate() ?? false;
Expand Down Expand Up @@ -176,12 +176,13 @@ class _SendScreenState extends State<SendScreen> {
),
)),
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(() {
Expand All @@ -196,8 +197,12 @@ class _SendScreenState extends State<SendScreen> {

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) {
bonomat marked this conversation as resolved.
Show resolved Hide resolved
return "Amount cannot be negative";
}

if (_destination == null) {
Expand Down