Skip to main content

Overview

Mappings define the transformation logic that converts blockchain data into entities that can be queried through your subgraph’s GraphQL API. Each mapping specifies:
  • Which events, calls, or blocks to handle
  • Which entities are affected
  • Which ABIs are needed
  • The AssemblyScript code that processes the data

Mapping Fields

kind
String
required
Must be ethereum/events for Ethereum event mappings.
apiVersion
String
required
Semver string of the version of the Mappings API that will be used by the mapping script.Current recommended version: 0.0.7
language
String
required
The language of the runtime for the Mapping API.Possible values:
  • wasm/assemblyscript - AssemblyScript compiled to WebAssembly
entities
Array<String>
required
A list of entities that will be ingested as part of this mapping.Must correspond to entity names defined in your GraphQL schema.
abis
Array<ABI>
required
ABIs for the contract classes that should be generated in the mapping.Each ABI entry has a name and file field.
eventHandlers
Array<EventHandler>
Handlers for specific events, which will be defined in the mapping script.See Event Handlers for details.
callHandlers
Array<CallHandler>
A list of functions that will trigger a handler and the name of the corresponding handlers in the mapping.See Call Handlers for details.
blockHandlers
Array<BlockHandler>
Defines block filters and handlers to process matching blocks.See Block Handlers for details.
file
Path
required
The path of the mapping script (AssemblyScript file).
Each mapping is required to supply at least one handler type: eventHandlers, callHandlers, or blockHandlers.

Event Handlers

Event handlers are triggered when specific events are emitted by the smart contract. This is the most common type of handler.
event
String
required
An identifier for an event that will be handled in the mapping script.For Ethereum contracts, this must be the full event signature to distinguish from events that may share the same name.Important:
  • No alias types can be used
  • uint will not work, uint256 must be used
  • Use indexed parameters as shown in the ABI
handler
String
required
The name of an exported function in the mapping script that should handle the specified event.This function must be exported from your mapping file.
topic0
String
A 0x prefixed hex string.If provided, events whose topic0 is equal to this value will be processed by the given handler. When topic0 is provided, only the topic0 value will be matched, and not the hash of the event signature.This is useful for processing anonymous events in Solidity, which can have their topic0 set to anything.By default, topic0 is equal to the hash of the event signature.
receipt
Boolean
Set to true to receive the transaction receipt in the handler.When enabled, the handler will have access to receipt data including logs, gas used, and status.

Event Handler Example

eventHandlers:
  - event: Transfer(indexed address,indexed address,uint256)
    handler: handleTransfer
  - event: Approval(indexed address,indexed address,uint256)
    handler: handleApproval

Call Handlers

Call handlers are triggered when specific functions are called on the smart contract. Useful for tracking state changes that don’t emit events.
Call handlers require the transaction to be successful. Failed transactions do not trigger call handlers.
function
String
required
An identifier for a function that will be handled in the mapping script.For Ethereum contracts, this is the normalized function signature to filter calls by.
handler
String
required
The name of an exported function in the mapping script that should handle the specified call.

Call Handler Example

callHandlers:
  - function: swap(uint256,uint256,address,bytes)
    handler: handleSwap
  - function: mint(address)
    handler: handleMint

Block Handlers

Block handlers are triggered for every block (or blocks matching a filter). Useful for tracking time-based state changes or aggregate statistics.
Block handlers can significantly impact indexing performance. Use filters to limit when they execute.
handler
String
required
The name of an exported function in the mapping script that should handle the block.
filter
BlockHandlerFilter
Definition of the filter to apply. If none is supplied, the handler will be called on every block.See Block Handler Filters for details.

Block Handler Filters

kind
String
required
The selected block handler filter.Possible values:
  • call - Only run the handler if the block contains at least one call to the data source contract

Block Handler Example

blockHandlers:
  - handler: handleBlock
    filter:
      kind: call

Pre-declared Calls

