Skip to main content
ClaudeWave
Slash Command65 repo starsupdated yesterday

migrate-web3

Migrate from @solana/web3.js to @solana/kit

Install in Claude Code
Copy
mkdir -p ~/.claude/commands && curl -fsSL https://raw.githubusercontent.com/solanabr/solana-ai-kit/HEAD/.claude/commands/migrate-web3.md -o ~/.claude/commands/migrate-web3.md
Then start a new Claude Code session; the slash command loads automatically.

migrate-web3.md

You are migrating a codebase from `@solana/web3.js` to `@solana/kit` (the modern Solana TypeScript SDK). This is a file-by-file migration with verification.

## Related Skills

- [ext/solana-dev/skill/references/kit-web3-interop.md](../skills/ext/solana-dev/skill/references/kit-web3-interop.md) - Kit/web3.js interop patterns and boundary handling
- [ext/solana-dev/skill/references/frontend-framework-kit.md](../skills/ext/solana-dev/skill/references/frontend-framework-kit.md) - Kit-first frontend patterns

## Step 1: Detect web3.js Usage

```bash
echo "Scanning for @solana/web3.js usage..."
echo ""

# Find all files importing web3.js
echo "Files importing @solana/web3.js:"
grep -rn "from.*@solana/web3\.js" --include="*.ts" --include="*.tsx" --include="*.js" --include="*.jsx" . | grep -v node_modules | grep -v ".next"

echo ""
echo "Total files:"
grep -rl "from.*@solana/web3\.js" --include="*.ts" --include="*.tsx" --include="*.js" --include="*.jsx" . | grep -v node_modules | grep -v ".next" | wc -l

echo ""
echo "Import breakdown:"
grep -roh "import.*from.*@solana/web3\.js" --include="*.ts" --include="*.tsx" . | grep -v node_modules | sort | uniq -c | sort -rn
```

## Step 2: Detect Specific APIs in Use

```bash
echo "Detecting specific web3.js APIs in use..."
echo ""

# Connection usage
echo "Connection instances:"
grep -rn "new Connection\|Connection(" --include="*.ts" --include="*.tsx" . | grep -v node_modules | wc -l

# PublicKey usage
echo "PublicKey usage:"
grep -rn "new PublicKey\|PublicKey\." --include="*.ts" --include="*.tsx" . | grep -v node_modules | wc -l

# Transaction usage
echo "Transaction/VersionedTransaction:"
grep -rn "new Transaction\|VersionedTransaction\|TransactionInstruction" --include="*.ts" --include="*.tsx" . | grep -v node_modules | wc -l

# Keypair usage
echo "Keypair usage:"
grep -rn "Keypair\." --include="*.ts" --include="*.tsx" . | grep -v node_modules | wc -l

# sendTransaction
echo "sendTransaction calls:"
grep -rn "sendTransaction\|sendAndConfirmTransaction" --include="*.ts" --include="*.tsx" . | grep -v node_modules | wc -l

# Token program
echo "SPL Token usage:"
grep -rn "@solana/spl-token" --include="*.ts" --include="*.tsx" . | grep -v node_modules | wc -l
```

## Step 3: Key Migration Mappings

Reference these when transforming each file:

### Core Type Mappings

| web3.js | @solana/kit | Notes |
|---------|-------------|-------|
| `Connection` | `createSolanaRpc()` | Functional, not class-based |
| `PublicKey` | `Address` (string type) | Use `address()` to validate |
| `Keypair` | `await generateKeyPair()` | Returns `CryptoKeyPair` |
| `Transaction` | `pipe(createTransactionMessage(...), ...)` | Functional pipeline |
| `VersionedTransaction` | `compileTransaction(msg)` | Compiled from message |
| `TransactionInstruction` | `IInstruction` | Interface, not class |
| `SystemProgram.transfer()` | `getTransferSolInstruction()` | From `@solana/system` |
| `sendAndConfirmTransaction` | `sendAndConfirmTransactionFactory()` | Factory pattern |
| `LAMPORTS_PER_SOL` | `lamports(1_000_000_000n)` | Branded `bigint` type |

### Package Mapping

| Old Package | New Package(s) |
|-------------|----------------|
| `@solana/web3.js` | `@solana/kit` (umbrella) |
| `@solana/spl-token` | `@solana/spl-token` (updated) or Codama-generated |
| `@solana/wallet-adapter-*` | `@solana/wallet-standard` + `@wallet-standard/react` |
| `@coral-xyz/anchor` | `@coral-xyz/anchor` (compatible with both) |

### Common Patterns

