[英]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.