UtxoManager

Manages UTXO state from transaction updates.

Handles:

  • Processing transaction updates (created/spent UTXOs)

  • Three-state UTXO lifecycle (AVAILABLE → PENDING → SPENT)

  • Transaction failure handling (unlock UTXOs)

  • Calculating balances from available UTXOs only

Transaction Status Handling:

  • SUCCESS/PARTIAL_SUCCESS: Create UTXOs (AVAILABLE), mark spent as SPENT

  • FAILURE: Don't create UTXOs, unlock spent UTXOs (PENDING → AVAILABLE)

Thread-safe: All operations are suspend functions using Room's built-in thread safety.

Constructors

Link copied to clipboard
constructor(utxoDao: UnshieldedUtxoDao)

Types

Link copied to clipboard
sealed class ProcessingResult

Result of processing a transaction update.

Functions

Link copied to clipboard
suspend fun calculateBalance(address: String): Map<String, BigInteger>

Calculate balance for an address.

Link copied to clipboard
suspend fun clearUtxos(address: String)

Delete all UTXOs for an address.

Link copied to clipboard
suspend fun debugDumpAllUtxos(address: String, tag: String)

Debug: Dump all UTXOs for an address to logs.

Link copied to clipboard

Get detailed UTXO list for an address.

Link copied to clipboard
suspend fun hasAnyUtxos(address: String): Boolean

Check if database has ANY UTXOs for this address (any state).

Link copied to clipboard
suspend fun hasAvailableUtxos(address: String): Boolean

Check if database has AVAILABLE UTXOs for this address.

Link copied to clipboard
suspend fun markUtxosAsSpent(utxoIds: List<String>)

Mark UTXOs as spent by their database IDs.

Link copied to clipboard
suspend fun markUtxosAsSpentByIntent(utxoIntentIds: List<Pair<String, Int>>, spentByLocalTx: Boolean = true): Int

Mark UTXOs as spent by their intent identifiers (intentHash + outputNo).

Link copied to clipboard
fun observeBalance(address: String): Flow<Map<String, BigInteger>>

Observe balance changes for an address (available UTXOs only).

Link copied to clipboard

Observe pending balance for an address (locked in pending transactions).

Link copied to clipboard
fun observeUtxoCounts(address: String): Flow<Map<String, Int>>

Observe UTXO counts per token type (available UTXOs only).

Link copied to clipboard

Process transaction update from subscription.

Link copied to clipboard
suspend fun resetSpentUtxosForHealing(address: String): Int

Reset ALL SPENT UTXOs to AVAILABLE for self-healing.

Link copied to clipboard
suspend fun selectAndLockUtxos(address: String, tokenType: String, requiredAmount: BigInteger): UtxoSelector.SelectionResult

Select and lock UTXOs for transaction (atomic operation).

Link copied to clipboard

Select and lock UTXOs for multiple token types (atomic operation).

Link copied to clipboard
suspend fun unlockUtxos(utxoIds: List<String>)

Unlock UTXOs (mark as AVAILABLE).