OpenZeppelin Blog

Indexing Smart Contracts with OpenZeppelin Subgraphs & The Graph - OpenZeppelin blog

Written by Jack Curatolo | August 3, 2021

The workshop was recorded on July 28th 2021 and led by Hadrien Croubois, Smart Contract Engineer at OpenZeppelin and Nader Dabit, Developer Relation Engineer, Edge & Node

The workshop covers the following:

  • Subgraph Explainer and use cases
  • Why dense subgraphs are better 
  • OZ Subgraph components
  • Using a modular OZ Subgraph from the ERC20 module
  • Assembling and Querying the Subgraph
  • Optimizing Subgraph potential in Smart Contract development

You can watch the video, view the slides, and learn how to use OpenZeppelin’s Subgraphs to index your smart contracts app.

What are Subgraphs?

DeFi applications depend on efficient indexing so trade and exchange data is up-to-date. Writing proprietary code for syncing and indexing is expensive, inefficient, and error prone. The Graph’s standardized and trusted Subgraphs have become the fastest, cheapest, and most reliable way to index and serve Defi applications.

Subgraphs enable developers to build discoverable APIs by pulling data indexed from Ethereum and other blockchains. The Graph learns what and how to index data based on subgraph descriptions, known as the subgraph manifest. The subgraph description defines the smart contracts of interest for a subgraph, the events in those contracts to pay attention to, and how to map event data to data that The Graph will store in its database.

Information is not about the individual pieces of data, it’s about the links between them. Like Wikipedia’s linking system, Subgraphs allow you to link specific bits of information to others, creating a powerful system of linked records that can be used for a multitude of applications.

Indexing using the OpenZeppelin Subgraphs Library

Creating dense Subgraphs is one of the most important aspects of indexing on-chain information. This is generally done by linking data types within a Subgraph pulling metadata from an object. Indexing using heavily linked data types means there is more information to work with for analysis and debugging improving outcomes for developers.

A few guidelines for creating dense subgraphs:

  • Create entities for high level concepts (tokens, balances, authorizations, loans)
  • Create entities for low level objects (addresses, events, transactions)
  • Provide as many crosslink as possible between entities
  • Design your contracts so that everything can be indexed using only events.

OpenZeppelin Contracts supports subgraphs in the following modules:

  • AccessControl
  • ERC20
  • ERC721
  • ERC1155
  • ERC1967upgrade
  • Ownable
  • Pausable
  • Timelock

We plan to add support for the following modules:

  • ERC20votes
  • ERC777

Get Started

Install from npm for local development:

  • npm install @openzeppelin/contracts
  • npm install @openzeppelin/subgraphs

For each module the library provides:

  • A schema of the corresponding entities
  • A datasource template that listen to events
  • Indexing logic in assembly script

The subgraph can be assembled manually or using the graph-compile tool as part of the @amxx/graphprotocol-utils package. Simply describe your application using a config.json file then generate your custom schema and manifest with all modules enabled. To get started building modular subgraphs with OpenZeppelin Contracts, head over to our official documentation page and visit the Github repository.

Video

Slides

To see the slides from the webinar click here.

Learn more

Learn more about OpenZeppelin Contracts and sign up for a free Defender account.

See the documentation: 

OpenZeppelin Defender Documentation

OpenZeppelin Subgraphs Documentation

Be part of the community