fn select_cardinal_utxo(
    &mut self,
    target_value: u64,
    prefer_under: bool,
  ) -> Result<(OutPoint, Amount)> {
    tprintln!(
      "looking for {} cardinal worth {target_value}",
      if prefer_under { "smaller" } else { "bigger" }
    );

    let inscribed_utxos = self
      .inscriptions
      .keys()
      .map(|satpoint| satpoint.outpoint)
      .collect::<BTreeSet<OutPoint>>();

    let mut best_match = None;
    for utxo in &self.utxos {
      if self.runic_utxos.contains(utxo)
        || inscribed_utxos.contains(utxo)
        || self.locked_utxos.contains(utxo)
      {
        continue;
      }

      let current_value = self.amounts[utxo].value.to_sat();

      let (_, best_value) = match best_match {
        Some(prev) => prev,
        None => {
          best_match = Some((*utxo, current_value));
          (*utxo, current_value)
        }
      };

      let abs_diff = |a: u64, b: u64| -> u64 { max(a, b) - min(a, b) };
      let is_closer = abs_diff(current_value, target_value) < abs_diff(best_value, target_value);

      let not_preference_but_closer = if prefer_under {
        best_value > target_value && is_closer
      } else {
        best_value < target_value && is_closer
      };

      let is_preference_and_closer = if prefer_under {
        current_value <= target_value && is_closer
      } else {
        current_value >= target_value && is_closer
      };

      let newly_meets_preference = if prefer_under {
        best_value > target_value && current_value <= target_value
      } else {
        best_value < target_value && current_value >= target_value
      };

      if is_preference_and_closer || not_preference_but_closer || newly_meets_preference {
        best_match = Some((*utxo, current_value))
      }
    }

    let (utxo, value) = best_match.ok_or(Error::NotEnoughCardinalUtxos)?;

    self.utxos.remove(&utxo);
    tprintln!("found cardinal worth {}", value);

    Ok((utxo, Amount::from_sat(value)))
  }
Cardinal UTXOs are those that are unlocked, contain no inscriptions, and
contain no runes, can therefore be used to pad transactions and pay fees.
Sometimes multiple cardinal UTXOs are needed and depending on the context
we want to select either ones above or under (when trying to consolidate
dust outputs) the target value.
src/wallet/transaction_builder.rs