# Wallet Balances

Wallet balances for wallets within the partner's scope, calculated from each wallet's transaction
history. One row per wallet.

`balance` is the sum of completed incoming amounts, less completed outgoing amounts, less any
pending outgoing amounts — pending outgoing transactions are reserved against the balance. `credit`
is the unspent promotional credit on the wallet, reported separately and not included in `balance`.

Balances are in the wallet's own currency; see `currency_code` on the `wallets` table.

**SQL:** `wallet_balances`

## Schema

| Column Name        | Data Type     | Description                                                                               |
|:-------------------|:--------------|:------------------------------------------------------------------------------------------|
| **Primary ID**     |               |                                                                                           |
| id                 | INTEGER       | Primary key. Equal to `wallet_id`, as there is one balance per wallet. Prefer `wallet_id` when joining |
| **Other IDs**      |               |                                                                                           |
| wallet_id          | INTEGER       | The wallet this balance belongs to                                                        |
| **Timestamps**     |               |                                                                                           |
| balance_at         | TIMESTAMP_NTZ | Timestamp of the most recent completed transaction behind the balance. Null for wallets with no completed transactions. Does not move when a pending outgoing transaction is reserved |
| **Attributes**     |               |                                                                                           |
| credit             | NUMBER        | Unspent promotional credit on the wallet, in the wallet's currency                        |
| balance            | NUMBER        | Wallet balance in the wallet's currency. Negative means the wallet is overdrawn            |

## Recent changes 

**This table was rebuilt in August 2026**  

This table now derives each wallet's balance from its transaction history and matches the balance
returned by the Monta API. Every wallet in your scope has a row, including wallets with no
transactions, which report a balance of `0` — expect a higher row count than before.

**`id` values have changed.** Each wallet still has a stable, unique `id`, but the previous values
did not carry across. If you store or join on `id`, re-key on `wallet_id`.

The `created_at`, `updated_at` and `negative_balance_at` columns have been removed, and a
`balance_at` column has been added.
 

## Inclusion Criteria

One row for every wallet within the partner's scope, resolved via the partner's `wallets` table.

Wallets with no transaction history are included and report a `balance` and `credit` of `0`, with a
null `balance_at`. This matches the Monta API, which also returns a zero balance for such wallets.

Soft-deleted wallets are excluded, as they are on the `wallets` table.

## Refresh cadence

This table is rebuilt three times daily, at 04:00, 12:00 and 20:00 Europe/Copenhagen, so balances
can trail the live value by up to around eight hours. Use `balance_at` to judge how current a given
row is, and see the Alignment note below if you need a real-time figure.

## Data flow

| Upstream tables  | Downstream tables |
|:-----------------|:------------------|
| `wallets`        | ---               |
| `transactions`   | ---               |

## Alignment

### Monta API

- Aligns with the Monta API [get/wallet](https://developer.monta.com/api/v1/wallets/walletId)
  endpoint: `balance` matches `balance.amount`, and `credit` matches `balance.credit`.
- That endpoint returns a **live** balance, while this table is refreshed three times daily. For a
  point-in-time figure the endpoint is authoritative; this table will agree once it next refreshes.
- The wallet **list** endpoint does not return balances, so this table is the efficient way to read
  balances for many wallets at once.
- Balances derive from the same transaction data exposed in the `transactions` table, so a wallet's
  balance can be reconciled from it:

```sql
select
    coalesce(sum(
        case
            when state = 'complete' and to_wallet_id = :wallet_id then to_amount
            when state = 'complete' and from_wallet_id = :wallet_id then -from_amount
            when state = 'pending' and from_wallet_id = :wallet_id then -from_amount
            else 0
        end
    ), 0) as balance
from transactions
where deleted_at is null
  and (to_wallet_id = :wallet_id or from_wallet_id = :wallet_id)
```

Transactions in any other state, such as `failed` or `reserved`, do not affect the balance.
