After moving my structs inside the pub mod xyz {...}, My Rust code:
#[state]
#[derive(Default)]
#[derive(Copy)]//index as pid
pub struct Ppool {
pub alloc_point: u64,
}
#[state]
pub struct Counter {
pub authority: Pubkey,
pub count: u64,
pub pool_array: [Ppool; 20],
}
impl Counter {
pub fn new(ctx: Context<Auth>) -> Result<Self> {
Ok(Self {
authority: *ctx.accounts.authority.key,
count: 0,
pool_array: [
Ppool {
alloc_point: 0,
}; 20],
})
}
My Rust program can compile, but Anchor does not run with error:
await program.state.rpc.new({
accounts: {
authority: provider.wallet.publicKey,
},
});
TypeError: Cannot read property 'rpc' of undefined
I know from this tutorial Basic1: https://project-serum.github.io/anchor/tutorials/tutorial-1.html#defining-a-program, it says "If you'd like to pass in your own type as an input to an instruction handler, then it must be defined in the same src/lib.rs file as the #[program] module, so that the IDL parser can pick it up.", and tutorial Basic4 about state structs...
Furthermore, if I move the Ppool struct outside of the mod, the Rust will compile, but Anchor will say "Error: User defined types not provided"... So I think the Ppool struct should stay inside the mod
but after moving my Ppool struct into the mod, my Ppool struct cannot have #[state] ... see the errors below:
error[E0277]: the trait bound Ppool: Clone is not satisfied
--> programs/basic-4/src/lib.rs:11:14
|
11 | #[derive(Copy)]//index as pid
| ^^^^ the trait Clone is not implemented for Ppool
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound Ppool: anchor_lang::AnchorDeserialize is not satisfied
--> programs/basic-4/src/lib.rs:16:5
|
16 | #[state]
| ^^^^^^^^ the trait anchor_lang::AnchorDeserialize is not implemented for Ppool
|
= note: required because of the requirements on the impl of anchor_lang::AnchorDeserialize for [Ppool; 20]
= help: see issue #48214
= help: add #![feature(trivial_bounds)] to the crate attributes to enable
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound Ppool: anchor_lang::AnchorSerialize is not satisfied
--> programs/basic-4/src/lib.rs:16:5
|
16 | #[state]
| ^^^^^^^^ the trait anchor_lang::AnchorSerialize is not implemented for Ppool
|
= note: required because of the requirements on the impl of anchor_lang::AnchorSerialize for [Ppool; 20]
= help: see issue #48214
= help: add #![feature(trivial_bounds)] to the crate attributes to enable
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
What kind of macro should I add for my Ppool struct inside the mod?
Solved by putting arrays into an array of structs... please see Anchor repo > tests > zero-copy