@@ -35,23 +35,21 @@ use crate::{
3535} ;
3636use utils:: {
3737 deser_amount_from_sats, deser_from_str, finalized_emer_txs, gethistory, listvaults_from_db,
38- presigned_txs, ser_amount, ser_to_string, unvault_tx, vaults_from_deposits,
38+ presigned_txs, ser_amount, ser_to_string, spend_entry , unvault_tx, vaults_from_deposits,
3939} ;
4040
4141use revault_tx:: {
4242 bitcoin:: {
4343 consensus:: encode, secp256k1, util:: bip32, Address , Amount , Network , OutPoint ,
4444 PublicKey as BitcoinPubKey , Transaction as BitcoinTransaction , TxOut , Txid ,
4545 } ,
46- miniscript:: DescriptorTrait ,
4746 scripts:: { CpfpDescriptor , DepositDescriptor , UnvaultDescriptor } ,
4847 transactions:: {
4948 spend_tx_from_deposits, transaction_chain, transaction_chain_manager, CancelTransaction ,
5049 CpfpableTransaction , EmergencyTransaction , RevaultPresignedTransaction , RevaultTransaction ,
5150 SpendTransaction , UnvaultEmergencyTransaction , UnvaultTransaction ,
5251 } ,
53- txins:: RevaultTxIn ,
54- txouts:: { DepositTxOut , RevaultTxOut , SpendTxOut } ,
52+ txouts:: { DepositTxOut , SpendTxOut } ,
5553} ;
5654
5755use std:: { collections:: BTreeMap , fmt} ;
@@ -924,14 +922,16 @@ impl DaemonControl {
924922 outpoints : & [ OutPoint ] ,
925923 destinations : & BTreeMap < Address , u64 > ,
926924 feerate_vb : u64 ,
927- ) -> Result < SpendTransaction , CommandError > {
925+ ) -> Result < ListSpendEntry , CommandError > {
928926 let revaultd = self . revaultd . read ( ) . unwrap ( ) ;
929927 manager_only ! ( revaultd) ;
930928 let db_file = & revaultd. db_file ( ) ;
931929
932930 // FIXME: have a feerate type to avoid that
933931 assert ! ( feerate_vb > 0 , "Spend feerate can't be null." ) ;
934932
933+ // TODO: remove the txins vec, just use the spent_vaults one.
934+ let mut spent_vaults = Vec :: with_capacity ( outpoints. len ( ) ) ;
935935 // Reconstruct the DepositTxin s from the outpoints and the vaults informations
936936 let mut txins = Vec :: with_capacity ( outpoints. len ( ) ) ;
937937 // If we need a change output, use the highest derivation index of the vaults
@@ -947,6 +947,7 @@ impl DaemonControl {
947947 change_index = vault. derivation_index ;
948948 }
949949 txins. push ( ( * outpoint, vault. amount , vault. derivation_index ) ) ;
950+ spent_vaults. push ( vault) ;
950951 } else {
951952 return Err ( CommandError :: InvalidStatus (
952953 vault. status ,
@@ -1071,7 +1072,12 @@ impl DaemonControl {
10711072 } ;
10721073 log:: debug!( "Final Spend transaction: '{:?}'" , tx_res) ;
10731074
1074- Ok ( tx_res)
1075+ Ok ( spend_entry (
1076+ & revaultd,
1077+ tx_res,
1078+ spent_vaults. iter ( ) ,
1079+ ListSpendStatus :: NonFinal ,
1080+ ) )
10751081 }
10761082
10771083 /// Store a new or update an existing Spend transaction in database.
@@ -1150,7 +1156,7 @@ impl DaemonControl {
11501156
11511157 let spend_tx_map = db_list_spends ( & db_path) . expect ( "Database must be available" ) ;
11521158 let mut listspend_entries = Vec :: with_capacity ( spend_tx_map. len ( ) ) ;
1153- for ( _, ( db_spend, deposit_outpoints ) ) in spend_tx_map {
1159+ for ( _, ( db_spend, _ ) ) in spend_tx_map {
11541160 let mut status = match db_spend. broadcasted {
11551161 Some ( true ) => ListSpendStatus :: Broadcasted ,
11561162 Some ( false ) => ListSpendStatus :: Pending ,
@@ -1182,62 +1188,12 @@ impl DaemonControl {
11821188 }
11831189 }
11841190
1185- let ( deposit_amount, mut cpfp_amount) = spent_vaults. iter ( ) . fold (
1186- ( Amount :: from_sat ( 0 ) , Amount :: from_sat ( 0 ) ) ,
1187- |( deposit_total, cpfp_total) , ( _, vault) | {
1188- let unvault = unvault_tx ( & revaultd, vault)
1189- . expect ( "Spent vault must have a correct unvault transaction" ) ;
1190-
1191- let cpfp_amount = Amount :: from_sat (
1192- unvault
1193- . cpfp_txin ( & revaultd. cpfp_descriptor , & revaultd. secp_ctx )
1194- . expect ( "Unvault tx has always a cpfp output" )
1195- . txout ( )
1196- . txout ( )
1197- . value ,
1198- ) ;
1199-
1200- ( deposit_total + vault. amount , cpfp_total + cpfp_amount)
1201- } ,
1202- ) ;
1203-
1204- let derivation_index = spent_vaults
1205- . values ( )
1206- . map ( |v| v. derivation_index )
1207- . max ( )
1208- . expect ( "Spent vaults should not be empty" ) ;
1209- let cpfp_script_pubkey = revaultd
1210- . cpfp_descriptor
1211- . derive ( derivation_index, & revaultd. secp_ctx )
1212- . into_inner ( )
1213- . script_pubkey ( ) ;
1214- let deposit_address = revaultd
1215- . deposit_descriptor
1216- . derive ( derivation_index, & revaultd. secp_ctx )
1217- . into_inner ( )
1218- . script_pubkey ( ) ;
1219- let mut cpfp_index = None ;
1220- let mut change_index = None ;
1221- for ( i, txout) in db_spend. psbt . tx ( ) . output . iter ( ) . enumerate ( ) {
1222- if cpfp_index. is_none ( ) && cpfp_script_pubkey == txout. script_pubkey {
1223- cpfp_index = Some ( i) ;
1224- cpfp_amount += Amount :: from_sat ( txout. value ) ;
1225- }
1226-
1227- if deposit_address == txout. script_pubkey {
1228- change_index = Some ( i) ;
1229- }
1230- }
1231-
1232- listspend_entries. push ( ListSpendEntry {
1233- psbt : db_spend. psbt ,
1234- deposit_outpoints,
1235- deposit_amount,
1236- cpfp_amount,
1237- cpfp_index : cpfp_index. expect ( "We always create a CPFP output" ) ,
1238- change_index,
1191+ listspend_entries. push ( spend_entry (
1192+ & revaultd,
1193+ db_spend. psbt ,
1194+ spent_vaults. values ( ) ,
12391195 status,
1240- } ) ;
1196+ ) ) ;
12411197 }
12421198
12431199 Ok ( listspend_entries)
0 commit comments