Skip to content

Latest commit

 

History

History
47 lines (33 loc) · 1.86 KB

File metadata and controls

47 lines (33 loc) · 1.86 KB
title Substreams Triggers

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:

  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, check out the tutorial.

مصادر إضافية

To scaffold your first project in the Development Container, check out one of the How-To Guide.