1

I have problem of borrowing accounts mutably from remaining accounts. Not exactly how to use AccountInfo, as error says that accounts does not live long enough. I am using Anchor 0.29, Solana-Program 0.17.4. Previously it was working, after upgrade AccountInfo started falling with lifetime issue. Tried a bunch of stuff even tried to use std::mem::replace accounts, but nothing helped so far. This is the function I am trying to fix, I know it does not do anything meaningful yet, but I am trying to fix lifetime issue here:

fn swap_spl<'info>(
    authority: &AccountInfo<'info>,
    remaining_accounts: &mut Iter<AccountInfo<'info>>,
    scratch: &mut Scratch,
) -> Result<AccountInfo<'info>> {     
    let accounts: Vec<AccountInfo<'info>> = remaining_accounts.take(7).cloned().collect::<Vec<_>>();
    SplSwapInfo::try_accounts(
        // Will be validated by the spl swap registry
        &Pubkey::default(),
        &mut &accounts[..],
        &[],
        &mut SplSwapInfoBumps::default(),
        &mut scratch.reallocs,
    )?;

    Ok(authority.to_account_info())
}

#[derive(Accounts)]
pub struct SplSwapInfo<'info> {
    /// CHECK:
    pub swap_pool: AccountInfo<'info>,

    /// CHECK:
    pub authority: AccountInfo<'info>,

    /// CHECK:
    #[account(mut)]
    pub vault_a: AccountInfo<'info>,

    /// CHECK:
    #[account(mut)]
    pub vault_b: AccountInfo<'info>,

    /// CHECK:
    #[account(mut)]
    pub token_mint: AccountInfo<'info>,

    /// CHECK:
    #[account(mut)]
    pub fee_account: AccountInfo<'info>,

    /// The address of the swap program
    /// CHECK:
    pub swap_program: AccountInfo<'info>,
}
  1. I tried using 'a:'info with &'a AccountInfo<'info> and even wise versa, does not help.
  2. Tried std::mem::replace accounts, does not help.
  3. Tried removing 'info lifetime at all, does not help.

The error itself is:

 error[E0597]: `accounts` does not live long enough
    |
    | fn swap_spl<'info>(
    |             ----- lifetime `'info` defined here
...
    |     let accounts: Vec<AccountInfo<'info>> = remaining_accounts.take(7).cloned().collect::<Vec<_>>();
    |         --------  ----------------------- type annotation requires that `accounts` is borrowed for `'info`
    |         |
    |         binding `accounts` declared here
...
    |         &mut &accounts[..],
    |               ^^^^^^^^ borrowed value does not live long enough
...
    | }
    | - `accounts` dropped here while still borrowed
7
  • BTW I tried using Rc::RefCell. Does not help though. I saw that AccountInfo uses it a lot. For example for data field. Commented Jul 4 at 17:14
  • I think I found the issue, but not sure why it was changed and how to fix it. The issue is in the instructions::spl_token_swap::SplSwapInfo fn try_accounts(__program_id: &anchor_lang::solana_program::pubkey::Pubkey, __accounts: &mut &'info [anchor_lang::solana_program::account_info::AccountInfo<'info>], __ix_data: &[u8], __bumps: &mut SplSwapInfoBumps, __reallocs: &mut std::collections::BTreeSet<anchor_lang::solana_program::pubkey::Pubkey>) -> anchor_lang::Result<Self> Lifetime of the __accounts. I can not change signature of the Accounts::try_accounts, so I don't know the fix. Commented Jul 4 at 19:45
  • 1
    This question is similar to: Lifetime error after upgrade of Anchor and Solana. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.
    – acheron
    Commented Jul 5 at 10:49
  • Sorry for mixing things probably, but it is not related to the Context or instruction handler. It specifically problem when using with Accounts::try_accounts, at least in my case. Commented Jul 5 at 11:57
  • The point is that try_accounts function signature is similar to: fn mock<'info>(acc: &'info [AccountInfo<'info>]) { for item in acc { println!("mock {:?}", item); } } which actually fails, but if you will make it like: fn mock<'info>(acc: &[AccountInfo<'info>]) { for item in acc { println!("mock {:?}", item); } } then everything works. So extra 'info lifetime breaks it. Commented Jul 5 at 12:29

0

Your Answer

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