0

I am trying to transfer tokens in a PDA and withdraw from that program, deposit works fine but it throws the error when I withdraw.

pub fn withdraw_token(
        ctx: Context<TokenPaymentAccounts>,
        bump: u8,
        currency: String,
    ) -> Result<()> {
        let bump = &[bump][..];
        let binding = [&[bump][..]];
        let program_data_account = &mut ctx.accounts.program_data;
        let mint = &ctx.accounts.mint;

        if ctx.accounts.signer.key().ne(&program_data_account.owner) {
            return err!(Error::InvalidAdmin);
        }

                transfer_checked(
                    CpiContext::new(
                        ctx.accounts.token_program.to_account_info(),
                        TransferChecked {
                            authority: ctx.accounts.program_pda.to_account_info(),
                            from: ctx.accounts.program_token_account.to_account_info(),
                            to: ctx.accounts.user_token_account.to_account_info(),
                            mint: mint.to_account_info(),
                        },
                    )
                    .with_signer(&binding),
                    program_data_account.total_usdc,
                    mint.decimals,
                )?;

                program_data_account.total_usdc = 0

        Ok(())
    }


#[derive(Accounts)]
#[instruction(bump: u8)]
pub struct TokenPaymentAccounts<'info> {
    #[account(
        mut,
        seeds = [PROGRAM_DATA_SEED, ID.key().as_ref()],
        bump = program_data.bump
    )]
    pub program_data: Account<'info, ProgramData>,

    #[account(
        mut,
        token::mint = mint,
        token::authority = program_pda
    )]
    pub program_token_account: Account<'info, TokenAccount>,

    #[account(
        mut,
        token::mint = mint,
        token::authority = signer
    )]
    pub user_token_account: Account<'info, TokenAccount>,

    pub mint: Account<'info, Mint>,

    /// CHECK: Program PDA
    #[account(
        mut,
        seeds = [PROGRAM_PDA_SEED],
        bump = bump
    )]
    pub program_pda: UncheckedAccount<'info>,

    #[account(mut)]
    pub signer: Signer<'info>,

    pub token_program: Program<'info, Token>,
    pub system_program: Program<'info, System>,
}```

1 Answer 1

1

You need to pass in all seeds when putting together the seeds for a PDA "signature", including PROGRAM_PDA_SEED. Try including it in your binding, ie:

        let binding = [&PROGRAM_PDA_SEED, &[bump][..]];

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.