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

fix: correct amounts in LSPS1 order summary #450

Merged
merged 3 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
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
26 changes: 24 additions & 2 deletions api/lsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/getAlby/nostr-wallet-connect/lnclient"
"github.com/getAlby/nostr-wallet-connect/logger"
"github.com/getAlby/nostr-wallet-connect/lsp"
decodepay "github.com/nbd-wtf/ln-decodepay"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -120,9 +121,30 @@ func (api *api) NewInstantChannelInvoice(ctx context.Context, request *NewInstan
return nil, err
}

paymentRequest, err := decodepay.Decodepay(invoice)
if err != nil {
logger.Logger.WithError(err).Error("Failed to decode bolt11 invoice")
return nil, err
}

invoiceAmount := uint64(paymentRequest.MSatoshi / 1000)
incomingLiquidity := uint64(0)
outgoingLiquidity := uint64(0)

if invoiceAmount < request.Amount {
// assume that the invoice is only the fee
// and that the user is requesting incoming liquidity (LSPS1)
incomingLiquidity = request.Amount
} else {
outgoingLiquidity = invoiceAmount - fee
}

newChannelResponse := &NewInstantChannelInvoiceResponse{
Invoice: invoice,
Fee: fee,
Invoice: invoice,
Fee: fee,
InvoiceAmount: invoiceAmount,
IncomingLiquidity: incomingLiquidity,
OutgoingLiquidity: outgoingLiquidity,
}

logger.Logger.WithFields(logrus.Fields{
Expand Down
7 changes: 5 additions & 2 deletions api/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ type NewInstantChannelInvoiceRequest struct {
}

type NewInstantChannelInvoiceResponse struct {
Invoice string `json:"invoice"`
Fee uint64 `json:"fee"`
Invoice string `json:"invoice"`
Fee uint64 `json:"fee"`
InvoiceAmount uint64 `json:"invoiceAmount"`
IncomingLiquidity uint64 `json:"incomingLiquidity"`
OutgoingLiquidity uint64 `json:"outgoingLiquidity"`
}
30 changes: 29 additions & 1 deletion frontend/src/screens/channels/CurrentChannelOrder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,32 @@ function PayLightningChannelOrder({ order }: { order: NewChannelOrder }) {
<div className="border rounded-lg">
<Table>
<TableBody>
{wrappedInvoiceResponse.outgoingLiquidity > 0 && (
<TableRow>
<TableCell className="font-medium p-3">
Spending Balance
</TableCell>
<TableCell className="text-right p-3">
{new Intl.NumberFormat().format(
wrappedInvoiceResponse.outgoingLiquidity
)}{" "}
sats
</TableCell>
</TableRow>
)}
{wrappedInvoiceResponse.incomingLiquidity > 0 && (
<TableRow>
<TableCell className="font-medium p-3">
Incoming Liquidity
</TableCell>
<TableCell className="text-right p-3">
{new Intl.NumberFormat().format(
wrappedInvoiceResponse.incomingLiquidity
)}{" "}
sats
</TableCell>
</TableRow>
)}
<TableRow>
<TableCell className="font-medium p-3 flex flex-row gap-1.5 items-center">
Fee
Expand All @@ -632,7 +658,9 @@ function PayLightningChannelOrder({ order }: { order: NewChannelOrder }) {
Amount to pay
</TableCell>
<TableCell className="font-semibold text-right p-3">
{new Intl.NumberFormat().format(parseInt(order.amount))}{" "}
{new Intl.NumberFormat().format(
wrappedInvoiceResponse.invoiceAmount
)}{" "}
sats
</TableCell>
</TableRow>
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ export type NewInstantChannelInvoiceRequest = {
export type NewInstantChannelInvoiceResponse = {
invoice: string;
fee: number;
invoiceAmount: number;
incomingLiquidity: number;
outgoingLiquidity: number;
};

export type RedeemOnchainFundsResponse = {
Expand Down
Loading