in-solana by Supanode

What Is CPI in Solana?

CPI (Cross-Program Invocation) is the mechanism that allows one Solana program to invoke another program’s instructions during execution. CPIs enable composability — programs can build on each other without duplicating logic.

How CPI works

When program A wants to call program B, it constructs an instruction (program ID, accounts, data) and calls invoke or invoke_signed. The runtime transfers control to program B, which executes the instruction and returns. Program A then continues execution.

invoke vs invoke_signed

  • invoke — calls another program using the caller’s signer privileges. All signer accounts must have signed the original transaction.
  • invoke_signed — calls another program with PDA signing. The calling program provides the PDA seeds, and the runtime verifies the PDA derivation. This is how programs sign transactions on behalf of PDAs they own.

Depth limit

CPIs can be nested up to 4 levels deep. Program A can call B, B can call C, C can call D, but D cannot make another CPI. This limit prevents infinite recursion and stack overflow.

Privilege extension

A CPI extends the signer and writable privileges of accounts from the original transaction. If an account was marked as a signer in the transaction, it remains a signer through CPIs. Programs cannot escalate privileges beyond what the original transaction declared.

FAQ

Does CPI cost extra compute units? Yes. Each CPI invocation adds overhead — roughly 1,000 CUs for the invocation itself, plus whatever the called program consumes.

Can a CPI modify the calling program’s accounts? No. Only the owning program can modify an account’s data. The called program can only modify accounts it owns.

What is re-entrancy? Solana prevents a program from being re-entered via CPI. If program A calls B, and B tries to call A back, the runtime rejects it.

Updated: