Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to cover amount with smallest single denomination from set #328

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading