Skip to main content

Processor Context

BatchContext is a generic interface defined as follows:

export interface BatchContext<Store, Item> {
/**
* access to the chain and the metadata
* @internal
*/
_chain: Chain
log: Logger
/**
* Handle for the data sink
*/
store: Store
/**
* Next batch of items to be processed, grouped into blocks
*/
blocks: BatchBlock<Item>[]
/**
* Signals that the processor has reached the chain head.
*
* The head block is always included in `.blocks`.
*/
isHead: boolean
}

Note that the Item type is inferred from the processor type and configuration. Store type is inferred from the Database instance passed into the run() method.

ctx._chain

Internal handle for direct access to the underlying chain state via RPC calls. Rarely used directly, but rather by the facade access classes generated by the typegen tools.

ctx.store

Interface for the target data sink. See Store Interface.

ctx.log

The native logger handle. See logging.

ctx.blocks

The on-chain data items (event logs and transaction call records) are grouped into blocks and canonically ordered by how the data is recorded on-chain. The shape of item objects is determined by the processor configuration done via the .addXXX() methods.

An idiomatic use of the context API is to iterate first over blocks and then over items for each block:

processor.run(new TypeormDatabase(), async (ctx) => {
for (const block of ctx.blocks) {
for (const item of c.items) {
//
}
}
});

The canonical ordering of ctx.blocks enables efficient in-memory data processing. For example, multiple updates of the same entity can be compressed into a single database transaction.

ctx.isHead

Set to true if the processor has reached the chain head. The last block ctx.blocks is then the current chain tip.