Skip to main content

solana_kit_anchor 0.11.1 copy "solana_kit_anchor: ^0.11.1" to clipboard
solana_kit_anchor: ^0.11.1 copied to clipboard

Anchor program runtime for Solana Kit Dart — discriminators, Anchor IDL parsing, and dynamic account and event codecs.

solana_kit_anchor #

pub package docs website CI coverage

Anchor program runtime for the Solana Kit Dart SDK: discriminators, Anchor IDL parsing, dynamic account and event codecs, and Anchor error resolution.

What you get #

  • Discriminators — Anchor sighash helpers (sha256("namespace:name")[0..8]) for instructions, accounts, and events
  • IDL parsing — a typed model of Anchor IDL 0.30 documents with struct, Rust-enum, tuple, option, vec, and array types
  • Dynamic coder — build codecs from an IDL at runtime: encode and decode instruction arguments and account data, and extract typed events from program logs (Program data: lines)
  • Error resolution — decode Anchor error codes against the standard Anchor table and program-defined IDL errors
  • Pure-Dart SHA-256 — no platform channels or external crypto dependencies

The package does not port the Anchor on-chain framework, the CLI, or client code generation. Programs that rely on generics in their IDLs are better served by Codama-generated clients; the dynamic coder rejects generic type instantiations instead of silently mis-encoding them.

AnchorIdlProgram.parse requires every instruction, account, and event discriminator to be a nonempty list of integer bytes (0..255). Missing, empty, or malformed values throw ArgumentError. Custom discriminator lengths are supported alongside Anchor's default eight-byte discriminators.

AnchorCoder throws ArgumentError when building a codec for a defined type that references itself, directly or through other types. This includes cycles through struct fields, enum variants, options, vectors, and arrays. Repeated references to acyclic types are supported.

Installation #

Install the package directly:

dependencies:
  "solana_kit_anchor": ^0.11.1

If your app uses several Solana Kit packages together, you can also depend on the umbrella package instead:

dart pub add solana_kit

Inside this monorepo, Dart workspace resolution uses the local package automatically.

Documentation #

For architecture notes, getting-started guides, and cross-package examples, start with the workspace docs site and then drill down into the package README and API reference.

Usage #

Parse an Anchor IDL and code accounts dynamically #

Use the runtime coder when a program ships an Anchor IDL: encode instruction arguments, decode account data, and pull typed events out of program logs without writing codecs by hand.

import 'dart:io';

import 'package:solana_kit_anchor/solana_kit_anchor.dart';

void main() {
  final idl = AnchorIdlProgram.parse(
    File('idls/counter.json').readAsStringSync(),
  );
  final coder = AnchorCoder(idl);

  final data = coder.encodeInstructionData('increment', {
    'delta': BigInt.one,
  });
  // 8-byte discriminator + encoded arguments, ready for an Instruction.
  print(data.length);

  // Account data as fetched from RPC, starting with the 8-byte
  // account discriminator.
  final fetchedAccountBytes = <int>[];
  final counter = coder.decodeAccount('Counter', fetchedAccountBytes);
  print(counter.data['count']); // BigInt

  // Program logs from a transaction, e.g. lines like
  // "Program data: dW5rbm93bkRlY29kZXI=".
  final logs = <String>[];
  final events = coder.decodeEventLogs(logs);
  for (final event in events) {
    print('${event.name}: ${event.data}');
  }
}

Discriminator helpers and error resolution round out the runtime: instructionDiscriminator, accountDiscriminator, eventDiscriminator, and anchorProgramError resolve against the standard Anchor table plus program-defined IDL errors.

Key APIs #

Fixed-array lengths are validated when their IDL type is parsed, including when a field's type is first accessed during codec construction. Lengths must be integers from zero to one million, inclusive, matching the default array decoder limit. Negative, fractional, non-finite, and excessive lengths throw ArgumentError.

  • anchorSighash(namespace, name), instructionDiscriminator, accountDiscriminator, eventDiscriminator
  • AnchorIdlProgram.parse, AnchorIdlType.parse
  • AnchorCoder: encodeInstructionData, decodeInstructionData, encodeAccount, decodeAccount, decodeEventLogs
  • anchorProgramError(code, idl: …), standardAnchorErrorMessages
  • sha256 (FIPS 180-4, pure Dart)

Reference #

The runtime mirrors the behavior of Anchor's TypeScript client (otter-sec/anchor, Apache-2.0) for discriminator computation, event emission format, and error code layout.

0
likes
150
points
749
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Anchor program runtime for Solana Kit Dart — discriminators, Anchor IDL parsing, and dynamic account and event codecs.

Homepage
Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

solana_kit_addresses, solana_kit_codecs, solana_kit_codecs_core, solana_kit_codecs_data_structures, solana_kit_codecs_numbers, solana_kit_codecs_strings, solana_kit_errors

More

Packages that depend on solana_kit_anchor