From d554a13cf4560891010f50e117754825c1f1be7a Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sat, 1 Aug 2026 15:02:01 -0700 Subject: Make NWC budgets accumulate and travel across devices MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit onRequest handled every request against the connection object captured at listen() time, so each spend was recorded against a stale snapshot — spentToday restarted from the old value on every payment and the daily budget never actually bound. The connection is now re-resolved from stored state per request, which also stops serving a connection revoked on another device mid-session. recordSpend now publishes the sync snapshot so the spend (and the payment's movement + vtxo change) reaches the user's other devices. Test: two payments must accumulate to 52. Co-Authored-By: Claude Fable 5 --- tools/nwc-test.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'tools') diff --git a/tools/nwc-test.js b/tools/nwc-test.js index 64d1cd6..7b7f8c3 100644 --- a/tools/nwc-test.js +++ b/tools/nwc-test.js @@ -92,6 +92,20 @@ r = await request('pay_invoice', { invoice: 'lnbc50u1p4xu8whsp5jjnp3jzhjrgp3992d check('over-limit payment refused', r?.error?.code === 'QUOTA_EXCEEDED', JSON.stringify(r?.error)); check('nothing was paid', paid.length === 0); +console.log('\n[cumulative spend]'); +// a real 21-sat invoice (long expired — the mock seam never pays anything); +// two payments must ACCUMULATE: recordSpend once restarted from a stale +// snapshot of the connection, so only the last payment ever counted +const INV21 = 'lnbc210n1p4xuk2wpp506wkjr0xk3677nu7je9c55vq4lzlkyd0ztcq2mlvumap0zpe3alqhp5ppg7g8qwpdv34hgpymhw446y37duzwcn388yp3pw05n7tlulyn2scqzysxqrrssrzjqv3dpepm8kfdxrk3sl6wzqdf49s9c0h9ljtjrek6c08r6aejlwcnur0dwyqqvusqqqqqqqlgqqqq86qqjqsp5dc0jrq94ke2f4dzx8c2dwqsc6a65eu56dt2j599l7kxp7q2hs6zq9qxpqysgqdjeft8gkl0uga24e502pvcp5vgsfap3dxuutcpgfaj33fffuqs9psmnrklshp3fg3py7vlnzsea90vj9ahqq5t9xuy67u3pk0sfnheqpn95f2g'; +r = await request('pay_invoice', { invoice: INV21 }); +check('first payment succeeds', !!r?.result?.preimage, JSON.stringify(r?.error || r)); +r = await request('pay_invoice', { invoice: INV21 }); +check('second payment succeeds', !!r?.result?.preimage, JSON.stringify(r?.error || r)); +{ + const spent = JSON.parse(store['fs:nwc']).conns.find((x) => x.id === 'test1').spentToday; + check('spend accumulates across payments', spent === 52, String(spent)); // 2 × (25 + 1 fee) from the mock seam +} + console.log('\n[nip04 fallback]'); r = await request('get_balance', {}, 'nip04'); check('nip04 request answered', r?.result?.balance === 42000*1000); -- cgit v1.2.3