cover method

List<Utxo> cover({
  1. required int amountNanoWit,
  2. required UtxoSelectionStrategy utxoStrategy,
})

Implementation

List<Utxo> cover({
  required int amountNanoWit,
  required UtxoSelectionStrategy utxoStrategy,
}) {
  List<Utxo> utxos = sortUtxos(utxoStrategy);
  if (utxos.isEmpty) {
    throw 'No UTXOS to select';
  }
  int utxoValue = 0;

  utxos.forEach((utxo) {
    utxoValue += utxo.value;
  });

  List<Utxo> selectedUtxos = [];

  if (amountNanoWit > utxoValue) {
    throw 'Insufficient funds';
  }
  while (amountNanoWit > 0) {
    // since the list is sorted - take the first item
    Utxo utxo = utxos.first;
    utxos.removeAt(0);
    selectedUtxos.add(utxo);
    amountNanoWit -= utxo.value;
  }
  return selectedUtxos;
}