From 2f66325ed1ac321b64f58a2395015a7cfaf1da4f Mon Sep 17 00:00:00 2001 From: Idalith Bustos Date: Fri, 14 Feb 2025 17:21:43 -0800 Subject: [PATCH 1/9] Comp-Updated --- .../src/pages/ar/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ .../src/pages/cs/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ .../src/pages/de/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ .../src/pages/en/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 132 ++++++++++++++++++ .../src/pages/es/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ .../src/pages/fr/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ .../creating/unit-testing-framework.mdx | 1 - .../src/pages/hi/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ .../src/pages/it/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ .../src/pages/ja/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ .../src/pages/ko/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ .../src/pages/mr/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ .../src/pages/nl/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ .../src/pages/pl/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ .../src/pages/pt/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ .../src/pages/ro/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ .../src/pages/ru/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ .../src/pages/sv/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ .../src/pages/tr/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ .../src/pages/uk/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ .../src/pages/ur/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ .../src/pages/vi/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ .../src/pages/zh/subgraphs/cookbook/_meta.js | 1 + .../cookbook/subgraph-composition.mdx | 128 +++++++++++++++++ 45 files changed, 2842 insertions(+), 1 deletion(-) create mode 100644 website/src/pages/ar/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/cs/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/de/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/es/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/fr/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/hi/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/it/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/ja/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/ko/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/mr/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/nl/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/pl/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/pt/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/ro/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/ru/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/sv/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/tr/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/uk/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/ur/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/vi/subgraphs/cookbook/subgraph-composition.mdx create mode 100644 website/src/pages/zh/subgraphs/cookbook/subgraph-composition.mdx diff --git a/website/src/pages/ar/subgraphs/cookbook/_meta.js b/website/src/pages/ar/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/ar/subgraphs/cookbook/_meta.js +++ b/website/src/pages/ar/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/ar/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/ar/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/ar/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/cs/subgraphs/cookbook/_meta.js b/website/src/pages/cs/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/cs/subgraphs/cookbook/_meta.js +++ b/website/src/pages/cs/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/cs/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/cs/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/cs/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/de/subgraphs/cookbook/_meta.js b/website/src/pages/de/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/de/subgraphs/cookbook/_meta.js +++ b/website/src/pages/de/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/de/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/de/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/de/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/en/subgraphs/cookbook/_meta.js b/website/src/pages/en/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/en/subgraphs/cookbook/_meta.js +++ b/website/src/pages/en/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..d09fb7f17535 --- /dev/null +++ b/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,132 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to speed up development time. Create a base subgraph with essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. +> - You may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +Composable subgraphs enable you to combine multiple subgraphs' data sources into a new subgraph, facilitating faster and more flexible subgraph development. + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse, mix, and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks composability and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). + +To learn how to define three `source` subgraphs check out the [subgraph composition example repo](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/es/subgraphs/cookbook/_meta.js b/website/src/pages/es/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/es/subgraphs/cookbook/_meta.js +++ b/website/src/pages/es/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/es/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/es/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/es/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/fr/subgraphs/cookbook/_meta.js b/website/src/pages/fr/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/fr/subgraphs/cookbook/_meta.js +++ b/website/src/pages/fr/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/fr/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/fr/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/fr/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/fr/subgraphs/developing/creating/unit-testing-framework.mdx b/website/src/pages/fr/subgraphs/developing/creating/unit-testing-framework.mdx index 4a7fc5f06f83..4ba4ab8d4111 100644 --- a/website/src/pages/fr/subgraphs/developing/creating/unit-testing-framework.mdx +++ b/website/src/pages/fr/subgraphs/developing/creating/unit-testing-framework.mdx @@ -1164,7 +1164,6 @@ De même que pour les sources de données dynamiques de contrat, les utilisateur ##### Exemple `subgraph.yaml` ```yaml - --- templates: - kind: file/ipfs diff --git a/website/src/pages/hi/subgraphs/cookbook/_meta.js b/website/src/pages/hi/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/hi/subgraphs/cookbook/_meta.js +++ b/website/src/pages/hi/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/hi/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/hi/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/hi/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/it/subgraphs/cookbook/_meta.js b/website/src/pages/it/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/it/subgraphs/cookbook/_meta.js +++ b/website/src/pages/it/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/it/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/it/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/it/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/ja/subgraphs/cookbook/_meta.js b/website/src/pages/ja/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/ja/subgraphs/cookbook/_meta.js +++ b/website/src/pages/ja/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/ja/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/ja/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/ja/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/ko/subgraphs/cookbook/_meta.js b/website/src/pages/ko/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/ko/subgraphs/cookbook/_meta.js +++ b/website/src/pages/ko/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/ko/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/ko/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/ko/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/mr/subgraphs/cookbook/_meta.js b/website/src/pages/mr/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/mr/subgraphs/cookbook/_meta.js +++ b/website/src/pages/mr/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/mr/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/mr/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/mr/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/nl/subgraphs/cookbook/_meta.js b/website/src/pages/nl/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/nl/subgraphs/cookbook/_meta.js +++ b/website/src/pages/nl/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/nl/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/nl/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/nl/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/pl/subgraphs/cookbook/_meta.js b/website/src/pages/pl/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/pl/subgraphs/cookbook/_meta.js +++ b/website/src/pages/pl/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/pl/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/pl/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/pl/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/pt/subgraphs/cookbook/_meta.js b/website/src/pages/pt/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/pt/subgraphs/cookbook/_meta.js +++ b/website/src/pages/pt/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/pt/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/pt/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/pt/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/ro/subgraphs/cookbook/_meta.js b/website/src/pages/ro/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/ro/subgraphs/cookbook/_meta.js +++ b/website/src/pages/ro/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/ro/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/ro/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/ro/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/ru/subgraphs/cookbook/_meta.js b/website/src/pages/ru/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/ru/subgraphs/cookbook/_meta.js +++ b/website/src/pages/ru/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/ru/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/ru/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/ru/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/sv/subgraphs/cookbook/_meta.js b/website/src/pages/sv/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/sv/subgraphs/cookbook/_meta.js +++ b/website/src/pages/sv/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/sv/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/sv/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/sv/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/tr/subgraphs/cookbook/_meta.js b/website/src/pages/tr/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/tr/subgraphs/cookbook/_meta.js +++ b/website/src/pages/tr/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/tr/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/tr/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/tr/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/uk/subgraphs/cookbook/_meta.js b/website/src/pages/uk/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/uk/subgraphs/cookbook/_meta.js +++ b/website/src/pages/uk/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/uk/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/uk/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/uk/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/ur/subgraphs/cookbook/_meta.js b/website/src/pages/ur/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/ur/subgraphs/cookbook/_meta.js +++ b/website/src/pages/ur/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/ur/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/ur/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/ur/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/vi/subgraphs/cookbook/_meta.js b/website/src/pages/vi/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/vi/subgraphs/cookbook/_meta.js +++ b/website/src/pages/vi/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/vi/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/vi/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/vi/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/zh/subgraphs/cookbook/_meta.js b/website/src/pages/zh/subgraphs/cookbook/_meta.js index 66c172da5ef0..9bcb270dc385 100644 --- a/website/src/pages/zh/subgraphs/cookbook/_meta.js +++ b/website/src/pages/zh/subgraphs/cookbook/_meta.js @@ -1,4 +1,5 @@ export default { + 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', cosmos: '', diff --git a/website/src/pages/zh/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/zh/subgraphs/cookbook/subgraph-composition.mdx new file mode 100644 index 000000000000..7a9ecaf2114b --- /dev/null +++ b/website/src/pages/zh/subgraphs/cookbook/subgraph-composition.mdx @@ -0,0 +1,128 @@ +--- +title: Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Subgraph Composition' +--- + +Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. + +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). +> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. + +## Overview + +### Benefits of Composition + +Subgraph composition is a powerful feature for scaling, allowing you to: + +- Reuse and combine existing data +- Speed up your subgraph's syncing speed +- Handle errors and optimize the resync +- Enhance your subgraph development to scale effortlessly + +## Setup + +This guide uses the Sushiswap v3 subgraph on Base chain. + +### Tools Needed + +The setup involves two subgraphs: + +1. **Source Subgraph**: Tracks event data as entities. +2. **Dependent Subgraph**: Uses the source subgraph as a data source. + +You can find these in the `source` and `dependent` directories. + +- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. +- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. + +While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. + +### Source Subgraph + +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. + +### Dependent Subgraph + +The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. + +## Get Started + +The following is a how-to-guide on using one subgraph as a data source for another. + +### Step 1. Set Up Your Source Subgraph + +To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: + +```yaml +specVersion: 1.3.0 +schema: + file: ./schema.graphql +dataSources: + - kind: subgraph + name: Factory + network: base + source: + address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' + startBlock: 82522 +``` + +Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. + +### Step 2. Define Handlers in Dependent Subgraph + +Below is an example of defining handlers in the dependent subgraph: + +```typescript +export function handleInitialize(trigger: EntityTrigger): void { + if (trigger.operation === EntityOp.Create) { + let entity = trigger.data + let poolAddressParam = Address.fromBytes(entity.poolAddress) + + // Update pool sqrt price and tick + let pool = Pool.load(poolAddressParam.toHexString()) as Pool + pool.sqrtPrice = entity.sqrtPriceX96 + pool.tick = BigInt.fromI32(entity.tick) + pool.save() + + // Update token prices + let token0 = Token.load(pool.token0) as Token + let token1 = Token.load(pool.token1) as Token + + // Update ETH price in USD + let bundle = Bundle.load('1') as Bundle + bundle.ethPriceUSD = getEthPriceInUSD() + bundle.save() + + updatePoolDayData(entity) + updatePoolHourData(entity) + + // Update derived ETH price for tokens + token0.derivedETH = findEthPerToken(token0) + token1.derivedETH = findEthPerToken(token1) + token0.save() + token1.save() + } +} +``` + +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. + +`EntityTrigger` has three fields: + +1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. +2. `type`: Indicates the entity type. +3. `data`: Contains the entity data. + +Developers can then determine specific actions for the entity data based on the operation type. + +## Key Takeaways + +- Use this powerful tool to scale your subgraph development and reuse existing data. +- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. +- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. + +This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. + +## Additional Resources + +To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). From 607f7911cb4ea44b5122734080bc177744c121db Mon Sep 17 00:00:00 2001 From: Idalith <126833353+idalithb@users.noreply.github.com> Date: Wed, 26 Feb 2025 11:53:09 -0800 Subject: [PATCH 2/9] Update website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Benoît Rouleau --- .../src/pages/en/subgraphs/cookbook/subgraph-composition.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx index d09fb7f17535..8788fe2f374b 100644 --- a/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx +++ b/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx @@ -3,7 +3,7 @@ title: Subgraph Composition with Sushiswap v3 on Base sidebarTitle: 'Subgraph Composition' --- -Leverage subgraph composition to speed up development time. Create a base subgraph with essential data, then build additional subgraphs on top of it. +Leverage Subgraph composition to speed up development time. Create a base subgraph with essential data, then build additional subgraphs on top of it. > - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). > - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. From 5b811f9ae3f4898ff07b3bfe8d8b9a61f17f1b48 Mon Sep 17 00:00:00 2001 From: Idalith <126833353+idalithb@users.noreply.github.com> Date: Wed, 26 Feb 2025 11:53:25 -0800 Subject: [PATCH 3/9] Update website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Benoît Rouleau --- .../src/pages/en/subgraphs/cookbook/subgraph-composition.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx index 8788fe2f374b..ecd4d0fb2d43 100644 --- a/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx +++ b/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx @@ -41,7 +41,7 @@ While the source subgraph is a standard subgraph, the dependent subgraph utilize ### Source Subgraph -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. +The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is `source/subgraph.yaml`. ### Dependent Subgraph From 584940a9cf57aaf387acb3e3bfeb777b5fb7e6e9 Mon Sep 17 00:00:00 2001 From: Idalith <126833353+idalithb@users.noreply.github.com> Date: Wed, 26 Feb 2025 11:53:34 -0800 Subject: [PATCH 4/9] Update website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Benoît Rouleau --- .../src/pages/en/subgraphs/cookbook/subgraph-composition.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx index ecd4d0fb2d43..b1094a0b7326 100644 --- a/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx +++ b/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx @@ -49,7 +49,7 @@ The dependent subgraph is in the `dependent/subgraph.yaml` directory, which spec ## Get Started -The following is a how-to-guide on using one subgraph as a data source for another. +The following is a how-to guide on using one Subgraph as a data source for another. ### Step 1. Set Up Your Source Subgraph From c58033b8325ea054181c95e6cbf86356f27afda3 Mon Sep 17 00:00:00 2001 From: Idalith <126833353+idalithb@users.noreply.github.com> Date: Wed, 26 Feb 2025 11:54:23 -0800 Subject: [PATCH 5/9] Update website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Benoît Rouleau --- .../src/pages/en/subgraphs/cookbook/subgraph-composition.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx index b1094a0b7326..3fc6168bfda2 100644 --- a/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx +++ b/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx @@ -127,6 +127,6 @@ This approach unlocks composability and scalability, simplifying both developmen ## Additional Resources -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). +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). To learn how to define three `source` subgraphs check out the [subgraph composition example repo](https://github.com/isum/subgraph-composition-example). From f208eca3750edd1cb42e3aabae65a3fc7baa89d1 Mon Sep 17 00:00:00 2001 From: Idalith <126833353+idalithb@users.noreply.github.com> Date: Wed, 26 Feb 2025 11:54:30 -0800 Subject: [PATCH 6/9] Update website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Benoît Rouleau --- .../src/pages/en/subgraphs/cookbook/subgraph-composition.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx index 3fc6168bfda2..42dfed685544 100644 --- a/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx +++ b/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx @@ -129,4 +129,4 @@ This approach unlocks composability and scalability, simplifying both developmen 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). -To learn how to define three `source` subgraphs check out the [subgraph composition example repo](https://github.com/isum/subgraph-composition-example). +To learn how to define three `source` Subgraphs, check out [this Subgraph composition example repo](https://github.com/isum/subgraph-composition-example). From ed27f92dbd1814d62d061d908a6cba64bda0b7e9 Mon Sep 17 00:00:00 2001 From: Idalith Bustos Date: Thu, 27 Feb 2025 14:59:15 -0800 Subject: [PATCH 7/9] Updating Draft --- .../cookbook/subgraph-composition.mdx | 65 ++++++++++--------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx index 42dfed685544..1b2a5ae736b5 100644 --- a/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx +++ b/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx @@ -1,59 +1,66 @@ --- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' +title: Enhance Your Subgraph Build Using Subgraph Composition with Sushiswap v3 on Base +sidebarTitle: 'Using Subgraph Composition with Sushiswap v3 on Base' --- -Leverage Subgraph composition to speed up development time. Create a base subgraph with essential data, then build additional subgraphs on top of it. +Leverage Subgraph composition to speed up development time. Create a base Subgraph with essential data, then build additional Subgraphs on top of it. +> Important Reminders: +> > - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. -> - You may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. +> - You can use existing Subgraphs, but they must be redeployed with `specVersion` 1.3.0, which doesn't require you to write new code. +> - You may want to restructure your Subgraph to split out the logic as you move to a composable Subgraph world. -## Overview +## Introduction -Composable subgraphs enable you to combine multiple subgraphs' data sources into a new subgraph, facilitating faster and more flexible subgraph development. +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. ### Benefits of Composition Subgraph composition is a powerful feature for scaling, allowing you to: - Reuse, mix, and combine existing data -- Speed up your subgraph's syncing speed +- Streamline development and queries +- Use multiple data sources (up to five source Subgraphs) +- Speed up your Subgraph's syncing speed - Handle errors and optimize the resync -## Setup +## Architecture Overview -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: +The setup for this example involves two Subgraphs: 1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. +2. **Dependent Subgraph**: Uses the source Subgraph as a data source. You can find these in the `source` and `dependent` directories. -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. +- The **source Subgraph** is a basic event-tracking Subgraph that records events emitted by relevant contracts. +- The **dependent Subgraph** references the source Subgraph as a data source, using the entities from the source as triggers. -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. +While the source Subgraph is a standard Subgraph, the dependent Subgraph uses the Subgraph composition feature. ### Source Subgraph -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is `source/subgraph.yaml`. +The source Subgraph tracks events from the Sushiswap v3 Subgraph on the Base chain. This Subgraph's configuration file is `source/subgraph.yaml`. + +> The `souce/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). ### Dependent Subgraph -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. +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. + +> 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). ## Get Started -The following is a how-to guide on using one Subgraph as a data source for another. +The following is a guide that illustrates how to use one Subgraph as a data source for another. This example uses: + +- Sushiswap v3 Subgraph on Base chain +- Two Subgraphs (but you can use up to **5 source** Subgraphs in your own development). ### Step 1. Set Up Your Source Subgraph -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: +To set the source Subgraph as a data source in the dependent Subgraph, include the following in `subgraph.yaml`: ```yaml specVersion: 1.3.0 @@ -68,11 +75,11 @@ dataSources: startBlock: 82522 ``` -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. +Here, `source.address` refers to the Deployment ID of the source Subgraph, and `startBlock` specifies the block from which indexing should begin. ### Step 2. Define Handlers in Dependent Subgraph -Below is an example of defining handlers in the dependent subgraph: +Below is an example of defining handlers in the dependent Subgraph: ```typescript export function handleInitialize(trigger: EntityTrigger): void { @@ -107,7 +114,7 @@ export function handleInitialize(trigger: EntityTrigger): void { } ``` -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. +In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source Subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. `EntityTrigger` has three fields: @@ -119,9 +126,9 @@ Developers can then determine specific actions for the entity data based on the ## Key Takeaways -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. +- Use this powerful tool to quickly scale your Subgraph development and reuse existing data. +- The setup includes creating a base source Subgraph and referencing it in a dependent Subgraph. +- You define handlers in the dependent Subgraph to perform actions based on changes in the source Subgraph's entities. This approach unlocks composability and scalability, simplifying both development and maintenance efficiency. @@ -129,4 +136,4 @@ This approach unlocks composability and scalability, simplifying both developmen 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). -To learn how to define three `source` Subgraphs, check out [this Subgraph composition example repo](https://github.com/isum/subgraph-composition-example). +To learn how to define three source Subgraphs, check out [this Subgraph composition example repo](https://github.com/isum/subgraph-composition-example). From c942a94d9fd89104219d0eb0a06044707b7238b1 Mon Sep 17 00:00:00 2001 From: Michael Macaulay <56690114+MichaelMacaulay@users.noreply.github.com> Date: Fri, 28 Feb 2025 14:00:11 -0500 Subject: [PATCH 8/9] Fix typos --- .../src/pages/en/subgraphs/cookbook/subgraph-composition.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx index 1b2a5ae736b5..706259dbfa33 100644 --- a/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx +++ b/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx @@ -43,7 +43,7 @@ While the source Subgraph is a standard Subgraph, the dependent Subgraph uses th The source Subgraph tracks events from the Sushiswap v3 Subgraph on the Base chain. This Subgraph's configuration file is `source/subgraph.yaml`. -> The `souce/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). +> 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). ### Dependent Subgraph From 90cf635542c8a7f618151dd2d4fea027db9f7bf2 Mon Sep 17 00:00:00 2001 From: benface Date: Sun, 2 Mar 2025 11:29:12 -0500 Subject: [PATCH 9/9] Delete outdated duplicates --- .../cookbook/subgraph-composition.mdx | 128 ------------------ .../cookbook/subgraph-composition.mdx | 128 ------------------ .../cookbook/subgraph-composition.mdx | 128 ------------------ .../cookbook/subgraph-composition.mdx | 128 ------------------ .../cookbook/subgraph-composition.mdx | 128 ------------------ .../cookbook/subgraph-composition.mdx | 128 ------------------ .../cookbook/subgraph-composition.mdx | 128 ------------------ .../cookbook/subgraph-composition.mdx | 128 ------------------ .../cookbook/subgraph-composition.mdx | 128 ------------------ .../cookbook/subgraph-composition.mdx | 128 ------------------ .../cookbook/subgraph-composition.mdx | 128 ------------------ .../cookbook/subgraph-composition.mdx | 128 ------------------ .../cookbook/subgraph-composition.mdx | 128 ------------------ .../cookbook/subgraph-composition.mdx | 128 ------------------ .../cookbook/subgraph-composition.mdx | 128 ------------------ .../cookbook/subgraph-composition.mdx | 128 ------------------ .../cookbook/subgraph-composition.mdx | 128 ------------------ .../cookbook/subgraph-composition.mdx | 128 ------------------ .../cookbook/subgraph-composition.mdx | 128 ------------------ .../cookbook/subgraph-composition.mdx | 128 ------------------ .../cookbook/subgraph-composition.mdx | 128 ------------------ 21 files changed, 2688 deletions(-) delete mode 100644 website/src/pages/ar/subgraphs/cookbook/subgraph-composition.mdx delete mode 100644 website/src/pages/cs/subgraphs/cookbook/subgraph-composition.mdx delete mode 100644 website/src/pages/de/subgraphs/cookbook/subgraph-composition.mdx delete mode 100644 website/src/pages/es/subgraphs/cookbook/subgraph-composition.mdx delete mode 100644 website/src/pages/fr/subgraphs/cookbook/subgraph-composition.mdx delete mode 100644 website/src/pages/hi/subgraphs/cookbook/subgraph-composition.mdx delete mode 100644 website/src/pages/it/subgraphs/cookbook/subgraph-composition.mdx delete mode 100644 website/src/pages/ja/subgraphs/cookbook/subgraph-composition.mdx delete mode 100644 website/src/pages/ko/subgraphs/cookbook/subgraph-composition.mdx delete mode 100644 website/src/pages/mr/subgraphs/cookbook/subgraph-composition.mdx delete mode 100644 website/src/pages/nl/subgraphs/cookbook/subgraph-composition.mdx delete mode 100644 website/src/pages/pl/subgraphs/cookbook/subgraph-composition.mdx delete mode 100644 website/src/pages/pt/subgraphs/cookbook/subgraph-composition.mdx delete mode 100644 website/src/pages/ro/subgraphs/cookbook/subgraph-composition.mdx delete mode 100644 website/src/pages/ru/subgraphs/cookbook/subgraph-composition.mdx delete mode 100644 website/src/pages/sv/subgraphs/cookbook/subgraph-composition.mdx delete mode 100644 website/src/pages/tr/subgraphs/cookbook/subgraph-composition.mdx delete mode 100644 website/src/pages/uk/subgraphs/cookbook/subgraph-composition.mdx delete mode 100644 website/src/pages/ur/subgraphs/cookbook/subgraph-composition.mdx delete mode 100644 website/src/pages/vi/subgraphs/cookbook/subgraph-composition.mdx delete mode 100644 website/src/pages/zh/subgraphs/cookbook/subgraph-composition.mdx diff --git a/website/src/pages/ar/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/ar/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/ar/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/cs/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/cs/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/cs/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/de/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/de/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/de/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/es/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/es/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/es/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/fr/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/fr/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/fr/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/hi/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/hi/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/hi/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/it/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/it/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/it/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/ja/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/ja/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/ja/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/ko/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/ko/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/ko/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/mr/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/mr/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/mr/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/nl/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/nl/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/nl/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/pl/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/pl/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/pl/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/pt/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/pt/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/pt/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/ro/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/ro/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/ro/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/ru/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/ru/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/ru/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/sv/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/sv/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/sv/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/tr/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/tr/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/tr/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/uk/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/uk/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/uk/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/ur/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/ur/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/ur/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/vi/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/vi/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/vi/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example). diff --git a/website/src/pages/zh/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/zh/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 7a9ecaf2114b..000000000000 --- a/website/src/pages/zh/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: Subgraph Composition with Sushiswap v3 on Base -sidebarTitle: 'Subgraph Composition' ---- - -Leverage subgraph composition to create a base subgraph that includes essential data, then build additional subgraphs on top of it. - -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - You can use existing subgraphs, but they must be redeployed with a new spec version, which doesn't require you to write new code. However, you may want to restructure your subgraph to split out the logic as you move to a composable subgraph world. - -## Overview - -### Benefits of Composition - -Subgraph composition is a powerful feature for scaling, allowing you to: - -- Reuse and combine existing data -- Speed up your subgraph's syncing speed -- Handle errors and optimize the resync -- Enhance your subgraph development to scale effortlessly - -## Setup - -This guide uses the Sushiswap v3 subgraph on Base chain. - -### Tools Needed - -The setup involves two subgraphs: - -1. **Source Subgraph**: Tracks event data as entities. -2. **Dependent Subgraph**: Uses the source subgraph as a data source. - -You can find these in the `source` and `dependent` directories. - -- The **source subgraph** is a basic event-tracking subgraph that records events emitted by relevant contracts. -- The **dependent subgraph** references the source subgraph as a data source, using the entities from the source as triggers. - -While the source subgraph is a standard subgraph, the dependent subgraph utilizes the subgraph composition feature. - -### Source Subgraph - -The source subgraph tracks events from the Sushiswap v3 subgraph on the Base chain. This subgraph's configuration file is in the `source/subgraph.yaml`. - -### Dependent Subgraph - -The dependent subgraph is in the `dependent/subgraph.yaml` directory, 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. - -## Get Started - -The following is a how-to-guide on using one subgraph as a data source for another. - -### Step 1. Set Up Your Source Subgraph - -To set the source subgraph as a data source in the dependent subgraph, include the following in `subgraph.yaml`: - -```yaml -specVersion: 1.3.0 -schema: - file: ./schema.graphql -dataSources: - - kind: subgraph - name: Factory - network: base - source: - address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz' - startBlock: 82522 -``` - -Here, `source.address` refers to the Deployment ID of the source subgraph, and `startBlock` specifies the block from which indexing should begin. - -### Step 2. Define Handlers in Dependent Subgraph - -Below is an example of defining handlers in the dependent subgraph: - -```typescript -export function handleInitialize(trigger: EntityTrigger): void { - if (trigger.operation === EntityOp.Create) { - let entity = trigger.data - let poolAddressParam = Address.fromBytes(entity.poolAddress) - - // Update pool sqrt price and tick - let pool = Pool.load(poolAddressParam.toHexString()) as Pool - pool.sqrtPrice = entity.sqrtPriceX96 - pool.tick = BigInt.fromI32(entity.tick) - pool.save() - - // Update token prices - let token0 = Token.load(pool.token0) as Token - let token1 = Token.load(pool.token1) as Token - - // Update ETH price in USD - let bundle = Bundle.load('1') as Bundle - bundle.ethPriceUSD = getEthPriceInUSD() - bundle.save() - - updatePoolDayData(entity) - updatePoolHourData(entity) - - // Update derived ETH price for tokens - token0.derivedETH = findEthPerToken(token0) - token1.derivedETH = findEthPerToken(token1) - token0.save() - token1.save() - } -} -``` - -In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source subgraph, passed as `EntityTrigger`. The handler updates the pool and token entities based on data from the new `Initialize` entity. - -`EntityTrigger` has three fields: - -1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`. -2. `type`: Indicates the entity type. -3. `data`: Contains the entity data. - -Developers can then determine specific actions for the entity data based on the operation type. - -## Key Takeaways - -- Use this powerful tool to scale your subgraph development and reuse existing data. -- The setup includes creating a base source subgraph and referencing it in a dependent subgraph. -- You define handlers in the dependent subgraph to perform actions based on changes in the source subgraph's entities. - -This approach unlocks modularity and scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -To use other advance features in your subgraph check out [subgraph advance features](/developing/creating/advanced/) and the [subgraph composition repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph). To learn how to define three `source` subgraphs check out the [subgraph-composition-example](https://github.com/isum/subgraph-composition-example).