I am trying to build a web3 based e-commerce site using Anchor.
I've just started learning about PDAs and there's a error I've been getting for hours, like the one in the title.
My contract:
#[program]
pub mod dailsap_store_contract {
use super::*;
pub fn create_collection(
ctx: Context<CreateCollection>,
name: String,
description: String,
image_uri: String,
) -> Result<()> {
let collection: &mut Account<Collection> = &mut ctx.accounts.collection;
let authority: &Signer = &ctx.accounts.authority;
let clock: Clock = Clock::get().unwrap();
let bump = *ctx.bumps.get("collection").unwrap();
if name.chars().count() > 50 {
return Err(ErrorCode::CollectionNameTooLong.into());
}
if description.chars().count() > 250 {
return Err(ErrorCode::CollectionDescriptionTooLong.into());
}
if image_uri.chars().count() > 60 {
return Err(ErrorCode::CollectionImageUrlTooLong.into());
}
collection.authority = *authority.key;
collection.timestamp = clock.unix_timestamp;
collection.name = name;
collection.description = description;
collection.image = image_uri;
collection.bump = bump;
Ok(())
}
pub fn update_collection(
ctx: Context<UpdateCollection>,
name: String,
description: String,
image_uri: String,
) -> Result<()> {
let base_collection: &mut Account<Collection> = &mut ctx.accounts.collection_account;
base_collection.name = name;
base_collection.description = description;
base_collection.image = image_uri;
Ok(())
}
}
#[derive(Accounts)]
pub struct CreateCollection<'info> {
#[account(mut)]
pub authority: Signer<'info>,
#[account(init, payer=authority, space = Collection::LEN, seeds=[b"collection", collection.key().as_ref()], bump)]
pub collection: Account<'info, Collection>,
#[account(address = system_program::ID)]
pub system_program: Program<'info, System>,
}
#[account]
pub struct Collection {
authority: Pubkey,
timestamp: i64,
name: String,
description: String,
image: String,
bump: u8,
}
Frontend:
const collection = anchor.web3.Keypair.generate();
const [collectionPDA, _] = await anchor.web3.PublicKey.findProgramAddress(
[
anchor.utils.bytes.utf8.encode("collection"),
collection.publicKey.toBuffer(),
],
program.programId
);
await program.methods
.createCollection(
"This is collection name",
"This is collection description",
"Hello World"
)
.accounts({
collection: collectionPDA,
authority: anchor.AnchorProvider.env().publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
})
.rpc();
The problem should be here: seeds=[b"collection", collection.key().as_ref()]
The source from which I received help: https://book.anchor-lang.com/anchor_in_depth/PDAs.html
But I'm getting errors in a way I don't understand
can you help?