|
| 1 | +--- |
| 2 | +name: indexer-external-calls |
| 3 | +description: >- |
| 4 | + Use when making RPC calls, fetch requests, or any external I/O from handlers. |
| 5 | + Effect API with createEffect, S schema validation, context.effect(), preload |
| 6 | + optimization (handlers run twice), cache and rateLimit options. |
| 7 | +metadata: |
| 8 | + managed-by: envio |
| 9 | +--- |
| 10 | + |
| 11 | +# External Calls (Effect API) |
| 12 | + |
| 13 | +Handlers run twice: parallel **preload pass** (warms caches), then **sequential pass** (state changes). All external I/O (fetch, RPC, APIs) MUST go through `createEffect` + `context.effect()` — otherwise it double-executes and blocks parallelization. |
| 14 | + |
| 15 | +## Define and call |
| 16 | + |
| 17 | +```ts |
| 18 | +import { S, createEffect } from "envio"; |
| 19 | + |
| 20 | +const getOwner = createEffect( |
| 21 | + { |
| 22 | + name: "getOwner", |
| 23 | + input: { tokenId: S.bigint }, |
| 24 | + output: S.union([S.string, null]), |
| 25 | + cache: true, |
| 26 | + rateLimit: false, |
| 27 | + }, |
| 28 | + async ({ input }) => { |
| 29 | + const res = await fetch(`https://api.example.com/owner/${input.tokenId}`); |
| 30 | + return res.json(); |
| 31 | + } |
| 32 | +); |
| 33 | + |
| 34 | +indexer.onEvent( |
| 35 | + { contract: "Token", event: "Transfer" }, |
| 36 | + async ({ event, context }) => { |
| 37 | + const owner = await context.effect(getOwner, { tokenId: event.params.tokenId }); |
| 38 | + } |
| 39 | +); |
| 40 | +``` |
| 41 | + |
| 42 | +## Pass minimum input |
| 43 | + |
| 44 | +`input` carries only what varies per call. Bake static config (URLs, tokens, channel IDs, env vars) into the effect body. Build payloads/strings inside the effect. |
| 45 | + |
| 46 | +```ts |
| 47 | +// ❌ input: { url, chatId, text } — config and pre-built strings leak into the call site |
| 48 | +// ✅ input: { usd, blockNumber } — only the values that vary per call |
| 49 | +``` |
| 50 | + |
| 51 | +Dedup is keyed by hash of `input`; leaner inputs dedupe better, validate faster, and let one effect serve many call sites. |
| 52 | + |
| 53 | +## Schema (`S`) |
| 54 | + |
| 55 | +`S.string`, `S.number`, `S.bigint`, `S.boolean`, `S.schema({ ... })`, `S.array(...)`, `S.union([..., null])`, `S.optional(...)`. |
| 56 | +Full ref: https://raw.githubusercontent.com/DZakh/sury/refs/tags/v9.3.0/docs/js-usage.md |
| 57 | + |
| 58 | +## RPC pattern (viem) |
| 59 | + |
| 60 | +```ts |
| 61 | +const client = createPublicClient({ transport: http(process.env.ENVIO_RPC_URL) }); |
| 62 | + |
| 63 | +const getTokenMetadata = createEffect( |
| 64 | + { |
| 65 | + name: "getTokenMetadata", |
| 66 | + input: S.string, |
| 67 | + output: { name: S.string, symbol: S.string, decimals: S.number }, |
| 68 | + cache: true, |
| 69 | + rateLimit: false, |
| 70 | + }, |
| 71 | + async ({ input: address }) => { |
| 72 | + const args = { address: address as `0x${string}`, abi: ERC20_ABI }; |
| 73 | + const [name, symbol, decimals] = await Promise.all([ |
| 74 | + client.readContract({ ...args, functionName: "name" }), |
| 75 | + client.readContract({ ...args, functionName: "symbol" }), |
| 76 | + client.readContract({ ...args, functionName: "decimals" }), |
| 77 | + ]); |
| 78 | + return { name, symbol, decimals: Number(decimals) }; |
| 79 | + } |
| 80 | +); |
| 81 | +``` |
| 82 | + |
| 83 | +## Options |
| 84 | + |
| 85 | +| Option | Type | Default | |
| 86 | +|---|---|---| |
| 87 | +| `name` | `string` | — | |
| 88 | +| `input` | `S.Schema` | — | |
| 89 | +| `output` | `S.Schema` | — | |
| 90 | +| `cache` | `boolean` | `false` | |
| 91 | +| `rateLimit` | `false \| { calls, per }` | required | |
| 92 | + |
| 93 | +Full reference: https://docs.envio.dev/docs/HyperIndex-LLM/hyperindex-complete |
0 commit comments