Please refer to the documentation website for a thorough guide on all Envio indexer features
The onBlock API of HyperIndex lets you run custom logic on every block or at fixed block intervals. This is useful for aggregations, time-series operations, and bulk updates.
In this example, we'll walk through how to use the onBlock handler to take a snapshot of a token's total supply every 1000 blocks.
First, import onBlock from generated:
import { onBlock } from "generated";The onBlock function takes two arguments:
- Options – configure the handler's name, the chain it should run on, and interval-related settings such as
interval,startBlock, andstopBlock. - Handler function – contains your custom logic. Similar event handlers, it has access to relevant data, which in this case is block values like
timestampandchainId.
Example usage:
onBlock(
{
name: "MY_BLOCK_HANDLER",
chain: CHAIN_ID,
interval: BLOCK_INTERVAL
},
async ({ block, context }) => {
// your onBlock logic here
}
)Before running the indexer locally, make sure you have the following installed:
Add your Envio API key to the .env file, then start the indexer:
pnpm devIf you make changes to config.yaml or schema.graphql, regenerate the type files:
pnpm codegenWhile indexer is running, visit the Envio Console(https://envio.dev/console) to open the GraphQL Playground and query your indexed data.