Andrii Kapinos
Andrii Kapinos

Reputation: 195

Is it possible to call any Solana instruction on the blockchain via CPI?

I have done CPI instrucion calls before, every time the Solana program had a github repo, which had a function to create an instruction. But recently I've bumped into a situation where there is a repo, but they provide nothing to do a CPI call, on their discord server they said that they don't expect any call from on-chain programs, only from clients. General question, is it possible to call any instruction from my on-chain program? For example by duplicating all the structs, and serializing them to binary form before invoking it?

Upvotes: 0

Views: 675

Answers (1)

Jon C
Jon C

Reputation: 8472

You can absolutely call any program from a CPI. You'll need to reverse-engineer the accounts and data expected by the program, and then call it like any other program, ie:

let encoded_instruction_data = [1, 0, 0, 0, 10]; // you'll need to reverse-engineer these
let account_metas = vec![
    AccountMeta::new(account1_info.key, false),
    AccountMeta::new_readonly(account2_info.key, false),
]; // you'll also need to reverse-engineer these based on working transactions
let instruction = Instruction::new_with_bytes(program_id, &encoded_instruction_data, account_metas);
invoke(&instruction, [account1_info.clone(), account2_info.clone()])

The only exception is if the program does instruction introspection to make sure that it isn't in a CPI context, but very few programs do this.

Upvotes: 2

Related Questions