【问题标题】:Solana Anchor Rust: How to convert a public key into an AccountInfo typeSolana Anchor Rust:如何将公钥转换为 AccountInfo 类型
【发布时间】:2021-08-31 10:36:36
【问题描述】:

我知道我可以通过 Context 结构为我的 Solana Rust 程序提供一个用户的令牌帐户,如 Anchor 教程 2 所示:https://project-serum.github.io/anchor/tutorials/tutorial-2.html#defining-a-program

#[derive(Accounts)]
pub struct Stake<'info> {
  pub user_reward_token_account: CpiAccount<'info, TokenAccount>,
  ...
}

但是如果我希望用户先将该用户的令牌帐户保存在某个用户的存储帐户中,然后让我的 Solana 程序从该用户的存储帐户中获取这些令牌帐户呢?

let user_acct = &ctx.accounts.user_acct;

然后在尝试向用户的代币账户铸造一些奖励代币时:

let cpi_accounts = MintTo {
  mint: ctx.accounts.reward_mint.to_account_info(),
  to: user_acct.reward_user,
  authority: ctx.accounts.pg_signer.clone()
};

编译时出错:预期结构anchor_lang::prelude::AccountInfo,找到结构anchor_lang::prelude::Pubkey

但是在anchor_lang::prelude::Pubkey中找不到这个to_account_info()方法

我查看了 Pubkey 文档:https://docs.rs/anchor-lang/0.13.2/anchor_lang/prelude/struct.Pubkey.html

但它并没有说明 AccountInfo ...

然后我尝试在https://docs.rs/anchor-lang/0.13.2/anchor_lang/prelude/struct.AccountInfo.html 的帮助下从reward_user 地址创建一个AccountInfo 结构体:

let to_addr = AccountInfo {
  key: &user_acct.reward_user,
  is_signer: false,
  is_writable: true,
  lamports: Rc<RefCell<&'a mut u64>>,
  data: Rc<RefCell<&'a mut [u8]>>,
  owner: &user_pda.user_acct,
  executable: false,
  rent_epoch: u64,
};

但这真的很难,我不知道lamports,data,rent_epoch值是什么......

那么如何将公钥转换为 AccountInfo 类型?

【问题讨论】:

    标签: rust solana


    【解决方案1】:

    您需要通过上下文传递帐户才能访问其数据。这种设计允许 Solana 通过在运行前知道需要哪些帐户和数据来更好地并行化交易。

    【讨论】:

    • 我该怎么做?
    • 能否提供更多信息?这没什么用
    • 质押示例在这里 - github.com/paramsNFT/nft_staking
    • 但是如果账户地址不存在并且是在链上生成的呢?
    猜你喜欢
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 2015-10-19
    • 2021-08-07
    • 2014-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多