Handlers
Handlers are straightforward functions utilized to execute custom logic on indexed data. These handlers are triggered either when the indexer captures live data or when processing past logs, depending on the configuration.
The EVMIndex provides a context object that contains the indexed data and is passed to the handler function. To access and utilize the context effectively, you can refer to the following example, which demonstrates how to access the context and assign appropriate types to it.
While it is possible to create handlers anywhere within the project, it is advisable to adhere to the handlers/<Contract>/<filename.ts>
pattern.
Furthermore, it is crucial to note that the handler functions must have named exports rather than default exports. Finally, these handler functions should be added to the config.yaml
file for proper integration and execution.
Important note
EVMIndex recommends utilizing Typechain for generating contract types. Here's an example showcasing how to generate types for contracts using Typechain. Automation for this process will be implemented in the future.
npm install --save-dev typechain @typechain/ethers-v5 @ethersproject/abstract-provider
npx typechain --target ethers-v5 --outDir ./types/generated ./abis/*.json
Specify your ABI path instead of ./abis/*.json
in the above command.
In this example, we are creating handlers for the Transfer
events.
import {TransferEventObject} from "@src/types/Meka"; // type generated by Typechain
import {Log} from "@ethersproject/abstract-provider";
export const handleTransfer = async (context: TransferEventObject & {txn: Log}) => {
console.log(context)
// your custom logic here
}
Notice how the functions have names exports such as export const
instead of export default
, this is known as named export.
Configuration
Check the example configuration here.
Example Project
Check the example project here