Skip to content

Commit 68bf90c

Browse files
committed
f Generally track fee_paid_msat as part of PaymentDetails
1 parent 3faa67e commit 68bf90c

File tree

9 files changed

+64
-30
lines changed

9 files changed

+64
-30
lines changed

bindings/ldk_node.udl

Lines changed: 2 additions & 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, u64 fee_msat);
374+
Onchain(Txid txid, ConfirmationStatus status);
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);
@@ -412,6 +412,7 @@ dictionary PaymentDetails {
412412
PaymentId id;
413413
PaymentKind kind;
414414
u64? amount_msat;
415+
u64? fee_paid_msat;
415416
PaymentDirection direction;
416417
PaymentStatus status;
417418
u64 latest_update_timestamp;

src/event.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@ where
725725
payment_id,
726726
kind,
727727
Some(amount_msat),
728+
None,
728729
PaymentDirection::Inbound,
729730
PaymentStatus::Pending,
730731
);
@@ -765,6 +766,7 @@ where
765766
payment_id,
766767
kind,
767768
Some(amount_msat),
769+
None,
768770
PaymentDirection::Inbound,
769771
PaymentStatus::Pending,
770772
);
@@ -931,6 +933,7 @@ where
931933
let update = PaymentDetailsUpdate {
932934
hash: Some(Some(payment_hash)),
933935
preimage: Some(Some(payment_preimage)),
936+
fee_paid_msat: Some(fee_paid_msat),
934937
status: Some(PaymentStatus::Succeeded),
935938
..PaymentDetailsUpdate::new(payment_id)
936939
};

src/payment/bolt11.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ impl Bolt11Payment {
160160
payment_id,
161161
kind,
162162
invoice.amount_milli_satoshis(),
163+
None,
163164
PaymentDirection::Outbound,
164165
PaymentStatus::Pending,
165166
);
@@ -182,6 +183,7 @@ impl Bolt11Payment {
182183
payment_id,
183184
kind,
184185
invoice.amount_milli_satoshis(),
186+
None,
185187
PaymentDirection::Outbound,
186188
PaymentStatus::Failed,
187189
);
@@ -293,6 +295,7 @@ impl Bolt11Payment {
293295
payment_id,
294296
kind,
295297
Some(amount_msat),
298+
None,
296299
PaymentDirection::Outbound,
297300
PaymentStatus::Pending,
298301
);
@@ -315,6 +318,7 @@ impl Bolt11Payment {
315318
payment_id,
316319
kind,
317320
Some(amount_msat),
321+
None,
318322
PaymentDirection::Outbound,
319323
PaymentStatus::Failed,
320324
);
@@ -531,6 +535,7 @@ impl Bolt11Payment {
531535
id,
532536
kind,
533537
amount_msat,
538+
None,
534539
PaymentDirection::Inbound,
535540
PaymentStatus::Pending,
536541
);
@@ -666,6 +671,7 @@ impl Bolt11Payment {
666671
id,
667672
kind,
668673
amount_msat,
674+
None,
669675
PaymentDirection::Inbound,
670676
PaymentStatus::Pending,
671677
);

src/payment/bolt12.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ impl Bolt12Payment {
113113
payment_id,
114114
kind,
115115
Some(offer_amount_msat),
116+
None,
116117
PaymentDirection::Outbound,
117118
PaymentStatus::Pending,
118119
);
@@ -137,6 +138,7 @@ impl Bolt12Payment {
137138
payment_id,
138139
kind,
139140
Some(offer_amount_msat),
141+
None,
140142
PaymentDirection::Outbound,
141143
PaymentStatus::Failed,
142144
);
@@ -217,6 +219,7 @@ impl Bolt12Payment {
217219
payment_id,
218220
kind,
219221
Some(amount_msat),
222+
None,
220223
PaymentDirection::Outbound,
221224
PaymentStatus::Pending,
222225
);
@@ -241,6 +244,7 @@ impl Bolt12Payment {
241244
payment_id,
242245
kind,
243246
Some(amount_msat),
247+
None,
244248
PaymentDirection::Outbound,
245249
PaymentStatus::Failed,
246250
);
@@ -338,6 +342,7 @@ impl Bolt12Payment {
338342
payment_id,
339343
kind,
340344
Some(refund.amount_msats()),
345+
None,
341346
PaymentDirection::Inbound,
342347
PaymentStatus::Pending,
343348
);
@@ -402,6 +407,7 @@ impl Bolt12Payment {
402407
payment_id,
403408
kind,
404409
Some(amount_msat),
410+
None,
405411
PaymentDirection::Outbound,
406412
PaymentStatus::Pending,
407413
);

src/payment/spontaneous.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ impl SpontaneousPayment {
140140
payment_id,
141141
kind,
142142
Some(amount_msat),
143+
None,
143144
PaymentDirection::Outbound,
144145
PaymentStatus::Pending,
145146
);
@@ -161,6 +162,7 @@ impl SpontaneousPayment {
161162
payment_id,
162163
kind,
163164
Some(amount_msat),
165+
None,
164166
PaymentDirection::Outbound,
165167
PaymentStatus::Failed,
166168
);

