1

在 pub mod xyz {...} 中移动我的结构后,我的 Rust 代码:

#[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],
        })
    }

我的 Rust 程序可以编译,但 Anchor 没有运行并出现错误:

await program.state.rpc.new({
  accounts: {
    authority: provider.wallet.publicKey,
  },
});

TypeError:无法读取未定义的属性“rpc”

我从本教程 Basic1 中知道:https ://project-serum.github.io/anchor/tutorials/tutorial-1.html#defining-a-program ,它说“如果您想将自己的类型传递为一个指令处理程序的输入,那么它必须在与 #[program] 模块相同的 src/lib.rs 文件中定义,以便 IDL 解析器可以接收它。”,以及关于状态结构的教程 Basic4...

此外,如果我将 Ppool 结构移到 mod 之外,Rust 将编译,但 Anchor 会说“错误:未提供用户定义的类型”......所以我认为 Ppool 结构应该留在 mod 内

但是在将我的 Ppool 结构移动到 mod 之后,我的 Ppool 结构不能有 #[state] ...请参阅以下错误:

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)

我应该在 mod 中为我的 Ppool 结构添加什么样的宏?

4

1 回答 1

1

通过将数组放入结构数组来解决...请参阅 Anchor repo > tests > zero-copy

于 2021-09-15T09:38:03.980 回答