> ## Documentation Index
> Fetch the complete documentation index at: https://docs.theblockchainlibrary.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Solana Program: Increment Counter

> A minimal Solana program (smart contract) that increments a counter account.

# Solana Program: Increment Counter

A minimal Solana program (smart contract) that increments a counter account.

A minimal Solana program (smart contract) that increments a counter account.

## Explanation

This Solana program reads a `u64` counter from an account's data, increments it, and writes it back.

**Solana-specific concepts:**

* Programs don't store state — data lives in separate **accounts**.
* `next_account_info` iterates over passed accounts.
* Account data is raw bytes; you must serialize/deserialize manually.

Use Anchor framework for more ergonomic Solana development.

## Code

```rust theme={null}
use solana_program::{
    account_info::{next_account_info, AccountInfo},
    entrypoint,
    entrypoint::ProgramResult,
    msg,
    program_error::ProgramError,
    pubkey::Pubkey,
};

entrypoint!(process_instruction);

pub fn process_instruction(
    _program_id: &Pubkey,
    accounts: &[AccountInfo],
    _instruction_data: &[u8],
) -> ProgramResult {
    let accounts_iter = &mut accounts.iter();
    let account = next_account_info(accounts_iter)?;

    let mut counter = u64::from_le_bytes(
        account.try_borrow_data()?[0..8]
            .try_into()
            .map_err(|_| ProgramError::InvalidInstructionData)?,
    );

    counter += 1;
    msg!("Counter is now: {}", counter);

    account.data.borrow_mut()[0..8]
        .copy_from_slice(&counter.to_le_bytes());

    Ok(())
}
```

***

**Canonical knowledge ID:** `code-example:solana-increment-counter`
