# Double-entry Bookkeeping

Source: https://docs.formance.com/v3.2/modules/ledger/core-concepts/double-entry

Stack: v3.2

Every transaction in Formance must balance: assets leaving one account must arrive in another. This is the principle of double-entry bookkeeping. Assets are never created or destroyed inside the ledger; they only move.

## What it's for

Double-entry is what makes the ledger trustworthy. It enforces conservation of assets at the database level, so any discrepancy between what entered the system and what accounts hold is structurally impossible, not a convention your code tries to maintain.

## The three rules

### 1. Transactions must balance

Within a single transaction, the total amount added to source components must equal the total amount added to destination components. No assets are created or destroyed. In a multi-asset ledger this holds independently for each asset — USD/2 sources must balance USD/2 destinations, EUR/2 sources must balance EUR/2 destinations, and so on.

**Invalid example:** A posting that credits `account:1` with 100 USD/2 without a corresponding debit violates this rule. The total destination increase (100) does not equal the total source increase (0), so the transaction is rejected.

### 2. The ledger-wide sum is always zero

Across all accounts, the sum of all destination components always equals the sum of all source components at all times. Assets are never created or destroyed. This invariant holds per asset — the ledger-wide balance of USD/2 is always zero, the ledger-wide balance of EUR/2 is always zero, and so on independently.

### 3. Accounts cannot go negative by default

An account's balance cannot fall below zero unless it is explicitly permitted. This prevents accounts from spending assets they do not have.

