3 minutes
Tutorial: Set Up a Substreams-Powered Subgraph on Solana
Successfully set up a trigger-based Substreams-powered subgraph for a Solana SPL token.
Get Started
For a video tutorial, check out How to Index Solana with a Substreams-powered Subgraph
Prerequisites
Before starting, make sure to:
- Complete the Getting Started Guide to set up your development environment using a Dev Container.
- Be familiar with The Graph and basic blockchain concepts such as transactions and Protobufs.
Step 1: Initialize Your Project
-
Open your Dev Container and run the following command to initialize your project:
1substreams init
-
Select the “minimal” project option.
-
Replace the contents of the generated
substreams.yaml
file with the following configuration, which filters transactions for the Orca account on the SPL token program ID:
1specVersion: v0.1.02package:3 name: my_project_sol4 version: v0.1.056imports: # Pass your spkg of interest7 solana: https://github.com/streamingfast/substreams-solana-spl-token/raw/master/tokens/solana-spl-token-v0.1.0.spkg89modules:10 - name: map_spl_transfers11 use: solana:map_block # Select corresponding modules available within your spkg12 initialBlock: 2600000821314 - name: map_transactions_by_programid15 use: solana:solana:transactions_by_programid_without_votes1617network: solana-mainnet-beta1819params: # Modify the param fields to meet your needs20 # For program_id: TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA21 map_spl_transfers: token_contract:orcaEKTdK7LKz57vaAYr9QeNsVEPfiu6QeMU1kektZE
Step 2: Generate the Subgraph Manifest
Once the project is initialized, generate a subgraph manifest by running the following command in the Dev Container:
1substreams codegen subgraph
You will generate asubgraph.yaml
manifest which imports the Substreams package as a data source:
1---2dataSources:3 - kind: substreams4 name: my_project_sol5 network: solana-mainnet-beta6 source:7 package:8 moduleName: map_spl_transfers # Module defined in the substreams.yaml9 file: ./my-project-sol-v0.1.0.spkg10 mapping:11 apiVersion: 0.0.712 kind: substreams/graph-entities13 file: ./src/mappings.ts14 handler: handleTriggers
Step 3: Define Entities in schema.graphql
Define the fields you want to save in your subgraph entities by updating the schema.graphql
file.
Here is an example:
1type MyTransfer @entity {2 id: ID!3 amount: String!4 source: String!5 designation: String!6 signers: [String!]!7}
This schema defines a MyTransfer
entity with fields such as id
, amount
, source
, designation
, and signers
.
Step 4: Handle Substreams Data in 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:
1import { Protobuf } from 'as-proto/assembly'2import { Events as protoEvents } from './pb/sf/solana/spl/token/v1/Events'3import { MyTransfer } from '../generated/schema'45export function handleTriggers(bytes: Uint8Array): void {6 const input: protoEvents = Protobuf.decode<protoEvents>(bytes, protoEvents.decode)78 for (let i = 0; i < input.data.length; i++) {9 const event = input.data[i]1011 if (event.transfer != null) {12 let entity_id: string = `${event.txnId}-${i}`13 const entity = new MyTransfer(entity_id)14 entity.amount = event.transfer!.instruction!.amount.toString()15 entity.source = event.transfer!.accounts!.source16 entity.designation = event.transfer!.accounts!.destination1718 if (event.transfer!.accounts!.signer!.single != null) {19 entity.signers = [event.transfer!.accounts!.signer!.single!.signer]20 } else if (event.transfer!.accounts!.signer!.multisig != null) {21 entity.signers = event.transfer!.accounts!.signer!.multisig!.signers22 }23 entity.save()24 }25 }26}
Step 5: Generate Protobuf Files
To generate Protobuf objects in AssemblyScript, run the following command:
1npm run protogen
This command converts the Protobuf definitions into AssemblyScript, allowing you to use them in the subgraph’s handler.
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
مصادر إضافية
For more advanced customization and optimizations, check out the official Substreams documentation.