【发布时间】:2021-08-13 14:49:49
【问题描述】:
在 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 知道,它说“如果您想将自己的类型作为输入传递给指令处理程序,那么它必须在同一个 src/lib.rs 文件中定义作为 #[program] 模块,以便 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 结构添加什么样的宏?
【问题讨论】:
-
您是在程序模块之外(在全局范围内)定义这些帐户吗?
-
嗨,将结构体移入模组后,Rust 可以编译,但 Anchor 提示“TypeError: Cannot read property 'rpc' of undefined”,如何调用结构体方法?
-
所以我必须将这些结构体移到 mod 中,但我的 Ppool 结构体不能有 #[state] 或 Anchor 的 idl 没有显示任何方法,因此 rpc 调用失败。我应该为我的 Ppool 结构添加什么样的宏?