Introducing a new way to review your PRs.

Read the announcement

solana-foundation / solana-go

Go SDK library and RPC client for the Solana Blockchain

View on GitHub

26 documents · Updated Nov 16, 2026

docs / system-and-stake-programs

System and stake program APIs

The System and Stake program APIs build typed Solana instructions for account setup, lamport movement, durable nonces, and stake control. The System package creates and manages accounts, while the Stake package models stake state and moves an account through initialization, delegation, activation, deactivation, and withdrawal. This page complements Program instruction builders and sits beside Token program APIs and Lookup table and auxiliary program APIs.

Program instruction model

The builders in programs/…/instructions.go and programs/…/instructions.go turn one program action into a typed instruction. Each action holds its input values and its ordered account roles, then produces a value that the transaction layer can compile and sign.

The package offers two entry styles:

  • Use a New...Instruction(...) constructor when all values are known at once.
  • Use a New...InstructionBuilder() value with chained setters when a flow fills values in stages.
  • Use ValidateAndBuild() when the caller wants missing values and missing account roles to fail before the transaction is assembled.

The account setters also mark each account as writable or as a signer. Those marks express the permission contract that the message builder later combines with other instructions. References: programs/…/instructions.go• L32-43, programs/…/CreateAccount.go• L47-107, programs/…/instructions.go• L30-40, programs/…/Initialize.go• L180-206

Account roles and runtime accounts

An account role names both an account and how the program uses it. A writable role can change during the call. A signer role must have a matching transaction signature. A sysvar is a read-only runtime account that supplies values such as rent, time, recent block hashes, or stake history.

The builders keep account order because the target program gives each position a fixed meaning. Constructors fill common sysvar roles with the canonical keys, but the builder setters allow callers to supply a key when a custom program or test needs one. References: programs/…/AdvanceNonceAccount.go• L27-47, programs/…/InitializeNonceAccount.go• L28-52, programs/…/DelegateStake.go• L26-45, programs/…/Authorize.go• L28-48

Instruction and state decoding

Both packages register a decoder for their program identifier. Decoding first selects the instruction kind, then restores the account roles from the account metadata supplied by the transaction decoder. Stake account data has a separate state decoder because an account's current state is not part of an instruction.

This split lets a caller inspect both sides of a transaction: the requested action and the account state that makes the action valid. References: programs/…/instructions.go• L121-203, programs/…/instructions.go• L82-169, programs/…/state.go• L203-309

System program APIs

The System program API covers the base account operations used by many higher-level programs. It can create an account with funding and ownership, change its allocated space or owner, transfer lamports, and operate a durable nonce account.

Account lifecycle

An account lifecycle starts with funding and account creation or with preparation of an existing account. The regular and seed-based forms serve the same broad goals, but seed-based forms describe an address derived from a base public key and a seed.

GoalInstruction familyMain result
Create and fund an accountCreateAccountCreates an account with lamports, space, and a program owner.
Create a derived accountCreateAccountWithSeedCreates a seed-derived account with the same funding, space, and owner choices.
Reserve account spaceAllocateGives a signer-owned account a data size without adding funds.
Reserve space on a derived accountAllocateWithSeedAllocates space and identifies the derived account's owner from seed data.
Set an account ownerAssignAssigns a signer account to a program.
Set a derived account ownerAssignWithSeedAssigns a seed-derived account to a program.

Seed-based operations use both the derived account and the base account that authorizes the derivation. The seed, base key, owner, and account roles must describe the same derived address or the System program cannot accept the request. References: programs/…/CreateAccount.go• L28-44, programs/…/CreateAccountWithSeed.go• L28-53, programs/…/Allocate.go• L28-35, programs/…/AllocateWithSeed.go• L28-47, programs/…/Assign.go• L28-35, programs/…/AssignWithSeed.go• L28-44

Lamport transfers

