countSpentUTXOs method

  1. @override
Future<int> countSpentUTXOs(
  1. String walletId
)
override

How many of the wallet's UTXO rows are spent.

The other half of keeping a per-event cost bounded (bead libspiffy-36jt): the projection's balance recalculation needs the spend history only for two counts it publishes (spentUtxoCount, and utxoCount over it), never for the rows themselves. Every backend counts through the (walletId, status) index without deserialising a row.

Implementation

@override
Future<int> countSpentUTXOs(String walletId) async {
  _ensureInitialized();

  // idx_utxos_wallet_status: counted, never read (bead libspiffy-36jt).
  final result = await _pool!.execute(
    Sql.named('''
      SELECT COUNT(*) FROM bitcoin_utxos
      WHERE wallet_id = @walletId AND status = 'spent'
    '''),
    parameters: {'walletId': walletId},
  );

  return result.first[0] as int;
}