getUtxos

Returns UTXOs that do not contains on-chain objects.

Type

getUtxos(address?: string): Promise<string[]>

Parameters

address

The address for which to return the UTXOs. If undefined, the UTXOs for the calling object are returned.

Return Value

Returns all unspent transaction outputs (UTXOs) of the address in question. The UTXOs are formatted as strings of the form <transaction-id>:<output-number>

Description

The UTXOs returned are guaranteed to not contain any on-chain objects. This makes it possible to ensure to not spend on-chain objects by mistake.

Example

import { Computer, Contract } from '@bitcoin-computer/lib'
import { chain, expect, network, url } from '../../utils/index.js'

describe('getUTXOs', () => {
  let computer1: Computer
  let computer2: Computer

  before('Fund computers', async () => {
    computer1 = new Computer({ chain, network, url })
    computer2 = new Computer({ chain, network, url })
    await computer1.faucet(1e8)
  })

  it('Should return all UTXOs that do not contain on-chain objects', async () => {
    // computer1 creates two UTXOs for computer2
    const txId1 = await computer1.send(10000n, computer2.getAddress())
    const txId2 = await computer1.send(10000n, computer2.getAddress())

    // Check that computer2 has both UTXOS
    const utxos = await computer2.getUTXOs({ address: computer2.getAddress(), isObject: false })
    expect(new Set(utxos)).deep.eq(new Set([`${txId1}:0`, `${txId2}:0`]))
  })

  it('Should not return UTXOs that contain on-chain objects', async () => {
    // A smart contract
    class C extends Contract {}

    // Broadcast UTXO containing an on-chain object
    const c = await computer1.new(C, [])

    // Check that the UTXO containing the on-chain object is not
    // returned by getUTXOs
    const utxos = await computer1.getUTXOs({ address: computer1.getAddress(), isObject: false })
    expect(!utxos.some((item) => item === c._id))
  })
})

Source