Introducing a new way to review your PRs.

Read the announcement

solana-foundation / program-examples

A repository of Solana program examples

View on GitHub

19 documents · Updated Nov 23, 2026

docs / token-program-examples

Token program workflows

The token examples show how Solana assets move from creation to use in products. They cover basic minting and transfers, Token-2022 rules, one-of-one assets, proof-based distributions, and exchange or fund-raising flows. Each workflow makes the asset, its authority, and the conditions for moving tokens clear.

Token lifecycle

The common token lifecycle starts with a mint that defines an asset and continues through token accounts that hold units of that asset. Product flows then add rules around who may mint, transfer, receive, or redeem those units. See Core token operations for the basic mint and account lifecycle.

Mint creation and supply

An SPL token is represented by a mint. The mint defines supply, decimal precision, and the authorities that control minting or freezing. Metadata gives clients a human-facing name, symbol, and image reference, while a token account holds a user's balance. NFTs use the same token model with zero decimals and a supply of one. The examples also show two authority models: a wallet can control minting, or a program-derived address (PDA) can control it for a program-managed asset.

References: tokens/…/README.md• L1-14 tokens/…/README.md• L24-57 tokens/…/create_token.rs• L38-54 tokens/…/init.rs• L12-16 tokens/…/create.rs• L21-39

Minting and transfers

Minting increases supply in a recipient token account when the mint authority permits it. A transfer moves existing units between token accounts and requires the sender or another approved authority to authorize the move. The basic examples also create the recipient's associated token account when needed, so a recipient does not need to approve receipt of tokens. The same product path is shown through Anchor, native, and Pinocchio program variants, while an external-delegate example adds a separate wallet-signature path.

An external delegate is an off-chain authority linked to a Solana user account. The transfer request binds the source, destination, amount, and a nonce into a signed message. The nonce advances after a valid transfer, which stops the same signed request from being used again.

References: tokens/…/mint_spl.rs• L16-65 tokens/…/transfer.rs• L16-80 tokens/…/transfer.ts• L19-45 tokens/…/lib.rs• L13-81 tokens/…/lib.rs• L153-186

Token-2022 behavior

Token-2022 keeps the core mint and account model but lets a mint or token account carry extra rules and data. These extensions make account size, initialization order, authorities, and transfer behavior part of the asset contract. See Token-2022 extensions for the extension-focused view.

Account and mint policies

Token-2022 examples show policies that affect the lifetime of a mint or account. A default account state controls whether new token accounts begin usable or frozen. An immutable owner prevents later reassignment of a token account. A mint close authority permits a named authority to close the mint and reclaim its rent. Non-transferable tokens cannot move after minting, and an interest-bearing mint changes the displayed value over time.

These policies can be combined. For example, one mint can be both non-transferable and closeable. The account or mint must reserve space for every selected extension before initialization, so choosing an extension changes the asset setup rather than only its later behavior.

References: tokens/…/create_mint.rs• L30-39 tokens/…/create_mint.rs• L68-101 tokens/…/create_token_account.rs• L20-23 tokens/…/create_mint.rs• L20-23 tokens/…/create_mint.rs• L21-30 tokens/…/create_mint.rs• L32-40

Transfer constraints

Transfer fees reduce the amount delivered to the recipient and keep the withheld amount for later collection. A transfer hook calls policy logic during a transfer, such as checking a wallet switch, counting transfers, charging a related fee, or reading a per-wallet state account. The hook's extra-account list tells Token-2022 which policy accounts must join the transfer.

Token-2022 can also make the transaction path part of the transfer rule. A CPI guard can reject an owner-authorized transfer when another program invokes Token-2022, while a memo-transfer rule can require a memo before tokens enter a protected account. These rules make the caller's invocation path and transaction context part of the asset contract.

Allow and block list flows separate list management from token issuance. One list can serve several mints, with modes that require an explicit allow, reject listed wallets, or require an allow only above a threshold. This lets an issuer apply a shared transfer policy without changing each asset's mint authority.

References: tokens/…/create_mint.rs• L31-41 tokens/…/create_mint.rs• L79-108 tokens/…/initialise_extra_account_metas_list.rs• L13-26 tokens/…/transfer_hook.rs• L16-55 tokens/…/lib.rs• L57-103 tokens/…/lib.rs• L124-169 tokens/…/README.md• L7-26 tokens/…/cpi_transfer.rs• L21-35 tokens/…/test.ts• L109-217 tokens/…/create_token_account.rs• L25-38 tokens/…/test.ts• L147-229

Token metadata and combined features

Token-2022 can keep metadata with the mint and can add several extensions to one asset. Mint-held metadata supports names, symbols, URIs, custom fields, and authority updates without a separate metadata account. A metadata pointer NFT can also store game state such as a level or collected resource with the asset. These examples show how descriptive data and asset rules can share the same mint identity.

