1

I am running into the error, in my Anchor program:

[Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: sum of account balances before and after instruction do not match]

I am doing a spl-token transfer through CPI:

    anchor_spl::token::transfer(
        CpiContext::new(
            token_program.to_account_info(), 
        Transfer {
            from: sender_token_account.to_account_info(),
            to: receiver_token_account.to_account_info(),
            authority: user.to_account_info(),
        }),
        amount.into()
    )?;

In the same transaction I am also reallocating the size of a PDA.

Anyone has a hint on how to avoid this error?

3
  • I dont think PDA reallocation has anything to do with this error. Can you explain more on the changes happening in this code
    – Pratik.js
    Commented Nov 23, 2022 at 14:51
  • I am doing two things: 1. Transfer SPL Tokens 2. Reallocate PDA account I tried to do them separately, then both work fine. But combining them in one function is throwing this error
    – Nelis.sol
    Commented Nov 23, 2022 at 15:56
  • Please post the full instruction logic. There's not enough information in the provided sample to debug why the account balances would be off. Commented Nov 23, 2022 at 16:27

2 Answers 2

1

I solved the same problem here:

https://solana.stackexchange.com/questions/4481/error-processing-instruction-0-sum-of-account-balances-before-and-after-instruc

Be very careful when making transactions. Put the native ones first and then the one you use with pda

If my contribution does not help you, I invite you to share the code to solve it.

0

Did you solve this? I am having the exact same issue.

I am able to realloc (increase and decrease) if I remove the token transfer code in the instruction. I'm also able to do the token transfer by itself if I remove the realloc.

But if I combine them both I can only increase the PDA's size, not decrease, and I get the same error [Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: sum of account balances before and after instruction do not match].

use anchor_spl::{
  associated_token::AssociatedToken,
  token::{self, Mint, Token, TokenAccount, Transfer as SplTransfer},
};

#[derive(Accounts)]
#[instruction(description: String)]
pub struct EditProfile<'info> {
  #[account(mut)]
  pub owner: Signer<'info>,

  #[account(mut)]
  pub token_mint: Account<'info, Mint>,

  #[account(mut,
    associated_token::mint = token_mint,
    associated_token::authority = owner,
  )]
  pub owner_ata: Account<'info, TokenAccount>,

  #[account(mut, seeds = [b"user", owner.key().as_ref()], bump)]
  pub user: Account<'info, User>,

  // 
  #[account(
    init_if_needed,
    payer = owner,
    associated_token::mint = token_mint,
    associated_token::authority = user,
  )]
  pub user_ata: Account<'info, TokenAccount>,

  #[account(
    mut,
    seeds = [b"profile", owner.key().as_ref()],
    bump,
    realloc = 8 + Post::BASE_SIZE + description.len(),
    realloc::payer = owner,
    realloc::zero = false,
  )]
  pub profile: Account<'info, Post>,

  pub system_program: Program<'info, System>,
  pub token_program: Program<'info, Token>,
  pub associated_token_program: Program<'info, AssociatedToken>,
  pub rent: Sysvar<'info, Rent>,
}

pub fn edit_profile(
  ctx: Context<EditProfile>,
  description: String,
) -> Result<()> {
  let profile = &mut ctx.accounts.profile;

  profile.edit(description);

  // instruction works if this is removed:
  token::transfer(
    CpiContext::new(
      ctx.accounts.token_program.to_account_info(),
      SplTransfer {
        from: ctx.accounts.owner_ata.to_account_info(),
        to: ctx.accounts.user_ata.to_account_info(),
        authority: ctx.accounts.owner.to_account_info(),
      }
    ),
    COST_TO_EDIT_PROFILE,
  )?;

  Ok(())
}

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.