3

I am trying to create a PDA with variable seeds like this

#[derive(Accounts)]
#[instruction(i: u8)]

pub struct SendRequest<'info> {
    #[account(mut)]
    pub owner: Signer<'info>,

    #[account(mut,seeds= ["client".as_ref(), owner.key().as_ref()],bump)]
    pub client: AccountLoader<'info, Client>,

    #[account(
        init,
        payer = owner,
        space = 200,
        seeds= ["request".as_ref(),owner.key().as_ref(),client.key().as_ref(),&[i]],
        bump
    )]
    pub request: AccountLoader<'info, RequestData>,

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

i'm using this account here

 pub fn send_request(ctx: Context<SendRequest>, request: Request, i: u8) -> Result<()> {
        let s = request.name();

        let name = String::from_utf8(s.to_vec()).unwrap();
        if validate_name(&name.as_bytes()) == false {
            return err!(AgentError::InvalidName);
        }
        let req = &mut ctx.accounts.request.load_init()?;
        req.requester = ctx.accounts.owner.to_account_info().key();
        req.request = request;
        Ok(())
    }

and i am finding this seed by doing this

const request = await anchor.web3.PublicKey.findProgramAddressSync(
      [
        Buffer.from("request"),
        program.provider.publicKey.toBuffer(),
        client.toBuffer(),
        //@ts-ignore
        [1]
      ],
      program.programId
    )[0];

Apparently this works for [0] but does not work for anything else like [1] or so

still i'm getting seed constraint violated error if i try in solana playground and signer's privillage escalated if i try doing anchor test.

1
  • apparently this works for [0], but not for [1] or anything else Commented Jan 6, 2023 at 17:04

3 Answers 3

3

You are accepting two arguments in send_request function: request: Request, i: u8

But only mentioned one argument in the instruction: #[instruction(i: u8)]

Replace the above with #[instruction(request: Request, i: u8)] and your program will work as intended.

0
1

You can replace #[instruction(i: u8)] with #[instruction(request: Request, i: u8)] or simply change send_request function parameters arrangement to i: u8, request: Request

0

Change [request_id] to &request_id.to_le_bytes()

Here is a reference:

use anchor_lang::prelude::*;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
pub mod pda {
    use super::*;

    pub fn initialize(ctx: Context<Initialize>, id: u8) -> Result<()> {
        Ok(())
    }
}

#[derive(Accounts)]
#[instruction(id: u8)]
pub struct Initialize<'info> {
    #[account(
        init,
        payer = signer,
        space = 8 + 8,
        seeds= ["seed".as_ref(), signer.key().as_ref(), &id.to_le_bytes()],
        bump
    )]
    pub pda: Account<'info, Data>,
    #[account(mut)]
    pub signer: Signer<'info>,

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

#[account]
pub struct Data {
    pub data: u64,
}

Also change [1] to Buffer.from([1])

Here is a reference:

describe("pda", () => {
  // Configure the client to use the local cluster.
  anchor.setProvider(anchor.AnchorProvider.env())

  const program = anchor.workspace.Pda as Program<Pda>

  const PDA = anchor.web3.PublicKey.findProgramAddressSync(
    [
      Buffer.from("seed"),
      program.provider.publicKey.toBuffer(),
      Buffer.from([1]),
      ,
    ],
    program.programId
  )[0]

  it("Is initialized!", async () => {
    // Add your test here.
    const tx = await program.methods.initialize(1).accounts({ pda: PDA }).rpc()
    console.log("Your transaction signature", tx)
  })
})
3
  • Still the same error, 5kqCiAbbtQfpvygEfzZqZyQDbYLj89Jgt2LVRKQdh3nS's signer privilege escalated Commented Jan 6, 2023 at 16:53
  • Can you share the instruction the pda is used in? which account is that public key for?
    – john
    Commented Jan 7, 2023 at 0:40
  • just edited the question. Commented Jan 7, 2023 at 5:18

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.