Performing CPIs with Anchor
Cross-Program Invocations (CPIs)
Chapter 2: Performing CPIs with Anchor
Anchor makes CPIs straightforward with CpiContext.
Key Points:
Set up CPIs with CpiContext.
Specify the target program and accounts.
Anchor manages the details.
Code Example:
pub fn transfer_sol(ctx: Context<TransferSol>, amount: u64) -> Result<()> {
let cpi_program = ctx.accounts.system_program.to_account_info();
let cpi_accounts = system_program::Transfer {
from: ctx.accounts.from.to_account_info(),
to: ctx.accounts.to.to_account_info(),
};
let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts);
system_program::transfer(cpi_ctx, amount)?;
Ok(())
}
Interactivity: Simulate a SOL transfer and check account balances.
Last updated