Please refer to the documentation website for a thorough guide on all Envio indexer features
With HyperIndex, the Effects API allows you to perform external calls from your handlers. These calls run in parallel with your handler logic, so they don’t block execution.
You can learn more about the Effects API in the documentation: https://docs.envio.dev/docs/HyperIndex/effect-api
The following example extends the factory pattern example to fetch the decimal of a token using an RPC call via Viem.
To create an effect in your handler, use the experimental_createEffect
function from the envio
package.
This function takes two arguments: effect options and a handler function.
Effect options:
name
: used for debugging and logginginput
: the input type of the effectoutput
: the output type of the effectcache
: whether to cache the effect result in the database
To use the effect, use context.effect
from your handlers, loaders, or other effects:
CONTRACT.EVENT.handler(async ({ event, context }) => {
const effectOutput = await context.effect(YOUR_EFFECT, EFFECT_INPUTS);
});
The following effect fetches the decimal of a token using an RPC call:
const client = createPublicClient({
chain: mainnet,
transport: http(process.env.ETHEREUM_MAINNET_RPC!),
});
const ERC20_ABI = parseAbi(["function decimals() view returns (uint8)"]);
const fetchTokenDetails = experimental_createEffect(
{
name: "fetchTokenDetails",
input: {
token: S.string,
},
output: {
decimal: S.number,
},
},
async ({ input }) => {
const decimals = await client.readContract({
address: input.token as `0x${string}`,
abi: ERC20_ABI,
functionName: "decimals",
});
console.log(`Token decimals: ${decimals}`);
return { decimal: decimals };
}
);
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 dev
If you make changes to config.yaml
or schema.graphql
, regenerate the type files:
pnpm codegen
While indexer running, visit the Envio Console(https://envio.dev/console) to open the GraphQL Playground and query your indexed data.