feat(wallet): a balance that isn't loaded yet says so, in pixels
Demo images / Build & push demo images (push) Failing after 2m18s
Demo images / Build & push demo images (push) Failing after 2m18s
An unloaded balance rendered as `0`. Zero is not a loading state — it is
a number, and it is the one number that frightens people. Someone
opening the dashboard while the RPCs were still in flight was told, in
the wallet's own typeface, that their money was gone.
There is no formatting fix for that. The fix is to stop claiming a
figure we do not have, so `null` now means "not known yet" and `0` means
"none", and the two are kept apart end to end: the refs start at null,
a rail becomes a number only when its call actually succeeds, and a
snapshot key that was never written stays unknown instead of becoming a
zero.
In place of the figure, a small dot-matrix scans in the rail's own
colour. It inherits currentColor, so on-chain shimmers orange, Lightning
yellow, Cashu purple, Fedimint blue and Ark teal with no colour table to
keep in sync — and it is sized to the figure it stands in for, so
nothing jumps when the real number lands. It carries role="status" and
names what it is waiting for; a shimmering box with no text is nothing
at all to a screen reader.
Two consequences worth stating. The total is withheld until every rail
that makes it up is known — summing nulls as zero would show a total
*lower* than the rails beneath it, which is worse than showing nothing
because it looks authoritative. And the Ark row stays hidden while its
balance is unknown, since "unknown" must not be read as "> 0" on the
many nodes with no Ark sidecar.
The LND app UI had the same bug in a different shape: its tiles start as
an em-dash, but renderBalances() runs on every poll including before the
first response, and `num(null && …)` is 0 — so the dashes were painted
over with "0 sats" almost immediately. Same treatment, in plain CSS.
Also fixes a stale assertion in AppHeroSection's suite, which has been
red since 9ccc325a changed "Restarting..." to a real ellipsis; and two
test proofs that used a plausible-looking hex string for `C`. The V3
codec never parses that field so it went unnoticed, but the V4 encoder
hands it to the reference implementation, which checks the point is
actually on secp256k1. Real curve points now.
Frontend: 996 tests green. Backend: 1436 green.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
fc98c1d8dd
commit
fa6fe32ef9
@@ -0,0 +1,104 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
/**
|
||||
* A balance figure, or — while it is still unknown — a pixel readout in place
|
||||
* of it.
|
||||
*
|
||||
* The problem this exists for: an unloaded balance used to render as `0`.
|
||||
* Zero is not "loading", it is a *number*, and it is the one number that
|
||||
* frightens people. Someone opening the dashboard while the RPCs are still in
|
||||
* flight was told, in the wallet's own typeface, that their money was gone.
|
||||
* There is no formatting fix for that — the fix is to not claim a figure we
|
||||
* do not have yet.
|
||||
*
|
||||
* So `sats` is nullable, and `null` means "not known yet" rather than "none".
|
||||
* Callers must keep that distinction alive: a balance ref should start at
|
||||
* `null` and only become a number when a call actually succeeds.
|
||||
*
|
||||
* The placeholder is a small dot-matrix that scans in the rail's own colour —
|
||||
* it inherits `currentColor`, so the on-chain row shimmers orange, Lightning
|
||||
* yellow, Cashu purple, Fedimint blue and Ark teal with no colour mapping to
|
||||
* keep in sync. It is deliberately about as wide as the figure it stands in
|
||||
* for, so nothing jumps when the real number lands.
|
||||
*/
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
/** Balance in sats, or null/undefined while it is still unknown. */
|
||||
sats: number | null | undefined
|
||||
/** Trailing unit. Set to '' for bare figures. */
|
||||
suffix?: string
|
||||
/** Named for screen readers, e.g. "on-chain balance". */
|
||||
label?: string
|
||||
}>(),
|
||||
{ suffix: 'sats', label: 'balance' },
|
||||
)
|
||||
|
||||
// 14 columns × 2 rows. Enough to read as a matrix rather than a spinner, and
|
||||
// close to the width of a five-figure sat amount.
|
||||
const CELLS = 28
|
||||
|
||||
/**
|
||||
* Built as one string rather than interpolated around a `<template>`, so the
|
||||
* space before the unit cannot be eaten by Vue's whitespace condensing — and
|
||||
* so a test reading `.text()` sees exactly what a person reads on screen.
|
||||
*/
|
||||
const display = computed(() => {
|
||||
if (props.sats == null) return ''
|
||||
const figure = props.sats.toLocaleString()
|
||||
return props.suffix ? `${figure} ${props.suffix}` : figure
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<span
|
||||
v-if="props.sats == null"
|
||||
class="balance-pixels"
|
||||
role="status"
|
||||
aria-live="polite"
|
||||
:aria-label="`Loading ${props.label}`"
|
||||
:title="`Loading ${props.label}…`"
|
||||
>
|
||||
<span v-for="i in CELLS" :key="i" class="balance-pixel" :style="{ '--i': i }" />
|
||||
</span>
|
||||
<span v-else>{{ display }}</span>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.balance-pixels {
|
||||
display: inline-grid;
|
||||
grid-template-columns: repeat(14, 3px);
|
||||
grid-auto-rows: 3px;
|
||||
gap: 1px;
|
||||
/* Sit on the text baseline so a row's height doesn't change when the real
|
||||
figure replaces this. */
|
||||
vertical-align: -1px;
|
||||
}
|
||||
|
||||
.balance-pixel {
|
||||
width: 3px;
|
||||
height: 3px;
|
||||
border-radius: 0.5px;
|
||||
background: currentColor;
|
||||
opacity: 0.16;
|
||||
/* The wave runs left-to-right across columns; the two rows of a column are
|
||||
offset slightly so it reads as a scan rather than a marching block. */
|
||||
animation: balance-pixel-scan 1.4s ease-in-out infinite;
|
||||
animation-delay: calc(var(--i) * 45ms);
|
||||
}
|
||||
|
||||
@keyframes balance-pixel-scan {
|
||||
0%, 70%, 100% { opacity: 0.16; }
|
||||
25% { opacity: 1; }
|
||||
45% { opacity: 0.42; }
|
||||
}
|
||||
|
||||
/* Motion is decoration here — the dimmed matrix still reads as "no figure
|
||||
yet", which is the part that carries the meaning. */
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.balance-pixel {
|
||||
animation: none;
|
||||
opacity: 0.35;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -221,15 +221,15 @@
|
||||
<div v-if="arkStatus?.available" class="grid grid-cols-3 gap-2 mb-4">
|
||||
<div class="p-3 bg-white/5 rounded-lg text-center">
|
||||
<p class="text-[11px] text-white/40 mb-1">Spendable</p>
|
||||
<p class="text-sm text-teal-400 font-medium">{{ (arkBalance?.spendable_sats ?? 0).toLocaleString() }} sats</p>
|
||||
<p class="text-sm text-teal-400 font-medium"><BalanceAmount :sats="arkBalance?.spendable_sats" label="spendable Ark balance" /></p>
|
||||
</div>
|
||||
<div class="p-3 bg-white/5 rounded-lg text-center">
|
||||
<p class="text-[11px] text-white/40 mb-1">Pending</p>
|
||||
<p class="text-sm text-white/70 font-medium">{{ (arkBalance?.pending_sats ?? 0).toLocaleString() }} sats</p>
|
||||
<p class="text-sm text-white/70 font-medium"><BalanceAmount :sats="arkBalance?.pending_sats" label="pending Ark balance" /></p>
|
||||
</div>
|
||||
<div class="p-3 bg-white/5 rounded-lg text-center">
|
||||
<p class="text-[11px] text-white/40 mb-1">On-chain</p>
|
||||
<p class="text-sm text-white/70 font-medium">{{ (arkBalance?.onchain_sats ?? 0).toLocaleString() }} sats</p>
|
||||
<p class="text-sm text-white/70 font-medium"><BalanceAmount :sats="arkBalance?.onchain_sats" label="on-chain Ark balance" /></p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -318,6 +318,7 @@ import { useI18n } from 'vue-i18n'
|
||||
import { rpcClient } from '@/api/rpc-client'
|
||||
import BaseModal from '@/components/BaseModal.vue'
|
||||
import LightningChannelsPanel from '@/components/LightningChannelsPanel.vue'
|
||||
import BalanceAmount from '@/components/BalanceAmount.vue'
|
||||
import { useTxExplorer, EXPLORER_PLACEHOLDER } from '@/composables/useTxExplorer'
|
||||
|
||||
const { t } = useI18n()
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { mount } from '@vue/test-utils'
|
||||
import BalanceAmount from '../BalanceAmount.vue'
|
||||
import HomeWalletCard from '@/views/home/HomeWalletCard.vue'
|
||||
import i18n from '@/i18n'
|
||||
|
||||
/**
|
||||
* The distinction this whole component exists to protect: `0` is a balance,
|
||||
* `null` is the absence of one. Rendering the first when you mean the second
|
||||
* tells someone their money is gone, in the wallet's own typeface. Every case
|
||||
* below is really one assertion — that the two never get confused.
|
||||
*/
|
||||
describe('BalanceAmount', () => {
|
||||
it('shows the pixel readout when the balance is not known yet', () => {
|
||||
const w = mount(BalanceAmount, { props: { sats: null, label: 'on-chain balance' } })
|
||||
expect(w.find('.balance-pixels').exists()).toBe(true)
|
||||
expect(w.text()).not.toContain('0')
|
||||
})
|
||||
|
||||
it('treats undefined the same as null', () => {
|
||||
// Optional props (`arkBalance?.spendable_sats`) arrive as undefined, not
|
||||
// null, and must not fall through to a figure.
|
||||
const w = mount(BalanceAmount, { props: { sats: undefined } })
|
||||
expect(w.find('.balance-pixels').exists()).toBe(true)
|
||||
})
|
||||
|
||||
it('shows a genuine zero as a figure, not as loading', () => {
|
||||
// The inverse mistake: a node that really has no coins must be told so
|
||||
// plainly, not left shimmering forever.
|
||||
const w = mount(BalanceAmount, { props: { sats: 0 } })
|
||||
expect(w.find('.balance-pixels').exists()).toBe(false)
|
||||
expect(w.text()).toBe('0 sats')
|
||||
})
|
||||
|
||||
it('formats a real balance with thousands separators', () => {
|
||||
const w = mount(BalanceAmount, { props: { sats: 9922 } })
|
||||
expect(w.text()).toBe('9,922 sats')
|
||||
})
|
||||
|
||||
it('can drop the unit for bare figures', () => {
|
||||
const w = mount(BalanceAmount, { props: { sats: 21, suffix: '' } })
|
||||
expect(w.text()).toBe('21')
|
||||
})
|
||||
|
||||
it('announces what is loading instead of being silently empty', () => {
|
||||
// A shimmering box with no text is nothing at all to a screen reader.
|
||||
const w = mount(BalanceAmount, { props: { sats: null, label: 'Cashu balance' } })
|
||||
const el = w.find('.balance-pixels')
|
||||
expect(el.attributes('role')).toBe('status')
|
||||
expect(el.attributes('aria-label')).toBe('Loading Cashu balance')
|
||||
})
|
||||
|
||||
it('inherits the rail colour rather than hard-coding one', () => {
|
||||
// The pixels are painted with currentColor, which is what makes the
|
||||
// on-chain row orange and the Cashu row purple with no colour table to
|
||||
// keep in sync. Guard the mechanism: a literal colour here would drift.
|
||||
const w = mount(BalanceAmount, { props: { sats: null } })
|
||||
expect(w.find('.balance-pixel').exists()).toBe(true)
|
||||
expect(w.html()).not.toMatch(/background:\s*#|rgb\(/)
|
||||
})
|
||||
|
||||
it('renders enough cells to read as a matrix', () => {
|
||||
const w = mount(BalanceAmount, { props: { sats: null } })
|
||||
expect(w.findAll('.balance-pixel').length).toBe(28)
|
||||
})
|
||||
})
|
||||
|
||||
describe('HomeWalletCard balances', () => {
|
||||
const base = {
|
||||
animate: false,
|
||||
walletConnected: true,
|
||||
walletOnchain: null,
|
||||
walletLightning: null,
|
||||
walletEcash: null,
|
||||
walletFedimint: null,
|
||||
walletArk: null,
|
||||
walletTransactions: [],
|
||||
isDev: false,
|
||||
}
|
||||
|
||||
const mountCard = (props: Record<string, unknown>) =>
|
||||
mount(HomeWalletCard, { props: { ...base, ...props }, global: { plugins: [i18n] } })
|
||||
|
||||
it('shows no figures at all before anything has loaded', () => {
|
||||
const w = mountCard({})
|
||||
// Six rows could be showing 0 sats here; none of them may.
|
||||
expect(w.findAll('.balance-pixels').length).toBeGreaterThan(0)
|
||||
expect(w.text()).not.toMatch(/\b0 sats\b/)
|
||||
})
|
||||
|
||||
it('withholds the total until every rail it sums is known', () => {
|
||||
// A total computed with nulls as 0 would read *lower* than the rails
|
||||
// beneath it — worse than showing nothing, because it looks authoritative.
|
||||
const w = mountCard({ walletOnchain: 5000, walletLightning: null, walletEcash: 0, walletFedimint: 0 })
|
||||
expect(w.text()).not.toContain('5,000 sats\n')
|
||||
expect(w.findAll('.balance-pixels').length).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
it('sums the total once every rail has reported', () => {
|
||||
const w = mountCard({ walletOnchain: 9000, walletLightning: 900, walletEcash: 22, walletFedimint: 0 })
|
||||
expect(w.text()).toContain('9,922 sats')
|
||||
expect(w.findAll('.balance-pixels').length).toBe(0)
|
||||
})
|
||||
|
||||
it('shows an empty wallet as zero rather than as loading', () => {
|
||||
const w = mountCard({ walletOnchain: 0, walletLightning: 0, walletEcash: 0, walletFedimint: 0 })
|
||||
expect(w.findAll('.balance-pixels').length).toBe(0)
|
||||
expect(w.text()).toContain('0 sats')
|
||||
})
|
||||
|
||||
it('keeps the Ark row hidden while its balance is unknown', () => {
|
||||
// Ark only appears once barkd reports something; "unknown" must not be
|
||||
// read as "> 0" and conjure a row on the many nodes with no Ark sidecar.
|
||||
const loaded = { walletOnchain: 1, walletLightning: 0, walletEcash: 0, walletFedimint: 0 }
|
||||
expect(mountCard({ ...loaded, walletArk: null }).text()).not.toContain('Ark')
|
||||
expect(mountCard({ ...loaded, walletArk: 0 }).text()).not.toContain('Ark')
|
||||
expect(mountCard({ ...loaded, walletArk: 7 }).text()).toContain('Ark')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user