@@ -1115,13 +1115,154 @@ impl Wallet {
11151115 /// Return the balance, separated into available, trusted-pending, untrusted-pending, and
11161116 /// immature values.
11171117 pub fn balance ( & self ) -> Balance {
1118- self . tx_graph . graph ( ) . balance (
1118+ let graph = self . tx_graph . graph ( ) ;
1119+
1120+ // TODO: Use min_confirmation to use tip - min_confirmations as new tip.
1121+ let chain_tip = self . chain . tip ( ) . block_id ( ) ;
1122+
1123+ // TODO: simplify once CanonicalView is available in a published bdk_chain release
1124+ let canonical_txs: HashMap < Txid , ( Arc < Transaction > , ChainPosition < ConfirmationBlockTime > ) > =
1125+ graph
1126+ . list_canonical_txs ( & self . chain , chain_tip, CanonicalizationParams :: default ( ) )
1127+ . map ( |ctx| {
1128+ (
1129+ ctx. tx_node . txid ,
1130+ ( ctx. tx_node . tx . clone ( ) , ctx. chain_position ) ,
1131+ )
1132+ } )
1133+ . collect ( ) ;
1134+
1135+ let mut immature = Amount :: ZERO ;
1136+ let mut trusted_pending = Amount :: ZERO ;
1137+ let mut untrusted_pending = Amount :: ZERO ;
1138+ let mut confirmed = Amount :: ZERO ;
1139+
1140+ for ( _spk_i, txout) in graph. filter_chain_unspents (
11191141 & self . chain ,
1120- self . chain . tip ( ) . block_id ( ) ,
1142+ chain_tip ,
11211143 CanonicalizationParams :: default ( ) ,
11221144 self . tx_graph . index . outpoints ( ) . iter ( ) . cloned ( ) ,
1123- |& ( k, _) , _| k == KeychainKind :: Internal ,
1124- )
1145+ ) {
1146+ match & txout. chain_position {
1147+ ChainPosition :: Confirmed { .. } => {
1148+ if txout. is_confirmed_and_spendable ( chain_tip. height ) {
1149+ confirmed += txout. txout . value ;
1150+ } else if !txout. is_mature ( chain_tip. height ) {
1151+ immature += txout. txout . value ;
1152+ }
1153+ }
1154+
1155+ ChainPosition :: Unconfirmed { .. } => {
1156+ let Some ( ( root_tx, _) ) = canonical_txs. get ( & txout. outpoint . txid ) else {
1157+ untrusted_pending += txout. txout . value ;
1158+ continue ;
1159+ } ;
1160+
1161+ let mut trusted = true ;
1162+
1163+ ' root: for input in root_tx. input . iter ( ) {
1164+ if input. previous_output . is_null ( ) {
1165+ continue ;
1166+ }
1167+
1168+ let vout = input. previous_output . vout as usize ;
1169+
1170+ match canonical_txs. get ( & input. previous_output . txid ) {
1171+ // Check first if it is confirmed, in case it is it can be marked as
1172+ // trusted
1173+ Some ( ( _, pos) ) if pos. is_confirmed ( ) => {
1174+ continue ;
1175+ }
1176+ Some ( ( parent_tx, _) ) => {
1177+ let is_ours = parent_tx
1178+ . output
1179+ . get ( vout)
1180+ . map ( |o| {
1181+ self . tx_graph
1182+ . index
1183+ . index_of_spk ( o. script_pubkey . clone ( ) )
1184+ . is_some ( )
1185+ } )
1186+ . unwrap_or ( false ) ;
1187+
1188+ if !is_ours {
1189+ trusted = false ;
1190+ break ' root;
1191+ }
1192+ }
1193+ None => {
1194+ trusted = false ;
1195+ break ' root;
1196+ }
1197+ }
1198+ }
1199+
1200+ if trusted {
1201+ graph
1202+ . walk_ancestors ( root_tx. clone ( ) , |_, ancestor_tx| -> Option < ( ) > {
1203+ let ancestor_txid = ancestor_tx. compute_txid ( ) ;
1204+ match canonical_txs. get ( & ancestor_txid) {
1205+ None => {
1206+ trusted = false ;
1207+ return None ;
1208+ }
1209+ Some ( ( _, pos) ) if pos. is_confirmed ( ) => return None ,
1210+ _ => { }
1211+ }
1212+ for input in ancestor_tx. input . iter ( ) {
1213+ if input. previous_output . is_null ( ) {
1214+ continue ;
1215+ }
1216+
1217+ let vout = input. previous_output . vout as usize ;
1218+
1219+ match canonical_txs. get ( & input. previous_output . txid ) {
1220+ Some ( ( _, pos) ) if pos. is_confirmed ( ) => {
1221+ continue ;
1222+ }
1223+ Some ( ( ancestor_tx, _) ) => {
1224+ let is_ours = ancestor_tx
1225+ . output
1226+ . get ( vout)
1227+ . map ( |o| {
1228+ self . tx_graph
1229+ . index
1230+ . index_of_spk ( o. script_pubkey . clone ( ) )
1231+ . is_some ( )
1232+ } )
1233+ . unwrap_or ( false ) ;
1234+
1235+ if !is_ours {
1236+ trusted = false ;
1237+ break ;
1238+ }
1239+ }
1240+ None => {
1241+ trusted = false ;
1242+ break ;
1243+ }
1244+ }
1245+ }
1246+ Some ( ( ) )
1247+ } )
1248+ . run_until_finished ( ) ;
1249+ }
1250+
1251+ if trusted {
1252+ trusted_pending += txout. txout . value ;
1253+ } else {
1254+ untrusted_pending += txout. txout . value ;
1255+ }
1256+ }
1257+ }
1258+ }
1259+
1260+ Balance {
1261+ immature,
1262+ trusted_pending,
1263+ untrusted_pending,
1264+ confirmed,
1265+ }
11251266 }
11261267
11271268 /// Add an external signer
0 commit comments