Skip to content
Onchain programs move tokens by issuing a cross-program invocation (CPI) into the Token Program. Your program builds a Token Program instruction, supplies the accounts it needs, and calls invoke (or invoke_signed when a program-derived address signs). The Token Program then runs that instruction with the privileges your program extends to it. This page covers the token-specific side of that flow. For the CPI mechanism itself — how invoke and invoke_signed work, how signer and writable privileges propagate, and the per-CPI cost model — see cross-program invocation.

​Common patterns

Most token CPIs follow the same shape: build the instruction, pass the token accounts and an authority, and invoke. The example below transfers tokens with TransferChecked, which verifies the mint and decimals as part of the transfer. To build the Anchor example in a project created with anchor init, add anchor-spl from the program’s folder (programs/<program-name>):
Terminal
Then add anchor-spl/idl-build to the idl-build feature in the program’s Cargo.toml. Without it, anchor build fails with Building IDL failed:
Cargo.toml
After you paste the example into src/lib.rs, run anchor keys sync to write your program’s address into declare_id!. anchor build also compiles the tests that anchor init created for its template program in programs/<program-name>/tests, so delete them or rewrite them for this program. If anchor build stops with Platform tools v1.52 is incompatible with SBPFv3 (Anchor 1.1 with a recent Solana CLI), see the workaround in Build your first program. The Pinocchio examples on this page use pinocchio 0.12 and pinocchio-token 0.8:
Terminal
Minting, burning, and closing accounts follow the same pattern with a different instruction — MintTo, Burn, and CloseAccount in Anchor’s anchor_spl::token module, or the matching builders in pinocchio-token. For complete, runnable programs that CPI into the Token Program in both Anchor and native Rust, see the token program examples — in particular the “Transfer Tokens” example.
Security: Check the token program and the accounts it acts on
  • Verify the token program account is spl_token::ID or spl_token_2022::ID before invoking it, and that it matches the owner of the token accounts and mint you pass. In Anchor, use Interface<'info, TokenInterface> with InterfaceAccount.
  • Use TransferChecked, not Transfer. It verifies the mint and decimals, and the Token Extensions Program requires it for mints with some extensions.
  • Check that the destination token account’s mint and owner are the ones you expect. A token transfer to the wrong account succeeds.
  • After the CPI, reload the token accounts before reading their balances (see Reload state after a CPI).

​Signing with a PDA authority

When the authority on a token account is a program-derived address owned by your program, the program signs the CPI itself with invoke_signed, passing the PDA seeds as signer seeds. In Anchor, use CpiContext::new_with_signer with the seeds instead of CpiContext::new. The instruction is otherwise identical to the examples above. See CPIs with PDA Signers for the full mechanics.

​Batching

The Batch instruction executes several Token Program instructions inside a single program invocation. Because each CPI carries a fixed compute cost, running several token operations in one batch CPI uses fewer compute units than issuing a separate CPI per operation — see the CPI cost model for how per-CPI costs add up. Lower compute usage lets you request a smaller compute-unit limit, which lowers the total priority fee at a given compute-unit price. This matters most for programs that perform many token operations in a single instruction. Common uses include fanning out a transfer to many recipients, or running a multi-step token flow (for example sync, transfer, and close) in a single CPI.
Batch only accepts Token Program instructions as children, and a batch cannot contain another batch. For the operations that move tokens (such as transfers, mints, and burns), the program verifies that the affected accounts are owned by the Token Program before executing them.

​Source reference

ItemDescriptionSource
BatchThe Batch instruction (discriminator 255).Source
process_batchBatch processor logic.Source

​Calling Batch via CPI

The pinocchio-token crate exposes a Batch builder under pinocchio_token::instructions::Batch. You stage each child instruction into the batch’s buffers and then issue a single CPI with invoke_unchecked. The example below uses pinocchio-token 0.8. A Batch is backed by three caller-provided buffers: one for the serialized instruction data, one for the per-child instruction account metas, and one for the account views passed to the CPI. Construct them as MaybeUninit slices, hand them to Batch::new, append children, then invoke:
Batch two transfers in one CPI
Batch::new writes the batch discriminator into the first byte of the data buffer and returns a Batch that tracks how much of each buffer has been used. Every Token Program instruction builder (such as Transfer, TransferChecked, MintTo, and Burn) implements the IntoBatch trait, so into_batch appends that instruction’s data, account metas, and account views to the batch. Calling invoke_unchecked issues the assembled batch as one CPI into the Token Program. From pinocchio-token 0.8, the batch invoke methods are unsafe. into_batch checks that a child’s accounts aren’t borrowed when you add it, but nothing checks again when the batch is invoked. Between adding the first child and invoking the batch, don’t borrow the data or lamports of any account in the batch; otherwise the CPI can change them while a reference is still alive. Versions 0.6 and 0.7 have safe invoke and invoke_signed batch methods instead. The buffers bound the batch’s capacity. Size the data buffer to 1 + Σ(2 + child_data_len) bytes (the discriminator, plus a 2-byte header and the serialized data for each child) and each account buffer to the total number of accounts across all children. If a child does not fit, into_batch returns ProgramError::InvalidArgument. For reference, Batch::MAX_DATA_LEN is 10 KiB and Batch::MAX_ACCOUNTS_LEN equals the runtime’s maximum CPI account count. For batches whose size is only known at runtime, enable the crate’s alloc feature and use BatchState::new(accounts_len, data_len) to allocate the buffers on the heap, then call as_batch to obtain a Batch. When a child instruction’s authority is a PDA owned by your program, sign the CPI with invoke_signed_unchecked instead of invoke_unchecked, passing the PDA seeds as Signer entries.

​Instruction data layout

A batch encodes its children one after another behind the 255 discriminator. All accounts are passed flat, in order, and sliced by num_accounts for each child. The builder produces exactly this wire format:
Batch Wire Format
A batch’s capacity is bounded by the buffers passed to Batch::new: the data buffer must hold the discriminator plus every child’s header and data, and the account buffers must hold every child’s accounts. Size them for the largest batch you build, or use BatchState (with the alloc feature) to size them at runtime. The combined CPI is still subject to the transaction’s account and size limits, so for very large fan-outs use Address Lookup Tables to fit more accounts.