| 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.
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
Transactionsobject, 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.