Setting up Handlers

The handlers take data from a particular source and transform it into entities that are defined within your schema. Handlers are written in TypeScript.

For each handler that is defined in studio.yaml under triggers, create an exported function of the same name in src/handlers folder.

blockflow codegen

Use above command to generate boilerplate code in path specified in studio.yaml


Handlers boilerplate

Based on the trigger-type contract/function or contract/event code vary.

  • Function Handler Code

import { IFunctionContext, IBind, ISecrets } from "@blockflow-labs/utils";

/**
 * @dev Function::approve(address _spender, uint256 _value)
 * @param context trigger object with contains {functionParams: {_spender ,_value }, transaction, block}
 * @param bind init function for database wrapper methods
 */
export const approveHandler = async (
  context: IFunctionContext,
  bind: IBind,
  secrets: ISecrets,
) => {
  // Implement your function handler logic for approve here

  const { functionParams, transaction, block } = context;
  const { _spender, _value } = functionParams;
};
  • Event Handler Code

import {
  IEventContext,
  IBind,
  Instance,
  ISecrets,
} from "@blockflow-labs/utils";

/**
 * @dev Event::Transfer(address from, address to, uint256 value)
 * @param context trigger object with contains {event: {from ,to ,value }, transaction, block, log}
 * @param bind init function for database wrapper methods
 */
export const TransferHandler = async (
  context: IEventContext,
  bind: IBind,
  secrets: ISecrets,
) => {
  // Implement your event handler logic for Transfer here

  const { event, transaction, block, log } = context;
  const { from, to, value } = event;
};

To read more about handlers code use this doc.