coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.61k stars 1.32k forks source link

is it possible to use Custom structure as Parameter in Program? #1710

Closed DedicatedDev closed 2 years ago

DedicatedDev commented 2 years ago

As you can see following code, I've use custom struct as Parameter. It has no any issue in build. but if I try to test, it is shows error message. IdlError: Type not found: {"type":{"defined":"Candidate"}} so node_modules/@project-serum/anchor/src/coder/borsh/idl.ts:94:19) I've checked this part, and this module check type of parameters. looks don't support custom data type.
pub fn register_candidate(ctx: Context, candidate:Candidate) -> Result<()> { let vote_account = &mut ctx.accounts.vote_account; if vote_account.candidates.contains(&candidate) { Err(ErrorCode::AlreadyRegistered.into()) } else { vote_account.candidates.push(candidate); Ok(()) } }

[account]

[derive(PartialEq)]

pub struct Candidate { pub name: String, pub age: String, pub photo: String, }

is someone there to give me advice? Best.

SailorSnoW commented 2 years ago

It's because the IDL take your Candidate struct as an account cause you have the attribute #[account] and not as a type.

Try to create another struct and just derive it with #[derive(AnchorSerialize, AnchorDeserialize, Clone, PartialEq)] so the IDL take it as a type and then you can use it correctly as a parameter

fanatid commented 2 years ago

Please use discord for questions https://discord.gg/GVRw4kHB