Transfer moves a caller-selected number of lamports from a writable signer account to a writable recipient. Lamports are the smallest unit used for SOL balances. TransferWithSeed performs the same kind of movement when the funding address is derived from a base key, a seed, and an owner.

The ordinary form needs a funding account and a recipient. The seed form needs the derived funding account, its signing base account, and the recipient, so the account roles carry the authority needed for the derived address. References: programs/…/Transfer.go• L28-79, programs/…/TransferWithSeed.go• L28-47, programs/…/TransferWithSeed.go• L76-110

Durable nonce operations

A durable nonce is a stored block-hash value that lets a transaction use a nonce account instead of relying only on a recent block hash. The NonceAccount state records the authorized nonce authority, the current nonce, and the fee rate stored with that account.

The nonce builders support the account's main lifecycle:

  • InitializeNonceAccount changes an uninitialized account into an initialized nonce account and sets its authority.
  • AdvanceNonceAccount consumes the current nonce and replaces it with a successor.
  • AuthorizeNonceAccount changes the key allowed to perform nonce operations.
  • WithdrawNonceAccount sends funds out while applying the nonce account balance rule.
  • UpgradeNonceAccount performs the one-time upgrade for a legacy nonce version.

The normal use sequence is shown below. Initialization and withdrawal also use the block-hash and rent sysvars where the System program requires them.

flowchart LR
    C[Create and fund nonce account] --> I[Initialize nonce authority]
    I --> A[Advance stored nonce]
    A --> U[Use nonce in a transaction]
    U --> A
    I --> H[Change nonce authority]
    I --> W[Withdraw funds]
    I --> G[Upgrade legacy nonce state]

NonceAccount is a decoded state value rather than an instruction builder. It exposes the account version, state, authorized public key, stored nonce, and fee calculator. References: programs/…/accounts.go• L24-34, programs/…/InitializeNonceAccount.go• L28-42, programs/…/AdvanceNonceAccount.go• L27-38, programs/…/AuthorizeNonceAccount.go• L28-38, programs/…/WithdrawNonceAccount.go• L28-47, programs/…/UpgradeNonceAccount.go• L27-34

Stake program APIs

The Stake program API manages stake accounts and their two authority roles. The staker controls stake actions such as delegation and deactivation. The withdrawer controls withdrawals. A lockup can add time or epoch limits and can name a custodian that can bypass those limits.

Stake account state

StakeStateV2 gives callers a typed view of the fixed 200-byte stake account state. Its discriminator identifies an uninitialized account, an initialized account, a delegated stake account, or a rewards pool. Initialized and delegated states include common metadata for authorities and lockup; delegated state also includes delegation details and flags.

StateMeaningMain data
StakeStateUninitializedReady for initializationNo stake metadata.
StakeStateInitializedHas authorities and lockup settingsMeta.
StakeStateStakeHolds delegated stakeMeta, StakeInfo, and StakeFlags.
StakeStateRewardsPoolReserved rewards-pool stateNo stake metadata.

Delegation identifies the validator vote account and records the stake amount plus activation and deactivation epochs. Meta holds the staker, withdrawer, and lockup settings. References: programs/…/state.go• L12-24, programs/…/state.go• L37-49, programs/…/state.go• L119-142, programs/…/state.go• L203-226

Stake account initialization

Initialize creates the authority and lockup metadata for a new stake account. Its convenience constructor uses the rent sysvar and sets a zero lockup with the System program as the default custodian. InitializeChecked uses account roles for the stake and withdraw authorities so the required authority checks are visible in the transaction account list.

Initialization is the boundary between an uninitialized account and an account that can hold stake. The account must already exist with the space and funding required by the Stake program; the instruction only sets its stake metadata. References: programs/…/Initialize.go• L27-40, programs/…/Initialize.go• L75-102, programs/…/Initialize.go• L180-206, programs/…/InitializeChecked.go• L26-40, programs/…/InitializeChecked.go• L112-131

Stake authorization

