Subgraphs are EVM-based, so this guide builds an EVM squid. See the EVM version of this page for the full walkthrough.
- Many-to-Many entity relations should be modeled explicitly as two many-to-one relations
- Full control over the target database (Postgres), including custom migrations and ad-hoc queries in the handler
- Custom target databases and data formats (e.g. CSV)
- Arbitrary code execution in the data handler
- Extension of the GraphQL API with arbitrary SQL
- Secret environment variables, allowing to seamlessly use private third-party JSON-RPC endpoints and integrate with external APIs
- Deployment slots and tags
- API caching
Squid setup
1. Install Squid CLI
Instructions here.2. Fetch the template
Create a squid from the minimalisticevm template:
3. Copy the schema file and generate entities
The minimal template already contains a dummyschema.graphql file. We replace it with the subgraph schema as is:
file=schema.graphl
squid-typeorm-codegen tool of the Squid SDK, then build the squid:
yarn codegen in subgraph.
After that, start the local database and regenerate migrations based on the generated entities using the squid-typeorm-migration tool:
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:
graph add <address> [<subgraph-manifest default: "./subgraph.yaml">] command, to generate typings, run:
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 filesubgraph.yaml, in SQD subscriptions to EVM data, including logs, are performed at the DataSourceBuilder definition customarily located at src/main.ts. The data source is configured directly by the code, unlike subgraphs which require handlers and events to be defined in the manifest file.
file=src/main.ts
0x2E645469f354BB4F5c8a05B3b30A929361cf77eC with topic0 within a specified list, and to include the address, topics and data fields of each log - there are no default fields, so every field the handler reads must be requested. The configuration also states that indexing should start from block 6175243, the height at which the contract was deployed. The Portal stream is public - no API key is needed - and covers real-time data all the way to the unfinalized chain head, so no RPC endpoint is required either.
Check out the list of supported networks for more details.
The above snippet is equivalent to the following subgraph.yaml:
file=subgraph.yaml
6. Transform and save the data
In Subgraph data is saved in themapping.ts file. The mapping function will receive an ethereum.Block as its only argument. In SQD, both the data source definition and the batch handler that saves the data live in main.ts.
We migrate the subgraph handlers that transform the event data into Gravatar objects. Instead of saving or updating gravatars one by one, the batch handler receives an ordered batch of event items the data source 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
0x... string into a byte array we use Node’s Buffer.
src/main.ts
7. Run the processor and GraphQL API
To start the indexing, (re)build the project and run the processor:4350 by default) with a GraphQL schema auto-generated from the schema file, run in a new terminal window
What’s Next?
- Compare your API to that of subgraph using the compareGraphQL script
- Have a closer look at the batch processor and the EVM Portal stream
- Learn how to deploy a squid to SQD Cloud for free
- Inspect a more complex Uniswap V3 squid which tracks Uniswap V3 trading data. It was migrated from the Uniswap V3 subgraph. It takes only a few hours to sync from scratch on a local machine.