11use crate :: bitcoin:: OutPoint ;
2+ use crate :: bitcoin:: hashes:: Hash ;
23use crate :: event:: { EventQueue , LdkEventHandler } ;
34use crate :: logging:: Logger ;
45use crate :: runtime:: Runtime ;
56use crate :: store:: { TxMetadataStore , TxStatus } ;
6- use crate :: { ChainSource , InitFailure , PaymentType , Seed , WalletConfig } ;
7+ use crate :: { ChainSource , InitFailure , PaymentType , Seed , WalletConfig , store } ;
78
89use bitcoin_payment_instructions:: PaymentMethod ;
910use bitcoin_payment_instructions:: amount:: Amount ;
@@ -18,7 +19,9 @@ use ldk_node::lightning::util::logger::Logger as _;
1819use ldk_node:: lightning:: util:: persist:: KVStore ;
1920use ldk_node:: lightning:: { log_debug, log_error, log_info} ;
2021use ldk_node:: lightning_invoice:: { Bolt11Invoice , Bolt11InvoiceDescription , Description } ;
21- use ldk_node:: payment:: { PaymentDetails , PaymentDirection , PaymentKind , PaymentStatus } ;
22+ use ldk_node:: payment:: {
23+ ConfirmationStatus , PaymentDetails , PaymentDirection , PaymentKind , PaymentStatus ,
24+ } ;
2225use ldk_node:: { DynStore , NodeError , UserChannelId , lightning} ;
2326
2427use graduated_rebalancer:: { LightningBalance , ReceivedLightningPayment } ;
@@ -27,7 +30,7 @@ use std::collections::HashMap;
2730use std:: fmt:: Debug ;
2831use std:: pin:: Pin ;
2932use std:: sync:: Arc ;
30-
33+ use std :: time :: SystemTime ;
3134use tokio:: sync:: watch;
3235
3336#[ derive( Debug , Clone , Copy ) ]
@@ -39,6 +42,7 @@ pub(crate) struct LightningWalletBalance {
3942pub ( crate ) struct LightningWalletImpl {
4043 pub ( crate ) ldk_node : Arc < ldk_node:: Node > ,
4144 logger : Arc < Logger > ,
45+ store : Arc < DynStore > ,
4246 payment_receipt_flag : watch:: Receiver < ( ) > ,
4347 channel_pending_receipt_flag : watch:: Receiver < u128 > ,
4448 splice_pending_receipt_flag : watch:: Receiver < u128 > ,
@@ -178,6 +182,7 @@ impl LightningWallet {
178182 let inner = Arc :: new ( LightningWalletImpl {
179183 ldk_node,
180184 logger,
185+ store,
181186 payment_receipt_flag,
182187 channel_pending_receipt_flag,
183188 splice_pending_receipt_flag,
@@ -299,14 +304,16 @@ impl LightningWallet {
299304 . bolt12_payment ( )
300305 . send_using_amount ( offer, amount. milli_sats ( ) , None , None ) ,
301306 PaymentMethod :: OnChain ( address) => {
307+ let amount_sats = amount. sats ( ) . map_err ( |_| NodeError :: InvalidAmount ) ?;
308+
302309 let balance = self . inner . ldk_node . list_balances ( ) ;
303310
304311 // if we have enough onchain balance, send onchain
305- if balance. spendable_onchain_balance_sats > amount . sats_rounding_up ( ) {
312+ if balance. spendable_onchain_balance_sats > amount_sats {
306313 self . inner
307314 . ldk_node
308315 . onchain_payment ( )
309- . send_to_address ( address, amount . sats_rounding_up ( ) , None )
316+ . send_to_address ( address, amount_sats , None )
310317 . map ( |txid| PaymentId ( * txid. as_ref ( ) ) )
311318 } else {
312319 // otherwise try to pay via splice out
@@ -326,7 +333,7 @@ impl LightningWallet {
326333 & chan. user_channel_id ,
327334 chan. counterparty_node_id ,
328335 address. clone ( ) ,
329- amount . sats_rounding_up ( ) ,
336+ amount_sats ,
330337 ) ?;
331338
332339 loop {
@@ -340,9 +347,30 @@ impl LightningWallet {
340347 if c. funding_txo
341348 . is_some_and ( |f| f != chan. funding_txo . unwrap ( ) )
342349 {
343- return Ok ( PaymentId (
344- * c. funding_txo . unwrap ( ) . txid . as_ref ( ) ,
345- ) ) ;
350+ let funding_txo = c. funding_txo . unwrap ( ) ;
351+
352+ let id = PaymentId ( funding_txo. txid . to_byte_array ( ) ) ;
353+ let details = PaymentDetails {
354+ id,
355+ kind : PaymentKind :: Onchain {
356+ txid : funding_txo. txid ,
357+ status : ConfirmationStatus :: Unconfirmed , // todo how do we update this?
358+ } ,
359+ amount_msat : Some ( amount_sats * 1_000 ) ,
360+ fee_paid_msat : Some ( 69 ) , // todo get real fee
361+ direction : PaymentDirection :: Outbound ,
362+ status : PaymentStatus :: Succeeded ,
363+ latest_update_timestamp : SystemTime :: now ( )
364+ . duration_since ( SystemTime :: UNIX_EPOCH )
365+ . unwrap ( )
366+ . as_secs ( ) ,
367+ } ;
368+
369+ store:: write_splice_out (
370+ self . inner . store . as_ref ( ) ,
371+ & details,
372+ ) ;
373+ return Ok ( id) ;
346374 }
347375 } ,
348376 None => {
0 commit comments