**Invalid example:** An account with a balance of 100 USD/2 cannot send 150 USD/2. Doing so would leave it with a balance of −50 USD/2, violating this rule. To allow an account to go below zero, use the `allowing overdraft` clause in Numscript. See [Overdraft](https://docs.formance.com/modules/numscript/reference/overdraft).

## The world account

Since no transaction can create assets, there must be a designated way to introduce new assets from outside the system. Note that the ledger tracks assets, not value — for currencies, this typically maps 1:1, but value (e.g. accounting in GBP while holding USD) is not represented in the ledger. There are two patterns.

### Using the world account

The `@world` account is a built-in account permitted to carry a negative balance. Sending from `@world` to any destination introduces assets into the ledger; `@world`'s output increases while the recipient's input increases by the same amount.

`@world` is Formance-specific and has no counterpart in conventional accounting, so it does not map cleanly onto a real-world funding source. It is the fastest way to get a ledger working and it is the right tool for tests, prototypes, and the specific cases where the origin of the assets genuinely is outside your books. For production models where each inflow has an identifiable external counterparty, prefer counter-part accounts below.

**Before:**

| Account     | Input | Output | Balance |
| ----------- | ----- | ------ | ------- |
| `account:1` | 0     | 0      | 0       |
| `world`     | 0     | 0      | 0       |

**Transaction:** send 100 USD/2 from `world` to `account:1`

**After:**

| Account     | Input | Output | Balance |
| ----------- | ----- | ------ | ------- |
| `account:1` | 100   | 0      | 100     |
| `world`     | 0     | 100    | -100    |

The `world` account carries −100. Total ledger-wide balance: `100 + (−100) = 0`.

### Using counter-part accounts

Instead of `@world`, use named counter-part accounts — one per payment method or external funding source. In accounting terms, these can be treated as Normal Debit asset accounts; their balance is negative because they are the counter-part. They represent the platform's obligation to those external parties.

**Example:** A customer pays 100 USD/2: 50 via credit card, 50 via bank transfer.

**Before:**

| Account                        | Input | Output | Balance |
| ------------------------------ | ----- | ------ | ------- |
| `order:1234`                   | 0     | 0      | 0       |
| `payment-method:credit-card`   | 0     | 0      | 0       |
| `payment-method:bank-transfer` | 0     | 0      | 0       |

**After:**

| Account                        | Input | Output | Balance |
| ------------------------------ | ----- | ------ | ------- |
| `order:1234`                   | 100   | 0      | 100     |
| `payment-method:credit-card`   | 0     | 50     | -50     |
| `payment-method:bank-transfer` | 0     | 50     | -50     |

The counter-part accounts carry negative balances representing what was received from each payment method. Reconciliation is straightforward: the balance of `payment-method:credit-card` is exactly the amount to reconcile against your credit card processor.

Counter-part accounts and reconciliation

By mapping counter-part accounts to your financial partners, you can reconcile your ledger against external systems by comparing each counter-part account's balance against the partner's reported balance.

## Overdraft and negative balances

Some accounts are designed to carry a negative balance: liability accounts, credit lines, or accounts that represent external obligations. The ledger permits this when you explicitly declare it in Numscript using the `allowing overdraft` clause.

```numscript
send [USD/2 1000] (
  source = @users:alice allowing unbounded overdraft
  destination = @vendors:acme
)
```

You can also cap the overdraft to a specific amount:

```numscript
send [USD/2 1000] (
  source = @users:alice allowing overdraft up to [USD/2 500]
  destination = @vendors:acme
)
```

**Example:** Alice's account has a zero balance but is permitted to go negative. She purchases USD/2 8000 of software from a vendor:

```numscript
send [USD/2 8000] (
  source = @users:alice allowing unbounded overdraft
  destination = @vendors:acme-software
)
```

Alice's account balance is now −USD/2 8000. When her salary is credited and the balance is settled:

```numscript
send [USD/2 8000] (
  source = @payroll
  destination = @users:alice
)
```

Use `allowing overdraft` when modeling credit lines, loans, liability accounts (such as counter-part payment method accounts), or any account that legitimately represents an obligation.

## Mapping to traditional debit/credit accounting

This section is for readers familiar with traditional double-entry accounting. If you're new to accounting, you can skip this section.

### The golden rule

The golden rule for converting a credit/debit accounting model to Formance's source/destination model is:

- **Credit** becomes **Destination**
- **Debit** becomes **Source**

### Normal accounts

In accounting practice, accounts are designated as _normal debit_ or _normal credit_ depending on whether they are on the asset or liability side of the balance sheet:

| Entry type   | Formance role   | Normal Debit (trad. balance) | Normal Credit (trad. balance) |
| ------------ | --------------- | ---------------------------- | ----------------------------- |
| Debit Entry  | **Source**      | Increases                    | Decreases                     |
| Credit Entry | **Destination** | Decreases                    | Increases                     |

### Conversion examples

#### Topping up a wallet with a bank transfer

A user tops up their wallet with a bank transfer. The user wallet is a liability (_normal credit_); the bank account is an asset (_normal debit_).

In the credit/debit model:

| Account      | Debit  | Credit |
| ------------ | ------ | ------ |
| User Wallet  | `$0`   | `$100` |
| Bank Account | `$100` | `$0`   |

Applying the golden rule:

| Account      | Source | Destination | Balance |
| ------------ | ------ | ----------- | ------- |
| User Wallet  | `$0`   | `$100`      | `$100`  |
| Bank Account | `$100` | `$0`        | `-$100` |

The bank account carries a negative balance here. As an asset account (_normal debit_), its balance in the source/destination model is negative. Formance supports this with the [`allowing overdraft` clause](https://docs.formance.com/modules/numscript/reference/overdraft) in Numscript.

#### Transferring money between two wallets

User 1 transfers `$100` from their wallet to User 2's wallet. Both wallets are liabilities (_normal credit_).

Starting balances:

| Account       | Balance |
| ------------- | ------- |
| User 1 Wallet | `$200`  |
| User 2 Wallet | `$0`    |

In the credit/debit model:

| Account       | Debit  | Credit |
| ------------- | ------ | ------ |
| User 1 Wallet | `$100` | `$0`   |
| User 2 Wallet | `$0`   | `$100` |

Applying the golden rule:

| Account       | Source | Destination | Balance |
| ------------- | ------ | ----------- | ------- |
| User 1 Wallet | `$100` | `$200`      | `$100`  |
| User 2 Wallet | `$0`   | `$100`      | `$100`  |

#### Withdrawing money from a wallet

A merchant withdraws `$50` from their wallet to their bank account. The wallet is a liability (_normal credit_); the bank account is an asset (_normal debit_).

Starting balances:

| Account         | Balance  |
| --------------- | -------- |
| Merchant Wallet | `$100`   |
| Bank Account    | `-$1000` |

In the credit/debit model:

| Account         | Debit | Credit |
| --------------- | ----- | ------ |
| Merchant Wallet | `$50` | `$0`   |
| Bank Account    | `$0`  | `$50`  |

Applying the golden rule:

| Account         | Source  | Destination | Balance |
| --------------- | ------- | ----------- | ------- |
| Merchant Wallet | `$50`   | `$100`      | `$50`   |
| Bank Account    | `$1000` | `$50`       | `-$950` |

#### Treasury movement between two bank accounts

A business moves `$250` from a US bank account to a UK bank account. Both are assets (_normal debit_).

Starting balances:

| Account         | Balance  |
| --------------- | -------- |
| US Bank Account | `-$1000` |
| UK Bank Account | `-$500`  |

In the credit/debit model:

| Account         | Debit  | Credit |
| --------------- | ------ | ------ |
| US Bank Account | `$0`   | `$250` |
| UK Bank Account | `$250` | `$0`   |

Applying the golden rule:

| Account         | Source  | Destination | Balance |
| --------------- | ------- | ----------- | ------- |
| US Bank Account | `$1000` | `$250`      | `-$750` |
| UK Bank Account | `$750`  | `$0`        | `-$750` |

Asset Account Balances

Asset accounts (_normal debit_) will have a negative balance in the source/destination model, because their source component (outflows) exceeds their destination component (inflows).

## Do / Don't

- **Do:** Use `@world` as the source when introducing new funds whose real-world origin is tracked outside the ledger, and when getting started or testing.
- **Do:** Prefer named counter-part accounts over `@world` in production models where each inflow has an identifiable external counterparty; they reconcile against your partners, `@world` does not.
- **Do:** Use `allowing overdraft` explicitly when modeling credit lines, debts, or liability accounts.
- **Don't:** Assume any account can go negative; it will fail without the overdraft clause.
- **Don't:** Create single-sided postings (source without a destination, or vice versa); the ledger will reject them.
