0

Im starting to receive an error when calling *ctx.bumps.get("account").unwrap(); within a method. But it only happens for some of my methods, I've reviewed my code and I cant seem to see whats going wrong.

When trying to initialize an account for a user. Below is the code I'm using.

#[derive(Accounts)]
pub struct CreateAccount<'info> {
    #[account(mut)]
    pub signer: Signer<'info>,

    #[account(
        init,
        space =  RacerAccount::LEN + 8,
        seeds =[
            b"account".as_ref(),
            signer.key().as_ref(),
        ],
        bump,
        payer = signer,
    )]
    pub racer_account: Account<'info, RacerAccount>,

    pub system_program: Program<'info, System>,
}

pub fn create_account(ctx: Context<CreateAccount>, racer_name: String) -> Result<()> {
    let racer_account = &mut ctx.accounts.racer_account;
    racer_account.name = racer_name;
    racer_account.address = ctx.accounts.signer.key();
    racer_account.level = 0;
    racer_account.bump = *ctx.bumps.get("account").unwrap();

    Ok(())
}

The following is how i'm deriving the address in order to pass it into the program method.

const getAccountPDA = async (
  signer: PublicKey,
  programId: PublicKey = new PublicKey(IDL.metadata.address)
) => {
  return await anchor.web3.PublicKey.findProgramAddress(
    [Buffer.from("account"), signer.toBuffer()],
    programId
  );
};

When I run my tests I am receiving the following error

panicked at 'called Option::unwrap() on a None value'

The error comes from this line racer_account.bump = *ctx.bumps.get("account").unwrap();

To make things more confusing the following method to create a race works fine...

#[derive(Accounts)]
#[instruction(race_name: String)]
pub struct CreateRace<'info> {
    #[account(mut)]
    pub signer: Signer<'info>,
    #[account(
        init,
        space =  Race::LEN + 8,
        seeds =[
            b"race".as_ref(),
            race_name.as_ref(),
        ],
        bump,
        payer = signer,

    )]
    pub race: Account<'info, Race>,

    pub system_program: Program<'info, System>,
}

pub fn create_race(
        ctx: Context<CreateRace>,
        race_name: String,
        entry_fee: u64,
        race_rank: u8,
        race_start: u64,
        race_end: u64,
        number_of_checkpoints: u8,
        estimations: Vec<String>,
    ) -> Result<()> {
        let race = &mut ctx.accounts.race;
        race.name = race_name;
        race.authority = ctx.accounts.signer.key();
        race.entry_fee = entry_fee;
        race.race_rank = race_rank;
        race.race_start = race_start;
        race.race_end = race_end;
        race.registered_racers = 0;
        race.number_of_checkpoints = number_of_checkpoints;
        race.estimations = estimations;
        race.bump = *ctx.bumps.get("race").unwrap();
        Ok(())
    }

PDA derived in my test like so

const getRacePDA = async (
  name: string,
  programId: PublicKey = new PublicKey(IDL.metadata.address)
) => {
  return await anchor.web3.PublicKey.findProgramAddress(
    [Buffer.from("race"), Buffer.from(name)],
    programId
  );
};

If anyone can shed some light as to why the bump doesn't exist in one of my methods but does in the other? I set the seeds, so the context should know about it...

What might I be missing ?

Thanks in advance.

1 Answer 1

0

The problem is caused because when you call *ctx.bumps.get("account").unwrap(); anchor tries to get the calculated bump of an account passed in the context called account while you dont have any account with that name.

In order to fix this you have to replace that piece of code with *ctx.bumps.get("racer_account").unwrap();, because you have an account called racer_account on the context and it has the bump calculated.

1
  • 1
    Many thanks, I didn't understand it wanted the name of the account variable, not the string passed to the seed
    – Duppie
    Aug 23, 2022 at 3:19

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.