# Account Hierarchy Examples

Source: https://docs.formance.com/examples/account-hierarchies

Stack: v3.2

These examples show how to structure account hierarchies for common fintech use cases. For the principles behind the naming and structure choices, see [Chart of Accounts](https://docs.formance.com/v3.2/modules/ledger/core-concepts/chart-of-accounts).

## E-commerce platform

```yaml
chart:
  customers:
    $customer_id:
      .pattern: ^[a-zA-Z0-9_-]+$
      wallet:
        .self: {}
        .metadata:
          type:
            default: customer_wallet
  merchants:
    $merchant_id:
      .pattern: ^[a-zA-Z0-9_-]+$
      earnings:
        .self: {}
      settlements:
        .self: {}
  orders:
    $order_id:
      .pattern: ^[a-zA-Z0-9_-]+$
      pending:
        .self: {}
      captured:
        .self: {}
  platform:
    fees:
      .self: {}
    taxes:
      vat:
        .self: {}
    refunds:
      pool:
        .self: {}
```

Key design decisions in this hierarchy:

- **Customers** have wallet accounts with a wallet sub-account, keeping balance state isolated per user.
- **Merchants** separate earnings (accrued revenue) from settlements (disbursed funds), making payout reconciliation straightforward.
- **Orders** have lifecycle accounts (`pending`, `captured`) so each order's state is traceable without relying on metadata.
- **Platform** centralizes fees, VAT, and refund pools as named accounts, giving finance a clean query surface for revenue reporting.

## Lending platform

```yaml
chart:
  borrowers:
    $borrower_id:
      .pattern: ^[a-zA-Z0-9_-]+$
      principal:
        .self: {}
        .metadata:
          type:
            default: borrower_principal
      interest:
        .self: {}
  lenders:
    $lender_id:
      .pattern: ^[a-zA-Z0-9_-]+$
      available:
        .self: {}
      deployed:
        .self: {}
  loans:
    $loan_id:
      .pattern: ^[a-zA-Z0-9_-]+$
      outstanding:
        .self: {}
      payments:
        .self: {}
```

Key design decisions in this hierarchy:

- **Borrowers** and **lenders** are separated into their own hierarchies, keeping lender capital distinct from borrower liabilities.
- **Loan accounts** (`outstanding`, `payments`) track state at the individual loan level; each loan's outstanding balance and payment history are isolated, making arrears management and early repayment calculation straightforward.

## Multi-currency wallet

```yaml
chart:
  users:
    $user_id:
      .pattern: ^[a-zA-Z0-9_-]+$
      wallet:
        USD:
          .self: {}
        EUR:
          .self: {}
        BTC:
          .self: {}
  exchange:
    liquidity:
      USD:
        .self: {}
      EUR:
        .self: {}
    fees:
      .self: {}
```

Key design decisions in this hierarchy:

- **Per-user, per-currency wallet accounts** (`users:{id}:wallet:USD`, `users:{id}:wallet:EUR`) allow clean balance isolation; an account's USD balance and EUR balance never interfere.
- **Exchange liquidity pools** hold pre-funded reserves for funded conversions. Debiting from `exchange:liquidity:USD` and crediting `users:{id}:wallet:USD` keeps BTC or EUR creation strictly bounded by what was pre-funded.
- **Exchange fees** are collected in a single platform account, making fee revenue reporting a simple balance query.
