The FewestCoinSelector class provides a coin selection algorithm that selects the fewest UTXOs required to meet the target amount. This algorithm is useful for minimizing the size of the transaction and the fees associated with it.

This class is a sub-class of AbstractCoinSelector | AbstractCoinSelector and implements the AbstractCoinSelector.performSelection | performSelection method to provide the actual coin selection logic.

Extends

  • AbstractCoinSelector

Constructors

new FewestCoinSelector()

new FewestCoinSelector(availableUTXOs): FewestCoinSelector

Constructs a new AbstractCoinSelector instance with an empty UTXO array.

Parameters

ParameterTypeDefault value
availableUTXOsUTXO[][]

Returns

FewestCoinSelector

Inherited from

AbstractCoinSelector.constructor

Source

transaction/abstract-coinselector.ts:53

Methods

adjustChangeOutputs()

private adjustChangeOutputs(changeAmount): void

Helper method to adjust change outputs.

Parameters

ParameterTypeDescription
changeAmountbigintThe amount to adjust change outputs by.

Returns

void

Source

transaction/coinselector-fewest.ts:324


calculateTotalOutputValue()

private calculateTotalOutputValue(): bigint

Calculates the total value of outputs (spend + change).

Returns

bigint

The total output value.

Source

transaction/coinselector-fewest.ts:179


createChangeOutputs()

private createChangeOutputs(change): UTXO[]

Creates change outputs based on the change amount and input denominations.

Parameters

ParameterTypeDescription
changebigintThe change amount to return.

Returns

UTXO[]

The change outputs.

Source

transaction/coinselector-fewest.ts:157


createSpendOutputs()

private createSpendOutputs(amount): UTXO[]

Creates spend outputs based on the target amount and input denominations.

Parameters

ParameterTypeDescription
amountbigintThe target amount to spend.

Returns

UTXO[]

The spend outputs.

Source

transaction/coinselector-fewest.ts:137


decreaseFee()

decreaseFee(feeReduction): SelectedCoinsResult

Decreases the fee by removing inputs if possible and adjusting change outputs.

Parameters

ParameterTypeDescription
feeReductionbigintThe amount by which the fee has decreased.

Returns

SelectedCoinsResult

Source

transaction/coinselector-fewest.ts:286


findMinimalUTXOSet()

private findMinimalUTXOSet(sortedUTXOs, totalRequired): UTXO[]

Finds the minimal set of UTXOs that can cover the total required amount.

Parameters

ParameterTypeDescription
sortedUTXOsUTXO[]Available UTXOs sorted by denomination (ascending).
totalRequiredbigintThe total amount required (target + fee).

Returns

UTXO[]

The minimal set of UTXOs.

Source

transaction/coinselector-fewest.ts:103


getMaxDenomination()

private getMaxDenomination(utxos): bigint

Gets the maximum denomination value from a list of UTXOs.

Parameters

ParameterTypeDescription
utxosUTXO[]The list of UTXOs.

Returns

bigint

The maximum denomination value.

Source

transaction/coinselector-fewest.ts:219


getMaxInputDenomination()

private getMaxInputDenomination(): bigint

Gets the maximum denomination value from the selected UTXOs.

Returns

bigint

The maximum input denomination value.

Source

transaction/coinselector-fewest.ts:198


getMaxOutputDenomination()

private getMaxOutputDenomination(): bigint

Gets the maximum denomination value from the spend and change outputs.

Returns

bigint

The maximum output denomination value.

Source

transaction/coinselector-fewest.ts:208


increaseFee()

increaseFee(additionalFeeNeeded): SelectedCoinsResult

Increases the total fee by first reducing change outputs, then selecting additional inputs if necessary.

Parameters

ParameterTypeDescription
additionalFeeNeededbigintThe additional fee needed.

Returns

SelectedCoinsResult

Returns true if successful, false if insufficient funds.

Source

transaction/coinselector-fewest.ts:232


performSelection()

performSelection(target, fee): SelectedCoinsResult

Performs coin selection to meet the target amount plus fee, using the smallest possible denominations and minimizing the number of inputs and outputs.

Parameters

ParameterTypeDescription
targetbigintThe target amount to spend.
feebigintThe fee amount to include in the selection.

Returns

SelectedCoinsResult

The selected UTXOs and outputs.

Overrides

AbstractCoinSelector.performSelection

Source

transaction/coinselector-fewest.ts:24


sortUTXOsByDenomination()

private sortUTXOsByDenomination(utxos, direction): UTXO[]

Sorts UTXOs by their denomination.

Parameters

ParameterTypeDescription
utxosUTXO[]The UTXOs to sort.
direction"asc" | "desc"The direction to sort (‘asc’ for ascending, ‘desc’ for descending).

Returns

UTXO[]

The sorted UTXOs.

Source

transaction/coinselector-fewest.ts:340


validateUTXOs()

private validateUTXOs(): void

Validates the available UTXOs.

Returns

void

Throws

Will throw an error if there are no available UTXOs.

Source

transaction/coinselector-fewest.ts:362