hive-tx v7
GitHub npm

Use

Streams

streamBlocks, streamBlockNumbers, and streamOperations follow the chain's irreversible blocks, in order, with no gaps or duplicates.

Streams read only irreversible blocks, so you never see a block that later disappears in a fork. Hive blocks become irreversible within seconds, so streams run close to real time.

All three are async generators. They catch up on past blocks in batches of 20, then poll every second. break stops a stream. If every node fails, the stream throws.

OptionDefaultMeaning
fromlast irreversible blockFirst block to yield
tononeLast block to yield; the stream ends after it
only_virtualfalsestreamOperations only: yield just virtual operations

streamOperations

Yields every operation in chain order, virtual operations included, like get_ops_in_block. Virtual operations are the ones the chain creates itself, such as rewards, order fills, and interest.

import { streamOperations } from 'hive-tx'

for await (const { block, trx_id, op } of streamOperations()) {
  if (op[0] === 'transfer') console.log(block, trx_id, op[1].from, op[1].amount)
  if (op[0] === 'producer_reward') console.log(block, op[1].producer)
}

Each item has block, trx_id, trx_in_block, op_in_trx, virtual_op, timestamp, and op.

streamBlocks

Yields whole blocks with their transactions.

import { streamBlocks } from 'hive-tx'

for await (const block of streamBlocks()) {
  for (const tx of block.transactions) {
    for (const [name, body] of tx.operations) {
      if (name === 'transfer') {
        console.log(block.block_num, tx.transaction_id, body.from, body.to, body.amount)
      }
    }
  }
}

streamBlockNumbers

Yields block numbers only, without fetching the blocks. Use it to drive your own fetching.

import { streamBlockNumbers } from 'hive-tx'

for await (const num of streamBlockNumbers()) {
  console.log(num)
}

Resume after a restart

Save the number of the last block you fully handled, then restart with from set to the next one:

for await (const block of streamBlocks({ from: savedBlockNum + 1 })) {
  await handle(block)
  savedBlockNum = block.block_num
}

Read a past range by setting both ends: streamBlocks({ from: 1000, to: 1999 }).