Aayanuali

spl-token-v1-to-v2

Migrates @solana/spl-token from v0.1.x (Token class) to v0.2+ (standalone functions)

solanaspl-tokentokenmigrationcodemod
Public
0 executions

Run locally

npx codemod spl-token-v1-to-v2

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 Token class instantiations
  • Converting static methods to standalone functions
  • Transforming instance methods to standalone function calls
  • Updating imports to remove Token and 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 connection and payer variables are available

Step 3: Prettier Formatting

Ensures consistent code style across all modified files.

Test Fixtures

The codemod includes 6 comprehensive test fixture pairs:

  1. 01-create-mint - Token.createMint() transformation
  2. 02-create-account - token.createAccount() transformation
  3. 03-transfer - token.transfer() transformation
  4. 04-mint-to - token.mintTo() transformation
  5. 05-get-account-info - token.getAccountInfo() and token.getMintInfo() transformations
  6. 06-full-workflow - Complete real-world example with multiple operations

Usage

bash

Key Changes to Note

  1. Connection and Payer Parameters: All v0.2+ functions require explicit connection and payer parameters
  2. Mint Parameter: Methods that previously used the Token instance now require the mint address as a parameter
  3. No More Empty Arrays: The [] parameter (for multi-signers) has been removed from most functions
  4. Simplified ATA: getAssociatedTokenAddress() now only requires mint and owner parameters

Author

Ayan Uali ayanuali@gmail.com

License

MIT

Ready to contribute?

Build your own codemod and share it with the community.