Skip to content

Commit 3faa67e

Browse files
committed
Fix amount calculation and track paid fees in PaymentKind::Onchain
We recently started tracking onchain payments in the store. It turns out the calculated amount was slightly off as in the outbound case it would include the paid fees. Here, we fix this minor bug and start tracking the fees separately.
1 parent 7e6f9c1 commit 3faa67e

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

bindings/ldk_node.udl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ interface ClosureReason {
371371

372372
[Enum]
373373
interface PaymentKind {
374-
Onchain(Txid txid, ConfirmationStatus status);
374+
Onchain(Txid txid, ConfirmationStatus status, u64 fee_msat);
375375
Bolt11(PaymentHash hash, PaymentPreimage? preimage, PaymentSecret? secret);
376376
Bolt11Jit(PaymentHash hash, PaymentPreimage? preimage, PaymentSecret? secret, LSPFeeLimits lsp_fee_limits);
377377
Bolt12Offer(PaymentHash? hash, PaymentPreimage? preimage, PaymentSecret? secret, OfferId offer_id, UntrustedString? payer_note, u64? quantity);

src/payment/store.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ pub enum PaymentKind {
302302
txid: Txid,
303303
/// The confirmation status of this payment.
304304
status: ConfirmationStatus,
305+
/// The on-chain fees that were paid for this payment.
306+
fee_msat: u64,
305307
},
306308
/// A [BOLT 11] payment.
307309
///
@@ -394,6 +396,7 @@ impl_writeable_tlv_based_enum!(PaymentKind,
394396
(0, Onchain) => {
395397
(0, txid, required),
396398
(2, status, required),
399+
(4, fee_msat, required),
397400
},
398401
(2, Bolt11) => {
399402
(0, hash, required),

src/wallet/mod.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,17 +186,29 @@ where
186186
// create and persist a list of 'static pending outputs' that we could use
187187
// here to determine the `PaymentKind`, but that's not really satisfactory, so
188188
// we're punting on it until we can come up with a better solution.
189-
let kind = crate::payment::PaymentKind::Onchain { txid, status: confirmation_status };
189+
let fee = locked_wallet.calculate_fee(&wtx.tx_node.tx).unwrap_or(Amount::ZERO);
190+
let fee_msat = fee.to_sat() * 1000;
190191
let (sent, received) = locked_wallet.sent_and_received(&wtx.tx_node.tx);
191192
let (direction, amount_msat) = if sent > received {
192193
let direction = PaymentDirection::Outbound;
193-
let amount_msat = Some(sent.to_sat().saturating_sub(received.to_sat()) * 1000);
194+
let amount_msat = Some(
195+
sent.to_sat().saturating_sub(fee.to_sat()).saturating_sub(received.to_sat())
196+
* 1000,
197+
);
194198
(direction, amount_msat)
195199
} else {
196200
let direction = PaymentDirection::Inbound;
197-
let amount_msat = Some(received.to_sat().saturating_sub(sent.to_sat()) * 1000);
201+
let amount_msat = Some(
202+
received.to_sat().saturating_sub(sent.to_sat().saturating_sub(fee.to_sat()))
203+
* 1000,
204+
);
198205
(direction, amount_msat)
199206
};
207+
let kind = crate::payment::PaymentKind::Onchain {
208+
txid,
209+
status: confirmation_status,
210+
fee_msat,
211+
};
200212

201213
let payment = PaymentDetails {
202214
id,

tests/integration_tests_rust.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use lightning::util::persist::KVStore;
2626

2727
use bitcoincore_rpc::RpcApi;
2828

29+
use bitcoin::hashes::Hash;
2930
use bitcoin::Amount;
3031
use lightning_invoice::{Bolt11InvoiceDescription, Description};
3132

@@ -281,7 +282,7 @@ fn start_stop_reinit() {
281282
}
282283

283284
#[test]
284-
fn onchain_spend_receive() {
285+
fn onchain_send_receive() {
285286
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
286287
let chain_source = TestChainSource::Esplora(&electrsd);
287288
let (node_a, node_b) = setup_two_nodes(&chain_source, false, true, false);
@@ -352,7 +353,7 @@ fn onchain_spend_receive() {
352353
node_a.onchain_payment().send_to_address(&addr_b, expected_node_a_balance + 1, None)
353354
);
354355

355-
let amount_to_send_sats = 1000;
356+
let amount_to_send_sats = 54321;
356357
let txid =
357358
node_b.onchain_payment().send_to_address(&addr_a, amount_to_send_sats, None).unwrap();
358359
generate_blocks_and_wait(&bitcoind.client, &electrsd.client, 6);
@@ -371,9 +372,29 @@ fn onchain_spend_receive() {
371372
let node_a_payments =
372373
node_a.list_payments_with_filter(|p| matches!(p.kind, PaymentKind::Onchain { .. }));
373374
assert_eq!(node_a_payments.len(), 2);
374-
let node_b_payments =
375-
node_b.list_payments_with_filter(|p| matches!(p.kind, PaymentKind::Onchain { .. }));
376-
assert_eq!(node_b_payments.len(), 3);
375+
376+
let payment_id = PaymentId(txid.to_byte_array());
377+
let payment_a = node_a.payment(&payment_id).unwrap();
378+
assert_eq!(payment_a.amount_msat, Some(amount_to_send_sats * 1000));
379+
match payment_a.kind {
380+
PaymentKind::Onchain { txid: _txid, status, fee_msat } => {
381+
assert_eq!(_txid, txid);
382+
assert!(matches!(status, ConfirmationStatus::Confirmed { .. }));
383+
assert!(fee_msat != 0);
384+
},
385+
_ => panic!("Unexpected payment kind"),
386+
}
387+
388+
let payment_b = node_a.payment(&payment_id).unwrap();
389+
assert_eq!(payment_b.amount_msat, Some(amount_to_send_sats * 1000));
390+
match payment_b.kind {
391+
PaymentKind::Onchain { txid: _txid, status, fee_msat } => {
392+
assert_eq!(_txid, txid);
393+
assert!(matches!(status, ConfirmationStatus::Confirmed { .. }));
394+
assert!(fee_msat != 0);
395+
},
396+
_ => panic!("Unexpected payment kind"),
397+
}
377398

378399
let addr_b = node_b.onchain_payment().new_address().unwrap();
379400
let txid = node_a.onchain_payment().send_all_to_address(&addr_b, true, None).unwrap();

0 commit comments

Comments
 (0)