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
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.
| State | Arrives |
|---|---|
sent | the gateway admitted it for its target slot |
included | a proposer included a copy or fragment; once per fragment, included counting up |
executed | the transaction ran in its slot's xblock |
finalized | its cblock is final; the loop ends |
failed | it executed and failed; fees are charged; the loop ends |
expired | fewer than t fragments were included within the fragment expiry; the loop ends |
Each update holds:
| Field | Type | Meaning |
|---|---|---|
id | hash | the transaction's ID |
state | one of the states above | where it is now |
included | integer | copies or fragments included so far |
threshold | integer | t, the fragments that make it readable |
transaction | Transaction | the full record: header, inclusions, executed slot, result, fees |
Leaving the loop early, with break or return, closes the stream.
Slots
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.
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:
const window = await acrux.gateway.call('getCurrentWindow', {})
const recent = await acrux.gateway.call('getRecentSlots', { limit: 16 })
const record = await acrux.gateway.call('getTransaction', { id })