FewestCoinSelector
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()
Constructs a new AbstractCoinSelector instance with an empty UTXO array.
Parameters
Parameter | Type | Default value |
---|---|---|
availableUTXOs | UTXO [] | [] |
Returns
Inherited from
AbstractCoinSelector.constructor
Source
transaction/abstract-coinselector.ts:53
Methods
adjustChangeOutputs()
Helper method to adjust change outputs.
Parameters
Parameter | Type | Description |
---|---|---|
changeAmount | bigint | The amount to adjust change outputs by. |
Returns
void
Source
transaction/coinselector-fewest.ts:324
calculateTotalOutputValue()
Calculates the total value of outputs (spend + change).
Returns
bigint
The total output value.
Source
transaction/coinselector-fewest.ts:179
createChangeOutputs()
Creates change outputs based on the change amount and input denominations.
Parameters
Parameter | Type | Description |
---|---|---|
change | bigint | The change amount to return. |
Returns
UTXO
[]
The change outputs.
Source
transaction/coinselector-fewest.ts:157
createSpendOutputs()
Creates spend outputs based on the target amount and input denominations.
Parameters
Parameter | Type | Description |
---|---|---|
amount | bigint | The target amount to spend. |
Returns
UTXO
[]
The spend outputs.
Source
transaction/coinselector-fewest.ts:137
decreaseFee()
Decreases the fee by removing inputs if possible and adjusting change outputs.
Parameters
Parameter | Type | Description |
---|---|---|
feeReduction | bigint | The amount by which the fee has decreased. |
Returns
SelectedCoinsResult
Source
transaction/coinselector-fewest.ts:286
findMinimalUTXOSet()
Finds the minimal set of UTXOs that can cover the total required amount.
Parameters
Parameter | Type | Description |
---|---|---|
sortedUTXOs | UTXO [] | Available UTXOs sorted by denomination (ascending). |
totalRequired | bigint | The total amount required (target + fee). |
Returns
UTXO
[]
The minimal set of UTXOs.
Source
transaction/coinselector-fewest.ts:103
getMaxDenomination()
Gets the maximum denomination value from a list of UTXOs.
Parameters
Parameter | Type | Description |
---|---|---|
utxos | UTXO [] | The list of UTXOs. |
Returns
bigint
The maximum denomination value.
Source
transaction/coinselector-fewest.ts:219
getMaxInputDenomination()
Gets the maximum denomination value from the selected UTXOs.
Returns
bigint
The maximum input denomination value.
Source
transaction/coinselector-fewest.ts:198
getMaxOutputDenomination()
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()
Increases the total fee by first reducing change outputs, then selecting additional inputs if necessary.
Parameters
Parameter | Type | Description |
---|---|---|
additionalFeeNeeded | bigint | The additional fee needed. |
Returns
SelectedCoinsResult
Returns true if successful, false if insufficient funds.
Source
transaction/coinselector-fewest.ts:232
performSelection()
Performs coin selection to meet the target amount plus fee, using the smallest possible denominations and minimizing the number of inputs and outputs.
Parameters
Parameter | Type | Description |
---|---|---|
target | bigint | The target amount to spend. |
fee | bigint | The fee amount to include in the selection. |
Returns
SelectedCoinsResult
The selected UTXOs and outputs.
Overrides
AbstractCoinSelector.performSelection
Source
transaction/coinselector-fewest.ts:24
sortUTXOsByDenomination()
Sorts UTXOs by their denomination.
Parameters
Parameter | Type | Description |
---|---|---|
utxos | UTXO [] | 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()
Validates the available UTXOs.
Returns
void
Throws
Will throw an error if there are no available UTXOs.
Source
Was this page helpful?