SPL Token v0.1.x → v0.2+ Codemod
Automates migration from @solana/spl-token v0.1.x (Token class-based API) to v0.2+ (standalone functions API).
Overview
This codemod transforms the old class-based Token API to the new functional API introduced in @solana/spl-token v0.2+. The migration includes:
- Removing
Tokenclass instantiations - Converting static methods to standalone functions
- Transforming instance methods to standalone function calls
- Updating imports to remove
Tokenand add specific function imports - Removing empty signer arrays (
[]) that are no longer needed
Migration Coverage
Token Class Instantiation
Before:
typescript
After:
typescript
Static Methods
Token.createMint()
Before:
typescript
After:
typescript
Token.getAssociatedTokenAddress()
Before:
typescript
After:
typescript
Instance Methods
token.createAccount()
Before:
typescript
After:
typescript
token.transfer()
Before:
typescript
After:
typescript
token.mintTo()
Before:
typescript
After:
typescript
token.burn()
Before:
typescript
After:
typescript
token.approve()
Before:
typescript
After:
typescript
token.revoke()
Before:
typescript
After:
typescript
token.getAccountInfo()
Before:
typescript
After:
typescript
token.getMintInfo()
Before:
typescript
After:
typescript
token.createAssociatedTokenAccount()
Before:
typescript
After:
typescript
Import Updates
Before:
typescript
After:
typescript
Pipeline Architecture
Step 1: JSSG Transform (scripts/codemod.ts)
Deterministic AST transformations using ast-grep:
- Tracks Token instance variables and their associated mint addresses
- Pattern matches and replaces all Token class methods
- Updates import statements
- Removes Token instantiations
Step 2: AI Fixups (GPT-4o)
Handles complex patterns that require context-aware transformations:
- Edge cases with complex variable scoping
- Nested function calls
- Dynamic property access
- Ensures
connectionandpayervariables are available
Step 3: Prettier Formatting
Ensures consistent code style across all modified files.
Test Fixtures
The codemod includes 6 comprehensive test fixture pairs:
- 01-create-mint - Token.createMint() transformation
- 02-create-account - token.createAccount() transformation
- 03-transfer - token.transfer() transformation
- 04-mint-to - token.mintTo() transformation
- 05-get-account-info - token.getAccountInfo() and token.getMintInfo() transformations
- 06-full-workflow - Complete real-world example with multiple operations
Usage
bash
Key Changes to Note
- Connection and Payer Parameters: All v0.2+ functions require explicit
connectionandpayerparameters - Mint Parameter: Methods that previously used the Token instance now require the mint address as a parameter
- No More Empty Arrays: The
[]parameter (for multi-signers) has been removed from most functions - Simplified ATA:
getAssociatedTokenAddress()now only requiresmintandownerparameters
Author
Ayan Uali ayanuali@gmail.com
License
MIT