BitcoinDev Logo

Coin Selection

When building a transaction, you must choose which UTXOs to spend so that their total value covers the payment amount plus fees. Coin selection is the process of picking those inputs and, when necessary, creating a change output back to your wallet. This page covers implementation (effective value, fee budget, selection loop), common strategies (largest-first, branch-and-bound, privacy-aware), and wallet best practices. For fee rate sources, see Fee Estimation.

The Problem

Given:

  • A set of available UTXOs (each with amount and script/address type, so you can compute input size)
  • A payment amount (in satoshis)
  • A target fee rate (sat/vB)

You must:

  1. Select inputs such that total input value ≥ payment amount + fee for the transaction
  2. Optionally create a change output if input value exceeds payment + fee (and change is above dust)

Fee depends on transaction size (inputs + outputs), so in practice you may iterate: pick inputs, estimate size, compute fee, then adjust (e.g. add one more input or reduce change).

Effective Value

The effective value of a UTXO is its amount minus the cost to spend it at a given fee rate:

effective_value = amount - (input_vbytes × fee_rate)

Input size (vbytes) depends on the script type (e.g. P2PKH ~148, P2WPKH ~68, P2TR ~57.5). At high fee rates, small UTXOs can have negative effective value and should be excluded from selection.

Simple Selection: Cover Amount + Fee

A minimal approach: sort UTXOs by effective value (or amount), then greedily add inputs until total input value ≥ payment + estimated fee. Estimate fee from a baseline size (e.g. one input, two outputs: payment + change) then add per-input size for each selected input.

Change Output

If selected inputs exceed payment + fee, the remainder is change. Create a new output (to an address you control) for that amount. If the remainder is below the dust threshold (e.g. 546 sats), you may add it to the fee (no change output) to avoid an unspendable output. In the snippets above, we only allow change ≥ 546 or exactly 0.

When to create change: Create a change output if the remainder is above the dust threshold (typically 546 sats). If change would be dust, either donate it to the miner (include in fee) or select different UTXOs.

Coin Selection Strategies

Beyond the simple greedy approach above, wallets use different strategies with different trade-offs:

StrategyDescriptionProsCons
Largest firstSort by value descending, select until payment + fee coveredSimple, minimizes inputs, fastMay overpay fees, poor privacy
Smallest firstSort by value ascendingConsolidates small UTXOs, better privacyMore inputs, higher fees
Exact matchUse a single UTXO that equals payment + feeNo change output, cleanRarely available; needs fallback
Branch and boundSearch for combination minimizing size/change/inputsOptimal, best privacyComputationally expensive
RandomRandomly select until sum ≥ payment + feeGood privacy, unpredictableMay be suboptimal

Privacy-aware selection (e.g. avoiding linking transactions, preferring smaller UTXOs when appropriate) improves user privacy. See Privacy Techniques for context.

Implementation Considerations

  • Iterative approach: Estimate fee from initial selection, then select UTXOs, then recalculate fee from actual size; adjust (e.g. add input or drop change) if needed.
  • Fee rate targets: Use Fee Estimation for sat/vB; common bands include low (1–5), medium (5–10), high (10–50), urgent (50+).
  • Validation: Before finalizing, verify UTXOs are still unspent, balance is sufficient, and fee rate meets target.

Best Practices

  • Prefer SegWit UTXOs (smaller size, lower fees).
  • Minimize number of inputs when fee is high.
  • Avoid creating dust change outputs.
  • Consider privacy when choosing a strategy (see Privacy Techniques).

Common Issues

  • Insufficient funds: Select more UTXOs, reduce payment, or wait for more funds.
  • Fee too low: Use fewer UTXOs if possible, or increase fee rate.
  • Dust change: Donate to miner (add to fee), increase payment, or pick different UTXOs.

Integration with Transaction Building

Use the selected UTXOs as inputs and amounts in your transaction: one output for the payment, and one for change (if any). Fee = sum(inputs) - sum(outputs). Get the fee rate from Fee Estimation.

  • Transaction Construction - Building the transaction from selected UTXOs
  • Fee Estimation - Getting fee rate (sat/vB)
  • Transaction Fees - Fee market and dust
  • UTXO Model - What UTXOs are and how they are spent
  • Wallet Development - HD wallets, address types, transaction creation
  • Privacy Techniques - Privacy-aware coin selection and wallet design