StakeAuthorize selects one of the two authority roles: StakeAuthorizeStaker or StakeAuthorizeWithdrawer. The Authorize family changes the selected role to a new public key. The checked forms require the new authority to appear as a signer, while the seed forms describe an authority derived from a base key, seed, and owner.

Authorization changes can depend on the lockup. The standard and checked builders therefore support an optional lockup authority account for a withdrawer change made before the lockup ends. The seed variants keep the derived authority details in their instruction arguments and use the authority base account as a signer. References: programs/…/types.go• L24-29, programs/…/Authorize.go• L28-48, programs/…/AuthorizeChecked.go• L28-48, programs/…/AuthorizeWithSeed.go• L27-44, programs/…/AuthorizeCheckedWithSeed.go• L27-47

Delegation and deactivation

DelegateStake links a stake account to a validator vote account. It supplies the clock, stake history, and stake configuration sysvars that the Stake program uses to apply activation rules. The resulting account moves into delegated stake state as activation progresses.

Deactivate starts cooldown for a delegated stake account. DeactivateDelinquent is a special path that uses a delinquent vote account and a reference vote account to deactivate stake under the program's delinquency rule. GetMinimumDelegation provides the program instruction used to query the current minimum delegation value. Redelegate remains exposed for decoding and construction but is marked deprecated since Solana v2.1.0. References: programs/…/DelegateStake.go• L26-45, programs/…/DelegateStake.go• L121-143, programs/…/Deactivate.go• L26-38, programs/…/DeactivateDelinquent.go• L26-38, programs/…/GetMinimumDelegation.go• L24-64, programs/…/Redelegate.go• L26-44

Lockup control

Lockup describes the time and epoch limits on stake withdrawals and names the custodian that can exempt an operation from those limits. SetLockup changes any of the lockup values and can replace the custodian. SetLockupChecked provides the checked form for time and epoch changes and can include a new lockup authority signer.

The argument types distinguish a value that is present from a value that is left unchanged. This lets a lockup update change only selected fields while preserving the others. References: programs/…/Lockup.go• L24-31, programs/…/types.go• L31-35, programs/…/types.go• L132-135, programs/…/SetLockup.go• L27-37, programs/…/SetLockupChecked.go• L27-41

Stake movement

Stake movement changes how lamports are arranged across stake accounts while keeping authority checks explicit. Split moves a chosen amount into a new uninitialized stake account. Merge drains a source stake account into a destination account. MoveStake moves active stake, while MoveLamports moves lamports between accounts that share the required authorities.

The account roles make the direction of each operation clear: source accounts and destination accounts are writable, and the stake authority signs. The destination state still must satisfy the Stake program's rules for the selected operation. References: programs/…/Split.go• L27-39, programs/…/Merge.go• L26-43, programs/…/MoveStake.go• L27-40, programs/…/MoveLamports.go• L27-40

Stake withdrawals

Withdraw sends unstaked lamports from a stake account to a recipient. It uses the clock and stake history sysvars because withdrawal eligibility depends on stake state and cooldown. The withdraw authority signs, and an optional lockup authority can support a withdrawal before the lockup expires.

Withdrawal is therefore normally the final step after deactivation and cooldown. The requested amount must also leave a valid account balance or drain the account as allowed by the Stake program. References: programs/…/Withdraw.go• L27-48, programs/…/Withdraw.go• L51-91, programs/…/Withdraw.go• L149-173, programs/…/Lockup.go• L24-31

Stake lifecycle flow

The following flow shows the normal relationship between System account setup and Stake account control.

flowchart LR
    F[Fund account] --> C[System create account]
    C --> I[Stake initialize]
    I --> D[Delegate to vote account]
    D --> A[Activation]
    A --> S[Active stake]
    S --> X[Deactivate]
    X --> Q[Cooldown]
    Q --> W[Withdraw unstaked lamports]
    S --> P[Split or move stake]
    P --> M[Merge stake accounts]

