coral_xyz_codegen

Code generation companion for coral_xyz. Generates typed Dart clients from Anchor and Quasar IDL files using build_runner and source_gen.

Installation

Add both packages to your pubspec.yaml:

dependencies:
  coral_xyz: ^1.0.0-beta.9

dev_dependencies:
  build_runner: ^2.4.0
  coral_xyz_codegen: ^1.0.0-beta.9

Usage

  1. Annotate a library with @AnchorProgram:
import 'package:coral_xyz/coral_xyz.dart';

@AnchorProgram(
  idlPath: 'target/idl/counter.json',
  programId: 'Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS',
)
library;
  1. Run code generation:
dart run build_runner build

This produces a .anchor.dart file with:

  • A typed program class with instruction methods
  • Account data classes with Borsh serialization
  • Instruction builder classes
  • Error classes with code mappings
  • Custom types (structs and enums) from the IDL

Configuration

Add builder options in build.yaml:

targets:
  $default:
    builders:
      coral_xyz_codegen|anchorBuilder:
        enabled: true
        options:
          idl_path: "target/idl"
          output_dir: "lib/generated"

What gets generated

Given a counter program IDL, the generator produces:

  • CounterProgram — typed program class with initialize(), increment() methods
  • CounterAccount — account data class with count field, Borsh decode/encode
  • InitializeInstruction, IncrementInstruction — instruction builders with account configs
  • CounterError — error enum with code-to-instance mapping

Requirements

  • Dart SDK ^3.9.0
  • coral_xyz ^1.0.0-beta.9
  • An Anchor or Quasar IDL JSON file

Libraries

builder
Builder entry point for build_runner.
coral_xyz_codegen
Code generation for coral_xyz.