PDA Derivation - Automatic Program Derived Address Calculation
Overview
Section titled “Overview”Program Derived Addresses (PDAs) are a fundamental concept in Solana programming, but calculating them manually during testing is tedious and error-prone. Testship automatically derives PDAs for you in real-time, handling simple seeds, complex account-based patterns, and even nested derivations that depend on on-chain data.
What are PDAs?
Section titled “What are PDAs?”PDAs are deterministic addresses derived from:
- A program ID
- One or more seeds (strings, public keys, or other data)
- A bump seed (to ensure the address is off the ed25519 curve)
Automatic Derivation
Section titled “Automatic Derivation”Simple PDAs (const + arg/account seeds)
Section titled “Simple PDAs (const + arg/account seeds)”For basic PDA patterns like:
#[account( seeds = [b"user", authority.key().as_ref()], bump)]Testship automatically:
- ✅ Identifies seeds from your program’s IDL
- ✅ Creates input fields only for variable seeds
- ✅ Auto-fills constant seeds (e.g.,
b"user") - ✅ Calculates PDA when all dependencies are met
- ✅ Shows live derivation status with loading indicator
Complex PDAs (account-based seeds)
Section titled “Complex PDAs (account-based seeds)”Testship’s real power is handling advanced patterns:
Multiple Seeds with Mixed Types
seeds = [b"vault", token_mint.key().as_ref(), owner.key().as_ref()]✅ Auto-derives when token_mint and owner are provided
Numeric Argument Seeds
seeds = [b"order", order_id.to_le_bytes().as_ref()]✅ Converts numbers to proper byte arrays (u8, u16, u32, u64, i8, i16, i32, i64)
Account Field Seeds (The Hard Part)
// Uses a field FROM another account as a seed#[account( seeds = [b"escrow", vault.authority.as_ref()], bump)]✅ Fetches the vault account from on-chain
✅ Decodes it using the IDL
✅ Extracts the authority field
✅ Uses it as a seed
This is where Testship truly shines - no other tool handles this automatically!
Features
Section titled “Features”Live Derivation Status
Section titled “Live Derivation Status”Watch PDAs being derived in real-time with clear visual feedback:
- 🟡 Idle - Waiting for dependencies (shows what’s missing)
- 🟠 Deriving - Currently calculating (with spinner animation)
- 🟢 Ready - Successfully derived (address auto-filled)
- 🔴 Error - Derivation failed (with detailed error message)
Visual Badges & Indicators
Section titled “Visual Badges & Indicators”PDA accounts are clearly marked:
- 🔑 PDA Badge - Yellow badge on PDA accounts
- ✅ Auto-PDA Badge - Green badge when successfully derived
- ⚠️ Loading Badge - Orange badge while deriving
- ❌ Error Badge - Red badge if derivation fails
Dependency Tracking
Section titled “Dependency Tracking”Testship shows exactly what’s needed:
Waiting for dependencies: • argument: order_id • account: vaultFill in the dependencies, and derivation happens automatically!
Smart Re-derivation
Section titled “Smart Re-derivation”- Auto-clears when dependencies change
- Refresh button to manually re-derive
- Preserves manual overrides if needed
- Debounced to avoid excessive calculations (500ms delay)
Field Extraction from Accounts
Section titled “Field Extraction from Accounts”For complex account-based seeds, Testship:
- Detects the account field pattern in IDL
- Fetches the account from the blockchain
- Decodes using BorshAccountsCoder
- Extracts the specific field (handles camelCase/snake_case)
- Converts to proper seed buffer
- Uses in PDA derivation
Canonical Bump Discovery
Section titled “Canonical Bump Discovery”Testship automatically:
- Uses
findProgramAddressSyncfor canonical bump - No need to specify bump manually
- Ensures PDAs are valid and secure
Example Usage
Section titled “Example Usage”Example 1: Simple User PDA
Section titled “Example 1: Simple User PDA”#[account( seeds = [b"user", authority.key().as_ref()], bump)]pub user: Account<'info, User>,What Testship does:
- Sees you connected your wallet (authority auto-filled)
- Derives
[b"user", wallet_pubkey] - Shows green “Auto-PDA” badge
- Address appears instantly ✨
Example 2: Complex Escrow PDA
Section titled “Example 2: Complex Escrow PDA”#[account( seeds = [b"escrow", vault.authority.as_ref(), vault.nonce.to_le_bytes().as_ref()], bump)]pub escrow: Account<'info, Escrow>,What Testship does:
- You fill in the vault address
- Testship fetches vault account from blockchain 🌐
- Decodes it using your IDL 📖
- Extracts
vault.authorityandvault.nonce🔍 - Converts nonce to bytes
- Derives the PDA
- Shows green “Auto-PDA” badge ✅
This complex derivation happens automatically in ~1 second!
Example 3: Multi-Step Nested PDAs
Section titled “Example 3: Multi-Step Nested PDAs”For instructions with dependent PDAs:
- Fill in initial account (e.g., mint address)
- First PDA derives automatically (e.g., vault)
- Second PDA uses first PDA as seed (e.g., vault_authority)
- All addresses populate in sequence
- Ready to execute! 🚀
Benefits
Section titled “Benefits”- ✅ Zero Manual Calculation - Never compute PDAs by hand again
- ✅ Error Prevention - No more typos or wrong seed order
- ✅ Lightning Fast - Even complex derivations in <1 second
- ✅ Visual Feedback - Always know what’s happening
- ✅ Learning Tool - Understand PDA patterns in your program
- ✅ Battle Tested - Handles the most complex Solana programs
Supported Seed Types
Section titled “Supported Seed Types”| Seed Kind | Description | Example |
|---|---|---|
const | Static bytes | b"user" |
arg | Instruction arguments | order_id |
account | Simple account pubkey | authority.key() |
account.field | Field from account | vault.authority |
Under the Hood
Section titled “Under the Hood”Testship uses:
@solana/web3.jsfor PDA derivation@coral-xyz/anchorfor IDL parsingBorshAccountsCoderfor account decoding- React hooks for reactive updates
- 500ms debouncing to avoid excessive calls
Limitations
Section titled “Limitations”Current limitations (to be improved):
- ⚠️ String seeds must be UTF-8 encoded
- ⚠️ Nested struct fields (e.g.,
account.data.field) not yet supported - ⚠️ Array/Vec seeds require manual specification
Most Anchor programs use simpler patterns that work perfectly!
Next Steps
Section titled “Next Steps”Learn how Testship remembers your frequently used accounts in Account Suggestions.