Tutorial: Configurar un Subgrafo Potenciado por Substreams en Solana
Reading time: 3 minutes
Successfully set up a trigger-based Substreams-powered subgraph for a Solana SPL token.
Comenzar
For a video tutorial, check out How to Index Solana with a Substreams-powered Subgraph
Prerequisites
Antes de comenzar, asegúrate de:
- Complete the Getting Started Guide to set up your development environment using a Dev Container.
- Estar familiarizado con The Graph y conceptos básicos de blockchain, como transacciones y Protobufs.
Paso 1: Inicializa tu proyecto
-
Abre tu Dev Container y ejecuta el siguiente comando para inicializar tu proyecto:
substreams init
-
Selecciona la opción de proyecto “mínimo”.
-
Reemplaza el contenido del archivo ‘substreams.yam’l generado con la siguiente configuración, que filtra las transacciones para la cuenta de Orca en el ID del programa SPL token:
specVersion: v0.1.0package: name: my_project_sol version: v0.1.0imports: # Pass your spkg of interest solana: https://github.com/streamingfast/substreams-solana-spl-token/raw/master/tokens/solana-spl-token-v0.1.0.spkgmodules: - name: map_spl_transfers use: solana:map_block # Select corresponding modules available within your spkg initialBlock: 260000082 - name: map_transactions_by_programid use: solana:solana:transactions_by_programid_without_votesnetwork: solana-mainnet-betaparams: # Modify the param fields to meet your needs # For program_id: TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA map_spl_transfers: token_contract:orcaEKTdK7LKz57vaAYr9QeNsVEPfiu6QeMU1kektZE
Paso 2: Generar el Manifiesto del Subgrafo
Una vez que el proyecto esté inicializado, genera un manifiesto de subgraph ejecutando el siguiente comando en el Dev Container:
substreams codegen subgrafo
Generarás un manifiesto subgraph.yaml que importa el paquete de Substreams como una fuente de datos:
---dataSources: - kind: substreams name: my_project_sol network: solana-mainnet-beta source: package: moduleName: map_spl_transfers # Module defined in the substreams.yaml file: ./my-project-sol-v0.1.0.spkg mapping: apiVersion: 0.0.7 kind: substreams/graph-entities file: ./src/mappings.ts handler: handleTriggers
Paso 3: Definir Entidades en schema.graphql
Define the fields you want to save in your subgraph entities by updating the schema.graphql
file.
Here is an example:
type MyTransfer @entity { id: ID! amount: String! source: String! designation: String! signers: [String!]!}
Este esquema define una entidad ‘MyTransfer’ con campos como ‘id’, ‘amount’, ‘source’, ‘designation’ y ‘signers’.
Paso 4: Manejar los datos de Substreams en ‘mappings.ts’
With the Protobuf objects generated, you can now handle the decoded Substreams data in your mappings.ts
file found in the ./src
directory.
The example below demonstrates how to extract to subgraph entities the non-derived transfers associated to the Orca account id:
import { Protobuf } from 'as-proto/assembly'import { Events as protoEvents } from './pb/sf/solana/spl/token/v1/Events'import { MyTransfer } from '../generated/schema'export function handleTriggers(bytes: Uint8Array): void { const input: protoEvents = Protobuf.decode<protoEvents>(bytes, protoEvents.decode) for (let i = 0; i < input.data.length; i++) { const event = input.data[i] if (event.transfer != null) { let entity_id: string = `${event.txnId}-${i}` const entity = new MyTransfer(entity_id) entity.amount = event.transfer!.instruction!.amount.toString() entity.source = event.transfer!.accounts!.source entity.designation = event.transfer!.accounts!.destination if (event.transfer!.accounts!.signer!.single != null) { entity.signers = [event.transfer!.accounts!.signer!.single!.signer] } else if (event.transfer!.accounts!.signer!.multisig != null) { entity.signers = event.transfer!.accounts!.signer!.multisig!.signers } entity.save() } }}
Paso 5: Generar Archivos Protobuf
Para generar objetos Protobuf en AssemblyScript, ejecuta el siguiente comando:
npm run protogen
Este comando convierte las definiciones de Protobuf en AssemblyScript, lo que te permite usarlas en el controlador del subgrafo.
Conclusion
Congratulations! You’ve successfully set up a trigger-based Substreams-powered subgraph for a Solana SPL token. You can take the next step by customizing your schema, mappings, and modules to fit your specific use case.
Video Tutorial
Recursos Adicionales
Para una personalización y optimización más avanzada, consulta la [documentación oficial de Substreams] (https://substreams.streamingfast.io/tutorials/solana).