Files
CatoandClaude Opus 5 3d5512f238 fix(wallets): a balance you can see, and a link to where it lives (#1056)
Reported: "it is not showing balance ... my profile also does not show
wallets ... i see two coinos profiles. i have never even used coinos."

Three separate bugs wearing one coat.

1. THE PROJECT BALANCE THAT WAS NEVER A COLUMN

Every public project renders "Bitcoin Balance" beside its funding address and
it has always read 0.00 — not because the addresses are empty, but because
`projects.bitcoin_balance_btc` DOES NOT EXIST. The page selects `*`, the field
comes back undefined, and `|| 0` prints a confident zero. The owner-only
"Refresh Balance" button could not repair it either: its route names the
missing column in a select list, PostgREST answers 42703, and the handler maps
that to "Project not found". Unfixable AND unfalsifiable from the UI.

This is the bug #946 fixed one level down for wallet cards — "an unchecked
balance is unknown, not zero", a fabricated zero presented as freshly read from
the blockchain. A project holding Bitcoin looked empty, and a visitor deciding
whether to fund it was misled either way.

Both columns are added NULLABLE and are NOT defaulted to 0, because NULL is the
state the old code could not express: nobody has ever asked the chain.
`bitcoin_balance_updated_at` — not the number — now decides what renders, so an
unread address shows "—" and "Not checked yet".

2. THE PAYMENT SURFACE WAS IN THE "MORE" MENU

A profile with two live wallets read, to its own owner, as a profile with no
way to be paid. Nothing was broken in the data: GET /api/wallets returned both,
countPublicWallets saw both, the tab was built. It was simply not in
PRIMARY_PROFILE_TAB_IDS, so it sat in the overflow menu where nobody looked. On
a platform whose premise is "get paid by anyone", the tab answering "how do I
pay this person?" is the one ranking that must never be made. A test had pinned
the old decision with `not.toContain('wallets')`; that assertion was the bug,
and it is now inverted with the reason written down.

3. FOUR WALLETS THAT ALL CLAIMED TO BE PRIMARY

Production held four primary wallets for one profile, ALL soft-deleted, while
the two live wallets were not primary at all — so "the primary wallet" resolved
to a deleted row, non-deterministically. Three holes, each stranding a flag
somewhere no later correction could reach:

  - enforceSinglePrimary() cleared siblings with `.eq('is_active', true)`, so a
    wallet deactivated WHILE primary kept the flag forever. A function named
    "enforce single primary" that skips rows enforces nothing. Now it clears
    every sibling, live or not.
  - The soft delete wrote is_active=false and left is_primary standing. It now
    clears both in the same write.
  - createWallet() decides is_primary from a count of ACTIVE wallets, so once an
    entity's wallets are all soft-deleted the next one is born primary. That one
    is not patched in code — the invariant is now the DATABASE's job via partial
    unique indexes scoped to live rows, so a duplicate cannot be written even by
    a client posting is_primary:true directly.

Verified before writing the constraint: zero entities platform-wide hold more
than one ACTIVE primary, so the indexes apply without a backfill conflict. The
four stranded flags are cleared by the same migration.

Not addressed here, deliberately: the two Coinos rows are the PLATFORM's
receiving wallet (orangecat@coinos.io, the default in config/ecosystem.ts) sitting
on a personal profile, and it is custodial. Moving it is a data change on live
rows and a product decision, not a bug fix.

16 tests. Each fix was mutated away and the matching gate fails: rail reverted to
the raw number (2 fail), enforceSinglePrimary re-scoped to active (1), wallets
back in overflow (1), migration deleted (2), soft delete leaving the flag (1).
Restored, all 16 pass. Full verify green: 380 files, 3432 tests.


Claude-Session: https://claude.ai/code/session_01HJRuvHJEBd8t7iRA9Sb1iw

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-15 06:17:38 -07:00
..