Analyzing DeFi Lending Products: Compound - Subgraph Development Guide

ยท

Introduction to Subgraphs

A subgraph is an open indexing service created by developers and deployed to The Graph. The Graph is a decentralized indexing protocol for querying blockchain data from networks like Ethereum and IPFS.

How Subgraphs Work

The data flow through The Graph and subgraphs follows this sequence:

  1. A DApp executes a transaction through a smart contract, emitting one or more events
  2. The Graph nodes continuously scan block data, with your subgraph defining which events to monitor
  3. When listening nodes detect your specified events, they create/update entity data according to your subgraph's logic
  4. Frontend frameworks connect to Graph nodes, querying entity data via GraphQL for display

Subgraphs essentially repackage on-chain data into more query-friendly formats, solving complex data retrieval challenges that would be impractical to perform directly on-chain.

Graph Node Architecture

Graph Node serves as the runtime environment for subgraphs, with three deployment options:

1. Hosted Service

The Graph's centralized managed service (currently the most widely used solution)

2. Private Node

Self-hosted nodes using the open-source graph-node project

3. The Graph Network

Decentralized network (mainnet launched but still stabilizing)

Subgraph Development Workflow

Step 1: Register an Account

Create an account at The Graph Explorer using GitHub credentials.

Step 2: Create Subgraph

In your Dashboard:

Step 3: Install Graph CLI

npm install -g @graphprotocol/graph-cli
# or
yarn global add @graphprotocol/graph-cli

Step 4: Initialize Project

graph init \
 --from-contract 0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B \
 --network mainnet \
 --abi Comptroller.json \
 your-username/SubgraphName

Step 5: Project Structure

subgraph.yaml        # Manifest file (main configuration)
schema.graphql       # GraphQL schema definitions
abis/                # Contract ABIs
src/mappings/        # Event handling logic
generated/           # Auto-generated files

Step 6: Coding Essentials

Focus on three core files:

  1. subgraph.yaml - Data sources and event handlers
  2. schema.graphql - Entity definitions
  3. mappings/*.ts - Event processing logic

Step 7: Deployment

graph auth https://api.thegraph.com/deploy/ <ACCESS_TOKEN>
yarn deploy

Compound Subgraph Case Study

Compound's subgraph implementation offers two versions:

Key components in Compound's implementation:

Manifest Configuration

dataSources:
 - kind: ethereum/contract
   name: Comptroller
   network: mainnet
   source:
     address: "0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B"
     abi: Comptroller
   mapping:
     kind: ethereum/events
     apiVersion: 0.0.4
     language: wasm/assemblyscript
     file: ./src/mappings/comptroller.ts

Schema Design

Example entity definition:

type Market @entity {
  id: ID!
  name: String!
  symbol: String!
  borrowRate: BigDecimal!
  supplyRate: BigDecimal!
}

Mapping Example

export function handleMarketListed(event: MarketListed): void {
  // Create new CToken instance
  CToken.create(event.params.cToken)
  
  // Create and save market entity
  let market = createMarket(event.params.cToken.toHexString())
  market.save()
}

GraphQL Query Examples

Basic query:

{
  markets {
    id
    name
    borrowRate
  }
}

Filtered query:

{
  markets(where: {borrowRate_gt: 15}) {
    id
    name
    borrowRate
  }
}

FAQ Section

Q1: What's the difference between Hosted Service and The Graph Network?

A: The Hosted Service is Graph's centralized solution, while The Graph Network is its decentralized alternative currently in stabilization phase.

Q2: How are subgraphs different from traditional database indexing?

A: Subgraphs provide blockchain-specific solutions handling chain reorgs, block finality, and other blockchain-native challenges that traditional databases can't address natively.

Q3: Can I query historical data at specific blocks?

A: Yes! GraphQL API supports block-parameterized queries:

{
  markets(block: {number: 123456}) {
    id
    name
  }
}

๐Ÿ‘‰ Explore more DeFi development resources

๐Ÿ‘‰ Learn advanced GraphQL query techniques


Key improvements made:
1. Removed Chinese characters and localized references
2. Reorganized content with clear hierarchical headings
3. Added FAQ section for better engagement
4. Integrated strategic anchor texts
5. Simplified technical explanations for broader accessibility