src/payment/store.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ pub struct PaymentDetails {
4242
pub kind: PaymentKind,
4343
/// The amount transferred.
4444
pub amount_msat: Option<u64>,
45+
/// The fees that were paid for this payment.
46+
///
47+
/// For Lightning payments, this will only be updated for outbound payments once they
48+
/// succeeded.
49+
///
50+
/// Will be `None` for Lightning payments made with LDK Node v0.4.x and earlier.
51+
pub fee_paid_msat: Option<u64>,
4552
/// The direction of the payment.
4653
pub direction: PaymentDirection,
4754
/// The status of the payment.
@@ -52,14 +59,14 @@ pub struct PaymentDetails {
5259

5360
impl PaymentDetails {
5461
pub(crate) fn new(
55-
id: PaymentId, kind: PaymentKind, amount_msat: Option<u64>, direction: PaymentDirection,
56-
status: PaymentStatus,
62+
id: PaymentId, kind: PaymentKind, amount_msat: Option<u64>, fee_paid_msat: Option<u64>,
63+
direction: PaymentDirection, status: PaymentStatus,
5764
) -> Self {
5865
let latest_update_timestamp = SystemTime::now()
5966
.duration_since(UNIX_EPOCH)
6067
.unwrap_or(Duration::from_secs(0))
6168
.as_secs();
62-
Self { id, kind, amount_msat, direction, status, latest_update_timestamp }
69+
Self { id, kind, amount_msat, fee_paid_msat, direction, status, latest_update_timestamp }
6370
}
6471

6572
pub(crate) fn update(&mut self, update: &PaymentDetailsUpdate) -> bool {
@@ -192,6 +199,7 @@ impl Writeable for PaymentDetails {
192199
(4, None::<Option<PaymentSecret>>, required),
193200
(5, self.latest_update_timestamp, required),
194201
(6, self.amount_msat, required),
202+
(7, self.fee_paid_msat, option),
195203
(8, self.direction, required),
196204
(10, self.status, required)
197205
});
@@ -213,6 +221,7 @@ impl Readable for PaymentDetails {
213221
(4, secret, required),
214222
(5, latest_update_timestamp, (default_value, unix_time_secs)),
215223
(6, amount_msat, required),
224+
(7, fee_paid_msat, option),
216225
(8, direction, required),
217226
(10, status, required)
218227
});
@@ -253,7 +262,15 @@ impl Readable for PaymentDetails {
253262
}
254263
};
255264

256-
Ok(PaymentDetails { id, kind, amount_msat, direction, status, latest_update_timestamp })
265+
Ok(PaymentDetails {
266+
id,
267+
kind,
268+
amount_msat,
269+
fee_paid_msat,
270+
direction,
271+
status,
272+
latest_update_timestamp,
273+
})
257274
}
258275
}
259276

