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(())
}
}