Available from spec version 1.2.0. Struct field access available from spec version 1.4.0. Declared calls are performed in parallel before the handler is run and can greatly speed up syncing. Mappings access the call results simply by using ethereum.call from the mappings.
label
String
required
A label for the call for error messages and identification.
call
String
required
The call specification in the format: <ABI>[<address>].<function>(<args>)Components:
  • ABI - The name of an ABI from the abis section
  • address - An expression resolving to the contract address
  • function - The name of a view function in the contract
  • args - Expressions for the function arguments

Expression Types

The expressions in pre-declared calls can be:
ExpressionDescription
event.addressThe address of the contract that emitted the event
event.params.<name>A simple parameter from the event
event.params.<name>.<index>A field from a struct parameter by numeric index
event.params.<name>.<fieldName>A field from a struct parameter by field name (spec version 1.4.0+)

Pre-declared Calls Example

eventHandlers:
  - event: Swap(indexed address,uint256,uint256,uint256,uint256,indexed address)
    handler: handleSwap
    calls:
      getReserves:
        label: getReserves
        call: Pair[event.address].getReserves()
      token0:
        label: getToken0
        call: Pair[event.address].token0()
      token1:
        label: getToken1
        call: Pair[event.address].token1()

ABI Configuration

ABIs must be specified for all contracts your mapping interacts with:
name
String
required
The name used to reference this ABI in the mapping code and elsewhere in the manifest.
file
Path
required
The path to the ABI JSON file.

ABI Example

abis:
  - name: ERC20
    file: ./abis/ERC20.json
  - name: UniswapV2Factory
    file: ./abis/UniswapV2Factory.json
  - name: UniswapV2Pair
    file: ./abis/UniswapV2Pair.json

Complete Mapping Examples

mapping:
  kind: ethereum/events
  apiVersion: 0.0.7
  language: wasm/assemblyscript
  entities:
    - User
    - Transfer
    - Approval
  abis:
    - name: ERC20
      file: ./abis/ERC20.json
  eventHandlers:
    - event: Transfer(indexed address,indexed address,uint256)
      handler: handleTransfer
    - event: Approval(indexed address,indexed address,uint256)
      handler: handleApproval
  file: ./src/erc20.ts

Best Practices

  • Event handlers: Best for most use cases. Fast and efficient.
  • Call handlers: Use when you need to track function calls that don’t emit events.
  • Block handlers: Use sparingly, only for time-based aggregations or when you absolutely need to process every block.
Pre-declared calls (available from spec 1.2.0+) can significantly improve indexing performance by:
  • Fetching data in parallel before the handler runs
  • Reducing the number of RPC calls during sync
  • Making your code cleaner
Use them whenever your handler needs to make contract calls.
Common mistakes:
  • Using uint instead of uint256
  • Missing indexed keyword
  • Wrong parameter order
  • Using alias types
Always copy the exact signature from your contract’s ABI.
Always check if entities exist before using them:
let entity = Entity.load(id)
if (entity == null) {
  entity = new Entity(id)
  // Set default values
}
  • Avoid complex computations in handlers
  • Don’t load entities unnecessarily
  • Use batch operations when possible
  • Minimize contract calls (use pre-declared calls instead)
The entities array should include all entities that your mapping creates or modifies. This:
  • Documents your mapping’s behavior
  • Helps with debugging
  • May be used for optimization in future versions

Handler Execution Order

Within a single transaction or block, handlers execute in this order:
  1. Call handlers - In the order calls were made
  2. Event handlers - In the order events were emitted
  3. Block handlers - After all other handlers
If you have multiple data sources, their handlers can interleave based on the actual order of events/calls in the block.

Error Handling

By default, any error in a handler will cause the subgraph to stop syncing. To allow handlers to fail without stopping the subgraph:
  1. Set specVersion to 0.0.4 or higher
  2. Add nonFatalErrors to the features list
  3. Use try-catch or if-null checks in your mapping code
specVersion: 0.0.4
features:
  - nonFatalErrors

Next Steps

Manifest Spec

View complete manifest specification

Data Sources

Learn about data source configuration

Build docs developers (and LLMs) love