0

if i use remaining accounts that are sent from anchor js client in rust anchor program I am getting this error

lifetime may not live long enough requirement occurs because of the type anchor_lang::prelude::Account<'_, anchor_spl::token::TokenAccount>, which makes the generic argument '_ invariant the struct anchor_lang::prelude::Account<'info, T> is invariant over the parameter 'info

This is my program code -

pub fn transfer_tokens<'info>(ctx: Context<TransferTokens<'info>>, token_amount: u64, bump: u8) -> Result<()> {
        let authority = ctx.accounts.authority.to_account_info();
        let sender_tokens = &ctx.accounts.from_token_account;
        // let recipient_tokens = &ctx.accounts.to_token_account;
        let token_program = &ctx.accounts.token_program;
        let seeds: &[&[u8]] = &[
            b"authority",
            &[bump]
        ];  
        let signer_seeds:&[&[&[u8]]] = &[&seeds[..]];
        //recepient ATA is collected from remaining_accounts send from client 
        let toata:&AccountInfo<'info>= &ctx.remaining_accounts[1];
        
        token::transfer(
            CpiContext::new_with_signer(
                token_program.to_account_info(),
                token::Transfer {
                    from: sender_tokens.to_account_info(),
                    to: toata.to_account_info(),
                    authority: authority,
                },
                signer_seeds
            ),
            token_amount,
        )?;
        Ok(())
   }

This is anchor js code -

const tx = await program.methods.transferTokens(new BN(87),Bump)
                .accounts({
                      fromTokenAccount: fromAta.address,
                      tokenProgram:TOKEN_PROGRAM_ID,
                      toTokenAccount: toAta,
                      authority: PDA
                })
                .remainingAccounts([{
                      pubkey: fromAta.address,
                      isWritable: true, // Set to true if the account should be writable
                    isSigner: false, // Set to true if the account is a signer
                  },
                  {
                    pubkey: toAta,
                    isWritable: true, // Set to true if the account should be writable
                    isSigner: false, // Set to true if the account is a signer
                  }
              ])
              .signers([])
              .rpc({});

How can I solve this lifetime error ? Any help will be much appreciated :))

1 Answer 1

3

You can solve this error by adding explicit lifetime annotions:

pub fn transfer_tokens<'c: 'info, 'info>(
    ctx: Context<'_, '_, 'c, 'info, TransferTokens>,
    token_amount: u64,
    bump: u8
) -> Result<()> {

This is currently required for remaining_accounts usage in anchor-lang 0.29.0.

Note: The error is a Rust compile-time error so the JS code you've shared is irrelevant to the problem.

Example

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.