簡體   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