The builders describe the requested transition, but the cluster decides whether the current account state, epoch, authority signatures, and supplied sysvars allow it. State decoding with DecodeStakeAccount is the main way to inspect the input state before selecting a transition. References: programs/…/CreateAccount.go• L28-44, programs/…/Initialize.go• L27-40, programs/…/DelegateStake.go• L26-45, programs/…/Deactivate.go• L26-38, programs/…/Withdraw.go• L27-48, programs/…/state.go• L299-309

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep each instruction constant, variant registration, and instruction-name mapping aligned when adding or changing a System or Stake instruction.
  • Keep account position, writable flags, signer flags, optional account positions, and convenience-constructor defaults aligned with the target program contract.
  • When a sysvar key changes, update every constructor that supplies that sysvar and every builder setter that permits an override.
  • Preserve the stake state discriminator meanings and the 200-byte account contract when changing StakeStateV2, Meta, or StakeInfo.
  • Keep the instruction-level pointer forms for authorization and lockup arguments distinct from the non-optional fields used by decoded on-chain state.
  • Review callers of Redelegate when its deprecation or the supported Solana program version changes.

Important failure modes

TriggerConsequenceGuard
A required parameter or account role is not setValidateAndBuild() returns an error, or direct encoding can fail while it reads missing valuesFill all builder values and call ValidateAndBuild() before adding the instruction to a transaction
A required signer is supplied with the wrong role or no signatureThe System or Stake program rejects the transactionUse the package setters so authority and funding roles carry the signer flag, then sign with those keys
A required sysvar is omitted or points to the wrong accountThe program cannot apply rent, time, block-hash, or stake-history rulesKeep the canonical sysvar accounts from the convenience constructors unless a controlled override is required
A seed, base key, owner, and derived account do not describe one addressThe seed-based System or Stake operation is rejectedTreat the seed inputs and derived account as one address contract
Withdrawal happens before deactivation or lockup expiry, or leaves an invalid balanceThe Stake program rejects the withdrawalDecode stake state first and include the lockup authority only when its signature is allowed and needed
A split target is not an uninitialized stake account, or a merge target is not compatibleThe split or merge fails on chainCheck source and destination state and authority compatibility before building the instruction
Stake account data is shorter than 200 bytes or has an unknown discriminatorDecodeStakeAccount returns an errorFetch the complete account data and handle only the four supported state values
A caller uses Redelegate on a cluster that removed supportThe deprecated instruction is rejectedUse the current stake movement and delegation flow instead

Key files

FileWhy to read it
programs/…/instructions.goDefines the System instruction family, program identifier, variant registration, and decode boundary.
programs/…/CreateAccount.goShows the basic account creation contract and its funding and new-account roles.
programs/…/CreateAccountWithSeed.goShows the seed-derived account contract and its base-account authority.
programs/…/Transfer.goShows the ordinary lamport transfer flow.
programs/…/TransferWithSeed.goShows lamport movement from a seed-derived funding address.
programs/…/InitializeNonceAccount.goShows how a nonce account receives its initial authority and sysvar roles.
programs/…/AdvanceNonceAccount.goShows the durable nonce advance operation and nonce authority role.
programs/…/accounts.goDefines the decoded nonce account state.
programs/…/instructions.goDefines the Stake instruction family and decode boundary.
programs/…/state.goDefines the stake account state, authority metadata, delegation data, and state decoder.
programs/…/Initialize.goShows stake account initialization and lockup defaults.
programs/…/Authorize.goShows changes to the staker and withdrawer authority roles.
programs/…/DelegateStake.goShows delegation and the runtime accounts needed for stake activation rules.
programs/…/SetLockup.goShows lockup updates and optional lockup fields.
programs/…/Split.goShows movement into a new stake account.
programs/…/Merge.goShows source-to-destination stake account merging.
programs/…/Withdraw.goShows the authority, sysvar, and recipient roles for stake withdrawal.