繁体   English   中英

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

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

我想在我的锚程序中实现一个链表类型的数据结构。 但是我无法让自己理解如何使用嵌套帐户。 以下是我正在使用的代码:

#[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;
    }
}

我想要实现的是一个名为 Endpoint 的帐户有一个名为“next”的参数,它存储另一个 Endpoint 帐户。

不,你不能那样做。

您无法查询链上的帐户。 只有将帐户放入指令中,您才能阅读帐户。 在 Anchor 中,您可以与之交互的帐户是Context中的帐户。

另一种方式,您可以分配一个存储结构数组的数据帐户。 因此,您可以读取数组中的所有这些结构。 不要忘记数据帐户大小无法调整,因此请确保根据需要进行分配。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM