Overview
Theworkspace object provides a convenient way to automatically load and access Anchor programs defined in your workspace. It searches for compiled IDLs in the target/idl directory and creates Program instances on demand.
Usage
How It Works
The workspace object uses a JavaScriptProxy to dynamically load programs when accessed:
- IDL Discovery: Searches the
target/idldirectory for matching IDL files - Name Matching: Converts the property name to camelCase and matches against IDL filenames
- Program Creation: Automatically creates a
Programinstance with the loaded IDL - Caching: Caches loaded programs for subsequent access
Workspace Type
Program instances.
Program Name Resolution
The workspace converts program names to camelCase for flexible access:Configuration
Anchor.toml Override
You can override workspace program locations inAnchor.toml:
Default Behavior
Without configuration overrides, the workspace:- Reads IDL files from
target/idl/ - Matches filenames against the requested program name (after converting both to camelCase)
- Uses the program address specified in the IDL’s
addressfield
Properties
Program
Access a program by name. The name is case-insensitive and can use any format.
Error Handling
Browser Environment
Attempting to use workspace in a browser throws an error:IDL Not Found
If no matching IDL file is found:IDL File Missing
If the IDL path doesn’t exist:Usage Examples
Basic Test Setup
Multiple Programs
Type-Safe Access
Testing with Multiple Clusters
Custom IDL Path
For programs with custom IDL locations specified inAnchor.toml:
Accessing Program Properties
Integration with Anchor CLI
The workspace object is designed to work seamlessly with the Anchor CLI workflow:Best Practices
Always Type Your Programs
Always Type Your Programs
Use the generated TypeScript types for type safety:
Run anchor build First
Run anchor build First
Ensure your programs are built before accessing the workspace:
Use in Tests, Not Production
Use in Tests, Not Production
The workspace is ideal for testing but shouldn’t be used in production clients:
Handle Missing Programs Gracefully
Handle Missing Programs Gracefully
Check if a program exists before using it: