diff --git a/website/src/pages/ar/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/ar/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/ar/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/ar/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/cs/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/cs/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/cs/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/cs/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/de/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/de/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/de/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/de/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/en/subgraphs/cookbook/_meta.js b/website/src/pages/en/subgraphs/cookbook/_meta.js index 1093ca048416..e642f12ef11d 100644 --- a/website/src/pages/en/subgraphs/cookbook/_meta.js +++ b/website/src/pages/en/subgraphs/cookbook/_meta.js @@ -1,6 +1,4 @@ export default { - 'subgraph-composition-three-sources': '', - 'subgraph-composition': '', 'subgraph-debug-forking': '', near: '', arweave: '', diff --git a/website/src/pages/en/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/en/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/en/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). diff --git a/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx b/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx deleted file mode 100644 index 706259dbfa33..000000000000 --- a/website/src/pages/en/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/es/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/es/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/es/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/es/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/fr/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/fr/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/fr/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/fr/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/hi/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/hi/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/hi/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/hi/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/it/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/it/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/it/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/it/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/ja/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/ja/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/ja/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/ja/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/ko/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/ko/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/ko/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/ko/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/mr/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/mr/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/mr/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/mr/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/nl/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/nl/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/nl/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/nl/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/pl/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/pl/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/pl/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/pl/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/pt/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/pt/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/pt/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/pt/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/ro/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/ro/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/ro/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/ro/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/ru/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/ru/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/ru/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/ru/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/sv/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/sv/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/sv/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/sv/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/tr/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/tr/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/tr/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/tr/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/uk/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/uk/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/uk/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/uk/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/ur/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/ur/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/ur/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/ur/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/vi/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/vi/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/vi/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/vi/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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). diff --git a/website/src/pages/zh/subgraphs/cookbook/subgraph-composition-three-sources.mdx b/website/src/pages/zh/subgraphs/cookbook/subgraph-composition-three-sources.mdx deleted file mode 100644 index 0c80e9d674c3..000000000000 --- a/website/src/pages/zh/subgraphs/cookbook/subgraph-composition-three-sources.mdx +++ /dev/null @@ -1,98 +0,0 @@ ---- -title: Aggregate Data Using Subgraph Composition -sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' ---- - -Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. - -> Important Reminders: -> -> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). -> - This feature requires `specVersion` 1.3.0. - -## Overview - -Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. - -## Prerequisites - -To deploy **all** Subgraphs locally, you must have the following: - -- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally -- An [IPFS](https://docs.ipfs.tech/) instance running locally -- [Node.js](https://nodejs.org) and npm - -## Get Started - -The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. - -### Specifics - -- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts. -- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. -- Each source Subgraph is optimized with a specific entity. -- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance. - -### Step 1. Deploy Block Time Source Subgraph - -This first source Subgraph calculates the block time for each block. - -- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. -- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the following commands: - -```bash -npm install -npm run codegen -npm run build -npm run create-local -npm run deploy-local -``` - -### Step 2. Deploy Block Cost Source Subgraph - -This second source Subgraph indexes the cost of each block. - -#### Key Functions - -- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. -- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly. - -To deploy this Subgraph locally, run the same commands as above. - -### Step 3. Define Block Size in Source Subgraph - -This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. - -#### Key Functions - -- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. -- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly. - -### Step 4. Combine Into Block Stats Subgraph - -This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. - -> Note: -> -> - Any change to a source Subgraph will likely generate a new deployment ID. -> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. -> - All source Subgraphs should be deployed before the composed Subgraph is deployed. - -#### Key Functions - -- It provides a consolidated data model that encompasses all relevant block metrics. -- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. - -## Key Takeaways - -- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. -- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. -- This feature unlocks scalability, simplifying both development and maintenance efficiency. - -## Additional Resources - -- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). -- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). -- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). 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 706259dbfa33..000000000000 --- a/website/src/pages/zh/subgraphs/cookbook/subgraph-composition.mdx +++ /dev/null @@ -1,139 +0,0 @@ ---- -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. - -> 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 `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. - -## Introduction - -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 -- 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 - -## Architecture Overview - -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. - -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 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.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` 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 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`: - -```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 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. - -## Additional Resources - -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).