rustsolana

Solana Anchor How to withdraw tokens from program PDA?


I can successfully stake USDC tokens from an user account to a program account, But to withdraw USDC tokens, it fails right after "check4":

  pub fn withdraw_usdc(ctx: Context<UsdcDoge>, amount: u64, nonce: u8) -> ProgramResult {
    let seeds = &[b"xyz".as_ref(), ctx.accounts.usdc_mint.key.as_ref(), &[nonce], ];
    let signer = &[&seeds[..]];
    
    let cpi_accounts = Transfer {
      from: ctx.accounts.usdc_pgid.to_account_info(),
      to: ctx.accounts.usdc_user.to_account_info(),
      authority: ctx.accounts.program_signer.clone(),
    };
    let cpi_program = ctx.accounts.token_program.to_account_info();
    let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer);
    
    token::transfer(cpi_ctx, amount).expect("transfer2 failed")?;
    Ok(())
  }

...

#[derive(Accounts)]
pub struct UsdcDoge<'info> {
  pub program_signer: AccountInfo<'info>,

  #[account(signer)]//authority should sign this txn
  pub authority: AccountInfo<'info>,

  pub usdc_mint: CpiAccount<'info, Mint>,

  #[account(mut, "usdc_user.owner == *authority.key")]
  pub usdc_user: CpiAccount<'info, TokenAccount>,

  #[account(mut)]
  pub usdc_pgid: CpiAccount<'info, TokenAccount>,

  #[account(mut,
  "doge_mint.mint_authority == COption::Some(*program_signer.key)")]
  pub doge_mint: CpiAccount<'info, Mint>,

  #[account(mut, "doge_user.owner == *authority.key")]
  pub doge_user: CpiAccount<'info, TokenAccount>,

  #[account(mut)]
  pub doge_pgid: CpiAccount<'info, TokenAccount>,

  //pub program_id: Pubkey<'info>,
  
  // We already know its address and that it's executable
  #[account(executable,"token_program.key == &token::ID")]
  pub token_program: AccountInfo<'info>,

  pub system_program: AccountInfo<'info>,
}

'Program log: Error: owner does not match'

Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: custom program error: 0x4

'Program log: --------------== withdraw_usdc',
'Program log: check1',
'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]',
'Program log: Instruction: Transfer',
'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 3402 of 188791 compute units',
'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
'Program log: transfer1 successful. amount: 5000000',
'Program log: check3',
'Program log: check4',
'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]',
'Program log: Instruction: Transfer',
'Program log: Error: owner does not match',
'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 2902 of 181411 compute units',
'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA failed: custom program error: 0x4',
'Program J1JQ8f2s77Xhx7xQCRZmMTK2j3HuahgeSMUuccL5ywnb consumed 200000 of 200000 compute units',
'Program J1JQ8f2s77Xhx7xQCRZmMTK2j3HuahgeSMUuccL5ywnb failed: custom program error: 0x4'

In Anchor script:

await program.rpc.withdrawUsdc(
  amt2, nonce, {
  accounts: {
    programSigner,
    authority: userKeys.publicKey,
    usdcMint,
    usdcUser,
    usdcPgid,
    dogeMint,
    dogeUser,
    dogePgid,
    tokenProgram: TOKEN_PROGRAM_ID,
    systemProgram: anchor.web3.SystemProgram.programId,
  },
  signers: [userKeys],
});

the above accounts object, amt2, and nonce values are exactly the same as that in the stakeUsdc function call... so I don't know what owner is wrong...

before it fails, USDC are on usdc_pgid account, which is made from:

usdcPgid = await createTokenAccount(provider, usdcMint, program.programId);//arguments: privider, mint, owner

The owner of usdc_pgid is program.programId, so I should make cpi_accounts to use programId as authority... like:

 authority: ctx.program_id.to_account_info(),

error[E0599]: no method named to_account_info found for reference &anchor_lang::prelude::Pubkey in the current scope --> programs/doge/src/lib.rs:265:33 | 265 | authority: ctx.program_id.to_account_info(), | ^^^^^^^^^^^^^^^ method not found in &anchor_lang::prelude::Pubkey

anchor_lang::CpiAccount https://docs.rs/anchor-lang/0.13.2/anchor_lang/struct.CpiAccount.html

anchor_lang::prelude::Pubkey https://docs.rs/anchor-lang/0.13.2/anchor_lang/prelude/struct.Pubkey.html

From above docs, Pubkey type does not have method to convert from Pubkey to AccountInfo type! How can I do this?


Solution

  • The program's token balance account must has a signer PDA as owner!

    So the owner of token balance account MUST be the programSigner!

    Anchor code:

    programVault = await createTokenAccount(provider, usdcMint, programSigner);
    

    the 3rd argument Must be the programSigner!!!