Skip to content

Commit 2145a11

Browse files
Init
1 parent 0c37875 commit 2145a11

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed

website/src/pages/en/subgraphs/guides/_meta.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export default {
2+
'subgraph-composition': '',
23
'subgraph-debug-forking': '',
34
near: '',
45
arweave: '',
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
---
2+
title: Aggregate Data Using Subgraph Composition
3+
sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs'
4+
---
5+
6+
Leverage Subgraph composition to speed up development time. Create a base Subgraph with essential data, then build additional Subgraphs on top of it.
7+
8+
Optimize your Subgraph by merging data from 3 independent, source Subgraphs into a single composable Subgraph to enhance data aggregation.
9+
10+
## Introduction
11+
12+
Composable Subgraphs enable you to combine multiple Subgraphs' data sources into a new Subgraph, facilitating faster and more flexible Subgraph development. Subgraph composition empowers you to create and maintain smaller, focused Subgraphs that collectively form a larger, interconnected dataset.
13+
14+
### Benefits of Composition
15+
16+
Subgraph composition is a powerful feature for scaling, allowing you to:
17+
18+
- Reuse, mix, and combine existing data
19+
- Streamline development and queries
20+
- Use multiple data sources (up to five source Subgraphs)
21+
- Speed up your Subgraph's syncing speed
22+
- Handle errors and optimize the resync
23+
24+
## Architecture Overview
25+
26+
The setup for this example involves two Subgraphs:
27+
28+
1. **Source Subgraph**: Tracks event data as entities.
29+
2. **Dependent Subgraph**: Uses the source Subgraph as a data source.
30+
31+
You can find these in the `source` and `dependent` directories.
32+
33+
- The **source Subgraph** is a basic event-tracking Subgraph that records events emitted by relevant contracts.
34+
- The **dependent Subgraph** references the source Subgraph as a data source, using the entities from the source as triggers.
35+
36+
While the source Subgraph is a standard Subgraph, the dependent Subgraph uses the Subgraph composition feature.
37+
38+
## Prerequisites
39+
40+
### Source Subgraphs
41+
- All Subgraphs need to be published with a **specVersion 1.3.0 or later** (Use the latest graph-cli version to be able to deploy composable Subgraphs)
42+
- See notes here: https://github.com/graphprotocol/graph-node/releases/tag/v0.37.0
43+
- Immutable entities only: All Subgraphs must have [immutable entities](https://thegraph.com/docs/en/subgraphs/best-practices/immutable-entities-bytes-as-ids/#immutable-entities ) when the Subgraph is deployed
44+
- Pruning can be used in the source Subgraphs, but only entities that are immutable can be composed on top of
45+
- Source Subgraphs cannot use grafting on top of existing entities
46+
- Aggregated entities can be composed on, but the composed entities on them cannot also use aggregations directly
47+
48+
### Composed Subgraphs
49+
- You can only compose up to a **maximum of 5 source Subgraphs**
50+
- Composed Subgraphs can only use **datasources from the same chain**
51+
- **Nested composition is not yet supported**: Composing on top of another composed Subgraph isn’t allowed at this time
52+
- Aggregated entities can be composed on, but the composed entities on them cannot also use aggregations directly
53+
- Developers cannot compose an onchain datasource with a Subgraph datasource (i.e. you can’t do normal event handlers and call handlers and block handlers in a composed Subgraph)
54+
55+
Additionally, you can explore the [example-composable-subgraph](https://github.com/graphprotocol/example-composable-subgraph) repository for a working implementation of composable subgraphs
56+
57+
### Source Subgraph
58+
59+
The source Subgraph tracks events from the Sushiswap v3 Subgraph on the Base chain. This Subgraph's configuration file is `source/subgraph.yaml`.
60+
61+
> The `source/subgraph.yaml` employs the advanced Subgraph feature, [declarative `eth_calls`](https://thegraph.com/docs/en/subgraphs/developing/creating/advanced/#declared-eth_call). To review the code for this `source/subgraph.yaml`, check out the [source Subgraph example repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph/blob/a5f13cb4b961f92d5c5631dca589c54feb1c0a19/source/subgraph.yaml).
62+
63+
### Dependent Subgraph
64+
65+
The dependent Subgraph is in the `dependent/subgraph.yaml` file, which specifies the source Subgraph as a data source. This Subgraph uses entities from the source to trigger specific actions based on changes to those entities.
66+
67+
> To review the code for this `dependent/subgraph.yaml`, check out the [dependent Subgraph example repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph/blob/main/dependant/subgraph.yaml).
68+
69+
## Get Started
70+
71+
The following is a guide that illustrates how to use one Subgraph as a data source for another. This example uses:
72+
73+
- Sushiswap v3 Subgraph on Base chain
74+
- Two Subgraphs (but you can use up to **5 source** Subgraphs in your own development).
75+
76+
### Step 1. Set Up Your Source Subgraph
77+
78+
To set the source Subgraph as a data source in the dependent Subgraph, include the following in `subgraph.yaml`:
79+
80+
```yaml
81+
specVersion: 1.3.0
82+
schema:
83+
file: ./schema.graphql
84+
dataSources:
85+
- kind: subgraph
86+
name: Factory
87+
network: base
88+
source:
89+
address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz'
90+
startBlock: 82522
91+
```
92+
93+
Here, `source.address` refers to the Deployment ID of the source Subgraph, and `startBlock` specifies the block from which indexing should begin.
94+
95+
### Step 2. Define Handlers in Dependent Subgraph
96+
97+
Below is an example of defining handlers in the dependent Subgraph:
98+
99+
```typescript
100+
export function handleInitialize(trigger: EntityTrigger<Initialize>): void {
101+
if (trigger.operation === EntityOp.Create) {
102+
let entity = trigger.data
103+
let poolAddressParam = Address.fromBytes(entity.poolAddress)
104+
105+
// Update pool sqrt price and tick
106+
let pool = Pool.load(poolAddressParam.toHexString()) as Pool
107+
pool.sqrtPrice = entity.sqrtPriceX96
108+
pool.tick = BigInt.fromI32(entity.tick)
109+
pool.save()
110+
111+
// Update token prices
112+
let token0 = Token.load(pool.token0) as Token
113+
let token1 = Token.load(pool.token1) as Token
114+
115+
// Update ETH price in USD
116+
let bundle = Bundle.load('1') as Bundle
117+
bundle.ethPriceUSD = getEthPriceInUSD()
118+
bundle.save()
119+
120+
updatePoolDayData(entity)
121+
updatePoolHourData(entity)
122+
123+
// Update derived ETH price for tokens
124+
token0.derivedETH = findEthPerToken(token0)
125+
token1.derivedETH = findEthPerToken(token1)
126+
token0.save()
127+
token1.save()
128+
}
129+
}
130+
```
131+
132+
In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source Subgraph, passed as `EntityTrigger<Initialize>`. The handler updates the pool and token entities based on data from the new `Initialize` entity.
133+
134+
`EntityTrigger` has three fields:
135+
136+
1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`.
137+
2. `type`: Indicates the entity type.
138+
3. `data`: Contains the entity data.
139+
140+
Developers can then determine specific actions for the entity data based on the operation type.
141+
142+
## Key Takeaways
143+
144+
- Use this powerful tool to quickly scale your Subgraph development and reuse existing data.
145+
- The setup includes creating a base source Subgraph and referencing it in a dependent Subgraph.
146+
- You define handlers in the dependent Subgraph to perform actions based on changes in the source Subgraph's entities.
147+
148+
This approach unlocks composability and scalability, simplifying both development and maintenance efficiency.
149+
150+
## Additional Resources
151+
152+
To use other advanced features in your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/) and [this Subgraph composition example repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph).
153+
154+
To learn how to define source Subgraphs, check out [this Subgraph composition example repo](https://github.com/isum/subgraph-composition-example).

0 commit comments

Comments
 (0)