Building Composable Programs

Cross-Program Invocations (CPIs)

Chapter 5: Building Composable Programs

Design programs for composability using CPIs.

  • Key Points:

    • Expose instructions for CPIs.

    • Use PDAs for shared state.

    • Example: Calling another program.

  • Code Example:

pub fn call_other_program(ctx: Context<CallOther>, data: u64) -> Result<()> {
    let cpi_program = ctx.accounts.other_program.to_account_info();
    let cpi_accounts = other_program::accounts::DoSomething {
        // Accounts here
    };
    let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts);
    other_program::do_something(cpi_ctx, data)?;
    Ok(())
}
  • Interactivity: Simulate calling another program and observe the interaction.

Last updated