selectUTXOsForAmount method
Select UTXOs for a specific amount (simple first-fit algorithm)
Implementation
List<BitcoinUtxo> selectUTXOsForAmount(WalletState state, BigInt amount) {
final availableUtxos = getAvailableUTXOs(state);
availableUtxos.sort((a, b) => b.satoshis.compareTo(a.satoshis)); // Largest first
final selected = <BitcoinUtxo>[];
BigInt totalSelected = BigInt.zero;
for (final utxo in availableUtxos) {
selected.add(utxo);
totalSelected += utxo.satoshis;
if (totalSelected >= amount) {
break;
}
}
if (totalSelected < amount) {
throw StateError('Insufficient funds: need $amount satoshis, have $totalSelected available');
}
return selected;
}