首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Solana Anchor如何定义带有结构数组的自定义类型?

Solana Anchor如何定义带有结构数组的自定义类型?
EN

Stack Overflow用户
提问于 2021-08-13 14:49:49
回答 1查看 2.4K关注 0票数 1

在将我的结构移动到pub mod xyz {.}之后,我的锈蚀代码:

代码语言:javascript
复制
#[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没有错误地运行:

代码语言:javascript
复制
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文件中定义它,这样IDL解析器就可以获取它。”

此外,如果我将Ppool结构移出mod之外,Rust将编译,但是Anchor会说"Error:用户定义的类型未提供“.所以我认为Ppool结构应该留在国防部内部

但是在将我的Ppool结构移动到mod之后,我的Ppool结构就不能有#状态.见以下错误:

代码语言:javascript
复制
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结构添加什么样的宏?

EN

回答 1

Stack Overflow用户

发布于 2021-09-15 09:38:03

通过将数组放入结构数组来解决.请参阅Anchor repo >test>零副本

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68774356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档