Search code examples
rustanchorsolanadecentralized-applicationssolana-cli

getttting temporary value dropped while borrow in solana smart contract


pub fn burn_liquidity(ctx: Context<BurnLiquidity>, _to: Pubkey) -> ProgramResult {
        let pool_account = &ctx.accounts.pool_account;
        let token1_account = &ctx.accounts.pool_token1_account;
        let token2_account = &ctx.accounts.pool_token2_account;
        let source = &ctx.accounts.source;
        let (reserve1, reserve2) = (pool_account.token1_balance, pool_account.token2_balance);
        let balance1: u64 = token1_account.amount;
        let balance2: u64 = token2_account.amount;
        let liquidity = source.amount;
        let total_supply = ctx.accounts.mint.supply;
        let amount1 = liquidity as f64 * balance1 as f64 / total_supply as f64;
        let amount2 = liquidity as f64 * balance2 as f64 / total_supply as f64;
        {
            let cpi_program = ctx.accounts.system_program.to_account_info();
            let mut cpi_accounts = UpdatePool {
                pool_account: pool_account.clone(),
            };

            let update_ctx = Context::new(
                cpi_program.key,
                &mut cpi_accounts,
                &[pool_account.to_account_info()],
            );
            let update_data = UpdateData {
                token1: pool_account.token1.to_string(),
                token2: pool_account.token2.to_string(),
                token1_amount: amount1 as u64,
                token2_amount: amount2 as u64,
            };
            update_pool(update_ctx, update_data);
        }

        
        Ok(())
    }

1.i have tried almost all possible way to eliminate this error does someone have any idea how to solve this 2.And this a smart contract on solana blockchain and i have used anchor in this the error i get is: enter image description here


Solution

  •             let update_ctx = Context::new(
                    cpi_program.key,
                    &mut cpi_accounts,
                    &[pool_account.to_account_info()],
                );
    

    update_ctx references the temporary array, thus Context cannot outlive yout temporary array. Binding your array to a variable beforehand will solve the error.

                let acc_info = [pool_account.to_account_info()];
                let update_ctx = Context::new(
                    cpi_program.key,
                    &mut cpi_accounts,
                    &acc_info,
                );