```typescript
// OLD: Connection
const connection = new Connection("https://api.mainnet-beta.solana.com");
const balance = await connection.getBalance(publicKey);

// NEW: RPC
import { createSolanaRpc } from "@solana/kit";
const rpc = createSolanaRpc("https://api.mainnet-beta.solana.com");
const balance = await rpc.getBalance(address).send();

// OLD: Transaction
const tx = new Transaction().add(instruction);
const sig = await sendAndConfirmTransaction(connection, tx, [payer]);

// NEW: Transaction message pipeline
import { pipe, createTransactionMessage, setTransactionMessageFeePayer,
    appendTransactionMessageInstruction, signAndSendTransactionMessageWithSigners } from "@solana/kit";
const msg = pipe(
    createTransactionMessage({ version: 0 }),
    m => setTransactionMessageFeePayer(payerAddress, m),
    m => setTransactionMessageLifetimeUsingBlockhash(blockhash, m),
    m => appendTransactionMessageInstruction(instruction, m),
);

// OLD: PublicKey
const pubkey = new PublicKey("So11111111111111111111111111111111111111112");

// NEW: Address
import { address } from "@solana/kit";
const addr = address("So11111111111111111111111111111111111111112");
```

## Step 4: Install New Dependencies

```bash
echo "Installing @solana/kit..."
npm install @solana/kit

# If using specific sub-packages
# npm install @solana/rpc @solana/signers @solana/transactions @solana/addresses

echo ""
echo "Current @solana packages:"
grep "@solana" package.json | grep -v "//"
```

## Step 5: Generate Migration Plan

For each file found in Step 1, create a migration plan:

1. **Read the file** - Identify all web3.js imports and usage
2. **Map imports** - Replace `@solana/web3.js` imports with `@solana/kit` equivalents
3. **Transform types** - `PublicKey` to `Address`, `Keypair` to `CryptoKeyPair`, etc.
4. **Transform patterns** - Class instantiation to functional calls
5. **Update tests** - Ensure test files use new APIs
6. **Verify compilation** - `npx tsc --noEmit` after each file

## Step 6: Migrate File by File

For each file:

```bash
# After migrating a file, verify it compiles
npx tsc --noEmit

if [ $? -ne 0 ]; then
    echo "TypeScript errors after migration. Review and fix before continuing."
fi
```

## Step 7: Verify Migration

```bash
echo "Verifying migration..."
echo ""

# Check for remaining web3.js imports
REMAINING=$(grep -rl "from.*@solana/web3\.js" --include="*.ts" --include="*.tsx" . | grep -v node_modules | grep -v ".next")

if [ -z "$REMAINING" ]; then
    echo "
anchor-engineerSubagent

Anchor framework specialist for rapid Solana program development. Use for building programs with Anchor macros, IDL generation, account validation, and standardized patterns. Prioritizes developer experience while maintaining security.\\n\\nUse when: Building new programs quickly, team projects needing standardization, projects requiring IDL for client generation, or when developer experience is prioritized over maximum CU optimization.

defi-engineerSubagent

DeFi integration specialist for composing with Solana protocols including Jupiter, Drift, Kamino, Raydium, Orca, Meteora, Marginfi, and Sanctum. Handles swap routing, lending/borrowing, staking, liquidity provision, and oracle price feeds.\n\nUse when: Integrating DeFi protocols, building swap interfaces, implementing lending/borrowing, setting up yield strategies, working with Pyth/Switchboard oracles, or composing multi-protocol transactions.

devops-engineerSubagent

CI/CD, infrastructure, and deployment specialist for Solana projects. Handles GitHub Actions, Docker, monitoring, RPC management, and Cloudflare Workers edge deployment.\n\nUse when: Setting up CI/CD pipelines, containerizing Solana validators or programs, configuring monitoring and alerting, managing RPC infrastructure, deploying edge workers, or automating build and deploy workflows.

game-architectSubagent

Senior Solana game architect for game system design, Unity/C# architecture, on-chain game state, player progression, NFT integration, and PlaySolana ecosystem. Use for high-level game design decisions, architecture reviews, and planning complex game systems.\n\nUse when: Designing new Solana games from scratch, planning game state on-chain, Unity project architecture, integrating with PlaySolana/PSG1, or deciding between implementation approaches.

mobile-engineerSubagent

React Native and Expo specialist for building Solana mobile dApps. Handles mobile wallet adapter integration, transaction signing UX, deep linking, and mobile-specific performance optimization.\n\nUse when: Building React Native or Expo mobile apps with Solana integration, implementing mobile wallet adapter flows, setting up deep links for transaction signing, or optimizing mobile dApp performance.

pinocchio-engineerSubagent

CU optimization specialist using Pinocchio framework. Use for performance-critical programs requiring 80-95% CU reduction vs Anchor. Specializes in zero-copy access, manual validation, and minimal binary size.\\n\\nUse when: CU limits are being hit, transaction costs are significant at scale, binary size must be minimized, or maximum throughput is required.

rust-backend-engineerSubagent

Rust backend specialist for building async services that interact with Solana blockchain. Builds APIs, indexing services, and off-chain processing using Axum, Tokio, and modern async patterns.\n\nUse when: Building REST/WebSocket APIs for Solana dApps, implementing transaction indexers, creating webhook services, or any Rust backend that interacts with Solana.

solana-architectSubagent

Senior Solana program architect for system design, account structures, PDA schemes, token economics, and cross-program composability. Use for high-level design decisions, architecture reviews, and planning complex multi-program systems.\n\nUse when: Designing new programs from scratch, planning account structures, optimizing PDA schemes, reviewing architecture for security, or deciding between implementation approaches.