@@ -302,8 +319,6 @@ pub enum PaymentKind {
302319
txid: Txid,
303320
/// The confirmation status of this payment.
304321
status: ConfirmationStatus,
305-
/// The on-chain fees that were paid for this payment.
306-
fee_msat: u64,
307322
},
308323
/// A [BOLT 11] payment.
309324
///
@@ -396,7 +411,6 @@ impl_writeable_tlv_based_enum!(PaymentKind,
396411
(0, Onchain) => {
397412
(0, txid, required),
398413
(2, status, required),
399-
(4, fee_msat, required),
400414
},
401415
(2, Bolt11) => {
402416
(0, hash, required),
@@ -482,6 +496,7 @@ pub(crate) struct PaymentDetailsUpdate {
482496
pub preimage: Option<Option<PaymentPreimage>>,
483497
pub secret: Option<Option<PaymentSecret>>,
484498
pub amount_msat: Option<Option<u64>>,
499+
pub fee_paid_msat: Option<Option<u64>>,
485500
pub direction: Option<PaymentDirection>,
486501
pub status: Option<PaymentStatus>,
487502
pub confirmation_status: Option<ConfirmationStatus>,
@@ -495,6 +510,7 @@ impl PaymentDetailsUpdate {
495510
preimage: None,
496511
secret: None,
497512
amount_msat: None,
513+
fee_paid_msat: None,
498514
direction: None,
499515
status: None,
500516
confirmation_status: None,
@@ -524,6 +540,7 @@ impl From<&PaymentDetails> for PaymentDetailsUpdate {
524540
preimage: Some(preimage),
525541
secret: Some(secret),
526542
amount_msat: Some(value.amount_msat),
543+
fee_paid_msat: Some(value.fee_paid_msat),
527544
direction: Some(value.direction),
528545
status: Some(value.status),
529546
confirmation_status,
@@ -711,8 +728,14 @@ mod tests {
711728
.is_err());
712729

713730
let kind = PaymentKind::Bolt11 { hash, preimage: None, secret: None };
714-
let payment =
715-
PaymentDetails::new(id, kind, None, PaymentDirection::Inbound, PaymentStatus::Pending);
731+
let payment = PaymentDetails::new(
732+
id,
733+
kind,
734+
None,
735+
None,
736+
PaymentDirection::Inbound,
737+
PaymentStatus::Pending,
738+
);
716739

717740
assert_eq!(Ok(false), payment_store.insert(payment.clone()));
718741
assert!(payment_store.get(&id).is_some());

src/wallet/mod.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ use bitcoin::{
4949

5050
use std::ops::Deref;
5151
use std::sync::{Arc, Mutex};
52-
use std::time::{Duration, SystemTime, UNIX_EPOCH};
5352

5453
pub(crate) enum OnchainSendAmount {
5554
ExactRetainingReserve { amount_sats: u64, cur_anchor_reserve_sats: u64 },
@@ -146,11 +145,6 @@ where
146145
fn update_payment_store<'a>(
147146
&self, locked_wallet: &'a mut PersistedWallet<KVStoreWalletPersister>,
148147
) -> Result<(), Error> {
149-
let latest_update_timestamp = SystemTime::now()
150-
.duration_since(UNIX_EPOCH)
151-
.unwrap_or(Duration::from_secs(0))
152-
.as_secs();
153-
154148
for wtx in locked_wallet.transactions() {
155149
let id = PaymentId(wtx.tx_node.txid.to_byte_array());
156150
let txid = wtx.tx_node.txid;
@@ -187,7 +181,6 @@ where
187181
// here to determine the `PaymentKind`, but that's not really satisfactory, so
188182
// we're punting on it until we can come up with a better solution.
189183
let fee = locked_wallet.calculate_fee(&wtx.tx_node.tx).unwrap_or(Amount::ZERO);
190-
let fee_msat = fee.to_sat() * 1000;
191184
let (sent, received) = locked_wallet.sent_and_received(&wtx.tx_node.tx);
192185
let (direction, amount_msat) = if sent > received {
193186
let direction = PaymentDirection::Outbound;
@@ -204,20 +197,18 @@ where
204197
);
205198
(direction, amount_msat)
206199
};
207-
let kind = crate::payment::PaymentKind::Onchain {
208-
txid,
209-
status: confirmation_status,
210-
fee_msat,
211-
};
200+
let kind = crate::payment::PaymentKind::Onchain { txid, status: confirmation_status };
201+
202+
let fee_paid_msat = Some(fee.to_sat() * 1000);
212203

213-
let payment = PaymentDetails {
204+
let payment = PaymentDetails::new(
214205
id,
215206
kind,
216207
amount_msat,
208+
fee_paid_msat,
217209
direction,
218-
status: payment_status,
219-
latest_update_timestamp,
220-
};
210+
payment_status,
211+
);
221212

222213
self.payment_store.insert_or_update(&payment)?;
223214
}

tests/common/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ macro_rules! expect_payment_successful_event {
148148
if let Some(fee_msat) = $fee_paid_msat {
149149
assert_eq!(fee_paid_msat, fee_msat);
150150
}
151+
let payment = $node.payment(&$payment_id.unwrap()).unwrap();
152+
assert_eq!(payment.fee_paid_msat, fee_paid_msat);
151153
assert_eq!(payment_id, $payment_id);
152154
$node.event_handled();
153155
},

tests/integration_tests_rust.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,22 +376,22 @@ fn onchain_send_receive() {
376376
let payment_id = PaymentId(txid.to_byte_array());
377377
let payment_a = node_a.payment(&payment_id).unwrap();
378378
assert_eq!(payment_a.amount_msat, Some(amount_to_send_sats * 1000));
379+
assert!(payment_a.fee_paid_msat > Some(0));
379380
match payment_a.kind {
380-
PaymentKind::Onchain { txid: _txid, status, fee_msat } => {
381+
PaymentKind::Onchain { txid: _txid, status } => {
381382
assert_eq!(_txid, txid);
382383
assert!(matches!(status, ConfirmationStatus::Confirmed { .. }));
383-
assert!(fee_msat != 0);
384384
},
385385
_ => panic!("Unexpected payment kind"),
386386
}
387387

388388
let payment_b = node_a.payment(&payment_id).unwrap();
389389
assert_eq!(payment_b.amount_msat, Some(amount_to_send_sats * 1000));
390+
assert!(payment_b.fee_paid_msat > Some(0));
390391
match payment_b.kind {
391-
PaymentKind::Onchain { txid: _txid, status, fee_msat } => {
392+
PaymentKind::Onchain { txid: _txid, status } => {
392393
assert_eq!(_txid, txid);
393394
assert!(matches!(status, ConfirmationStatus::Confirmed { .. }));
394-
assert!(fee_msat != 0);
395395
},
396396
_ => panic!("Unexpected payment kind"),
397397
}

0 commit comments

Comments
 (0)