Disparadores de Substreams
Reading time: 2 minutes
Use Custom Triggers and enable the full use GraphQL.
Descripción
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
El siguiente código demuestra cómo definir una función ‘handleTransactions’ en un controlador de subgraph. Esta función recibe bytes sin procesar de Substreams como parámetro y los decodifica en un objeto ‘Transactions’. Para cada transacción, se crea una nueva entidad en el subgrafo.
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:
- Los bytes que contienen los datos de Substreams se decodifican en el objeto ‘Transactions’ generado, y este objeto se utiliza como cualquier otro objeto de AssemblyScript.
- Iterando sobre las transacciones
- Crear una nueva entidad de subgrafo para cada transacción.
To go through a detailed example of a trigger-based subgraph, check out the tutorial.
Recursos Adicionales
To scaffold your first project in the Development Container, check out one of the How-To Guide.