so what i want to do is I want to do a CPI call From program B to program A, But I want to this via a rust crate, not from program B. Like this

use Crate::create_cpi;

fn do_something(ctx ...) -> Result<()>{

   create_cpi( data)?;

here Crate is a rust crate, i want to do the CPI from the rust Crate (If Possible), Or it should return something like CPI Context or Instruction, so The user can just sign that instruction and send that transaction without much hassle. i want the user to pass as less data as possible. so they can write their code more without worrying about the CPI.

does anyone have any idea how to pull it off or any example they know of? Any reference will be helpful Thanks!

  • If your question in answered. Please mark this as answered. Thank you!
    – vicyyn
    Commented Jan 11, 2023 at 20:25

2 Answers 2


First, you need to create the 2 programs. Let's define 2 programs, puppet and puppet-master where puppet-master does a CPI into puppet.

anchor init puppet
anchor new puppet-master

Then you need to import the puppet program into the puppet-master program by adding the following line to the [dependencies] section of the Cargo.toml file inside the puppet-master program folder:

puppet = { path = "../puppet", features = ["cpi"]}

The features = ["cpi"] is used so we can not only use puppet's types but also its instruction builders and cpi functions. Without those, we would have to use low level solana syscalls. Fortunately, anchor provides abstractions on top of those. By enabling the cpi feature, the puppet-master program gets access to the puppet::cpi module. Anchor generates this module automatically and it contains tailor-made instructions builders and cpi helpers for the program.


Import the program b in program a cargo.toml like it is done in pic refer this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.