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

> ## Agent Instructions
> This page is part of the Solana developer documentation at https://solana.com/developers/docs. Append `.md` to any docs URL, or send `Accept: text/markdown`, to get the page as Markdown.
> Index of all pages: https://solana.com/llms.txt. Per-section indexes such as https://solana.com/developers/docs/payments/llms.txt are smaller; prefer them when working on one task.
> Coding agents: install the official Solana skill for current APIs and conventions with `npx skills add https://github.com/solana-foundation/solana-dev-skill` (also served at https://solana.com/SKILL.md).
> Code in the cookbook and task pages comes from tested example files. Prefer it over writing API calls from memory.

# Διαχείριση λογαριασμών

> Ανάκτηση και αποσειριοποίηση λογαριασμών Anchor με αυτόματο χειρισμό του discriminator

## Κατανόηση των discriminator των λογαριασμών Anchor

Τα προγράμματα Anchor προσθέτουν στην αρχή όλων των δεδομένων λογαριασμού έναν
discriminator 8 byte. Ο discriminator αυτός είναι ένα hash του ονόματος του τύπου
λογαριασμού και χρησιμοποιείται για να επαληθευτεί ότι τα δεδομένα του
λογαριασμού αντιστοιχούν στον αναμενόμενο τύπο.

```
┌─────────────────────────────────────────────────┐
│ Account Data Layout                             │
├────────────────┬────────────────────────────────┤
│ Discriminator  │ Account Fields                 │
│ (8 bytes)      │ (variable size)                │
└────────────────┴────────────────────────────────┘
```

Τα βοηθητικά εργαλεία λογαριασμών του `anchor-litesvm` χειρίζονται αυτόματα τον discriminator.

<Warning>
  Αυτά τα παραδείγματα απαιτούν build του προγράμματός σας για SBPF v0. Το
  `anchor-litesvm` 0.4 χρησιμοποιεί το LiteSVM 0.11, το οποίο δεν μπορεί να
  φορτώσει προγράμματα SBPF v3, με αποτέλεσμα η `build_with_program` να κάνει
  panic με το μήνυμα `Failed to add program: Instruction(InvalidAccountData)`.
  Με Anchor 1.2 και νεότερες εκδόσεις, κάντε build με `anchor build --arch v0`
  και εκτελέστε `anchor test --skip-build`. Για το Anchor 1.1.x, καθώς και για το
  πού να τοποθετήσετε το τεστ και το IDL, δείτε τον οδηγό
  [Γρήγορης εκκίνησης](/developers/docs/tools/litesvm/additional-crates/testing-with-anchor-litesvm#installation).
</Warning>

Οι τύποι λογαριασμών προέρχονται από το `declare_program!(your_program)`, το
οποίο τους δημιουργεί από το IDL του προγράμματός σας: δομές λογαριασμών στο
`your_program::accounts` και άλλους τύπους, όπως τα enum, στο
`your_program::types`. Μην δηλώνετε δομές `#[account]` μέσα στο ίδιο το αρχείο
τεστ: η μακροεντολή αναμένει το `ID` του προγράμματος στη ρίζα του crate, το
οποίο ένα crate τεστ δεν διαθέτει.

## Ανάκτηση λογαριασμών Anchor

### Βασική ανάκτηση λογαριασμού

```rust
use anchor_lang::prelude::*;
use anchor_litesvm::{AnchorLiteSVM, Pubkey, Signer};

anchor_lang::declare_program!(your_program);

use your_program::accounts::UserAccount;

#[test]
fn test_fetch_account() {
    let mut ctx = AnchorLiteSVM::build_with_program(
        your_program::ID,
        include_bytes!("../../../target/deploy/your_program.so"),
    );

    let user = ctx.create_funded_account(10_000_000_000).unwrap();

    // ... initialize the account ...

    let (user_pda, _) = Pubkey::find_program_address(
        &[b"user", user.pubkey().as_ref()],
        &your_program::ID,
    );

    // Fetch and deserialize the account
    let account: UserAccount = ctx.get_account(&user_pda).unwrap();

    assert_eq!(account.name, "Alice");
    assert_eq!(account.authority, user.pubkey());
}
```

<Info>
  Η `get_account<T>()` επαληθεύει αυτόματα τον discriminator των 8 byte, ώστε να διασφαλίζεται ότι τα δεδομένα του λογαριασμού αντιστοιχούν στον αναμενόμενο τύπο.
</Info>

### Αποσειριοποίηση χωρίς έλεγχο

Για PDA ή προσαρμοσμένες διατάξεις λογαριασμών όπου η επαλήθευση του
discriminator ενδέχεται να αποτύχει, χρησιμοποιήστε την `get_account_unchecked`:

```rust
use anchor_lang::prelude::*;
use anchor_litesvm::AnchorLiteSVM;

anchor_lang::declare_program!(your_program);

use your_program::accounts::UserAccount;

#[test]
fn test_fetch_unchecked() {
    let ctx = AnchorLiteSVM::build_with_program(
        your_program::ID,
        include_bytes!("../../../target/deploy/your_program.so"),
    );

    // Fetch without discriminator validation
    let account: UserAccount = ctx.get_account_unchecked(&some_pda).unwrap();
}
```

<Warning>
  Χρησιμοποιείτε την `get_account_unchecked` με προσοχή. Χωρίς επαλήθευση του
  discriminator, ενδέχεται να αποσειριοποιήσετε μη έγκυρα δεδομένα. Χρησιμοποιήστε
  την μόνο όταν είστε βέβαιοι για τον τύπο του λογαριασμού.
</Warning>

## Άμεση χρήση της μονάδας λογαριασμών

Μπορείτε επίσης να χρησιμοποιήσετε απευθείας τις συναρτήσεις αποσειριοποίησης λογαριασμών:

```rust
use anchor_lang::prelude::*;
use anchor_litesvm::{get_anchor_account, get_anchor_account_unchecked, LiteSVM};

anchor_lang::declare_program!(your_program);

use your_program::accounts::UserAccount;

#[test]
fn test_direct_account_fetch() {
    let mut svm = LiteSVM::new();
    // ... setup ...

    // Fetch with discriminator check
    let account: UserAccount = get_anchor_account(&svm, &account_pubkey).unwrap();

    // Fetch without discriminator check
    let account: UserAccount = get_anchor_account_unchecked(&svm, &account_pubkey).unwrap();
}
```

## Σφάλματα λογαριασμών

Το enum `AccountError` παρέχει αναλυτικές πληροφορίες σφάλματος:

```rust
use anchor_lang::prelude::*;
use anchor_litesvm::{AccountError, AnchorLiteSVM};

anchor_lang::declare_program!(your_program);

use your_program::accounts::UserAccount;

#[test]
fn test_handle_errors() {
    let ctx = AnchorLiteSVM::build_with_program(
        your_program::ID,
        include_bytes!("../../../target/deploy/your_program.so"),
    );

    let result = ctx.get_account::<UserAccount>(&nonexistent_pubkey);

    match result {
        Ok(account) => println!("Found account: {}", account.name),
        Err(AccountError::AccountNotFound(pubkey)) => {
            println!("Account not found: {}", pubkey);
        }
        Err(AccountError::DiscriminatorMismatch) => {
            println!("Wrong account type!");
        }
        Err(AccountError::DeserializationError(msg)) => {
            println!("Failed to deserialize: {}", msg);
        }
    }
}
```

| Σφάλμα                          | Περιγραφή                                       |
| ------------------------------ | ------------------------------------------------- |
| `AccountNotFound(Pubkey)`      | Δεν υπάρχει λογαριασμός στη συγκεκριμένη διεύθυνση            |
| `DiscriminatorMismatch`        | Ο discriminator του λογαριασμού δεν αντιστοιχεί στον αναμενόμενο τύπο |
| `DeserializationError(String)` | Αποτυχία αποσειριοποίησης των δεδομένων του λογαριασμού                |

## Εργασία με πολλαπλούς τύπους λογαριασμών

### Ανάκτηση διαφορετικών τύπων λογαριασμών

Εδώ το `your_program` ορίζει λογαριασμούς `Config`, `UserProfile` και `Order`,
καθώς και ένα enum `OrderStatus` (`Pending`, `Completed`, `Cancelled`) το οποίο
αποθηκεύεται στο `Order`:

```rust
use anchor_lang::prelude::*;
use anchor_litesvm::AnchorLiteSVM;

anchor_lang::declare_program!(your_program);

use your_program::accounts::{Config, Order, UserProfile};
use your_program::types::OrderStatus;

#[test]
fn test_multiple_account_types() {
    let ctx = AnchorLiteSVM::build_with_program(
        your_program::ID,
        include_bytes!("../../../target/deploy/your_program.so"),
    );

    // Each account type is deserialized correctly
    let config: Config = ctx.get_account(&config_pda).unwrap();
    let profile: UserProfile = ctx.get_account(&profile_pda).unwrap();
    let order: Order = ctx.get_account(&order_pda).unwrap();

    assert_eq!(config.fee_bps, 100);
    assert_eq!(profile.username, "alice");
    assert!(matches!(order.status, OrderStatus::Pending));
}
```

### Έλεγχος ύπαρξης λογαριασμού πριν από την ανάκτηση

```rust
use anchor_lang::prelude::*;
use anchor_litesvm::{AnchorLiteSVM, Pubkey};

anchor_lang::declare_program!(your_program);

use your_program::accounts::UserAccount;

#[test]
fn test_check_before_fetch() {
    let ctx = AnchorLiteSVM::build_with_program(
        your_program::ID,
        include_bytes!("../../../target/deploy/your_program.so"),
    );

    let user_pda = Pubkey::new_unique();

    // Check existence first
    if ctx.account_exists(&user_pda) {
        let account: UserAccount = ctx.get_account(&user_pda).unwrap();
        println!("Found: {}", account.name);
    } else {
        println!("Account not initialized yet");
    }
}
```

## Πλήρες παράδειγμα

Ακολουθεί ένα ολοκληρωμένο παράδειγμα που δείχνει τη διαχείριση λογαριασμών με
ένα πρόγραμμα μετρητή, του οποίου η `initialize` δημιουργεί ένα PDA `Counter`
(`count`, `authority`, `bump`) και η `increment` προσθέτει ένα στο `count`:

```rust
use anchor_lang::prelude::*;
use anchor_litesvm::{AnchorLiteSVM, Pubkey, Signer, TestHelpers};

anchor_lang::declare_program!(counter);

#[test]
fn test_counter_workflow() {
    let mut ctx = AnchorLiteSVM::build_with_program(
        counter::ID,
        include_bytes!("../../../target/deploy/counter.so"),
    );

    let authority = ctx.svm.create_funded_account(10_000_000_000).unwrap();

    // Derive the counter PDA
    let (counter_pda, bump) = Pubkey::find_program_address(
        &[b"counter", authority.pubkey().as_ref()],
        &counter::ID,
    );

    // Verify counter doesn't exist yet
    assert!(!ctx.account_exists(&counter_pda));

    // Initialize using generated client types
    let init_ix = ctx.program()
        .accounts(counter::client::accounts::Initialize {
            authority: authority.pubkey(),
            counter: counter_pda,
            system_program: anchor_lang::system_program::ID,
        })
        .args(counter::client::args::Initialize {})
        .instruction()
        .unwrap();

    ctx.execute_instruction(init_ix, &[&authority])
        .unwrap()
        .assert_success();

    // Verify counter exists and has correct initial state
    assert!(ctx.account_exists(&counter_pda));
    let counter: counter::accounts::Counter = ctx.get_account(&counter_pda).unwrap();
    assert_eq!(counter.count, 0);
    assert_eq!(counter.authority, authority.pubkey());
    assert_eq!(counter.bump, bump);

    // Increment using generated client types
    let increment_ix = ctx.program()
        .accounts(counter::client::accounts::Increment {
            authority: authority.pubkey(),
            counter: counter_pda,
        })
        .args(counter::client::args::Increment {})
        .instruction()
        .unwrap();

    ctx.execute_instruction(increment_ix.clone(), &[&authority])
        .unwrap()
        .assert_success();

    // Verify incremented
    let counter: counter::accounts::Counter = ctx.get_account(&counter_pda).unwrap();
    assert_eq!(counter.count, 1);

    // An identical transaction with the same blockhash is rejected as
    // AlreadyProcessed, so move to a new blockhash before repeating it
    ctx.svm.expire_blockhash();

    // Increment again
    ctx.execute_instruction(increment_ix, &[&authority])
        .unwrap()
        .assert_success();

    let counter: counter::accounts::Counter = ctx.get_account(&counter_pda).unwrap();
    assert_eq!(counter.count, 2);

    println!("Counter test passed! Final count: {}", counter.count);
}
```

<Info>
  Η `execute_instruction(...).unwrap()` αποσυμπιέζει μόνο την ίδια την κλήση. Μια
  συναλλαγή που απορρίπτεται από το runtime εξακολουθεί να επιστρέφει `Ok`, γι' αυτό
  προσθέστε στην αλυσίδα την `.assert_success()` ώστε το τεστ να αποτυγχάνει όταν
  αποτυγχάνει η συναλλαγή.
</Info>