References: tokens/…/lib.rs• L8-30 tokens/…/test_metadata.rs• L64-149 tokens/…/README.md• L1-11 tokens/…/create_mint.rs• L21-30

NFT and claim workflows

NFT and claim examples use different forms of trust. An NFT flow establishes an asset and its collection identity. A claim flow establishes eligibility from a published snapshot and pays only wallets that prove an entry in that snapshot. See NFT and Merkle token flows for the detailed child page.

NFT collection lifecycle

The NFT workflow creates a collection asset, mints a one-of-one item, and then verifies the item's collection link. Metadata records identify the asset and the master edition limits it to one token. Collection verification is a separate authority step, so an NFT can exist before its collection membership is trusted. The collection authority is derived from the program, which keeps verification tied to the program's authority rather than to a client-supplied address.

References: tokens/…/create_collection.rs• L16-19 tokens/…/mint_nft.rs• L16-19 tokens/…/verify_collection.rs• L18-74 tokens/…/verify_collection.rs• L11-84

Merkle token distribution

A Merkle claim flow distributes a fixed token allocation without storing every eligible wallet on chain. An off-chain snapshot becomes a single root. Each claimant supplies an amount, position, and proof that the program checks against that root before it pays from a funded vault. A claim receipt records the completed position and prevents a second payment for the same allocation. The tree generator and the on-chain verifier share the leaf and proof rules, including zero-hash padding for odd tree levels. See NFT and Merkle token flows for the proof and receipt model.

References: tokens/…/README.md• L7-27 tokens/…/README.md• L47-69 tokens/…/lib.rs• L18-109 tokens/…/generate_merkle_tree.rs• L41-75 tokens/…/generate_merkle_tree.rs• L125-153

The following diagram shows the main path from an asset definition to either a user balance or a guarded distribution.

flowchart LR
    A[Create mint] --> B[Mint supply]
    B --> C[Transfer to token accounts]
    C --> D[Product use]
    B --> E[Fund token vault]
    E --> F[Verify NFT or claim proof]
    F --> G[Pay verified recipient]

Exchange and fundraising workflows

Exchange examples place token movement behind a shared condition. An escrow exchanges two fixed amounts between two parties. An automated market maker exchanges against pool reserves. A fundraiser collects one token into a vault and releases or returns it according to the campaign result. See Token exchange and fundraising for the child page.

Direct escrow exchange

The escrow workflow lets a maker publish an offer for a fixed amount of token A in return for a fixed amount of token B. The offered tokens stay in a program-controlled vault until a taker supplies the wanted tokens. The program then completes both sides of the exchange or lets the maker recover the offer through the refund path. The offer state binds the maker, token mints, amounts, and vault so another account cannot substitute a different deal.

References: tokens/…/README.md• L3-13 tokens/…/test_escrow.rs• L159-230 tokens/…/make_offer.rs• L41-125 tokens/…/take_offer.rs• L38-73 tokens/…/refund_offer.rs• L31-70

Pool-based token exchange

The swap workflow uses an automated market maker (AMM) as a standing counterparty. Liquidity providers place two tokens into a pool and receive liquidity tokens that represent their share. Traders exchange one pool asset for the other at a price based on the pool reserves, with a fee and a minimum acceptable output protecting the trade. Providers can later withdraw their proportional share.

References: tokens/…/README.md• L1-14 tokens/…/README.md• L34-54 tokens/…/lib.rs• L16-43 tokens/…/test_token_swap.rs• L276-398

Fundraising lifecycle

The fundraiser workflow creates a campaign for one target token and a time window. Contributors send that token to the campaign vault while the campaign is open. If the target is met, the campaign maker receives the vault balance. If the time ends before the target is met, each contributor can recover the amount recorded for that contributor.

References: tokens/…/readme.MD• L1-5 tokens/…/lib.rs• L14-41 tokens/…/test_fundraiser.rs• L167-227

Workflow relationships

These examples share a small set of product boundaries. Basic token operations create assets and balances. Token-2022 adds rules that can affect every later account or transfer. NFT metadata and Merkle proofs add identity or eligibility checks. Exchange and fundraising programs add a vault and a state transition that decides when tokens may leave it.

Workflow needSupporting exampleMain result
Define and move an assetCore token operationsA mint and user token balances
Enforce token-specific policyToken-2022 extensionsTransfers or accounts subject to mint extensions
Establish NFT identityNFT and Merkle token flowsA minted item with collection status
Distribute a fixed allocationNFT and Merkle token flowsProof-backed payments from a vault
Exchange or raise tokensToken exchange and fundraisingConditional movement between users and vaults

