1

I am trying to unwrap SOL from a WSOL account and then initialize the token account again.

I am creating the account like so

   pub authority_pda: UncheckedAccount<'info>,                                                                                                                 
   #[account(                                                                                                                                                  
       init,                                                                                                                                                   
       payer = payer,                                                                                                                                          
       token::mint = mint,                                                                                                                                     
       token::authority = authority_pda,                                                                                                                       
   )]                                                                                                                                                          
   pub token_account: Account<'info, TokenAccount>,    

To unwrap I close the account

 anchor_spl::token::close_account(program_unwrap_wsol_context.with_signer(&[&[AUTH_SEED.as_bytes(), &[bump]][..]]))?;

Then I transfer part of the unwrapped amount (didnt include code). Then I transfer the rent amount to to the WSOL account I closed earlier.

anchor_lang::system_program::transfer(program_to_program_account_context.with_signer(&[&[AUTH_SEED.as_bytes(), &[bump]][..]]), account_rent)?;

And then I try to recreate the token account.

let program_open_wsol_account_context = CpiContext::new(
    ctx.accounts.token_program.to_account_info(),
    anchor_spl::token::InitializeAccount3 {
        account: ctx.accounts.intermediate_wsol_account.to_account_info(),
        mint: ctx.accounts.wsol_mint.to_account_info(),
        authority: ctx.accounts.intermediate_authority.to_account_info()
    }
);
   
anchor_spl::token::initialize_account3(program_open_wsol_account_context.with_signer(&[&[AUTH_SEED.as_bytes(), &[bump]][..]]))?;

Everything works except for the initialize_account3 instruction. I get this error in the logs.

"Program log: Instruction: InitializeAccount3",
"Program log: Error: InvalidAccountData",

What am I doing wrong?

1 Answer 1

1

Before initializing any account, you need to "create" it, meaning you system_instruction::allocate 165 bytes and system_instruction::assign it to the token program.

3
  • Thanks for the response Jon. So to clarify the allocate and assign can be invoked by anyone, ie dont need any signer seeds?
    – guccimane
    Commented Jun 23, 2023 at 21:06
  • When you create an acc with system program you generate a keypair to represent that account. The private key signs, and is typically discarded after that tx. So tl;dr you shouldn't need to manually specify any additional signers.
    – Whiteseal
    Commented Jun 23, 2023 at 22:58
  • Hello @Whiteseal, I am creating the account using a program PDA so think that I do?
    – guccimane
    Commented Jun 23, 2023 at 23:16

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.