aboutsummaryrefslogtreecommitdiffstats
path: root/tools/unified-balance-test.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-11-15 19:30:16 -0700
committerAdam Soltys <[email protected]>2026-11-15 19:30:16 -0700
commite883fefbc9baff516af25869560cf6198c603fab (patch)
tree5781667c93d057f04d42ce2af603635ec943fb4e /tools/unified-balance-test.js
parent5554c7066113976b49c18c5c7505dd252769e72d (diff)
downloadcoinosv3-e883fefbc9baff516af25869560cf6198c603fab.tar.gz
coinosv3-e883fefbc9baff516af25869560cf6198c603fab.zip
One balance: Spending and Savings on the same seed are one account. "Do you think we could combine the ARC and savings Bitcoin accounts into a single account? So if someone receives into a on-chain Bitcoin address, would hold it there, but they'd have a combined history, and it would select which coins to spend based on where you're paying out of, but the balance would be unified" … "Yeah go for it" (Adam, 2026-11-15). Step 1 of two: the wallet shows one balance card (the total, Ark and on-chain, with money mid-board still counted; an Instant / On-chain split appears only when there's some in both). History is one timeline, and a move between the two halves is one row priced at its cost: a board's on-chain Sent and its Boarded become "Made instant" (or "Making instant" while it confirms), and an offboard to our own address becomes "Moved on-chain". An offboard to someone else reads Sent. Receive leads with the payment address, with a Bitcoin address button for on-chain payers; the address stays on-chain and is all that's offered offline. Payments start from the Ark coins, and an on-chain address is paid from on-chain coins when there are any, leaving Ark only when there aren't. Accounts lists one row, Settings → Wallet has one balance row (Spending's coin settings plus the public key), and a per-device "Show Spending and Savings separately" brings the two-card carousel back as it was. Sign-ups are now this one account instead of Spending-only with Savings added later. Older Spending-only accounts are converted the same way, and the Add savings row only shows when the balances are shown apart. Not yet (step 2): covering a shortfall by boarding or offboarding automatically, and the "available now" split on the amount screen. New unified-balance-test (folded rows, their costs, the separate Spending timeline, on-chain-first routing) passes, along with signup-quick (one balance, Bitcoin address on Receive, separate toggle and back), balance-settings-row and send-enter-keys (both now opt into separate; send-enter-keys also enters through the seed-phrase door since Create became a sign-up), features-smoke, onboard-backup, gift-profile-setup, footer-support, pos, chat-layout, account-delete, lock-wallet, padlock-flow, profile-actions, gift-build, ark-ln-savings-small, carousel-flick, amber-dm-upgrade, send-lnaddr-advance, account-switch-leak and ark-offline-init. feed-choice-per-wallet fails at its second wallet's import, and fails the same way on 5554c70.
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VMan6SwkivduFDDkMmnXjZ
Diffstat (limited to 'tools/unified-balance-test.js')
-rw-r--r--tools/unified-balance-test.js127
1 files changed, 127 insertions, 0 deletions
diff --git a/tools/unified-balance-test.js b/tools/unified-balance-test.js
new file mode 100644
index 0000000..c349368
--- /dev/null
+++ b/tools/unified-balance-test.js
@@ -0,0 +1,127 @@
+// One balance (Adam, 2026-11-15: "combine the Ark and savings Bitcoin
+// accounts into a single account … a combined history … the balance would be
+// unified"). The Ark feature's side of it, without a browser:
+// - the combined timeline tells a move between the two halves once: a board
+// (its on-chain Sent + its Boarded) and an offboard to our own address
+// (its Moved to Savings + the on-chain Received) are one row each, worth
+// what the move cost; a board still confirming is one pending row; an
+// offboard to someone else stays a payment
+// - the separate Spending timeline is untouched
+// - an on-chain address is paid from on-chain coins when there are any, and
+// leaves Ark (offboard) only when there aren't
+// Run: bun tools/unified-balance-test.js
+import { buildFeatures } from '../src/features/index.js';
+import { getArkConfig } from '../src/api.js';
+
+let fails = 0;
+const check = (name, ok, detail = '') => {
+ console.log(` ${ok ? '✓' : '✗'} ${name}${detail ? ' — ' + String(detail).slice(0, 240) : ''}`);
+ if (!ok) fails++;
+};
+
+globalThis.fetch = async () => { throw new Error('offline (test)'); };
+globalThis.window = undefined;
+globalThis.localStorage = {
+ _d: {}, getItem(k) { return this._d[k] ?? null; }, setItem(k, v) { this._d[k] = String(v); },
+ removeItem(k) { delete this._d[k]; }, key(i) { return Object.keys(this._d)[i] ?? null; },
+ get length() { return Object.keys(this._d).length; },
+};
+const cfg = getArkConfig();
+localStorage.setItem('btc-wallet-arkinfo:' + cfg.ark, JSON.stringify({ info: { minBoardAmountSat: 1000, boardFees: {} }, tip: 1, at: Date.now() }));
+
+const T = Date.now() - 3600_000;
+const A = 'a'.repeat(64), B = 'b'.repeat(64), C = 'c'.repeat(64), D = 'd'.repeat(64), E = 'e'.repeat(64);
+const arkState = {
+ _rev: 1,
+ vtxos: [{ id: 'v1', amountSat: 20000, state: 'spendable' }],
+ movements: [
+ { id: 'm-board', type: 'board', amountSat: 9000, status: 'complete', txid: A, ts: T },
+ { id: 'm-off', type: 'offboard', amountSat: 5000, feeSat: 300, status: 'complete', txid: B, to: 'bc1qown', ts: T + 1000 },
+ { id: 'm-off-ext', type: 'offboard', amountSat: 7000, feeSat: 300, status: 'complete', txid: D, to: 'bc1qsomeoneelse', ts: T + 2000 },
+ { id: 'm-recv', type: 'receive', amountSat: 1234, status: 'complete', vtxoId: 'v1', ts: T + 3000 },
+ ],
+ actions: [
+ { id: `board-${T + 4000}`, type: 'board', step: 'funded', amountSat: 3000, feeSat: 0, fundingTxid: C },
+ ],
+};
+// the on-chain wallet's view: board fundings out, the own offboard in
+const txs = [
+ { txid: A, net: -9200, confirmed: true, blockTime: T / 1000 },
+ { txid: B, net: 5000, confirmed: true, blockTime: (T + 1000) / 1000 },
+ { txid: C, net: -3150, confirmed: false },
+ { txid: E, net: 40000, confirmed: true, blockTime: (T + 5000) / 1000 },
+];
+
+const store = {};
+const wallet = {
+ watchOnly: false, offline: false, spendable: 0, feeRates: { halfHourFee: 1 },
+ utxos: [], txs, history: txs, receive: [], change: [],
+ account: () => ({}),
+ isOnchainAddress: (a) => /^bc1q/.test(a),
+ loadFeatureState: (n, fb) => JSON.parse(store[n] || 'null') || fb,
+ saveFeatureState: (n, st) => { store[n] = JSON.stringify(st); },
+ _cacheKey: () => 'test', saveCache: () => {},
+ nostrPubkey: () => 'ab'.repeat(32),
+ registerCacheExtension: () => {}, registerRealtimeHook: () => {}, registerLoadHook: () => {},
+ registerScanHook: () => {}, registerCacheSavedHook: () => {}, registerCoinLock: () => {},
+ loadArkState: () => arkState, peekArkState: () => arkState, saveArkState: () => {},
+ _arkNs: () => 'ns', _arkKey: () => 'k', _arkHoldKey: () => 'h', _arkVtxoOwner: () => null, adoptArkState: () => null,
+ freshReceive: () => ({ address: 'bc1qtest' }), freshChange: () => ({ address: 'bc1qchange' }),
+ balance: () => ({ spendableSat: 0 }), emit: () => {}, on: () => {},
+};
+let unified = true;
+const ui = { account: 'spending', send: { recipients: [{ address: '' }] } };
+let features = [];
+const ctx = {
+ h: (tag, attrs, ...kids) => ({ tag, attrs: attrs || {}, kids: kids.flat().filter((k) => k != null && k !== false) }),
+ ui, render: () => {}, wallet,
+ hook: (name, ...args) => { for (const f of features) if (f[name]) { const v = f[name](...args); if (v) return v; } return null; },
+ getAccount: () => ui.account, setAccount: (a) => { ui.account = a; }, unifiedBalance: () => unified,
+ goHome: () => {}, openMnemonic: async () => {},
+ fmtAmount: (n) => Number(n).toLocaleString('en-US'), unitLabel: () => 'sats', unitTag: () => 'sats', getUnit: () => 'sats',
+ parseAmount: (v) => parseInt(v, 10) || 0, amountField: (n) => String(n),
+ copyBtn: () => null, pasteBtn: () => null, toast: () => {}, blankSend: () => ({ recipients: [{ address: '' }] }),
+ openExternal: () => {},
+};
+features = buildFeatures(ctx);
+const ark = features.find((f) => f.id === 'ark');
+const text = (n) => typeof n === 'string' || typeof n === 'number' ? String(n) : n && n.kids ? n.kids.map(text).join(' ') : '';
+
+console.log('[one timeline]');
+{
+ const rows = ark.historyEntries(txs, 'all');
+ const replaced = rows.map((e) => e.replacesTxid).filter(Boolean).sort();
+ check('the board, the own offboard and the confirming board stand in for their on-chain rows', replaced.join() === [A, B, C].sort().join(), replaced.map((x) => x[0]).join());
+ check('the offboard to someone else is not a move', !replaced.includes(D));
+ const byTx = Object.fromEntries(rows.filter((e) => e.replacesTxid).map((e) => [e.replacesTxid, text(e.render())]));
+ check('the board reads Made instant, worth its cost (9,200 out, 9,000 in)', /Made instant/.test(byTx[A]) && /\b200\b/.test(byTx[A]) && /9,000/.test(byTx[A]), byTx[A]);
+ check('the own offboard reads Moved on-chain, worth its fee', /Moved on-chain/.test(byTx[B]) && /\b300\b/.test(byTx[B]), byTx[B]);
+ check('the confirming board is one pending row', /Making instant/.test(byTx[C]) && /Pending/.test(byTx[C]) && /\b150\b/.test(byTx[C]), byTx[C]);
+ const others = rows.filter((e) => !e.replacesTxid).map((e) => text(e.render())).join(' | ');
+ check('the other Ark rows are as they were', /Received/.test(others) && !/Boarded/.test(others), others);
+ check('the offboard to someone else reads Sent, not Moved to Savings', /Sent/.test(others) && !/Savings/.test(others), others);
+}
+
+console.log('[the Spending timeline, shown apart]');
+{
+ const rows = ark.historyEntries([], 'spending');
+ check('no row stands in for an on-chain one', rows.every((e) => !e.replacesTxid), rows.length);
+ check('the board is still its own Boarded row', rows.some((e) => /Boarded/.test(text(e.render()))));
+ check('Savings gets no Ark rows', ark.historyEntries(txs, 'savings').length === 0);
+}
+
+console.log('[paying an on-chain address]');
+{
+ ui.arkOffboardSend = null; wallet.spendable = 50000;
+ ark.matchSendText('bc1qrecipient');
+ check('with on-chain coins it is an ordinary on-chain send', !ui.arkOffboardSend);
+ ui.arkOffboardSend = null; wallet.spendable = 0;
+ ark.matchSendText('bc1qrecipient');
+ check('without them it leaves Ark', !!ui.arkOffboardSend && ui.arkOffboardSend.address === 'bc1qrecipient');
+ ui.arkOffboardSend = null; wallet.spendable = 50000; unified = false;
+ ark.matchSendText('bc1qrecipient');
+ check('shown apart, Spending still pays it from Ark (as before)', !!ui.arkOffboardSend);
+}
+
+console.log(fails ? `FAIL (${fails})` : 'PASS');
+process.exit(fails ? 1 : 0);