I'm facing an issue in my instruction trying to generate a PDA from a string and user.key(), just like in the PDA Anchor tutorial. I tried it in various ways and still, I get the same error: Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: custom program error: 0x0
This is my instruction:
#[derive(Accounts)]
pub struct CreateCollection<'info> {
#[account(
init,
payer=user,
space = 8 + 32 + 1,
seeds = [b"user-collection", user.key().as_ref()],
bump
)]
pub collection: Account<'info, Collection>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>,
}
pub fn handler(
ctx: Context<CreateCollection>,
) -> Result<()> {
let collection = &mut ctx.accounts.collection;
let user = &ctx.accounts.user;
collection.user = *user.key;
collection.bump = *ctx.bumps.get("collection").unwrap();
Ok(())
}
#[account]
pub struct Collection {
pub user: Pubkey,
pub bump: u8,
}
And this is my test:
it("Can create a Collection", async () => {
const [collectionPublicKey, _] = web3.PublicKey.findProgramAddressSync(
[Buffer.from("user-collection"), anchorProvider.wallet.publicKey.toBuffer()],
program.programId,
)
await program.methods
.createCollection()
.accounts({
collection: collectionPublicKey,
user: anchorProvider.wallet.publicKey,
systemProgram: web3.SystemProgram.programId,
})
.rpc()
let account = await program.account.collection.fetch(collectionPublicKey)
console.log(JSON.stringify(account, null, 2))
console.log(collectionPublicKey)
});