Anchor CPI
Cross Program Invocations (CPIs) allow one program to invoke instructions on another program.
The process of implementing a CPI is the same as that of creating a instruction where you must specify: 1. The program ID of the program being called 2. The accounts required by the instruction 3. Any instruction data required as arguments
This pattern ensures the CPI has all the information needed to invoke the target program's instruction.
The System Program's transfer instruction requires two accounts:
`from`: The account sending SOL.
`to`: The account receiving SOL.
Anchor CPI
invoke_signed()
Setup
1. anchor init program-a cd program-b
2. anchor new program-b
3. Cargo.toml (program-a) add dependencies to program-b -> program-b = {path = "../program-b", features= ["cpi"]}
4. Anchor.toml - set resolution to false
Run tests
anchor build && anchor test
Start Local Validator
cd .anchor/
solana-test-validator
Explorer
- open explorer.solana
- switch to localhost
- find test output transaction signature
Expected Output
> Program logged: "Instruction: Initialize"
> Program logged: "Greetings from: Program A"
> Program invoked: System Program
> Program returned success
> Program invoked: Unknown Program (6grzSV1SpnQjt9Jf7cMtqQDUqaC7SZDXvKE5GCGrVHxZ)
> Program logged: "Instruction: Initialize"
> Program logged: "Greetings from: Progrma B"
> Program consumed: 676 of 190087 compute units
> Program returned success
> Program consumed: 10862 of 200000 compute units
> Program returned success
Read other posts?