borrowed value does not live long enough

#[derive(Accounts, Clone)]
pub struct ProxyWithdraw<'info> {
    /**
     * Raydium accounts
     */
    /// CHECK: Safe
    pub amm_program: UncheckedAccount<'info>,
    /// CHECK: Safe. Amm account
    #[account(mut)]
    pub amm: UncheckedAccount<'info>,
    /// CHECK: Safe. Amm authority Account
    #[account()]
    pub amm_authority: UncheckedAccount<'info>,
    /// CHECK: Safe. amm open_orders Account
    #[account(mut)]
    pub amm_open_orders: UncheckedAccount<'info>,
    /// CHECK: Safe. amm target_orders Account. To store plan orders infomations.
    #[account(mut)]
    pub amm_target_orders: UncheckedAccount<'info>,
    /// CHECK: Safe. pool lp mint account. Must be empty, owned by $authority.
    #[account(mut)]
    pub amm_lp_mint: UncheckedAccount<'info>,
    /// CHECK: Safe. amm_coin_vault Amm Account to withdraw FROM,
    #[account(mut)]
    pub amm_coin_vault: UncheckedAccount<'info>,
    /// CHECK: Safe. amm_pc_vault Amm Account to withdraw FROM,
    #[account(mut)]
    pub amm_pc_vault: UncheckedAccount<'info>,
    /// CHECK: Safe. OpenBook program id
    pub market_program: UncheckedAccount<'info>,
    /// CHECK: Safe. OpenBook market Account. OpenBook program is the owner.
    #[account(mut)]
    pub market: UncheckedAccount<'info>,
    /// CHECK: Safe. OpenBook coin_vault Account
    #[account(mut)]
    pub market_coin_vault: UncheckedAccount<'info>,
    /// CHECK: Safe. OpenBook pc_vault Account
    #[account(mut)]
    pub market_pc_vault: UncheckedAccount<'info>,
    /// CHECK: Safe. OpenBook vault_signer Account
    pub market_vault_signer: UncheckedAccount<'info>,
    /// CHECK: Safe. user lp token Account. Source lp, amount is transferable by $authority.
    #[account(mut)]
    pub user_token_lp: UncheckedAccount<'info>,
    /// CHECK: Safe. user token coin Account. user Account to credit.
    #[account(mut)]
    pub user_token_coin: InterfaceAccount<'info, TokenAccount>,
    /// CHECK: Safe. user token pc Account. user Account to credit.
    #[account(mut)]
    pub user_token_pc: UncheckedAccount<'info>,
    /// CHECK: Safe. User wallet account
    #[account(mut)]
    pub user_owner: Signer<'info>,
    /// CHECK: Safe. OpenBook event queue account
    #[account(mut)]
    pub market_event_q: UncheckedAccount<'info>,
    /// CHECK: Safe. OpenBook bid account
    #[account(mut)]
    pub market_bids: UncheckedAccount<'info>,
    /// CHECK: Safe. OpenBook ask account
    #[account(mut)]
    pub market_asks: UncheckedAccount<'info>,
    /**
     * Orbitlen accounts
     */
    #[account(
        mut,
        seeds = [ORBITLEN_ACCOUNT_SEED.as_bytes(), user_owner.key().as_ref()],
        bump,
    )]
    pub orbitlen_account: AccountLoader<'info, OrbitlenAccount>,
    #[account(mut)]
    pub bank: AccountLoader<'info, Bank>,
    /// CHECK: Seed constraint check
    #[account(
        mut,
        seeds = [
            LIQUIDITY_VAULT_SEED.as_bytes(),
            bank.key().as_ref(),
        ],
        bump = bank.load()?.liquidity_vault_bump,
    )]
    pub bank_liquidity_vault: AccountInfo<'info>,
    /// CHECK: Safe. The spl token program
    pub token_program: Program<'info, Token>,
}

/// withdraw instruction
pub fn withdraw_process(
    ctx: Context<'_, '_, '_, '_, ProxyWithdraw<'_>>,
    amount: u64
) -> Result<()> {
    let coin_amount_before = ctx.accounts.user_token_coin.amount;
    msg!("coin_amount_before: {}", coin_amount_before);

    {
        let accounts = ctx.accounts;
        let user_token_coin_ai = accounts.user_token_coin.to_account_info();
        let user_token_coin_ai_clone = user_token_coin_ai.clone();

        let uc = UncheckedAccount::try_from(user_token_coin_ai_clone.as_ref()); // error here

        let cpi_accounts = Withdraw {
            amm: accounts.amm.clone(),
            amm_authority: accounts.amm_authority.clone(),
            amm_open_orders: accounts.amm_open_orders.clone(),
            amm_target_orders: accounts.amm_target_orders.clone(),
            amm_lp_mint: accounts.amm_lp_mint.clone(),
            amm_coin_vault: accounts.amm_coin_vault.clone(),
            amm_pc_vault: accounts.amm_pc_vault.clone(),
            market_program: accounts.market_program.clone(),
            market: accounts.market.clone(),
            market_coin_vault: accounts.market_coin_vault.clone(),
            market_pc_vault: accounts.market_pc_vault.clone(),
            market_vault_signer: accounts.market_vault_signer.clone(),
            user_token_lp: accounts.user_token_lp.clone(),
            user_token_coin: uc,
            user_token_pc: accounts.user_token_pc.clone(),
            user_owner: accounts.user_owner.clone(),
            market_event_q: accounts.market_event_q.clone(),
            market_bids: accounts.market_bids.clone(),
            market_asks: accounts.market_asks.clone(),
            token_program: accounts.token_program.clone(),
        };
        let cpi_program = accounts.amm_program.to_account_info();
        CpiContext::new(cpi_program, cpi_accounts);
    }

    Ok(())
}

console:

error[E0597]: `user_token_coin_ai_clone` does not live long enough
   --> programs/orbit_len/src/instructions/raydium/withdraw.rs:106:45
    |
95  |     ctx: Context<'_, '_, '_, '_, ProxyWithdraw<'_>>,
    |     --- has type `anchor_lang::context::Context<'_, '_, '_, '_, instructions::raydium::withdraw::ProxyWithdraw<'1>>`
...
104 |         let user_token_coin_ai_clone = user_token_coin_ai.clone();
    |             ------------------------ binding `user_token_coin_ai_clone` declared here
105 |
106 |         let uc = UncheckedAccount::try_from(user_token_coin_ai_clone.as_ref());
    |                                             ^^^^^^^^^^^^^^^^^^^^^^^^---------
    |                                             |
    |                                             borrowed value does not live long enough
    |                                             argument requires that `user_token_coin_ai_clone` is borrowed for `'1`
...
132 |     }
    |     - `user_token_coin_ai_clone` dropped here while still borrowed

Related Articles

fetch vault info error: 111115X9BNuV9BeVkKmcU1WzeKpMyRbpMrbiabeots using Raydium AMM `getPoolInfoFromRpc` method

I am getting the following error trying to fetch details about a Raydium AMM pool using the getPoolInfoFromRpc method 1 | var Im=Object.create;var eo=Object.defineProperty,Bm=Object.defineProperties,xm=Object.getOwnPropertyDescriptor,Sm=Object.getOwnPropertyDescriptors,Km=Object.getOwnPropertyNames,tr=Object.getOwnPropertySymbols,Cm=Object.getPrototypeOf,Ms=Object.prototype.hasOwnProperty,Hu=Object.prototype.propertyIsEnumerable;var ju=(i,e,t)=>e…