Change checklist

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

  • Keep the mint's token program, decimal precision, extension set, and associated token account rules aligned between the client and each consuming workflow.
  • Treat a vault's token mint and authority as one contract. A change to either can make a valid offer, claim, or campaign unable to release funds.
  • Keep NFT metadata, collection references, and collection-authority checks aligned so minting does not imply verified membership.
  • Keep Merkle leaf encoding, proof rules, claim positions, and receipt identity aligned between the snapshot tool and the claim program.
  • Preserve the terminal conditions for escrow offers, AMM trades, and fundraisers. Changing when a vault can pay or refund changes the user-facing contract.
  • When a Token-2022 extension changes transfer output or required accounts, update every flow that treats the requested amount as the delivered amount.
  • For CPI-guard and memo-transfer flows, keep the invocation path and same-transaction memo requirement aligned with the account policy.
  • Keep each example's package manifest, workspace override, and available lockfile state consistent. Removed build helpers must not leave a client or program using a stale dependency or instruction contract.
  • When a PDA becomes an authority, keep its seed, bump handling, stored authority, and signer use aligned across initialization, minting, metadata, and verification.
  • When an external signature authorizes a transfer, keep the signed domain, transfer fields, nonce, and recovered address contract aligned with the on-chain verifier.

Important failure modes

TriggerConsequenceGuard
A client uses the base token program for a Token-2022 mintAccount creation or transfer does not satisfy the mint's token rulesCarry the mint's token program through account and transfer operations
A transfer-fee workflow treats the requested amount as the received amountThe recipient gets less than the value shown by the callerCalculate and handle the extension fee and later fee collection
A transfer hook lacks its policy accounts or rejects the wallet stateA normal-looking transfer fails during token processingSupply the hook context and keep the wallet policy state valid
A CPI-guarded source account is used in an owner-authorized CPI transferToken-2022 rejects the transfer even though the owner signed itUse a permitted transfer path or change the guard state through its owner-authorized control
A memo-required destination receives a transfer without a preceding memoToken-2022 rejects the incoming transferPut the memo and transfer in the same transaction in the required order
A Token-2022 extension is added after mint or account initializationThe asset cannot be initialized with the selected policyReserve extension space and initialize extensions before the base object
A mint or account uses the wrong authority modelA program cannot mint, update metadata, close, or verify the assetCheck the expected wallet or PDA authority at every authority action
An NFT collection link is not verified by the collection authorityApplications cannot treat the item as a verified collection memberRun the separate collection verification step
A Merkle tree builder and verifier encode a leaf differentlyAn eligible claimant cannot prove eligibilityUse one leaf, hash, order, and index contract
A claim receipt or vault boundary is changed incorrectlyA claim can pay twice or a vault can pay outside its intended stateBind receipt identity to the allocation and gate vault release
A swap output falls below the trader's minimumThe trade is rejected rather than silently returning too littleCheck the minimum output before the pool transfer
A fundraiser ends without its target or releases before its targetContributors lose the expected claim or refund pathEnforce time and target conditions for each terminal action
An external signature omits a transfer field or reuses a nonceA valid signature can authorize a different transfer or be replayedHash the full transfer context and consume the nonce once
A package manifest, workspace override, or available lockfile resolves incompatible client and program dependenciesAn example can fail before its token workflow runs or can use mismatched instruction typesKeep the manifest, workspace override, and any checked-in lockfile synchronized for each example variant

Key files

FileWhy to read it
tokens/…/README.mdDefines the shared mint, metadata, decimal, and NFT model.
tokens/…/create_token.rsShows a mint and its human-facing metadata being created together.
tokens/…/transfer.rsShows recipient account setup and token movement.
tokens/…/lib.rsShows transfer authorization from an external wallet signature with replay protection.
tokens/…/init.rsDefines the program-controlled mint authority lifecycle.
tokens/…/create.rsShows how the program-controlled authority is used for an NFT mint.
tokens/…/create_mint.rsShows a mint policy that controls the state of new token accounts.
tokens/…/create_mint.rsShows how transfer fees change token movement and authority duties.
tokens/…/transfer_hook.rsShows policy logic that can accept or reject a Token-2022 transfer.
tokens/…/cpi_transfer.rsShows a transfer policy that distinguishes direct calls from program-invoked transfers.
tokens/…/test.tsShows the guarded transfer failure and the permitted retry path.
tokens/…/create_token_account.rsShows an account that requires memos on incoming transfers.
tokens/…/test.tsShows the required memo and transfer transaction relationship.
tokens/…/lib.rsDefines the mint-held metadata lifecycle.
tokens/…/README.mdShows an NFT with metadata and game state held by the mint.
tokens/…/mint_nft.rsDefines the collection NFT minting lifecycle.
tokens/…/verify_collection.rsDefines the separate collection verification step.
tokens/…/README.mdGives the fixed allocation and proof-backed claim model.
tokens/…/lib.rsDefines root updates, claims, receipts, and vault payment.
tokens/…/take_offer.rsShows the guarded two-sided exchange and offer close path.
tokens/…/lib.rsDefines the pool, liquidity, swap, and withdrawal paths.
tokens/…/lib.rsDefines the campaign, contribution, success, and refund paths.