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(availableUXTOs?): FewestCoinSelector

Constructs a new AbstractCoinSelector instance with an empty UTXO array.

Parameters

ParameterTypeDefault valueDescription
availableUXTOs?UTXOEntry[][]The initial available UTXOs.

Returns

FewestCoinSelector

Inherited from

AbstractCoinSelector.constructor

Source

transaction/abstract-coinselector.ts:99

Accessors

availableUXTOs

get availableUXTOs(): UTXO[]

Gets the available UTXOs.

set availableUXTOs(value): void

Sets the available UTXOs.

Parameters

ParameterTypeDescription
valueUTXOLike[]The UTXOs to set.

Returns

UTXO[]

The available UTXOs.

Source

transaction/abstract-coinselector.ts:47


changeOutputs

get changeOutputs(): UTXO[]

Gets the change outputs.

set changeOutputs(value): void

Sets the change outputs.

Parameters

ParameterTypeDescription
valueUTXOLike[]The change outputs to set.

Returns

UTXO[]

The change outputs.

Source

transaction/abstract-coinselector.ts:83


spendOutputs

get spendOutputs(): UTXO[]

Gets the spend outputs.

set spendOutputs(value): void

Sets the spend outputs.

Parameters

ParameterTypeDescription
valueUTXOLike[]The spend outputs to set.

Returns

UTXO[]

The spend outputs.

Source

transaction/abstract-coinselector.ts:67

Methods

performSelection()

performSelection(target): SelectedCoinsResult

The largest first coin selection algorithm.

This algorithm selects the largest UTXOs first, and continues to select UTXOs until the target amount is reached. If the total value of the selected UTXOs is greater than the target amount, the remaining value is returned as a change output.

Parameters

ParameterTypeDescription
targetSpendTargetThe target amount to spend.

Returns

SelectedCoinsResult

The selected UTXOs and change outputs.

Overrides

AbstractCoinSelector.performSelection

Source

transaction/coinselector-fewest.ts:27


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:149


validateTarget()

private validateTarget(target): void

Validates the target amount.

Parameters

ParameterTypeDescription
targetSpendTargetThe target amount to validate.

Returns

void

Throws

Will throw an error if the target amount is less than or equal to 0.

Source

transaction/coinselector-fewest.ts:168


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:179