coral_xyz 1.0.0-beta.8
coral_xyz: ^1.0.0-beta.8 copied to clipboard
A Dart client for Anchor programs on Solana. Provides type-safe program interactions, IDL parsing, and seamless integration with Solana's Anchor framework.
example/README.md
π Coral XYZ Anchor Examples #
This directory contains comprehensive examples demonstrating how to use the Dart Coral XYZ Anchor client library. Each example is self-contained and shows different aspects of Anchor program interaction.
π Available Examples #
π° Basic Examples #
basic_usage.dart
Comprehensive feature overview - Demonstrates the essential components and workflows for interacting with Anchor programs.
- Connection setup and management
- Wallet and keypair handling
- Provider configuration
- IDL loading and parsing
- Program instance creation
- Core utility functions
Run with: dart basic_usage.dart
counter_basic.dart
TypeScript tutorial equivalent - Mirrors the "basic-1" tutorial from TypeScript Anchor documentation.
- IDL loading and parsing
- Connection and provider setup
- Account generation
- Transaction building patterns
- TypeScript compatibility patterns
Run with: dart counter_basic.dart
program_interaction.dart
Production patterns - Shows how to interact with deployed Anchor programs in real applications.
- RPC calls and account fetching
- PDA (Program Derived Address) generation
- Error handling for network operations
- Account lookup patterns
- IDL structure inspection
Run with: dart program_interaction.dart
- Full program interaction lifecycle
- Advanced IDL structures
- Complex transaction building
- Account management patterns
- Error handling strategies
- Performance considerations
Run with:
π Advanced Examples #
complete_example.dart
Full workflow demonstration - Complete end-to-end example showing advanced Anchor workflows.
- Sample counter program interaction
- Transaction building and execution
- Account data fetching and parsing
- Event handling and subscription
- Error handling patterns
- Multiple transaction patterns
Run with: dart complete_example.dart
event_system_example.dart
Event handling patterns - Demonstrates the Anchor event system implementation.
- Event listening and subscription
- Event filtering by type and criteria
- Event callback handling
- Transaction log parsing
- Real-time event monitoring
Run with: dart event_system_example.dart
π― TypeScript Anchor Parity #
These examples are designed to provide equivalent functionality to the TypeScript @coral-xyz/anchor
package:
TypeScript Pattern | Dart Equivalent | Example File |
---|---|---|
anchor.workspace.Counter |
Program(idl, provider) |
counter_basic.dart |
program.methods.initialize() |
program.methods.initialize() |
complete_example.dart |
program.account.counter.fetch() |
program.account.counter.fetch() |
program_interaction.dart |
Event listeners | Event system implementation | event_system_example.dart |
π Prerequisites #
Before running these examples, ensure you have:
-
Dart SDK (>= 3.0.0)
-
coral_xyz package added to your project
-
Network connectivity for RPC calls (examples use devnet)
Common Usage Patterns #
Basic Setup Pattern #
π¦ Adding to Your Project #
Add the Coral XYZ Anchor package to your pubspec.yaml
:
dependencies:
coral_xyz_anchor: ^1.0.0
π§ Running Examples #
# Run any example
dart example/basic_usage.dart
dart example/counter_basic.dart
dart example/program_interaction.dart
# Or with specific Dart SDK
/path/to/dart example/basic_usage.dart
π¦ Example Status #
All examples are designed to run without requiring a local Solana validator:
- β Compile and run successfully
- β Demonstrate core concepts
- β Show error handling patterns
- β Include TypeScript equivalents
- β Mock network calls gracefully
π€ Contributing #
When adding new examples:
- Keep them focused - Each example should demonstrate specific concepts
- Include extensive comments - Explain every step for beginners
- Add TypeScript equivalents - Show how it relates to TS Anchor
- Handle errors gracefully - Don't assume network connectivity
- Stay under 200 lines - Keep examples concise and readable
π Additional Resources #
- Anchor Framework Documentation
- Solana Developer Documentation
- TypeScript Anchor Package
- Coral XYZ Anchor Dart API Reference
Happy coding with Anchor and Dart! οΏ½