0

I am currently getting into sc development and playing around a bit. Basically I wanted to create a counter where anyone can call the decrease or increase func and modify the value.

I think my current code basically initializes a counter for every account/public key, which I am fine with since I just wanna understand how things work but I cant call any of my function other than the initalize.

The hello world func are disabled from calling in the playground but work through code with anchor.

the decrease and increase funcs throw the error "Program failed to complete"

// Import anchor
use anchor_lang::prelude::*;

declare_id!("7aAKLxbTS8nGaD5QEpbTooNhqjYn8s6sMmQ4gZJYsQrg");

#[derive(Accounts)]
pub struct Empty {}

#[account]
#[derive(Default)]
pub struct Interactions {
    counter: u8,
}

//used to modify the Interactions Struct
#[derive(Accounts)]
pub struct SetInteractions<'info> {
    #[account(init, payer = signer, space = 1 + 1)]
    pub set: Account<'info, Interactions>,
    #[account(mut)]
    pub signer: Signer<'info>,
    pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct UpdateInteractions<'info> {
    pub set: Account<'info, Interactions>,
}

//Initalize account with one call
//change/decrease/increase with another func

#[program]
mod hello_world {
    use super::*;

    pub fn hello(_ctx: Context<Empty>) -> Result<()> {
        msg!("Hello, World!");
        Ok(())
    }

    pub fn another_func(_ctx: Context<Empty>) -> Result<()> {
        msg!("Inside another Func");
        Ok(())
    }

    pub fn initalize_acc(ctx: Context<SetInteractions>) -> Result<()> {
        ctx.accounts.set.counter = 0;
        msg!("Initalize account");
        Ok(())
    }

    pub fn decrease_counter(ctx: Context<UpdateInteractions>, number: u8) -> Result<()> {
        ctx.accounts.set.counter -= number;
        msg!("Decrease counter");
        Ok(())
    }

    pub fn increase_counter(ctx: Context<UpdateInteractions>, number: u8) -> Result<()> {
        ctx.accounts.set.counter += number;
        msg!("Increased counter");
        Ok(())
    }
}

1 Answer 1

3

Why transaction fails

The program fails to complete because Anchor accounts require at least 8 bytes of data because of Anchor's discriminator. The following line in SetInstructions struct sets the account space to 2 bytes

#[account(init, payer = signer, space = 1 + 1)]

it should instead be:

#[account(init, payer = signer, space = 8 + 1)]

How to get more details about transactions

There is also a way to get more details about why transaction failed. You'll automatically get all the information about the transaction if you enable "Show transaction details" setting (from bottom left settings icon):

Settings

Now you should see the program logs after sending the transaction:

Log Messages:
  Program 8wXmVh76MaosucmMo6ChnFFCWjFU4hE6pV1M2hB36NHc invoke [1]
  Program log: Instruction: InitalizeAcc
  Program 11111111111111111111111111111111 invoke [2]
  Program 11111111111111111111111111111111 success
  Program log: panicked at 'range start index 8 out of range for slice of length 2', c7eabcd8-78d0-45c1-8bbd-b9d02877fa8b/src/lib.rs:9:1
  Program 8wXmVh76MaosucmMo6ChnFFCWjFU4hE6pV1M2hB36NHc consumed 6584 of 200000 compute units
  Program 8wXmVh76MaosucmMo6ChnFFCWjFU4hE6pV1M2hB36NHc failed: SBF program panicked

As seen from the logs, the actual error is range start index 8 out of range for slice of length 2. This means we're trying to access an index that's larger than the length of the slice due to not having enough space in the account.

Tests disabled in UI

The hello world func are disabled from calling in the playground but work through code with anchor.

This was a problem with the yesterday's update — the test UI was updated with lots of new changes but no account/argument case wasn't handled properly. It should be fixed now!

1
  • Appreciate the detailed answer, really helped me out! Dec 24, 2023 at 1:20

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.