From 4454bfdaa8f00a34a98cb20ce151cbb565e4edd9 Mon Sep 17 00:00:00 2001 From: Piotr Stachyra Date: Fri, 4 Dec 2026 20:24:03 +0100 Subject: [PATCH] Size Android QuickPay invoices from the wallet USD quote --- test/specs/send.e2e.ts | 65 ++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 15 deletions(-) diff --git a/test/specs/send.e2e.ts b/test/specs/send.e2e.ts index 368de00f..8dc3d6f0 100644 --- a/test/specs/send.e2e.ts +++ b/test/specs/send.e2e.ts @@ -16,6 +16,7 @@ import { elementByIdWithin, expectText, expectTextWithin, + formatSats, getReceiveAddress, receiveOnchainFunds, sleep, @@ -334,7 +335,8 @@ describe('@send - Send', () => { // send to lightning invoice w/ amount (quickpay) console.info('Sending to lightning invoice w/ amount (quickpay)...'); - const { paymentRequest: invoice7 } = await lnd.addInvoice({ value: '1000' }); + const { smallSats, aboveCapSats } = await quickPayInvoiceAmounts(amtAfterUnified5); + const { paymentRequest: invoice7 } = await lnd.addInvoice({ value: String(smallSats) }); // enable quickpay console.info('Enabling quickpay...'); @@ -349,11 +351,12 @@ describe('@send - Send', () => { await tapAndExpect({ id: 'Close' }, { anyVisible: [{ id: 'TotalBalance-primary' }] }); const amtAfterLightning2 = await totalBalanceChangedFrom(totalBalance, amtAfterUnified5); console.info({ amtAfterLightning2 }); - await expectSpendingBalanceText('5 000'); + let spendingSats = 6_000 - smallSats; + await expectSpendingBalanceText(formatSats(spendingSats)); // send to unified invoice w/ amount (quickpay) console.info('Sending to unified invoice w/ amount (quickpay)...'); - const { paymentRequest: invoice8 } = await lnd.addInvoice({ value: '1000' }); + const { paymentRequest: invoice8 } = await lnd.addInvoice({ value: String(smallSats) }); const unified7 = encode(onchainAddress, { lightning: invoice8, }); @@ -365,27 +368,35 @@ describe('@send - Send', () => { await withMoveToSparkIntroRecovery(async () => { await expect(totalBalance).not.toHaveText(amtAfterLightning2); }); - await expectSpendingBalanceText('4 000'); + spendingSats -= smallSats; + await expectSpendingBalanceText(formatSats(spendingSats)); // send to lightning invoice w/ amount (skip quickpay for large amounts) console.info('Sending to lightning invoice w/ amount (skip quickpay for large amounts)...'); - // TEMP: receive more funds to be able to pay 10k invoice - console.info('Receiving lightning funds...'); - await mineBlocks(1); - await electrum?.waitForSync(); - const receive2 = await getReceiveAddress('lightning'); - await swipeFullScreen('down'); - const r = await lnd.sendPaymentSync({ paymentRequest: receive2, amt: '10000' }); - console.info({ r }); - await acknowledgeReceivedPayment(); - await expectSpendingBalanceText('14 000'); + // Leave room for the channel reserve and fees. Each inbound payment is limited + // to 10,000 sats on this 100,000-sat channel (bitkit-android#364). + while (spendingSats < aboveCapSats + 2_000) { + await mineBlocks(1); + await electrum?.waitForSync(); + const receive2 = await getReceiveAddress('lightning'); + await swipeFullScreen('down'); + const funding = await lnd.sendPaymentSync({ paymentRequest: receive2, amt: '10000' }); + if (funding.paymentError) throw new Error(`Lightning top-up failed: ${funding.paymentError}`); + await acknowledgeReceivedPayment(); + spendingSats += 10_000; + await expectSpendingBalanceText(formatSats(spendingSats)); + } - const { paymentRequest: invoice9 } = await lnd.addInvoice({ value: '10000' }); + const { paymentRequest: invoice9 } = await lnd.addInvoice({ value: String(aboveCapSats) }); console.info({ invoice9 }); await enterGateAddress(invoice9, { acceptCameraPermission: false }); await elementById('ReviewAmount').waitForDisplayed(); + if (driver.isAndroid) { + await expect(reviewAmt).toHaveText(formatSats(aboveCapSats)); + expect(await displayedUsd('ReviewAmount-secondary')).toBeGreaterThan(5); + } await swipeFullScreen('down'); // sanity check activities displayed @@ -487,6 +498,30 @@ describe('@send - Send', () => { }); }); +async function quickPayInvoiceAmounts(balanceText: string) { + if (driver.isIOS) return { smallSats: 1_000, aboveCapSats: 10_000 }; + + const usd = await displayedUsd('TotalBalance-secondary'); + const satsPerDollar = Number(balanceText.replace(/\s/g, '')) / usd; + if (!Number.isFinite(satsPerDollar) || satsPerDollar < 1) { + throw new Error(`Invalid QuickPay quote: ${balanceText} sats = USD ${usd}`); + } + + // Target at most $1 for each automatic payment and $8 for manual review. + // Both leave headroom around the default $5 limit when the quote refreshes. + return { + smallSats: Math.min(1_000, Math.floor(satsPerDollar)), + aboveCapSats: Math.ceil(8 * satsPerDollar), + }; +} + +async function displayedUsd(balanceId: string): Promise { + const symbol = await elementByIdWithin(balanceId, 'MoneyFiatSymbol'); + await expect(symbol).toHaveText('$'); + const amount = await elementByIdWithin(balanceId, 'MoneyText'); + return Number((await amount.getText()).replace(/,/g, '')); +} + async function expectSpendingBalanceText(expected: string): Promise { const wait = () => expectTextWithin('ActivitySpending', expected); if (driver.isAndroid) {