Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 50 additions & 15 deletions test/specs/send.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
elementByIdWithin,
expectText,
expectTextWithin,
formatSats,
getReceiveAddress,
receiveOnchainFunds,
sleep,
Expand Down Expand Up @@ -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...');
Expand All @@ -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,
});
Expand All @@ -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
Expand Down Expand Up @@ -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<number> {
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<void> {
const wait = () => expectTextWithin('ActivitySpending', expected);
if (driver.isAndroid) {
Expand Down