2
use anchor_lang::prelude::*;
use anchor_spl::token::TokenAccount;

#[derive(Accounts, Clone)]
pub struct InstructionTest<'info> {
    pub account: Account<'info, TokenAccount>,
}

pub fn test(ctx: Context<InstructionTest>) {
    let account_info = ctx.accounts.account.to_account_info();
    let unchecked_account = UncheckedAccount::try_from(&account_info); // report error
}

account_info does not live long enough

1

1 Answer 1

0

Let me see if I can explain it correctly. According to anchor_lang doc, below is definition of UncheckedAccount

#[derive(Debug, Clone)]
pub struct UncheckedAccount<'info>(&'info AccountInfo<'info>);

So according to this, the lifetime of param passed (AccountInfo) should match the lifetime of object UncheckedAccount.

Lets see the below code

pub fn test(ctx: Context<InstructionTest>) {
  let account_info = ctx.accounts.account.to_account_info();
  let unchecked_account = UncheckedAccount::try_from(&account_info);//report error
}

The lifetime of account_info is the lifetime of function which calls test function. So we can assume lifetime of ctx is longer than the test function at the minimum. The variable unchecked_account will die at the end of the function test, so unchecked_account and account_info lifetimes do not match

But according to the definition of UncheckedAccount the lifetime of inner param should match the lifetime of UncheckedAccount, so this gives an error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.