4 minutes
Writing AssemblyScript Mappings
Overview
The mappings take data from a particular source and transform it into entities that are defined within your schema. Mappings are written in a subset of TypeScript called AssemblyScript which can be compiled to WASM (WebAssembly). AssemblyScript is stricter than normal TypeScript, yet provides a familiar syntax.
Writing Mappings
For each event handler that is defined in subgraph.yaml
under mapping.eventHandlers
, create an exported function of the same name. Each handler must accept a single parameter called event
with a type corresponding to the name of the event which is being handled.
In the example Subgraph, src/mapping.ts
contains handlers for the NewGravatar
and UpdatedGravatar
events:
1import { NewGravatar, UpdatedGravatar } from '../generated/Gravity/Gravity'2import { Gravatar } from '../generated/schema'34export function handleNewGravatar(event: NewGravatar): void {5 let gravatar = new Gravatar(event.params.id)6 gravatar.owner = event.params.owner7 gravatar.displayName = event.params.displayName8 gravatar.imageUrl = event.params.imageUrl9 gravatar.save()10}1112export function handleUpdatedGravatar(event: UpdatedGravatar): void {13 let id = event.params.id14 let gravatar = Gravatar.load(id)15 if (gravatar == null) {16 gravatar = new Gravatar(id)17 }18 gravatar.owner = event.params.owner19 gravatar.displayName = event.params.displayName20 gravatar.imageUrl = event.params.imageUrl21 gravatar.save()22}
The first handler takes a NewGravatar
event and creates a new Gravatar
entity with new Gravatar(event.params.id.toHex())
, populating the entity fields using the corresponding event parameters. This entity instance is represented by the variable gravatar
, with an id value of event.params.id.toHex()
.
The second handler tries to load the existing Gravatar
from the Graph Node store. If it does not exist yet, it is created on-demand. The entity is then updated to match the new event parameters before it is saved back to the store using gravatar.save()
.
Recommended IDs for Creating New Entities
It is highly recommended to use Bytes
as the type for id
fields, and only use String
for attributes that truly contain human-readable text, like the name of a token. Below are some recommended id
values to consider when creating new entities.
-
transfer.id = event.transaction.hash
-
let id = event.transaction.hash.concatI32(event.logIndex.toI32())
-
For entities that store aggregated data, for e.g, daily trade volumes, the
id
usually contains the day number. Here, using aBytes
as theid
is beneficial. Determining theid
would look like
1let dayID = event.block.timestamp.toI32() / 864002let id = Bytes.fromI32(dayID)
- Convert constant addresses to
Bytes
.
const id = Bytes.fromHexString('0xdead...beef')
There is a Graph Typescript Library which contains utilities for interacting with the Graph Node store and conveniences for handling smart contract data and entities. It can be imported into mapping.ts
from @graphprotocol/graph-ts
.
Handling of entities with identical IDs
When creating and saving a new entity, if an entity with the same ID already exists, the properties of the new entity are always preferred during the merge process. This means that the existing entity will be updated with the values from the new entity.
If a null value is intentionally set for a field in the new entity with the same ID, the existing entity will be updated with the null value.
If no value is set for a field in the new entity with the same ID, the field will result in null as well.
Code Generation
In order to make it easy and type-safe to work with smart contracts, events and entities, the Graph CLI can generate AssemblyScript types from the Subgraph’s GraphQL schema and the contract ABIs included in the data sources.
This is done with
1graph codegen [--output-dir <OUTPUT_DIR>] [<MANIFEST>]
but in most cases, Subgraphs are already preconfigured via package.json
to allow you to simply run one of the following to achieve the same:
1# Yarn2yarn codegen34# NPM5npm run codegen
This will generate an AssemblyScript class for every smart contract in the ABI files mentioned in subgraph.yaml
, allowing you to bind these contracts to specific addresses in the mappings and call read-only contract methods against the block being processed. It will also generate a class for every contract event to provide easy access to event parameters, as well as the block and transaction the event originated from. All of these types are written to <OUTPUT_DIR>/<DATA_SOURCE_NAME>/<ABI_NAME>.ts
. In the example Subgraph, this would be generated/Gravity/Gravity.ts
, allowing mappings to import these types with.
1import {2 // The contract class:3 Gravity,4 // The events classes:5 NewGravatar,6 UpdatedGravatar,7} from '../generated/Gravity/Gravity'
In addition to this, one class is generated for each entity type in the Subgraph’s GraphQL schema. These classes provide type-safe entity loading, read and write access to entity fields as well as a save()
method to write entities to store. All entity classes are written to <OUTPUT_DIR>/schema.ts
, allowing mappings to import them with
1import { Gravatar } from '../generated/schema'
Note: The code generation must be performed again after every change to the GraphQL schema or the ABIs included in the manifest. It must also be performed at least once before building or deploying the Subgraph.
Code generation does not check your mapping code in src/mapping.ts
. If you want to check that before trying to deploy your Subgraph to Graph Explorer, you can run yarn build
and fix any syntax errors that the TypeScript compiler might find.