Testing CPI calls on devnet (e.g. programs both deployed to devnet, anchor.toml set to devnet) yields this error:
'Unknown program Ev6JrN5HqrKwXhoB9jucLdn51yzzDvWmBHkubXWavRio',
'Program <theCpiCaller> consumed 27039 of 200000 compute units',
'Program <theCpiCaller> failed: An account required by the instruction is missing'
Where the CPI gets invoked like this:
#[derive(Accounts)]
pub struct DummyMsgCpi<'info> {
/// CHECK: Checked by dummy program
pub dummy_acc: UncheckedAccount<'info>,
}
pub fn handler(ctx: Context<DummyMsgCpi>) -> Result<()> {
let program_id: Pubkey =
Pubkey::from_str("Ev6JrN5HqrKwXhoB9jucLdn51yzzDvWmBHkubXWavRio").unwrap();
invoke(
&Instruction {
program_id,
accounts: vec![AccountMeta::new_readonly(
ctx.accounts.dummy_acc.key(),
false,
)],
data: get_function_hash("global", "msg").to_vec(),
},
&[ctx.accounts.dummy_acc.to_account_info()],
)?;
Ok(())
}
pub fn get_function_hash(namespace: &str, name: &str) -> [u8; 8] {
let preimage = format!("{}:{}", namespace, name);
let mut hash = [0u8; 8];
hash.copy_from_slice(
&anchor_lang::solana_program::hash::hash(preimage.as_bytes()).to_bytes()
[..8],
);
hash
}
And program Ev6JrN5HqrKwXhoB9jucLdn51yzzDvWmBHkubXWavRio
looks like this for reference:
#[derive(Accounts)]
pub struct Msg<'info> {
/// CHECK: no security
pub dummy_acc: UncheckedAccount<'info>,
}
pub fn handler(ctx: Context<Msg>) -> Result<()> {
msg!("You sent: {:?}", ctx.accounts.dummy_acc.key());
Ok(())
}
Best guess is that's a problem generating the correct discriminator