Skip to content

Commit

Permalink
Attempt to find single large enough denomination from set for input
Browse files Browse the repository at this point in the history
  • Loading branch information
rileystephens28 committed Oct 22, 2024
1 parent 63df694 commit 22ef570
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/transaction/coinselector-fewest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,13 @@ export class FewestCoinSelector extends AbstractCoinSelector {
* @returns {UTXO[]} The minimal set of UTXOs.
*/
private findMinimalUTXOSet(sortedUTXOs: UTXO[], totalRequired: bigint): UTXO[] {
// Use a greedy algorithm to select the fewest UTXOs
// Starting from the largest denominations to minimize the number of inputs
// First, try to find the smallest single UTXO that covers the total required amount
const singleUTXO = sortedUTXOs.find((utxo) => BigInt(denominations[utxo.denomination!]) >= totalRequired);
if (singleUTXO) {
return [singleUTXO];
}

// If no single UTXO is sufficient, use a greedy algorithm starting from the largest denominations
const utxos = [...sortedUTXOs].reverse(); // Largest to smallest
let totalValue = BigInt(0);
const selectedUTXOs: UTXO[] = [];
Expand Down

0 comments on commit 22ef570

Please sign in to comment.