Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
feat: pay lsps1 orders with internal wallet (#538)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Bumann <[email protected]>
  • Loading branch information
rolznz and bumi authored Jun 30, 2024
1 parent 0f3a285 commit 8efe0d4
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 10 deletions.
89 changes: 84 additions & 5 deletions frontend/src/screens/channels/CurrentChannelOrder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Node,
OpenChannelRequest,
OpenChannelResponse,
PayInvoiceResponse,
} from "src/types";

import { Payment, init } from "@getalby/bitcoin-connect-react";
Expand Down Expand Up @@ -610,6 +611,18 @@ function PayLightningChannelOrder({ order }: { order: NewChannelOrder }) {
order.lspUrl,
]);

const canPayInternally =
channels &&
wrappedInvoiceResponse &&
channels.some(
(channel) =>
channel.localSpendableBalance / 1000 >
wrappedInvoiceResponse.invoiceAmount
);
const [isPaying, setPaying] = React.useState(false);
const [paid, setPaid] = React.useState(false);
const [payExternally, setPayExternally] = React.useState(false);

return (
<div className="flex flex-col gap-5">
<AppHeader
Expand Down Expand Up @@ -679,11 +692,77 @@ function PayLightningChannelOrder({ order }: { order: NewChannelOrder }) {
</TableBody>
</Table>
</div>
<Payment
invoice={wrappedInvoiceResponse.invoice}
payment={newChannel ? { preimage: "dummy preimage" } : undefined}
paymentMethods="external"
/>
{paid ? (
<div className="flex gap-2 items-center justify-center">
<Loading /> <p>Waiting for channel to be opened...</p>
</div>
) : (
<>
{canPayInternally && (
<>
<LoadingButton
loading={isPaying}
className="mt-4"
onClick={async () => {
try {
if (!csrf) {
throw new Error("csrf not loaded");
}
setPaying(true);
const payInvoiceResponse =
await request<PayInvoiceResponse>(
`/api/payments/${wrappedInvoiceResponse.invoice}`,
{
method: "POST",
headers: {
"X-CSRF-Token": csrf,
"Content-Type": "application/json",
},
}
);
if (payInvoiceResponse) {
setPaid(true);
toast({
title: "Channel successfully requested",
});
}
setPaid(true);
} catch (e) {
toast({
variant: "destructive",
title: "Failed to send: " + e,
});
console.error(e);
}
setPaying(false);
}}
>
Pay and open channel
</LoadingButton>
{!payExternally && (
<Button
type="button"
variant="link"
className="text-muted-foreground text-xs"
onClick={() => setPayExternally(true)}
>
Pay with another wallet
</Button>
)}
</>
)}

{(payExternally || !canPayInternally) && (
<Payment
invoice={wrappedInvoiceResponse.invoice}
payment={
newChannel ? { preimage: "dummy preimage" } : undefined
}
paymentMethods="external"
/>
)}
</>
)}
</div>
</>
)}
Expand Down
9 changes: 4 additions & 5 deletions frontend/src/screens/wallet/Send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ export default function Send() {

const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
if (!csrf) {
throw new Error("csrf not loaded");
}
try {
if (!csrf) {
throw new Error("csrf not loaded");
}
setLoading(true);
const payInvoiceResponse = await request<PayInvoiceResponse>(
`/api/payments/${invoice.trim()}`,
Expand All @@ -68,9 +68,8 @@ export default function Send() {
title: "Failed to send: " + e,
});
console.error(e);
} finally {
setLoading(false);
}
setLoading(false);
};

const copy = () => {
Expand Down

0 comments on commit 8efe0d4

Please sign in to comment.