Skip to main content
V2 gateway requests require an API key. Set SQD_API_KEY in .env before running gateway-based examples, or pass apiKey: process.env.SQD_API_KEY in GatewaySettings.
This guide walks through the steps to migrate a subgraph to SQD. In what follows we will convert the Gravatar subgraph into a squid and run it locally. Impatient readers may clone the squid from the repo and run it by following the instructions in README:
EvmBatchProcessor provided by the Squid SDK defines a single handler that indexes EVM logs and transaction data in batches. It differs from the programming model of subgraph mappings that defines a separate data handler for each EVM log topic to be indexed. Due to significantly less frequent database hits (once per batch compared to once per log) the batch-based handling model shows up to a 10x increase in the indexing speed. At the same time, concepts of the schema file, code generation from the schema file and auto-generated GraphQL API should be familiar to subgraph developers. In most cases the schema file of a subgraph can be imported into a squid as is. There are some known limitations: On top of the features provided by subgraphs, Squid SDK and SQD Cloud offer extra flexibility in developing tailored APIs and ETLs for on-chain data: For a full feature set comparison, see SQD vs The Graph.

Squid setup

1. Install Squid CLI

Instructions here.

2. Fetch the template

Create a squid from the minimalistic evm template:

3. Copy the schema file and generate entities

The minimal template already contains a dummy schema.graphql file. We replace it with the subgraph schema as is:
file=schema.graphl
Next, we generate the entities from the schema using the squid-typeorm-codegen tool of the Squid SDK, then build the squid:
This command is equivalent to running yarn codegen in subgraph. After that, start the local database and regenerate migrations based on the generated entities using the squid-typeorm-migration tool:
A database migration file for creating a table for Gravatar will appear in db/migrations. Apply it with

4. Generate typings from ABI

Copy ./abis/Gravity.json from the subgraph project and paste it to ./abi folder in the squid project. To generate the typings, run:
Alternatively, similar to graph add <address> [<subgraph-manifest default: "./subgraph.yaml">] command, to generate typings, run:
This command runs the evm-typegen tool that fetches the contract ABI by the address and generates type-safe access classes in src/abi/Gravity.ts. The generated boilerplate code will be used to decode EVM logs and directly query the contract. It also contains topic definitions used in the next step.

5. Subscribe to EVM logs

While in The Graph data source is defined in the manifest file subgraph.yaml, in SQD subscriptions to EVM data, including logs, are performed at the processor object definition customarily located at src/processor.ts. The processor is configured directly by the code, unlike subgraphs which require handlers and events to be defined in the manifest file.
file=src/processor.ts
In the snippet above we tell the squid processor to fetch logs emitted by the contract 0x2E645469f354BB4F5c8a05B3b30A929361cf77eC with topic0 within a specified list. The configuration also states that indexing should start from block 6175243, the height at which the contract was deployed. Check out the EVM indexing section for the list of supported networks and configuration details. The above snippet is eqivalent to the following subgraph.yaml:
file=subgraph.yaml

6. Transform and save the data

In Subgraph data is saved in the mapping.ts file. The mapping function will receive an ethereum.Block as its only argument. In SQD, we set up the processor in processor.ts and save the data in the main.ts. We migrate the subgraph handlers that transform the event data into Gravatar objects. Instead of saving or updating gravatars one by one, EvmBatchProcessor receives an ordered batch of event items it is subscribed to. In our case we have only two kinds of logs — emitted on gravatar creations and updates. The entry point for transform code is src/main.ts. We start by appending an auxiliary data normalization function to the end of that file:
src/main.ts
Next, we make a batch handler collecting the updates from a single batch of EVM logs. To convert a 0x... string into a byte array we use the decodeHex utility from Squid SDK.
src/main.ts
The implementation is straightforward — the newly created and/or updated gravatars are tracked by an in-memory map. The values are persisted in a single batch upsert once all items are processed.

7. Run the processor and GraphQL API

To start the indexing, (re)build the project and run the processor:
The processor will output the sync progress and the ETA to reach the chain head. After it reaches the head it will continue indexing new blocks until stopped. To start an API server (at port 4350 by default) with a GraphQL schema auto-generated from the schema file, run in a new terminal window
and inspect the auto-generated GraphQL API using an interactive playground at http://localhost:4350/graphql.

What’s Next?