Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(send): toast when trying to pay LN invoice with 0 balance #1965

Merged
merged 1 commit into from
Jun 6, 2024
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
31 changes: 10 additions & 21 deletions src/utils/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import {
addPeer,
decodeLightningInvoice,
getLightningBalance,
getOpenChannels,
} from './lightning';
import { EAvailableNetwork } from './networks';
import { savePeer } from '../store/utils/lightning';
Expand Down Expand Up @@ -618,7 +617,7 @@ export const processBitcoinTransactionData = async ({
try {
// Reset existing transaction state and prepare for a new one.
await resetSendTransaction();
await setupOnChainTransaction({});
await setupOnChainTransaction();

let response;
let error: ToastOptions | undefined; //Information that will be passed as a notification.
Expand All @@ -639,16 +638,15 @@ export const processBitcoinTransactionData = async ({
if (inputValue > onchainBalance) {
onchainBalance = inputValue;
}
const openChannels = getOpenChannels();

// Filter for the lightning invoice.
const filteredLightningInvoice = data.find(
(d) => d.qrDataType === EQRDataType.lightningPaymentRequest,
) as TLightningUrl;
// Look for a Lightning invoice
const lightningInvoice = data.find((d) => {
return d.qrDataType === EQRDataType.lightningPaymentRequest;
}) as TLightningUrl;
let decodedLightningInvoice: TInvoice | undefined;
if (filteredLightningInvoice) {
if (lightningInvoice) {
const decodeInvoiceRes = await decodeLightningInvoice({
paymentRequest: filteredLightningInvoice.lightningPaymentRequest ?? '',
paymentRequest: lightningInvoice.lightningPaymentRequest,
});
if (decodeInvoiceRes.isOk()) {
decodedLightningInvoice = decodeInvoiceRes.value;
Expand All @@ -664,19 +662,10 @@ export const processBitcoinTransactionData = async ({
}

// Attempt to pay with lightning first.
// 1. Check we have a lightning invoice
// 2. Ensure the invoice has not expired.
// 3. Ensure we have some open channels.
// 4. Ensure we have a lightning balance.
if (
decodedLightningInvoice &&
!decodedLightningInvoice.is_expired &&
openChannels.length &&
spendingBalance
) {
if (decodedLightningInvoice && !decodedLightningInvoice.is_expired) {
// Ensure we can afford to pay the lightning invoice. If so, pass it through.
if (spendingBalance >= requestedAmount) {
response = filteredLightningInvoice;
if (spendingBalance !== 0 && spendingBalance >= requestedAmount) {
response = lightningInvoice;
} else {
const amount = requestedAmount - spendingBalance;
const { bitcoinFormatted } = getBitcoinDisplayValues({
Expand Down
Loading