On-chain program instruction builders
The program packages give Go callers a typed way to describe Solana instructions and the accounts that those instructions use. They turn program actions into transaction-ready instructions while also providing account state models needed to read results from chain data. Token-2022 confidential clients also turn encrypted account state into proof data and ordered instruction sequences.
System model
An instruction has two related parts: typed data for the program action and an ordered set of account metadata. A builder keeps these parts together until it produces an instruction that can be added to a transaction. The instruction then exposes its target program ID, account list, and encoded data through the common Solana instruction interface.
The builder model supports both construction and inspection. A caller can build an instruction for a new transaction, or the SDK can decode an existing instruction by using its program ID and restoring the account roles from the transaction account list.
The main instruction path moves from a program-specific intent to a serialized transaction instruction.
flowchart LR
A[Program action] --> B[Typed builder]
B --> C[Parameters and ordered account roles]
C --> D[Built instruction]
D --> E[Transaction]
E --> F[Encoded wire data]
F --> G[Program execution or SDK decoding]
Instruction builder model
The builders provide a shared shape across programs while preserving each program's account rules and data types. This lets an engineer move between program packages without treating every instruction as an untyped byte array.
Builder lifecycle
Each instruction family has a builder constructor and typed setters for its data and accounts. A convenience constructor fills the same values in one call. Build creates the instruction, while ValidateAndBuild checks required values and account slots before it does so; building does not submit the instruction to the network. The resulting instruction is then composed with other instructions in a transaction.
Account setters also express the role that each account has in the instruction. A role can require write access, a signature, or both. The order of these accounts is part of the program contract, so a builder is safer than assembling the list by hand. Built variants use the same concrete pointer shape as decoded variants, which lets callers inspect either source in the same way.
References: programs/…/CreateAccount.go• L28-107 programs/…/CreateAccount.go• L207-222 programs/…/DelegateStake.go• L26-95 programs/…/Transfer.go• L1-100 programs/…/impl_pointer_consistency_test.go• L25-66
Account layouts
An account layout is the typed view of bytes stored in a Solana account. Instruction account lists identify which accounts a program may use, while account layouts describe the state that those accounts hold after the program processes an instruction. These are separate contracts and must not be confused.
The supported core layouts are:
| Program area | Main account state | System meaning |
|---|---|---|
| System | Nonce account | Stores nonce state, its authority, the current nonce, and the fee value used by the nonce flow. |
| Stake | StakeStateV2 | Uses a state discriminator for uninitialized, initialized, delegated stake, and rewards-pool states. Delegated state includes authorities, lockup data, delegation, and observed credits. |
| Vote | Versioned vote state | Stores validator identity, voting and withdrawal authorities, commission data, vote history, epoch credits, and timestamps. Newer versions add newer validator and reward fields. |
| SPL Token | Mint, token account, and multisig | Represents supply and decimals, token ownership and amount, optional authorities, account state, and signer thresholds. |
| Token-2022 | Token state plus extensions | Keeps the base token model and adds extension types for features such as fees, hooks, metadata pointers, and account controls. Confidential extensions add encrypted balances, supply, and fee state. |
| Address lookup table | Table metadata and addresses | Stores authority and life-cycle fields together with the addresses that a versioned message can resolve. |
These layouts are useful when an engineer must connect an instruction to the state it changes. They also provide the boundary for decoding RPC account data into a form that application code can inspect. Confidential Token-2022 account-info helpers add the state needed to prepare balance updates and proofs: pending and available balances for transfers, and confidential supply for mint and burn actions.
References: programs/…/accounts.go• L24-92 programs/…/state.go• L12-24 programs/…/state.go• L97-214 programs/…/state.go• L299-309 programs/…/state.go• L25-33 programs/…/state.go• L225-277 programs/…/state.go• L634-644 programs/…/accounts.go• L24-40 programs/…/accounts.go• L166-193 programs/…/types.go• L80-113 programs/…/confidential_transfer_account_info.go• L14-30 programs/…/confidential_transfer_account_info.go• L95-169 programs/…/confidential_mint_burn_account_info.go• L14-29 programs/…/address-lookup.go• L60-67
Program families
The packages group builders by the on-chain program that owns the instruction. The core packages cover account and stake life cycles, token movement and authority, and the supporting programs used to fit and operate transactions.
Core program actions
The System Program builders cover account creation, allocation, assignment, lamport transfers, and durable nonce actions. Stake builders cover stake initialization, authority changes, delegation, movement, activation, withdrawal, and lockup management. Vote builders cover vote account setup, voting, authorization, commission, and vote-state updates.
These actions often form a cross-program flow. For example, creating a program-owned account can require a System Program instruction before another program initializes the account. Delegating stake then links a stake account to a validator vote account and the required sysvar accounts. The child page System and stake programs describes those account roles and flows.
References: programs/…/instructions.go• L45-85 programs/…/instructions.go• L43-80 programs/…/DelegateStake.go• L26-45 programs/…/instructions.go• L54-77 program_ids.go• L17-29
Token program actions
The SPL Token package models the common token life cycle: mint and account setup, transfers, delegated authority, minting, burning, freezing, thawing, and account closure. Checked variants let the caller include mint and decimal checks in the instruction data. Associated-token-account builders create the conventional token account for a wallet and mint, with an idempotent form for safe repeats.
It also exposes token operations that support account sizing, amount display, and native SOL handling. These operations return values through Solana return data instead of writing a result account, while immutable-owner setup provides compatibility with associated token account flows.
GetAccountDataSizeasks the program for the required account size for a mint.AmountToUiAmountconverts a raw amount to a UTF-8 display string.UiAmountToAmountconverts a display amount to a raw integer amount.WithdrawExcessLamportsmoves lamports above the rent minimum from a token, mint, or multisig account.UnwrapLamportsmoves all or part of the lamports held by a native SOL token account to a destination.Batchgroups token instructions for one p-token call. It does not support nested batches.
The p-token-only actions use the normal Token Program address under the current replacement model. They are not a separate program family with a separate program ID. The child page Token programs explains how to choose between the original token program, Token-2022, and the associated account APIs.
References: programs/…/instructions.go• L177-203 programs/…/instructions.go• L251-264 programs/…/AmountToUiAmount.go• L27-32 programs/…/AmountToUiAmount.go• L126-137 programs/…/GetAccountDataSize.go• L27-30 programs/…/GetAccountDataSize.go• L101-113 programs/…/InitializeImmutableOwner.go• L26-32 programs/…/WithdrawExcessLamports.go• L27-34 programs/…/UnwrapLamports.go• L27-37 programs/…/Batch.go• L27-37 programs/…/Batch.go• L85-101 programs/…/Batch_test.go• L68-99 programs/…/Batch_test.go• L180-229
Confidential Token-2022 actions
The confidential Token-2022 builders describe encrypted token movement and supply changes without making the protected amounts plain transaction data. Client helpers use current account state and encryption keys to prepare updated decryptable balances, encrypted auditor values, and the proofs required by the Token-2022 program.
The builders cover three related extension areas:
- Confidential transfers configure accounts, deposit or withdraw tokens, transfer balances, apply pending balances, empty accounts, and control credit modes.
- Confidential transfer fees initialize fee state, harvest withheld fees, withdraw fees, and control whether fees can move to the mint.
- Confidential mint and burn actions initialize protected supply, rotate supply protection, update the decryptable supply cache, mint or burn from a confidential balance, and apply pending burns.
Token-2022 also exposes ordinary transfer-fee actions. They configure fees, transfer with a checked fee, harvest withheld fees, and withdraw fees from a mint or selected accounts. When selected accounts are used with a multisig authority, the signer accounts stay between the authority and the source accounts because that order is part of the program contract.
Account-info helpers connect stored encrypted state to these actions. Transfer and withdrawal helpers check the available balance and produce the new encrypted balance. Pending-balance helpers combine the encrypted pending and available values. Supply helpers account for applied burns before they generate mint or rotation proofs.
Proof location means the place from which an instruction gets a zero-knowledge proof. A proof can be in a sibling instruction or in a context-state account. The client helpers use one location for the complete operation: when proofs are inlined they return the verification instructions in program-read order; when proofs use context accounts they pass those accounts and return only the Token-2022 instruction. The Token-2022 builder adds the matching proof account or instruction-sysvar account.
This diagram shows how a confidential action joins encrypted account state, proof data, and the final instruction sequence.
flowchart LR
A[Confidential token intent] --> B[Typed Token-2022 client]
C[Encrypted account state and keys] --> B
B --> D[Updated encrypted balance or supply]
B --> E[Proof data]
E --> F{Proof location}
F -->|Inline| G[Sibling proof verification instructions]
F -->|Context state| H[Proof context accounts]
D --> I[Confidential Token-2022 instruction]
G --> I
H --> I
I --> J[Transaction]
The builders keep single-authority and multisig use consistent. With multisig, the authority account is not the signer and the member accounts follow the account layout as signers. Fee withdrawal from many source accounts has one extra rule: source accounts trail the multisig signer accounts. These account positions are part of the on-chain contract.
Confidential client tests cover both proof locations and verify the resulting extension data, proof order, account presence, encrypted balance changes, and proof validity. An operation cannot mix context-state proofs with inlined proofs. Proof generation also rejects insufficient funds, balance overflow, wrong decryption keys, and missing proof data.
References: programs/…/confidential_common.go• L22-64 programs/…/confidential_common.go• L101-117 programs/…/confidential_transfer_account_info.go• L14-90 programs/…/confidential_transfer_account_info.go• L95-169 programs/…/confidential_mint_burn_account_info.go• L14-110 programs/…/confidential_mint_burn_client.go• L12-111 programs/…/confidential_mint_burn_client.go• L114-182 programs/…/confidential_transfer_fee_client.go• L10-127 programs/…/TransferFeeExtension.go• L24-84 programs/…/TransferFeeExtension.go• L387-415 programs/…/extension_test.go• L163-270 programs/…/confidential_client_common_test.go• L60-173 programs/…/confidential_transfer_client_test.go• L105-135 programs/…/confidential_mint_burn_client_test.go• L69-79 programs/…/mint.go• L10-60
Lookup and auxiliary actions
Address lookup table builders create, extend, freeze, deactivate, and close tables. A lookup table reduces the static account-key burden of a versioned message by allowing the message to refer to stored addresses. The SDK can fetch the referenced tables, decode their state, and resolve the message lookups as one flow.
The extension action accepts new addresses together with the table and its authority. A payer and the System Program are optional accounts used when the extension needs more rent. Its decoder rejects a declared address count that cannot fit in the remaining instruction data before it allocates storage. The child page Lookup table and auxiliary programs covers these supporting APIs.
Auxiliary builders control transaction conditions or attach supporting actions. Compute Budget instructions set limits, prices, heap size, or loaded account data limits. Memo instructions record UTF-8 text with optional signer accounts. The BPF loader flow deploys program data through an initial setup, chunked writes, and a finalization step.
References: programs/…/instructions.go• L28-43 programs/…/ExtendLookupTable.go• L14-30 programs/…/ExtendLookupTable.go• L87-119 programs/…/ExtendLookupTable.go• L162-178 programs/…/extend_count_test.go• L11-21 programs/…/resolve.go• L26-47 programs/…/resolve.go• L48-119 programs/…/instruction.go• L43-63 programs/…/Create.go• L28-103 programs/…/loader.go• L100-225
Zero-knowledge proof actions
The zero-knowledge proof program builders close proof context state and verify ElGamal-related proofs. Verification can carry proof bytes in the instruction or read them from a proof account at a supplied offset. An optional context state account lets the program keep proof-related state across instructions. The child page Zero-knowledge proof program describes these proof types and account roles.
The proof instruction data identifies the proof action first. The builder checks that the supplied proof matches that action before it creates the instruction. This keeps proof data and proof type aligned while allowing both direct and account-backed verification flows.
References: programs/…/instruction.go• L12-29 programs/…/instruction.go• L51-117
Program identity
Every instruction must name the program that will receive it. The SDK supplies standard Solana and SPL program IDs as defaults, and each instruction package uses its default when a caller does not select another identity.
Default program identities
The shared program ID definitions include the System, Config, Stake, Vote, BPF loader, Compute Budget, Address Lookup Table, SPL Token, Token-2022, associated token account, Memo, token swap, token lending, and metadata program IDs. These constants make the intended on-chain target explicit when a transaction combines instructions from several packages.
The package-level ProgramID value is the default for packages such as System, Stake, Token, Token-2022, Vote, Address Lookup Table, Compute Budget, Memo, and the associated token account package. A package-level SetProgramID can change that default and register decoding for the new ID. This setting is shared by later instructions from that package, so it is best treated as process-wide configuration. The p-token batch and lamport actions continue to use the Token package's normal program identity rather than a second p-token address.
References: program_ids.go• L17-45 program_ids.go• L47-85 programs/…/instructions.go• L32-42 programs/…/instructions.go• L30-40 programs/…/instructions.go• L15-25 programs/…/instructions.go• L34-46 programs/…/Batch.go• L33-37
Per-instruction overrides
Token and Token-2022 instructions also support an instruction-level program ID override. A caller can build an instruction and set its target on the built value, which allows original SPL Token and Token-2022 instructions to coexist without changing shared package state. This is the preferred choice when concurrent work may build instructions for both programs or for a compatible custom deployment.
The override changes only the target program ID. It does not change the instruction data, account order, or account roles. The caller must still choose account layouts and instruction variants that the selected program understands.
References: programs/…/instructions.go• L270-298 programs/…/instructions.go• L351-356 programs/…/instructions.go• L304-322 programs/…/instructions.go• L479-484 programs/…/program_id_override_test.go• L1-120 programs/…/program_id_override_test.go• L1-120
Instruction decoding
The same typed definitions support the reverse path from a transaction back to a program action. A decoder uses the registered program ID to select the package, reads the instruction variant from the data, and attaches the supplied transaction accounts to that variant. This makes decoded instructions useful for transaction inspection and audit work without losing account roles.
Variant and account recovery
Each program maps its wire-level instruction identifier to a named variant. The variant carries the program-specific data, while the account metadata remains in the transaction's account list. When the SDK decodes an instruction, it restores the account list into the typed instruction where the variant supports account assignment. Invalid data or an incomplete account list produces an error instead of a partly usable instruction.
The wire identifier format is program-specific. System, Stake, Vote, and Address Lookup Table use a 32-bit variant identifier, while Token, Token-2022, associated token account, and Compute Budget use an 8-bit identifier. Token decoding also maps the non-contiguous p-token IDs for excess-lamport withdrawal, lamport unwrapping, and batching.
The zero-knowledge proof program uses a one-byte action identifier. Its decoder accepts either the exact length for an inlined proof or a four-byte little-endian offset for a proof account, and it checks the allowed account count for each form.
References: programs/…/instructions.go• L133-203 programs/…/instructions.go• L94-169 programs/…/instructions.go• L308-398 programs/…/instructions.go• L417-453 programs/…/Batch.go• L183-215 programs/…/instructions.go• L332-533 programs/…/instructions.go• L75-137 programs/…/instructions.go• L51-107 programs/…/instruction.go• L120-163
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the instruction variant ID, variant name, and decoder registration aligned when adding or changing a program action.
- Keep account setter order and signer or writable flags aligned with the on-chain program contract. A changed order can produce a valid-looking but unusable transaction.
- Keep the base token account model and Token-2022 extension sizing or state rules aligned when adding a token feature.
- Keep each Token-2022 confidential sub-action ID, typed payload, proof location, and account layout aligned with the on-chain interface.
- Keep confidential client proof order aligned with the proof offsets and context-account order expected by Token-2022. Do not introduce mixed inline and context proof locations for one operation.
- Keep account-info balance and supply calculations aligned with the encrypted values carried by the client instruction. Preserve insufficient-funds and overflow checks.
- Keep confidential proof generators and their tests aligned with the Go builders. A missing proof or changed ciphertext can alter encoded data without changing the outer instruction shape.
- Keep a package default program ID separate from instruction-level overrides so mixed Token and Token-2022 transactions remain safe for concurrent callers.
- Keep p-token action IDs, the Token Program target, batch sub-instruction headers, and non-nesting rules aligned when changing p-token support.
- Keep return-data formats aligned with their parsers. Account-size results and raw amount results are little-endian integers, while UI amount results are UTF-8 text.
- Keep account state discriminators and version tags aligned with their decoders. Older System, Stake, Vote, Token, or lookup-table data must remain readable when the on-chain state gains a new variant.
- Keep lookup-table address derivation, fetched table state, and message lookup resolution consistent. A missing or stale table cannot resolve the account keys used by a versioned message.
- Keep lookup-table variable-length payload guards aligned with decoder allocation. A declared address count must fit in the remaining instruction bytes before address storage is created.
- Keep vote-state update count guards aligned with the lockout wire size. A declared lockout count must fit in the remaining instruction bytes before lockout storage is created.
- Keep each zero-knowledge proof action aligned with its proof type, proof encoding form, and allowed account layout when adding or changing proof instructions.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Required builder data or an account slot is missing | Build can produce an instruction that fails during encoding or execution | Use ValidateAndBuild for externally supplied values and set every required account explicitly. |
| Account roles or order do not match the program contract | The program rejects the instruction or checks the wrong signer or writable account | Use the program's typed account setters and review the account layout for the selected variant. |
A shared package ProgramID is changed while other goroutines build instructions | Instructions can target the wrong program and decoder registration can no longer reflect caller intent | Use instruction-level overrides for Token and Token-2022; avoid shared mutation for mixed concurrent work. |
| A Token-2022 extension is used with a base-sized account | Account creation or initialization fails because the account has insufficient space | Determine the required extension set before creating the mint or token account. |
| A confidential proof uses the wrong sibling offset or context account | The confidential action cannot find or verify its proof | Use one proof location for the operation and preserve proof order when building offsets or context accounts. |
| Confidential client inputs use the wrong encrypted state or key | Proof generation fails or the new decryptable balance or supply does not match the instruction | Build account-info values from the matching on-chain state and use the related ElGamal and AE keys. |
| Confidential fee sources are placed before multisig signers | The Token-2022 program reads the account list with the wrong roles | Keep authority, multisig signers, and source accounts in the required order. |
| A confidential fee withdrawal proof is built against changing source fees | The proof becomes invalid before the transaction lands | Prefer harvest-to-mint followed by withdrawal from the mint when source fees can change. |
| A referenced lookup table is missing or its data is truncated | Message account keys remain incomplete and instruction account indexes cannot resolve | Fetch and decode every table, then resolve lookups only after all table entries are present. |
| A lookup-table or vote-state decoder trusts an oversized count | Malformed instruction data can cause excessive allocation or a panic | Check that the declared count fits in the remaining bytes before allocation. |
| Memo data is empty or not valid UTF-8 | The Memo Program rejects the instruction | Validate the message before building the memo instruction. |
| A p-token batch contains nested instructions, empty data, or a sub-instruction above its one-byte limits | The batch is rejected or cannot be encoded without losing its account or data length | Validate the batch and keep each sub-instruction within 255 accounts and 255 data bytes. |
| A decoded batch receives fewer accounts than its headers declare | Sub-instruction account roles cannot be restored | Pass the complete flat account list to decoding and preserve each sub-instruction's account count. |
| Token return data is the wrong type or too short | Size and raw-amount parsing fails, or UI output cannot be used as text | Use the matching parser and check the return-data encoding before consuming the result. |
| Proof data does not match the selected zero-knowledge proof action | The builder rejects the instruction or the proof program cannot verify it | Select the action from the proof type and use the matching inline or proof-account form. |
| A zero-knowledge proof instruction has an invalid data length or account count | The decoder rejects the instruction as malformed | Preserve the action-specific proof length and account layout. |
Key files
| File | Why to read it |
|---|---|
| program_ids.go | Defines the standard program identities used by the builders. |
| programs/…/instructions.go | Shows the common instruction wrapper, program identity, variant registry, and decode path. |
| programs/…/CreateAccount.go | Shows the typed builder flow and the relationship between instruction data and ordered account roles. |
| programs/…/accounts.go | Defines the System Program nonce account view. |
| programs/…/DelegateStake.go | Shows a stake instruction whose account list includes program accounts and sysvars. |
| programs/…/state.go | Defines the stake account state variants and delegation layout. |
| programs/…/instructions.go | Lists the vote actions represented by typed instructions. |
| programs/…/types.go | Defines vote instruction payloads and guarded decoding for vote state updates. |
| programs/…/state.go | Defines versioned vote account state and its decoding boundary. |
| programs/…/instructions.go | Defines SPL Token actions, p-token dispatch, and the per-instruction program ID override. |
| programs/…/Batch.go | Shows grouped token execution, flat account recovery, size limits, and non-nesting rules. |
| programs/…/AmountToUiAmount.go | Shows token amount conversion through program return data. |
| programs/…/GetAccountDataSize.go | Shows account-size queries and their little-endian return-data parser. |
| programs/…/UnwrapLamports.go | Shows native SOL unwrapping and single-authority or multisig account roles. |
| programs/…/WithdrawExcessLamports.go | Shows rent-excess withdrawal from Token Program accounts. |
| programs/…/accounts.go | Defines the base mint, token account, and multisig layouts. |
| programs/…/impl_pointer_consistency_test.go | Protects the shared builder and decoder variant shape. |
| programs/…/instructions.go | Defines base token actions, extension actions, and Token-2022 target selection. |
| programs/…/TransferFeeExtension.go | Shows transfer-fee sub-actions and the authority, signer, and source-account order. |
| programs/…/types.go | Defines the extension types that expand Token-2022 account behavior. |
| programs/…/confidential_common.go | Defines shared confidential extension decoding and authority-account conventions. |
| programs/…/confidential_transfer_account_info.go | Connects confidential transfer account state to balance changes and transfer proofs. |
| programs/…/confidential_mint_burn_account_info.go | Connects confidential mint state to supply changes, mint proofs, and burn proofs. |
| programs/…/confidential_mint_burn_client.go | Shows confidential supply rotation, mint, burn, and pending-burn client flows. |
| programs/…/confidential_transfer_fee_client.go | Shows confidential fee harvesting, withdrawal, and proof preparation. |
| programs/…/confidential_client_common_test.go | Defines the shared checks for proof location, order, offsets, ciphertexts, and verification. |
| programs/…/confidential_transfer_client_test.go | Shows transfer client coverage for inline and context-state proof flows. |
| programs/…/confidential_mint_burn_client_test.go | Shows mint, burn, and supply-rotation client coverage and the no-mixed-location rule. |
| programs/…/extension_test.go | Verifies official Token-2022 instruction bytes, transfer-fee account order, and extension state round trips. |
| programs/…/mint.go | Defines the proof group needed by confidential mint actions. |
| programs/…/ExtendLookupTable.go | Shows lookup-table extension accounts, variable-length address data, and decode guards. |
| programs/…/extend_count_test.go | Protects lookup-table decoding from oversized address counts. |
| programs/…/resolve.go | Connects RPC table reads to versioned-message lookup resolution. |
| programs/…/instruction.go | Defines transaction-wide compute and account-data controls. |
| programs/…/Create.go | Defines memo message validation and optional signer accounts. |
| programs/…/loader.go | Shows the multi-transaction program deployment flow. |
| programs/…/instruction.go | Defines zero-knowledge proof actions, verification forms, context-state accounts, and decoding rules. |