Quantcast
Channel: Recent Questions - Solana Stack Exchange
Viewing all articles
Browse latest Browse all 7969

Reallocing one account bigger and one smaller in one ix in anchor gives `sum of account balances before and after instruction do not match`

$
0
0

I have the following MRE, where I create two PDAs and realloc them "in opposite directions" i.e. I make one account 1 byte larger and the other 1 byte smaller. Calling this, yields sum of account balances before and after instruction do not match.. Only reallocing one account in either direction makes this error go away. Reallocing both accounts in the same direction i.e. making both bigger/smaller also makes the error go away. What am I doing wrong here? I'm using anchor v0.30.1.

MRE Program code:

use anchor_lang::prelude::*;declare_id!("8owLmone5oQWwAZPKeYEmMciBg8Q533UcHBjcU6niV9E");#[program]pub mod realloc_demo {    use super::*;    pub fn init(_ctx: Context<Initialize>) -> Result<()> {        Ok(())    }    pub fn demo(_ctx: Context<Demo>) -> Result<()> {        Ok(())    }    }#[derive(Accounts)]pub struct Initialize<'info> {    #[account(mut)]    pub signer: Signer<'info>,    #[account(        init,        payer = signer,        seeds = [b"Acc1"],        space = 9,        bump    )]    pub acc1: Account<'info, DemoAcc>,    #[account(        init,        payer = signer,        seeds = [b"Acc2"],        space = 9,        bump    )]    pub acc2: Account<'info, DemoAcc>,    pub system_program: Program<'info, System>,}#[derive(Accounts)]pub struct Demo<'info> {    #[account(mut)]    pub signer: Signer<'info>,    #[account(        mut,        realloc = 8,        realloc::payer = signer,        realloc::zero = false,        seeds = [b"Acc1"],        bump    )]    pub acc1: Account<'info, DemoAcc>,    #[account(        mut,        realloc = 10,        realloc::payer = signer,        realloc::zero = false,        seeds = [b"Acc2"],        bump     )]    pub acc2: Account<'info, DemoAcc>,    pub system_program: Program<'info, System>,}#[account]pub struct DemoAcc {}

MRE client typescript test code:

import * as anchor from "@coral-xyz/anchor";import { Program } from "@coral-xyz/anchor";import { ReallocDemo } from "../target/types/realloc_demo";describe("example", () => {  const provider = anchor.AnchorProvider.env();  anchor.setProvider(provider);  const program = anchor.workspace.ReallocDemo as Program<ReallocDemo>;  it("Init", async () => {    const transactionSignature = await program.methods.init().rpc();    console.log("Your transaction signature", transactionSignature);  });  it("Realloc (fails)", async () => {    const sig = await program.methods.demo().rpc();    console.log("Your transaction signature", sig);  });});

Viewing all articles
Browse latest Browse all 7969

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>