2

That is what am i doing, transferring my NFTs to TokenAccount with authority as stake_account which is a PDA. How can I tranfer NFTs back to a certian wallet in anchor-lang.

use anchor_spl::token::{ Token, TokenAccount, Transfer }

Token Account:

    #[account(
        init_if_needed,
        seeds = [mint.key().as_ref(), b"nfts_vault_seed", from.key().as_ref()],
        bump,
        //space = 82,
        payer = from, 
        token::mint = mint,
        token::authority = stake_account, //THIS authority is PDA  
    )]
    pub nfts_vault: Account<'info, TokenAccount>,

Transfer NFT to TokenAccount:

token::transfer(
       CpiContext::new(
          ctx.accounts.token_program.to_account_info(),
          token::Transfer {
            from: ctx.accounts.from.to_account_info(),
            to: ctx.accounts.nfts_vault.to_account_info(),
            authority: ctx.accounts.from.to_account_info(),
          }
       ), 1
)?;

1 Answer 1

1

Here's a code snippet to transfer tokens from a token account with PDA authority:

let config_bump = ctx.accounts.config.bump;
let config_seeds = &[
    "config".as_bytes().as_ref(),
     ctx.accounts.config.admin.as_ref(),
     &[config_bump]
];
let signer = &[&config_seeds[..]];
        
anchor_spl::token::transfer(
    CpiContext::new(
        ctx.accounts.token_program.to_account_info(),
        Transfer {
            from: ctx.accounts.staking_pool.to_account_info(),
            to: ctx.accounts.staker_token_account.to_account_info(),
            authority: ctx.accounts.config.to_account_info(),
         }
    ).with_signer(signer),
    1
)?;

Here a transfer is being made from staking_pool account to staker_token_account. Since staking_pool is owned by a PDA(config) which can't sign client-side, we add .with_signer() to our CpiContext.

Our signer is derived from the seeds and bump of the config account.

2
  • Thanks, buddy. Can you share your config PDA snippet? My PDA does not have to_account_info(). Commented Dec 5, 2022 at 8:49
  • Thanks a lot, buddy. I redeployed the Solana Program with your provided solution and all problems were solved. Commented Dec 5, 2022 at 12:33

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.