Solana

The Sentio SDK enables indexing data from the Solana blockchain, known for its high throughput and unique account model.

Core Concepts

Solana integration focuses on processing instructions within transactions.

Processors

Solana integration uses a single main processor type:

  1. SolanaBaseProcessor(or generated subclasses): Binds to a specific Solana program address (contract ID). It requires an InstructionCoder (like one provided by Anchor's Typescript client) to decode instructions.
    • Typical Use Case: Monitoring interactions with a specific Solana program (e.g., DeFi protocol, NFT marketplace).
    • Binding: Typically done via generated processors: MyProgramProcessor.bind({ address: 'program_id_...', network: SolanaChainId.SOLANA_MAINNET, startBlock: 150000000 })
    • Processors can be generated using the Sentio CLI and an Anchor IDL: sentio gen --idl <idl_path_or_fetch_address> solana

Handlers

The primary handler type is for instructions:

  • onInstruction(instructionName, handler(instruction, ctx, accounts?)): Triggered when an instruction matching the instructionName (defined in the IDL) is executed within a transaction targeting the bound program address. The instruction object contains the decoded instruction data (instruction.data).
    • The accounts argument (optional) provides the string addresses of the accounts involved in the instruction, in the order defined by the IDL.
    • By default, only top-level instructions are processed. Set processInnerInstruction: true in the bind options to also process instructions executed via Cross-Program Invocation (CPI).

Context (ctx)

Instruction handlers receive a SolanaContext object providing:

  • Chain information: network (SolanaChainId), slot (equivalent to block number).
  • Program details: address (bound program address), programName.
  • Standard SDK outputs: ctx.meter.Counter('...'), ctx.eventLogger.emit('...').

Instruction Coder

A crucial part of Solana processing is the InstructionCoder. The SDK expects an object with a decode method capable of taking raw instruction data (Buffer or base58 string) and returning a structured Instruction object (typically { name: string, data: any }).

  • Anchor: If using Anchor, the Typescript client generated from an IDL usually provides a compatible coder (program.coder.instruction). Sentio's code generation (sentio gen) automatically wires this up.
  • Manual: For non-Anchor programs or custom decoding, you need to provide your own implementation conforming to the InstructionCoder interface during processor binding.

Getting Started Example (Anchor Program)

import { SolanaContext, SolanaChainId } from "@sentio/sdk/solana";
// Assuming types and processor generated by `sentio gen` from an Anchor IDL
import { MyAnchorProgramProcessor, InitializeEvent } from "./types/solana/my_anchor_program.js"; 

const PROGRAM_ADDRESS = "G1...YourProgramAddress...";

MyAnchorProgramProcessor.bind({
  address: PROGRAM_ADDRESS,
  network: SolanaChainId.SOLANA_DEVNET, 
  startBlock: 180000000, // Example start slot
  // processInnerInstruction: true, // Optional: Set true to process CPI instructions
})
.onInitialize(async (instruction: InitializeEvent, ctx: SolanaContext, accounts) => {
  // instruction.data contains decoded arguments for the 'initialize' instruction
  const initialValue = instruction.data.initialValue;
  const adminAccount = accounts[0]; // Access accounts by index if needed

  ctx.meter.Counter("initializations").add(1);
  ctx.eventLogger.emit("Initialized", {
    distinctId: adminAccount, // Use an account address as distinctId
    initValue: initialValue.toString(),
    program: PROGRAM_ADDRESS,
  });
})
.onSomeOtherInstruction(async (instruction, ctx) => {
  // Handler for another instruction defined in the IDL
  ctx.meter.Counter("other_instruction_calls").add(1);
  ctx.eventLogger.emit("OtherInstructionCalled", {
    arg1: instruction.data.arg1, 
    program: PROGRAM_ADDRESS,
  });
});

This documentation covers the Solana integration. For detailed API information, refer to solana-processor.ts, solana-context.ts, and the generated code from your program's IDL.