selectUtxosLargestFirst function

List<BtcUtxo> selectUtxosLargestFirst(
  1. List<BtcUtxo> utxos,
  2. int targetAmount
)

Ports selectUTXOsLargestFirst.ts: sort by value descending, accumulate until targetAmount is covered.

Implementation

List<BtcUtxo> selectUtxosLargestFirst(List<BtcUtxo> utxos, int targetAmount) {
  final sorted = [...utxos]
    ..sort((a, b) => int.parse(b.value).compareTo(int.parse(a.value)));
  final selected = <BtcUtxo>[];
  var total = 0;
  for (final utxo in sorted) {
    selected.add(utxo);
    total += int.parse(utxo.value);
    if (total >= targetAmount) break;
  }
  return selected;
}