Manual Coin Selection
Manual coin selection allows you to explicitly choose which UTXOs (unspent transaction outputs) from your wallet to use when building a transaction. By default, LWK automatically add all UTXOs to cover the transaction amount and fees. However, manual coin selection gives you control over which specific UTXOs are spent, which can be useful for privacy, UTXO management, or specific transaction requirements.
When you enable manual coin selection, only the UTXOs you specify will be used. The transaction builder will not automatically add additional UTXOs, so you must ensure the selected UTXOs provide sufficient funds to cover the transaction amount and fees.
To use manual coin selection, first retrieve the available UTXOs from your wallet using Wollet::utxos(), then call TxBuilder::set_wallet_utxos() before calling finish(). The selected UTXOs must belong to the wallet generating the transaction.
The set_wallet_utxos() method takes the following argument:
utxos(Vec<OutPoint>): A vector of outpoints (transaction ID and output index) identifying the UTXOs to use. Each outpoint must correspond to a UTXO owned by the wallet.
Getting Available UTXOs
Before selecting UTXOs manually, you need to retrieve the list of available UTXOs from your wallet:
let utxos = w.wollet.utxos()?;
utxos = wollet.utxos()
Manual Coin Selection for L-BTC
The simplest use case is selecting specific L-BTC UTXOs for a transaction:
let sent_satoshi = 200_000;
let mut pset = w
.tx_builder()
.add_recipient(&node_address, sent_satoshi, policy_asset)?
.set_wallet_utxos(vec![utxos[0].outpoint])
.finish()?;
signer.sign(&mut pset)?;
// Broadcast the transaction
let tx = w.wollet.finalize(&mut pset).unwrap();
let tx = serialize(&tx);
builder = network.tx_builder()
builder.add_lbtc_recipient(node_address, sent_satoshi)
builder.set_wallet_utxos([utxos[0].outpoint()])
unsigned_pset = builder.finish(wollet)
signed_pset = signer.sign(unsigned_pset)
finalized_pset = wollet.finalize(signed_pset)
tx = finalized_pset.extract_tx()
Manual Coin Selection with Assets
When sending assets, you must include sufficient L-BTC UTXOs to cover transaction fees. If you only select asset UTXOs without L-BTC, the transaction will fail with an InsufficientFunds error.
Next Steps
After learning about manual coin selection, you can:
- Learn about external UTXOs for using UTXOs not in your wallet
- Explore transaction creation for other transaction building options
- Use send all funds to automatically select all L-BTC UTXOs
Previous: Transaction Creation