4

I know I can give my Solana Rust program an user's token account via a Context struct as shown in Anchor tutorial 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>,
  ...
}

But what if I want users to save that user's token account in certain user's storage account first, then let my Solana program get those token accounts from that user's storage account?

let user_acct = &ctx.accounts.user_acct;

Then when trying to mint some reward tokens to the user's token account:

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

I got an error at compilation: expected struct anchor_lang::prelude::AccountInfo, found struct anchor_lang::prelude::Pubkey

but this to_account_info() method is not found in anchor_lang::prelude::Pubkey

I checked the Pubkey doc: https://docs.rs/anchor-lang/0.13.2/anchor_lang/prelude/struct.Pubkey.html

But it does not say anything about AccountInfo ...

Then I tried to make an AccountInfo struct from the reward_user address with the help of https://docs.rs/anchor-lang/0.13.2/anchor_lang/prelude/struct.AccountInfo.html:

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,
};

But it is really hard and I do not know what the lamports, data, rent_epoch values are...

So how can I convert a public key into AccountInfo type?

4

1 回答 1

3

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

于 2021-08-31T16:19:49.240 回答