NETSoon

Docs SDK

Subscribing

Follow one transaction through its trail and watch slots start and execute with @acrux/sdk, or read the raw event stream.

The SDK reads the gateway's event stream for you. tx.follow gives one transaction's trail; slots.subscribe gives every slot; gateway.subscribe gives every protocol event.

Follow a transaction

follow.ts
const { id } = await acrux.send(tx, { n: 3, t: 2, signer: payer })

for await (const update of acrux.tx.follow(id)) {
  console.log(update.state, `${update.included} of ${update.threshold}`)
}

follow(id) is an async iterator. It opens a stream filtered to the transaction, reads the gateway's record of it once the stream is open, and yields each step forward once. The loop ends after the last state.

StateArrives
sentthe gateway admitted it for its target slot
includeda proposer included a copy or fragment; once per fragment, included counting up
executedthe transaction ran in its slot's xblock
finalizedits cblock is final; the loop ends
failedit executed and failed; fees are charged; the loop ends
expiredfewer than t fragments were included within the fragment expiry; the loop ends

Each update holds:

FieldTypeMeaning
idhashthe transaction's ID
stateone of the states abovewhere it is now
includedintegercopies or fragments included so far
thresholdintegert, the fragments that make it readable
transactionTransactionthe full record: header, inclusions, executed slot, result, fees

Leaving the loop early, with break or return, closes the stream.

Slots

slots.ts
const stop = acrux.slots.subscribe((slot) => {
  console.log(slot.slot, slot.status)
})

// later
stop()

The callback receives each Slot twice: when it starts, and again when it executes with its agreed set, merged order and state hash. subscribe returns the function that stops it.

Every event

acrux.gateway.subscribe is the contract's raw stream client: any filter, every event type.

events.ts
const sub = acrux.gateway.subscribe({ kind: 'all' }, (event) => {
  if (event.type === 'slot.agreed') console.log(event.slot, event.data.agreedSet)
})

// later
sub.close()

The stream opens with a stream.snapshot. A subscribe without a cursor is answered with a stream.resync, and one with a retained cursor with the events after it; on a snapshot or a resync, replace local state with it. Event stream

Reads at rest

acrux.gateway.call calls any JSON-RPC method, typed by the contract:

ts
const window = await acrux.gateway.call('getCurrentWindow', {})
const recent = await acrux.gateway.call('getRecentSlots', { limit: 16 })
const record = await acrux.gateway.call('getTransaction', { id })