|
| 1 | +Here you’ll walk through a Solana based example for setting up your Substreams-powered subgraph project. If you haven’t already, first check out the [Getting Started Guide](https://github.com/streamingfast/substreams/blob/enol/how-to-guides/docs/new/how-to-guides/intro-how-to-guides.md) for more information on how to initialize your project. |
| 2 | + |
| 3 | +Consider the following example of a Substreams manifest (`substreams.yaml`), a configuration file similar to the `subgraph.yaml`, using the SPL token program Id: |
| 4 | + |
| 5 | +```graphql |
| 6 | +specVersion: v0.1.0 |
| 7 | +package: |
| 8 | + name: my_project_sol |
| 9 | + version: v0.1.0 |
| 10 | + |
| 11 | +imports: #Pass your spkg of interest |
| 12 | + solana: https://github.com/streamingfast/substreams-solana-spl-token/raw/master/tokens/solana-spl-token-v0.1.0.spkg |
| 13 | + |
| 14 | +modules: |
| 15 | + |
| 16 | + - name: map_spl_transfers |
| 17 | + use: solana:map_block #Select corresponding modules available within your spkg |
| 18 | + initialBlock: 260000082 |
| 19 | + |
| 20 | + - name: map_transactions_by_programid |
| 21 | + use: solana:solana:transactions_by_programid_without_votes |
| 22 | + |
| 23 | +network: solana-mainnet-beta |
| 24 | + |
| 25 | +params: #Modify the param fields to meet your needs |
| 26 | + #For program_id: TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA |
| 27 | + map_spl_transfers: token_contract:orcaEKTdK7LKz57vaAYr9QeNsVEPfiu6QeMU1kektZE |
| 28 | +``` |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +Now see the corresponding subgraph manifest **(`subgraph.yaml`)** using a Substreams package as the data source: |
| 33 | + |
| 34 | +```yaml |
| 35 | +specVersion: 1.0.0 |
| 36 | +description: my-project-sol Substreams-powered-Subgraph |
| 37 | +indexerHints: |
| 38 | + prune: auto |
| 39 | +schema: |
| 40 | + file: ./schema.graphql |
| 41 | +dataSources: |
| 42 | + - kind: substreams |
| 43 | + name: my_project_sol |
| 44 | + network: solana-mainnet-beta |
| 45 | + source: |
| 46 | + package: |
| 47 | + moduleName: map_spl_transfers |
| 48 | + file: ./my-project-sol-v0.1.0.spkg |
| 49 | + mapping: |
| 50 | + apiVersion: 0.0.7 |
| 51 | + kind: substreams/graph-entities |
| 52 | + file: ./src/mappings.ts |
| 53 | + handler: handleTriggers |
| 54 | +``` |
| 55 | +
|
| 56 | +Once your manifests are created, define in the `schema.graphql` the data fields you’d like saved in your subgraph entities: |
| 57 | + |
| 58 | +```graphql |
| 59 | +type MyTransfer @entity { |
| 60 | + id: ID! |
| 61 | + amount: String! |
| 62 | + source: String! |
| 63 | + designation: String! |
| 64 | + signers: [String!]! |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +The Protobuf object is generated in AssemblyScript by running `npm run protogen` after running `substreams codegen subgraph` in the devcontainer, so you can import it in the subgraph code. Then transform your decoded Substreams data within the `src/mappings.ts` file, just like in a standard subgraph: |
| 69 | + |
| 70 | +```tsx |
| 71 | +import { Protobuf } from "as-proto/assembly"; |
| 72 | +import { Events as protoEvents } from "./pb/sf/solana/spl/token/v1/Events"; |
| 73 | +import { MyTransfer } from "../generated/schema"; |
| 74 | +
|
| 75 | +export function handleTriggers(bytes: Uint8Array): void { |
| 76 | + const input: protoEvents = Protobuf.decode<protoEvents>(bytes, protoEvents.decode); |
| 77 | + |
| 78 | + for (let i=0; i<input.data.length; i++) { |
| 79 | + const event = input.data[i]; |
| 80 | + |
| 81 | + if (event.transfer != null) { |
| 82 | + let entity_id: string = `${event.txnId}-${i}`; |
| 83 | + const entity = new MyTransfer(entity_id); |
| 84 | + entity.amount = (event.transfer!.instruction!.amount).toString(); |
| 85 | + entity.source = event.transfer!.accounts!.source; |
| 86 | + entity.designation = event.transfer!.accounts!.destination; |
| 87 | + |
| 88 | + if (event.transfer!.accounts!.signer!.single != null){ |
| 89 | + entity.signers = [event.transfer!.accounts!.signer!.single.signer]; |
| 90 | + } |
| 91 | + else if (event.transfer!.accounts!.signer!.multisig != null) { |
| 92 | + entity.signers = event.transfer!.accounts!.signer!.multisig!.signers; |
| 93 | + } |
| 94 | + entity.save(); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +Here's what you’re seeing in the `mappings.ts`: |
| 101 | + |
| 102 | +1. The bytes containing Substreams data are decoded into the generated `Transactions` object, this object is used like any other AssemblyScript object |
| 103 | +2. Looping over the transactions |
| 104 | +3. Create a new subgraph entity for every transaction |
| 105 | + |
| 106 | +> Note: It's beneficial to have more of your logic in Substreams, as it allows for a parallelized model, whereas triggers are linearly consumed in `graph-node`. |
| 107 | +> |
0 commit comments