Skip to content

Commit

Permalink
chore: check if amount > 80% of total spendable/receivable
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Jul 19, 2024
1 parent 118c20d commit 42159f2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 54 deletions.
36 changes: 14 additions & 22 deletions frontend/src/screens/wallet/Receive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,28 +127,20 @@ export default function Receive() {
title="Receive"
description="Create a lightning invoice that can be paid by any bitcoin lightning wallet"
/>
{!!channels?.length && (
<>
{/* If all channels have less than 20% incoming capacity, show a warning */}
{channels?.every(
(channel) =>
channel.remoteBalance <
(channel.localBalance + channel.remoteBalance) * 0.2 ||
parseInt(amount) * 1000 > channel.remoteBalance
) && (
<Alert>
<AlertTriangle className="h-4 w-4" />
<AlertTitle>Low receiving capacity</AlertTitle>
<AlertDescription>
You likely won't be able to receive payments until you{" "}
<Link className="underline" to="/channels/incoming">
increase your receiving capacity.
</Link>
</AlertDescription>
</Alert>
)}
</>
)}
{hasChannelManagement &&
parseInt(amount || "0") * 1000 >
0.8 * balances.lightning.totalReceivable && (
<Alert>
<AlertTriangle className="h-4 w-4" />
<AlertTitle>Low receiving capacity</AlertTitle>
<AlertDescription>
You likely won't be able to receive payments until you{" "}
<Link className="underline" to="/channels/incoming">
increase your receiving capacity.
</Link>
</AlertDescription>
</Alert>
)}
<div className="flex gap-12 w-full">
<div className="w-full max-w-lg">
{invoice ? (
Expand Down
57 changes: 25 additions & 32 deletions frontend/src/screens/wallet/Send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,20 @@ export default function Send() {
const { toast } = useToast();
const [isLoading, setLoading] = React.useState(false);
const [invoice, setInvoice] = React.useState("");
const [invoiceDetails, setInvoiceDetails] = React.useState<Invoice | null>(
null
);
const [payResponse, setPayResponse] =
React.useState<PayInvoiceResponse | null>(null);
const [paymentDone, setPaymentDone] = React.useState(false);
const [showConfirmation, setShowConfirmation] = React.useState(false);

if (!balances || !channels) {
return <Loading />;
}

const handleContinue = () => {
try {
new Invoice({ pr: invoice });
setShowConfirmation(true);
setInvoiceDetails(new Invoice({ pr: invoice }));
} catch (error) {
toast({
variant: "destructive",
Expand Down Expand Up @@ -97,7 +98,7 @@ export default function Send() {
title: "Failed to send: " + e,
});
setInvoice("");
setShowConfirmation(false);
setInvoiceDetails(null);
console.error(e);
}
setLoading(false);
Expand All @@ -119,27 +120,21 @@ export default function Send() {
title="Send"
description="Pay a lightning invoice created by any bitcoin lightning wallet"
/>
{!!channels?.length && (
<>
{/* If all channels have less or equal balance than their reserve, show a warning */}
{channels?.every(
(channel) =>
channel.localBalance <=
channel.unspendablePunishmentReserve * 1000
) && (
<Alert>
<AlertTriangle className="h-4 w-4" />
<AlertTitle>Low spending balance</AlertTitle>
<AlertDescription>
You won't be able to make payments until you{" "}
<Link className="underline" to="/channels/outgoing">
increase your spending balance.
</Link>
</AlertDescription>
</Alert>
)}
</>
)}
{invoiceDetails &&
hasChannelManagement &&
invoiceDetails.satoshi * 1000 >
0.8 * balances.lightning.totalSpendable && (
<Alert>
<AlertTriangle className="h-4 w-4" />
<AlertTitle>Low spending balance</AlertTitle>
<AlertDescription>
You won't be able to make payments until you{" "}
<Link className="underline" to="/channels/outgoing">
increase your spending balance.
</Link>
</AlertDescription>
</Alert>
)}
<div className="flex gap-12 w-full">
<div className="w-full max-w-lg">
{paymentDone ? (
Expand All @@ -164,7 +159,7 @@ export default function Send() {
className="mt-4 w-full"
onClick={() => {
setPaymentDone(false);
setShowConfirmation(false);
setInvoiceDetails(null);
setPayResponse(null);
setInvoice("");
}}
Expand All @@ -185,15 +180,13 @@ export default function Send() {
</>
)}
</>
) : showConfirmation ? (
) : invoiceDetails ? (
<form onSubmit={handleSubmit} className="grid gap-5">
<div className="">
<p className="text-lg mb-5">Payment Details</p>
<p className="font-bold">
{new Invoice({ pr: invoice }).satoshi} sats
</p>
<p className="font-bold">{invoiceDetails.satoshi} sats</p>
<p className="text-muted-foreground">
{new Invoice({ pr: invoice }).description}
{invoiceDetails.description}
</p>
</div>
<div className="flex gap-5">
Expand All @@ -206,7 +199,7 @@ export default function Send() {
Confirm Payment
</LoadingButton>
<Button
onClick={() => setShowConfirmation(false)}
onClick={() => setInvoiceDetails(null)}
variant="secondary"
>
Back
Expand Down

0 comments on commit 42159f2

Please sign in to comment.