Substreams-powered Subgraphs > Substreams Triggers

Substreams Triggers

Reading time: 2 min

Custom triggers allow you to send data directly into your subgraph mappings file and entities (similar to tables and fields), enabling full use of the GraphQL layer. By importing the Protobuf definitions emitted by your Substreams module, you can receive and process this data within your subgraph’s handler, ensuring efficient and streamlined data management within the subgraph framework.

Note: If you haven’t already, visit one of the How-To Guides found here to scaffold your first project in the Development Container.

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).trasanctions // 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:

  1. The bytes containing Substreams data are decoded into the generated Transactions object, this object is used like any other AssemblyScript object
  2. Looping over the transactions
  3. Create a new subgraph entity for every transaction

To go through a detailed example of a trigger-based subgraph, click here.

ページを編集

Introduction
Tutorial
ページを編集