$npx -y skills add agiprolabs/claude-trading-skills --skill solana-tx-buildingSolana transaction construction including instruction building, account resolution, compute budget, priority fees, and versioned transactions
| 1 | # Solana Transaction Building |
| 2 | |
| 3 | This skill covers how to construct, simulate, and inspect Solana transactions programmatically. It addresses the full anatomy of a Solana transaction — from raw instruction encoding to versioned transaction formats, compute budget management, priority fees, and address lookup tables. |
| 4 | |
| 5 | **Safety**: This skill is for transaction *construction* and *analysis* only. Scripts in this skill NEVER sign or submit real transactions. Always simulate before sending. Never auto-sign. |
| 6 | |
| 7 | ## Transaction Anatomy |
| 8 | |
| 9 | A Solana transaction consists of: |
| 10 | |
| 11 | 1. **Signatures**: One or more Ed25519 signatures (64 bytes each) |
| 12 | 2. **Message**: The serializable payload containing: |
| 13 | - **Header**: Counts of required signers, read-only signers, read-only non-signers |
| 14 | - **Account keys**: Array of all pubkeys referenced by instructions |
| 15 | - **Recent blockhash**: 32-byte hash for replay protection (expires ~60-90 seconds) |
| 16 | - **Instructions**: Array of program calls |
| 17 | |
| 18 | ### Transaction Size Limit |
| 19 | |
| 20 | The hard limit is **1232 bytes** for the entire serialized transaction. This constrains how many instructions and accounts you can include. Strategies to stay within the limit: |
| 21 | |
| 22 | - Use versioned transactions with Address Lookup Tables (ALTs) |
| 23 | - Minimize the number of accounts per instruction |
| 24 | - Combine related operations into single instructions where supported |
| 25 | - Split complex operations across multiple transactions |
| 26 | |
| 27 | ### Instruction Format |
| 28 | |
| 29 | Each instruction contains three fields: |
| 30 | |
| 31 | ``` |
| 32 | Instruction { |
| 33 | program_id_index: u8, // Index into the account keys array |
| 34 | accounts: [u8], // Indices into account keys array |
| 35 | data: [u8], // Opaque byte array interpreted by the program |
| 36 | } |
| 37 | ``` |
| 38 | |
| 39 | ### Account Meta |
| 40 | |
| 41 | Every account referenced in an instruction has metadata: |
| 42 | |
| 43 | ``` |
| 44 | AccountMeta { |
| 45 | pubkey: Pubkey, // 32-byte public key |
| 46 | is_signer: bool, // Must sign the transaction |
| 47 | is_writable: bool, // Will be written to by this instruction |
| 48 | } |
| 49 | ``` |
| 50 | |
| 51 | The four combinations determine the account's role: |
| 52 | |
| 53 | | is_signer | is_writable | Role | |
| 54 | |-----------|-------------|------| |
| 55 | | true | true | Fee payer, token owner performing transfer | |
| 56 | | true | false | Multisig co-signer, read-only authority | |
| 57 | | false | true | Destination account, PDA being written | |
| 58 | | false | false | Program ID, sysvar, clock | |
| 59 | |
| 60 | ## Legacy vs Versioned Transactions |
| 61 | |
| 62 | ### Legacy Transactions |
| 63 | |
| 64 | The original format. All accounts must be listed in the account keys array. With the 1232-byte limit, you can fit roughly 20-35 accounts depending on instruction data size. |
| 65 | |
| 66 | ### Versioned Transactions (v0) |
| 67 | |
| 68 | Introduced to support **Address Lookup Tables (ALTs)**. A v0 transaction includes: |
| 69 | |
| 70 | - A version prefix byte (`0x80` for v0) |
| 71 | - The same message structure as legacy |
| 72 | - An additional `address_table_lookups` array |
| 73 | |
| 74 | ALTs let you reference accounts by a compact index into an on-chain table rather than including the full 32-byte pubkey. This dramatically increases the number of accounts a transaction can reference. |
| 75 | |
| 76 | ``` |
| 77 | AddressTableLookup { |
| 78 | account_key: Pubkey, // The ALT account address |
| 79 | writable_indexes: [u8], // Indices for writable accounts |
| 80 | readonly_indexes: [u8], // Indices for read-only accounts |
| 81 | } |
| 82 | ``` |
| 83 | |
| 84 | **When to use v0**: Any transaction referencing more than ~20 accounts, Jupiter swaps with multi-hop routes, complex DeFi interactions. |
| 85 | |
| 86 | ## Compute Budget |
| 87 | |
| 88 | Every transaction has a compute budget that determines how many compute units (CUs) it can consume and what priority fee to pay. |
| 89 | |
| 90 | ### Compute Budget Instructions |
| 91 | |
| 92 | Two key instructions from the Compute Budget Program (`ComputeBudget111111111111111111111111111111`): |
| 93 | |
| 94 | **1. Set Compute Unit Limit** |
| 95 | ``` |
| 96 | Instruction data: [0x02, <units as u32 LE>] |
| 97 | ``` |
| 98 | Sets the maximum CUs this transaction can consume. Default is 200,000 per instruction (max 1,400,000 per transaction). Setting this lower than needed causes the transaction to fail. Setting it higher wastes budget but does not cost more (you only pay for requested, not consumed). |
| 99 | |
| 100 | **2. Set Compute Unit Price** |
| 101 | ``` |
| 102 | Instruction data: [0x03, <micro_lamports as u64 LE>] |
| 103 | ``` |
| 104 | Sets the price per CU in micro-lamports. This is the priority fee mechanism. The total priority fee is: |
| 105 | |
| 106 | ``` |
| 107 | priority_fee = compute_unit_limit * compute_unit_price / 1_000_000 |
| 108 | ``` |
| 109 | |
| 110 | ### Priority Fee Estimation |
| 111 | |
| 112 | To estimate an appropriate priority fee: |
| 113 | |
| 114 | 1. Call `getRecentPrioritizationFees` RPC method with the accounts your transaction touches |
| 115 | 2. Look at the median or 75th percentile fee from recent slots |
| 116 | 3. During congestion, fees spike — monitor and adjust dynamically |
| 117 | |
| 118 | ```python |
| 119 | import httpx |
| 120 | |
| 121 | def get_priority_fees(rpc_url: str, accounts: list[str]) -> list[dict]: |
| 122 | """Fetch recent prioritization fees for given accounts.""" |
| 123 | resp = httpx.post(rpc_url |