coral_xyz_codegen 1.0.0-beta.9
coral_xyz_codegen: ^1.0.0-beta.9 copied to clipboard
Code generation for coral_xyz — generates typed Dart clients from Anchor/Quasar IDL files using build_runner.
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 #
- Annotate a library with
@AnchorProgram:
import 'package:coral_xyz/coral_xyz.dart';
@AnchorProgram(
idlPath: 'target/idl/counter.json',
programId: 'Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS',
)
library;
- 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 withinitialize(),increment()methodsCounterAccount— account data class withcountfield, Borsh decode/encodeInitializeInstruction,IncrementInstruction— instruction builders with account configsCounterError— 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