/substreams:substreams-ethereumfrom substreams
Develop Substreams for Ethereum and EVM contracts: ABI codegen, event decoding via match_and_decode, raw topic0 decoding, eth_call/RpcBatch, and token-metadata stores. Decodes log data into typed protobuf objects, one message type per event.
Ships in substreams. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Expert assistant for Ethereum / EVM contract Substreams: log extraction, ABI decoding, and RPC enrichment.
Cross-cutting manifests, stores, and performance live in substreams-dev. SQL module → substreams-sql. Self-managed sink ops → substreams-sink-deploy-local. StreamingFast-hosted SQL sink → substreams-hosted-sink.
EVM is not Solana. Do not use instruction/account/discriminator patterns here. Decoding model = logs + topics + ABI.
Always decode log data and topics into typed fields. Emitting only presence/address filters, raw bytes, hex blobs, or a JSON string of the payload is invalid for contract Substreams in this skill.
Each selected event gets its own dedicated protobuf message with fields matching that event's parameters.
| Required | Forbidden |
|---|---|
e.g. message Swap { … }, message Mint { … }, message Transfer { … } | One generic Event / Log with optional fields for every event |
| Distinct message per event name/topic0 the user asked for | Shared bag: string event_name + map / sparse optional args |
Module output may wrap them: repeated Swap swaps and repeated Mint mints, or oneof of those messages | Single table of mixed rows with unused columns for other events |
Decode indexed params from topics[1..] and non-indexed from data into named typed values | Leave data as bytes / hex |
uint256 → string (decimal) via BigInt; addresses → 0x-prefixed hex string | Dump the whole log as JSON |
log_data_json, raw_data, args_json, serde_json::to_string |
Why: sinks, SQL (schema.table per message), and consumers need stable per-event schemas — not opaque blobs or a one-size-fits-all row.
// ✅ One message type per event; wrapper holds several lists (or a oneof)
message Swap {
uint64 block_number = 1;
string tx_hash = 2;
uint32 log_index = 3;
string pool = 4;
string amount0_in = 5; // uint256 → decimal string
}
message Mint { uint64 block_number = 1; string owner = 2; string amount = 3; }
message Events {
repeated Swap swaps = 1;
repeated Mint mints = 2;
}
// ❌ Forbidden: one mega-message for all events
message GenericEvent {
string event = 1; // "swap" | "mint" | …
string pool = 2; // only set for swap
bytes raw_data = 3;
}
Scope: applies to every selected event you emit. Topic0 matching alone is not enough — params must be decoded. Pure block/tx stats modules that never claim to decode a contract (T1.1) are exempt; any module that filters on a contract/event must decode. uint256 never fits in u64 — use substreams::scalar::BigInt, emit a decimal string; silent truncation is a correctness bug, not a style issue.
Do not write Rust, protos, or manifests until the user has answered (or already stated) the items below.
Same protocol as substreams-dev (and all Substreams skills):
Examples T4.1 / T4.2 are silent ships — a full pipeline built from a vague prompt ("track whale activity") with nothing asked. Vague scope is a signal to ask, not to guess.
Walk this list top to bottom, one question per turn:
| Order | Question focus | Example choices (always end with custom) |
|---|---|---|
| 1 | Network | Ethereum mainnet · Base · Arbitrum · Polygon · Other / custom |
| 2 | Contract address(es) | Known protocols from common-contracts · Other / paste address |
| 3 | Event(s) | Named event presets for that contract · All events · Other / list events or topic0 |
| 4 | Fields / output shape | Suggested field set · Minimal (block, tx, log_index, key params) · Other / describe fields |
| 5 | Enrichment | None · Token symbol/decimals via RPC · USD pricing · Other / describe |
| 6 | Output / sink destination | Full list below · Other / custom |
| 7 | Block range | Suggested window · Recent head · Other / enter blocks |
"All events" + SQL/ClickHouse: one protobuf message and typically one table per event. Prefer a ladder for a first hosted deploy: high-value events (e.g. Swap) → liquidity (Mint/Burn) → full surface. Before ClickHouse, rename reserved column names (index, keys, …) per substreams-sql.
Ask only when destination is unknown. Offer all of these; last = custom:
| Choice | Follow-on |
|---|---|
substreams run only | Verify with CLI/GUI first |
| SQL — self-managed | substreams-sql → substreams-sink-deploy-local |
| SQL — StreamingFast hosted sink | substreams-sql → substreams-hosted-sink |
| The Graph / graph-out | substreams-sink (EntityChanges) |
| Custom app sink | substreams-sink |
| Not sure yet | Start with run-only |
| Other / enter a custom answer | Free-text |
If the user says only "SQL", ask self-managed vs hosted in a separate turn (plus custom). Hosted still needs a correct SQL module first.
If the user names a protocol without an address, propose the mainnet address from references/common-contracts.md as a choice, then still confirm events in a later turn.
One turn when ambiguous. Prefer the highest-quality source available.
| Choice | When to use | How you decode |
|---|---|---|
| ABI JSON | User has ABI / verified on Etherscan / published | Abigen in build.rs → match_and_decode (preferred) |
| Solidity source | No ABI JSON; source available | Derive canonical signature → keccak256 → topic0; hand-decode (T6.1) |
| Known signature | ERC-20/721/1155 or documented event | Known topic0 constant + hand-decoded data words |
| Other / enter a custom answer | User describes another source | Follow their layout; still structured decode |
Decision rules:
Deep dive: references/abi-codegen.md.
From contract address(es) + event(s):
source: sf.ethereum.type.v2.Block as the input and add blockFilter: { module: ethereum_common:index_events, query: { string: "evt_addr:0x…" } } (lowercase 0x hex; evt_addr:/evt_sig: keys; index_calls for call_to:/call_method:). The index is computed once for the whole chain and is likely already cached. Import as ethereum_common: [email protected] (CLI ≥ 1.20.0; older CLIs need https://spkg.io/v1/packages/ethereum-common/v0.3.3). See substreams-dev → references/block-filtering.md.blockFilter is block-level, so still check log.address and topics[0] before decoding (cheapest reject).ethereum_common:filtered_events — alternative that returns only matching events (extra module, no tx context); you must override its default params.Keep the module count small: one extraction map reading the block, not one per event. Call substreams::skip_empty_output() when a block yields nothing.
Details: references/abi-codegen.md.
[dependencies]
substreams = "0.8.0"
substreams-ethereum = "0.12.0"
buffa = { version = "0.9", default-features = false, features = ["std", "fast-utf8"] }
buffa-types = { version = "0.9", default-features = false }
hex = "0.4"
hex-literal = "0.4" # hyphen in Cargo.toml, underscore in `use hex_literal::hex`
[build-dependencies]
substreams-ethereum = "0.12.0" # REQUIRED for Abigen in build.rs
buffa-build = "0.9" # REQUIRED for domain protobuf codegen in build.rs
[lib]
crate-type = ["cdylib"]
[profile.release]
lto = true
opt-level = "s"
strip = "debuginfo"
substreams-ethereum must be in both [dependencies] and [build-dependencies] when using Abigen.
buffa and buffa-types must be your own direct dependencies. Generated protobuf code emits absolute ::buffa:: paths, and the re-export through substreams does not satisfy them — omit the dependency and src/pb fails to compile with unresolved-crate errors. buffa-types supplies the well-known types (Timestamp, Any) that generated code references.
getrandom and init!() are not required. Older docs say to add a getrandom dependency and call substreams_ethereum::init!(); — that guidance is stale. 0.12 declares no getrandom at all, and init!() expands to nothing, kept only so existing code keeps compiling. Declaring getrandom yourself is what causes the wasm32 build error, so leave both out.
fn main() {
substreams_ethereum::Abigen::new("UniswapV3Pool", "abi/uniswap_v3_pool.json")
.expect("Failed to load ABI")
.generate()
.expect("Failed to generate bindings")
.write_to_file("src/abi/uniswap_v3_pool.rs")
.expect("Failed to write bindings");
}
Create src/abi/mod.rs declaring each generated module (pub mod uniswap_v3_pool;), and mod abi; in lib.rs.
Domain protobufs are generated in the same build.rs that runs Abigen, via buffa_build, and land in
OUT_DIR — nothing protobuf-related is checked in. Every EVM example in this repo does it this way.
// build.rs — alongside the Abigen calls above
buffa_build::Config::new()
.files(&["proto/events.proto"])
.includes(&["proto/"])
.preserve_unknown_fields(false)
.compile()
.expect("compiling protos");
// src/lib.rs — the path comes from the .proto's `package` declaration
mod pb {
pub mod myproject {
pub mod v1 {
include!(concat!(env!("OUT_DIR"), "/myproject.v1.mod.rs"));
}
}
}
preserve_unknown_fields(false) drops round-trip fidelity WASM modules do not need and keeps decode
cheap. Lazy views are off by default: a handler taking a *LazyView needs .lazy_views(true) added
to that config. This requires buffa-build in [build-dependencies].
Generating into a checked-in src/pb/ with buf generate and a buf.gen.yaml also works, and is what
the SDK repos do since they pull block models from the BSR — see substreams-dev for the trade-off.
specVersion: v0.1.0
package:
name: my_eth_substreams
version: 1.7.1
url: https://github.com/myorg/my-eth-substreams # set both — silences build warnings
description: Decoded Uniswap V2 swaps on Ethereum mainnet
network: mainnet
protobuf: # REQUIRED — `substreams pack` fails without it
files:
- my_events.proto
importPaths:
- ./proto
binaries: # REQUIRED — `substreams pack` fails without it
default:
type: wasm/rust-v1
# filename = Cargo.toml [package] name with hyphens → underscores,
# which is NOT necessarily the package.name above
file: ./target/wasm32-unknown-unknown/release/my_eth_substreams.wasm
modules:
- name: map_events
kind: map
initialBlock: 18000000 # pin to test/data range — not genesis
inputs:
- source: sf.ethereum.type.v2.Block
output:
type: proto:mypackage.v1.MyOutput
protobuf: and binaries: are not optional — every working example in this repo carries both, and a manifest without them does not pack.
sf.ethereum.type.v2.Block is a well-known source — no imports: entry is needed for the block type. (If you do import the eth spkg, it is substreams-ethereum, never sf-ethereum, which does not exist.)
use substreams::errors::Error;
use substreams::Hex;
use substreams_ethereum::pb::eth::v2 as eth;
use substreams_ethereum::Event; // REQUIRED for .match_and_decode()
// Hex::encode is lowercase and has NO 0x — always re-prefix for emitted addresses/tx hashes.
fn hex0x(b: &[u8]) -> String { format!("0x{}", Hex::encode(b)) }
const POOL: [u8; 20] = hex_literal::hex!("b4e16d0168e52d35cacd2c6185b44281ec28c9dc");
#[substreams::handlers::map]
fn map_events(block: eth::Block) -> Result<MyEvents, Error> {
let mut out = Vec::new();
for trx in block.transactions() { // successful txs only
let tx_hash = hex0x(&trx.hash);
for (log, _call) in trx.logs_with_calls() {
if log.address != POOL { // 1) cheapest reject: address
continue;
}
// 2) MUST decode into typed fields (not JSON, not raw bytes)
// Module path = write_to_file stem (e.g. uniswap_v2_pair), not Abigen::new name.
if let Some(swap) = abi::uniswap_v2_pair::events::Swap::match_and_decode(log) {
out.push(Swap {
block_number: block.number,
tx_hash: tx_hash.clone(),
log_index: log.index,
sender: hex0x(&swap.sender),
amount0_in: swap.amount0_in.to_string(), // BigInt → decimal string
});
}
}
}
Ok(MyEvents { swaps: out })
}
For a handler processing one event type, block.events::<E>(&[&ADDR]) collapses the whole nest into a single loop — see references/abi-codegen.md.
| Rule | Why |
|---|---|
use substreams_ethereum::Event; | Without this trait import, .match_and_decode() / .decode() do not resolve. Error reads "no method named decode found" — the single most common EVM build failure. |
block.transactions() = successful only | Filters status == 1. Good for protocol events. |
trx.logs_with_calls() excludes reverted calls | Skips state_reverted calls and sorts by ordinal. A reverted internal call still emits logs that never reached chain state. |
Never iterate call.logs directly | You would include logs from reverted sub-calls. Use logs_with_calls() or block.logs(). |
| Need failed txs / full counts? | Iterate &block.transaction_traces (all traces) and check status yourself. |
Filter log.address then topics[0] first | Cheapest reject before decoding. |
uint256 → BigInt → decimal string | u64/u128 silently truncate real token amounts. |
Emitted addresses/tx = 0x + lowercase hex | Hex::encode has no 0x. Use format!("0x{}", Hex::encode(…)) (or hex0x above). Keep store keys the same format you use when looking them up. |
| Do not invent topic0 or param order | Derive from ABI or keccak256 of the canonical signature. |
| One proto message per event | Swap, Mint, … — never a single generic event for all types. |
Matching topic0 is only step 1. You must continue and decode the payload into a structured output object (see Hard rule).
if let Some(swap) = abi::uniswap_v2_pair::events::Swap::match_and_decode(log) {
// swap.amount0_in is a substreams::scalar::BigInt — already typed
}
match_and_decode checks topic0 and decodes in one step; it returns None on mismatch, so an explicit topic0 guard is optional (though still the cheaper reject when scanning many logs).
topic0 = keccak256("EventName(type1,type2,...)") over the canonical signature: no parameter names, no spaces, uint256 not uint.
// keccak256("Swap(address,uint256,uint256,uint256,uint256,address)")
const SWAP_TOPIC: [u8; 32] =
hex_literal::hex!("d78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822");
if log.topics.is_empty() || log.topics[0] != SWAP_TOPIC {
continue;
}
Indexed vs non-indexed is the thing agents get wrong: topics[0] = event signature; topics[1..] = indexed params, one 32-byte word each, in declaration order (max 3); data = non-indexed params, ABI-encoded 32-byte words. An address in a topic is left-padded — it is topic[12..32]. Use log.topics.len() to disambiguate overloaded events.
Verified topic0 constants: references/common-contracts.md. Full decoding guide: references/abi-codegen.md.
Contract state that is not in the log (a pool's token0, a token's symbol/decimals) requires an RPC call.
use substreams_ethereum::rpc::RpcBatch;
// execute() → Result<_, String> (not a std Error — do not `?` into substreams::Error).
// Per-call failure is more common: decode → Option (None on revert / bad return).
let batch = match RpcBatch::new()
.add(abi::erc20::functions::Symbol {}, addr.to_vec())
.add(abi::erc20::functions::Decimals {}, addr.to_vec())
.execute()
{
Ok(b) => b,
Err(_) => return Ok(Default::default()),
};
let symbol = RpcBatch::decode::<_, abi::erc20::functions::Symbol>(&batch.responses[0])
.unwrap_or_else(|| "UNKNOWN".to_string());
let decimals = RpcBatch::decode::<_, abi::erc20::functions::Decimals>(&batch.responses[1])
.map(|d| d.to_u64() as u32)
.unwrap_or(18u32);
RPC is the top performance and correctness trap on EVM. An eth_call is a network request that blocks the block's processing until it returns, so it slows a run more than any computation. Before reaching for it: call only when you need the result (e.g. the first time you see a token); take data from execution traces (e.g. storage_changes) when possible; and read static data such as token name/symbol/decimals from the ERC20 Token Metadata Foundational Store (- foundational-store: [email protected] input) where it is deployed — the docs list Ethereum mainnet only (other networks: ask on Discord), so elsewhere keep the cache-store pattern below. Rules:
RpcBatch — never one execute() / .call() per field.set_if_not_exists store, keyed by address — pay the RPC once per contract, not once per block. Never a HashMap inside a map handler: it is rebuilt every block and re-issues every call.symbol() returning bytes32, or missing entirely). Decode returns None — fall back to a default, do not unwrap().Eval note: T3.1 agents that skipped pool.token0()/token1() resolution averaged 41% correctness; with the RpcBatch + cache-store pattern, 100%. This is the highest-value pattern in this skill.
Full worked example (map_pool_tokens → store_pool_tokens → map_swaps): references/rpc-and-tokens.md.
| Goal | Approach |
|---|---|
| Contract with a published ABI | substreams init → ethereum-minimal, drop ABI in abi/, wire Abigen |
| Full block, custom decode | Hand-written map on sf.ethereum.type.v2.Block (examples T1.2 / T6.1) |
| Filter by address/topic | substreams init → ethereum-common (cached filters) |
| Events → SQL | This skill for the map, then substreams-sql for db_out |
After generators, still enforce the pre-flight event list and address filters.
network: set for the target chain, input sf.ethereum.type.v2.Block, crate versions as above.substreams-ethereum in both [dependencies] and [build-dependencies] if using Abigen; use substreams_ethereum::Event;. Store handlers also need StoreNew + the accessor trait in scope.block.transactions() + logs_with_calls() + address filter + topic0 filter; only requested events matched.uint256 → BigInt → decimal string; addresses/tx → 0x + lowercase hex (not bare Hex::encode).set_if_not_exists store.initialBlock = start of needed range; test with substreams run -s <block> -t +100 -o jsonl.*_json / raw_data blobs; no u64 token amounts.substreams-sql before first deploy.| Example | Pattern |
|---|---|
| T1.1 block-stats | Block-level stats over block.transaction_traces (no decoding) |
| T1.2 usdc-transfers | ERC-20 Transfer topic0 filter + uint256 decode |
| T2.1 nft-mints | ERC-721 Transfer with zero-address from (mint detection) |
| T2.2 univ2-swaps | Abigen + match_and_decode + store-backed token metadata |
| T2.3 sql-sink | EVM events → Postgres db_out |
| T3.1 univ3-usd-price | Canonical RPC pattern: RpcBatch + set_if_not_exists + sqrtPriceX96 pricing |
| T3.2 cross-dex-volume | Multi-contract aggregation → graph_out |
| T6.1 eth-univ2-no-abi | topic0 derived from Solidity source, no ABI JSON |
| T4.1 / T4.2 | Cautionary — silent ship, no clarifying questions asked |
| Symptom | Fix |
|---|---|
no method named 'decode' found / match_and_decode unresolved | Add use substreams_ethereum::Event; |
Abigen not found in build.rs | Add substreams-ethereum to [build-dependencies] |
| Empty output | Wrong address (when comparing as strings, both sides lowercase and same 0x policy), wrong topic0, or initialBlock past the data |
| Fewer events than expected | Iterating only top-level receipt.logs of a subset, or filtering failed txs incorrectly |
| Extra events vs Etherscan | Iterating call.logs directly and including state_reverted calls — use logs_with_calls() |
| Token amounts wrong / negative / truncated | uint256 parsed into u64/i64 — use BigInt, emit decimal string |
symbol() returns garbage or panics | Non-compliant token (bytes32 symbol) — handle None, don't unwrap() |
| Very slow / RPC timeouts | Unbatched or uncached eth_call — batch + set_if_not_exists store |
| spkg import 404 | Use substreams-ethereum, NOT sf-ethereum (doesn't exist); verify the release exists |
hex_literal unresolved | Cargo key is hex-literal (hyphen); use hex_literal::hex (underscore) |
getrandom "not supported by default" on wasm32 | Something in your tree declares getrandom. 0.12 declares none itself — remove your own dep rather than adding a features = ["custom"] pin |
cannot find function 'new' in ... Store / no method 'set_if_not_exists' | Store handler missing use substreams::store::StoreNew; (the macro emits ::new()) and the accessor trait |
ClickHouse SYNTAX_ERROR on index / keys | Rename columns in proto (log_index, topic_keys) — substreams-sql |
substreams-dev · SQL module: substreams-sql · Hosted sink: substreams-hosted-sink · Self-managed sink: substreams-sink-deploy-local · Testing: substreams-testingWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
substreams, ethereum, evm, abi-decoding, event-decoding, protobuf
npx claudepluginhub streamingfast/substreams-skills --plugin substreams-dev12 skills in this plugin
/substreams-bitcoinExpert knowledge for developing Bitcoin Substreams. Use when working with Bitcoin blockchain data, UTXO model, transaction parsing, address extraction, or Esplora API compatibility./substreams-convertExpert knowledge for converting existing blockchain indexing projects into Substreams. Use when migrating a subgraph (The Graph) or a Solana program/contract into a Substreams module pipeline./substreams-devCross-cutting Substreams development — manifests, module graphs (map/store/index), protobuf, performance, debugging. For Ethereum/EVM contract decoding use substreams-ethereum; for Solana programs use substreams-solana; for SQL sinks use substreams-sql.