getAncestors

Returns the ancestor transaction history for a revision or transaction id.

Type

getAncestors(location: string, verbosity?: number): Promise<string[] | Map<string, string>>

Parameters

location

A transaction id or a revision (<txid>:<vout>). Ancestors are resolved for the transaction id.

verbosity (optional)

  • omitted / not 1 — array of ancestor transaction ids
  • 1Map<txId, hex> of those transactions

Return Value

Ancestor transaction ids (default), or a map of id → raw hex when verbosity === 1.

Syntax

computer.getAncestors(rev)
computer.getAncestors(rev, 1)

Inside smart contracts (InnerComputer)

  • Starting location must be confirmed.
  • Empty arrays are valid stable results when there are no ancestors.
  • Missing or unconfirmed starts invalidate the evaluation.
  • Verbosity maps are a client-side convenience; contracts typically use the default string[] form.

See Contract – Querying.

Example

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

describe('getAncestors', () => {
  class Counter extends Contract {
    n: number

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

  it('Should return the ancestor transactions id of a given revision', async () => {
    const computer = new Computer({ chain, network, url })
    await computer.faucet(1e8)

    const counter = await computer.new(Counter, [])
    await counter.inc()
    await counter.inc()

    const ancestors = await computer.getAncestors(counter._rev)
    expect(ancestors).to.be.an('array').that.has.lengthOf(3)
    expect(ancestors).includes(counter._id.substring(0, 64))
    expect(ancestors).includes(counter._rev.substring(0, 64))
    expect(ancestors).includes((await computer.prev(counter._rev))!.substring(0, 64))
  })
})

Source