2

in this code example:

use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::{
    account_info::{next_account_info, AccountInfo},
    declare_id,
    entrypoint::ProgramResult,
    msg,
    program_error::ProgramError,
    pubkey::Pubkey,
};

mod state;
use state::*;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[cfg(not(feature = "no-entrypoint"))]
use solana_program::entrypoint;

#[cfg(not(feature = "no-entrypoint"))]
entrypoint!(process_instruction);

pub fn process_instruction(
    _program_id: &Pubkey,
    accounts: &[AccountInfo],
    instruction_data: &[u8],
) -> ProgramResult {
    let (instruction_discriminant, instruction_data_inner) = instruction_data.split_at(1);
    match instruction_discriminant[0] {
        0 => {
            msg!("Instruction: Increment");
            process_increment_counter(accounts, instruction_data_inner)?;
        }
        _ => {
            msg!("Error: unknown instruction")
        }
    }
    Ok(())
}

pub fn process_increment_counter(
    accounts: &[AccountInfo],
    _instruction_data: &[u8],
) -> Result<(), ProgramError> {
    let account_info_iter = &mut accounts.iter();

    let counter_account = next_account_info(account_info_iter)?;
    assert!(
        counter_account.is_writable,
        "Counter account must be writable"
    );

    let mut counter = Counter::try_from_slice(&counter_account.try_borrow_mut_data()?)?;
    counter.count += 1;
    counter.serialize(&mut *counter_account.data.borrow_mut())?;

    msg!("Counter state incremented to {:?}", counter.count);
    Ok(())
}

Where is the creation of the PDA? The instruction processing only increments the counter, it feels like the Program Derived Account exists already.

1 Answer 1

3

Looks like they did not include the creation of the counter in the program but instead are handling it on the Client side using System Program. Take a look at the tests:

const allocIx: TransactionInstruction = SystemProgram.createAccount({
  fromPubkey: payer,
  newAccountPubkey: counter,
  lamports: await connection.getMinimumBalanceForRentExemption(
    COUNTER_ACCOUNT_SIZE
  ),
  space: COUNTER_ACCOUNT_SIZE,
  programId: PROGRAM_ID,
});

This is a little different than how they've implemented in the anchor example, where they've explicitly created an initialize function in the program:

pub fn initialize_counter(_ctx: Context<InitializeCounter>) -> Result<()> {
    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.