Skip to content
SummaryUse invoke for CPIs where all required signers have already signed the original transaction. No PDA signing is needed. Signer and writable privileges extend from the caller to the callee.

​CPIs without PDA signers

When a CPI doesn’t require PDA signers, the invoke function is used. The invoke function calls the invoke_signed function with an empty signers_seeds array. The empty signers array indicates that no PDAs are required for signing.
Invoke function
The examples below make a CPI using Anchor and Native Rust. It includes a single instruction that transfers SOL from one account to another.

​Anchor

The following examples show two approaches to implementing CPIs in an Anchor program. The examples are functionally equivalent, but demonstrate different levels of abstraction.
  • Example 1: Uses Anchor’s CpiContext and helper function.
  • Example 2: Uses the system_instruction::transfer function from the solana_program crate.
  • Example 3: Constructs the CPI instruction manually. This approach is useful when no crate exists to help build the instruction you want to invoke.
To run one of these examples locally with the Anchor CLI:
  1. Create a project with a single-file program and a Mocha test:
    Terminal
  2. Replace programs/cpi/src/lib.rs with the program below and tests/cpi.ts with the test.
  3. Point the program’s declare_id! at your project’s program ID:
    Terminal
    Without this step, anchor build only warns Program ID mismatch detected, and anchor test fails with an error such as DeclaredProgramIdMismatch (Anchor 1.1.2) or This program may not be used for executing instructions.
  4. Build the program and run the test:
    Terminal

​Rust

The following example shows how to make a CPI from a program written in Native Rust. It includes a single instruction that transfers SOL from one account to another. (The test file uses LiteSVM to test the program.)
The test uses litesvm with the Kit API and runs with Vitest. Install the test dependencies with npm i -D vitest litesvm @solana/kit @solana-program/system. Build the program with cargo build-sbf, copy the .so file from target/deploy next to the test file as program.so, then run the test with npx vitest run. The program’s Cargo.toml needs these dependencies: