简体   繁体   English

Solana Anchor 中的嵌套帐户(可能是帐户中的自定义数据类型)

[英]Nested Accounts in Solana Anchor (Maybe a custom data types in account)

I would like to achieve a linked list type data structure in my anchor program.我想在我的锚程序中实现一个链表类型的数据结构。 However I can't bring myself to understand how I can use nested account.但是我无法让自己理解如何使用嵌套帐户。 Following is the code I am using:以下是我正在使用的代码:

#[derive(Accounts)]
pub struct Create<'info> {
    #[account(init, payer = user, space = 8 + 32 + 8 + 8 + 32 )]
    pub endpoint: Account<'info, Endpoint>,
    #[account(mut)]
    pub user: Signer<'info>,
    pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct Update<'info> {
    pub authority: Signer<'info>,
    #[account(mut, has_one = authority)]
    pub endpoint: Account<'info, Endpoint>,
}

#[account]
pub struct Endpoint {
    pub authority: Pubkey,
    pub data: u64,
    pub len: u64,
    pub next: Endpoint
}

impl Endpoint {
    pub fn push(&mut self, data:u64){
        if self.len  == 0 {
            self.data = data;
        } else {
            // Recursion to push data into the next node
        }
        self.len += 1;
    }
}

What I want to achieve is that an account named Endpoint has a parameter named 'next' which stores another Endpoint account.我想要实现的是一个名为 Endpoint 的帐户有一个名为“next”的参数,它存储另一个 Endpoint 帐户。

No, you can't do that.不,你不能那样做。

You can't query an account in the on-chain.您无法查询链上的帐户。 You can only read an account only if you put the account inside your instruction.只有将帐户放入指令中,您才能阅读帐户。 In Anchor, the account you can interact with is the Account inside the Context .在 Anchor 中,您可以与之交互的帐户是Context中的帐户。

Another way, you can allocate a data account that stores an array of struct.另一种方式,您可以分配一个存储结构数组的数据帐户。 So you can read all those structs inside the array.因此,您可以读取数组中的所有这些结构。 Don't forget that the data account size can't be adjusted, so make sure you allocate it as much as you need.不要忘记数据帐户大小无法调整,因此请确保根据需要进行分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Solana Anchor:如何为/读取关联帐户创建#[account(seeds)]? - Solana Anchor: How to make #[account(seeds)] for/ read associated accounts? Solana Anchor 如何使用结构数组定义自定义类型? - Solana Anchor how to define custom types with array of structs? 锚点:Solana 帐户初始化问题 - Anchor: Solana Account Initialization Issue 在 Solana 帐户中存储数据的问题 - Problems in storing data in Solana Account 如何在具有多个帐户的 Solana 程序中引用特定帐户? - How to reference a specific account in Solana programs with multiple accounts? Solana 锚点错误:发送交易失败:交易无效:交易未能正确清理账户偏移 - Solana Anchor Error: failed to send transaction: invalid transaction: Transaction failed to sanitize accounts offsets correctly solana rpc get_program_accounts_with_config return Tokenkeg... 从账户二级索引中排除 - solana rpc get_program_accounts_with_config return Tokenkeg... excluded from account secondary indexes 索拉纳计划。 失败:无法序列化或反序列化帐户数据:未知&#39; - Solana program. failed: Failed to serialize or deserialize account data: Unknown' 将 Solana 空投到特定帐户 - Airdropping Solana to a Specific Account 在这个 Solana 智能合约中,在输入指令和账户数据之间应用 XOR 有什么作用? - What does applying XOR between the input instructions & account data accomplish in this Solana smart contract?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM