anchor icon indicating copy to clipboard operation
anchor copied to clipboard

IDL Build Fails with instruction args not found in scope when using args in seeds

Open ChewingGlass opened this issue 5 months ago • 1 comments

Consider the following instruction:

use anchor_lang::{prelude::*, solana_program::hash::hash};

#[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug)]
pub struct InitializeThingWithNameArgs {
  pub name: String
}

#[account]
#[derive(InitSpace)
pub struct NameThing {
  #[max_len(200)]
  pub name: String
}

#[derive(Accounts)]
#[instruction(args: InitializeThingWithNameArgs)]
pub struct InitializeThingWithName<'info> {
    #[account(mut)]
    pub payer: Signer<'info>,
    #[account(
        init,
        payer = payer,
        space = 8 + NameThing::INIT_SPACE,
        seeds = [
            "name".as_bytes(),
            &hash(&args.name.as_bytes()[..]).to_bytes()
        ],
        bump
    )]
    pub name_pda: Box<Account<'info, NameThing>>,
}

Compiling this will fail with:

error[E0425]: cannot find value `args` in this scope
   |
68 |             &hash(&args.name.as_bytes()[..]).to_bytes()
   |                    ^^^^ not found in this scope
Error: Building IDL failed

Seems like you can't use args inside a function inside seeds when building IDL with resolution?

The following does seem to work:

fn hash_name(name: &str) -> [u8; 32] {
    hash(name.as_bytes()).to_bytes()
}

seeds = [
            "name".as_bytes(),
            &hash_name(args.name.as_str())
 ]

ChewingGlass avatar Aug 29 '24 15:08 ChewingGlass