Substreams Triggers
Reading time: 2 minutes
Use Custom Triggers and enable the full use GraphQL.
Обзор
Custom Triggers allow you to send data directly into your subgraph mappings file and entities, which are similar to tables and fields. This enables you to fully use the GraphQL layer.
By importing the Protobuf definitions emitted by your Substreams module, you can receive and process this data in your subgraph’s handler. This ensures efficient and streamlined data management within the subgraph framework.
Defining handleTransactions
The following code demonstrates how to define a handleTransactions
function in a subgraph handler. This function receives raw Substreams bytes as a parameter and decodes them into a Transactions
object. For each transaction, a new subgraph entity is created.
export function handleTransactions(bytes: Uint8Array): void { let transactions = assembly.eth.transaction.v1.Transactions.decode(bytes.buffer).transactions // 1. if (transactions.length == 0) { log.info('No transactions found', []) return } for (let i = 0; i < transactions.length; i++) { // 2. let transaction = transactions[i] let entity = new Transaction(transaction.hash) // 3. entity.from = transaction.from entity.to = transaction.to entity.save() }}
Here’s what you’re seeing in the mappings.ts
file:
- The bytes containing Substreams data are decoded into the generated
Transactions
object, this object is used like any other AssemblyScript object - Looping over the transactions
- Create a new subgraph entity for every transaction
To go through a detailed example of a trigger-based subgraph, check out the tutorial.
Дополнительные ресурсы
To scaffold your first project in the Development Container, check out one of the How-To Guide.