query

Returns unspent smart-object revisions matching the query (legacy API).

Type

type Query = {
  mod?: string
  publicKey?: string
  limit?: number
  offset?: number
  order?: 'ASC' | 'DESC'
}

async query(q: Query): Promise<string[]>

Parameters

q

Key Description
publicKey Filter by public key present in the output script
mod Filter smart objects created with this module specifier (membership)
limit Maximum number of revisions returned
offset Number of results to skip
order ASC or DESC

There is no ids parameter on the current implementation.

Return Value

An array of revision strings for unspent smart objects (same as getOUTXOs with verbosity: 0).

Description

Historically used as the main “find my objects” API. Prefer:

const revs = await computer.getOUTXOs({ publicKey, mod, limit, offset, order })
// or with full rows:
const rows = await computer.getOUTXOs({ publicKey, verbosity: 1 })

To list module deploys (source on chain), use getModules / getModule, not query / getOUTXOs.

Example

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

describe('query', () => {
  // A smart contract
  class Counter extends Contract {
    n: number

    constructor() {
      super({ n: 0 })
    }
    inc() {
      this.n += 1
    }
  }

  let computer: Computer
  let publicKey: string
  let counter: Counter
  let mod: string

  before('Before tests for query', async () => {
    computer = new Computer({ chain, network, url })
    publicKey = computer.getPublicKey()
    await computer.faucet(1e8)

    // Deploy module
    mod = await computer.deploy(`export ${Counter}`)

    // Encode on-chain object and create it by broadcasting the transaction
    const { effect, tx } = await computer.encode({ exp: 'new Counter()', mod })
    await computer.broadcast(tx!)

    // Increment on-chain object
    counter = effect.res as SmartContract<typeof Counter>
    await counter.inc()
    await computer.waitForIndexed(counter._rev)
  })

  // Query by public key
  it('Should return the latest revisions for a public key', async () => {
    const revs = await computer.query({ publicKey })
    expect(revs.includes(counter._rev)).to.be.true
  })

  // Query by module specifier
  it('Should return the latest revisions for a module specifier', async () => {
    const [rev] = await computer.query({ mod })
    expect(rev).eq(counter._rev)
  })

  // Query by multiple parameters
  it('Should return the latest revisions for multiple parameters', async () => {
    const revs = await computer.query({ publicKey, limit: 1 })
    expect(revs.length).eq(1)
  })
})

Source