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

feat: show pending and failed transactions for nwc #3292

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"dependencies": {
"@bitcoinerlab/secp256k1": "^1.1.1",
"@getalby/sdk": "^3.6.0",
"@getalby/sdk": "^3.8.2",
"@headlessui/react": "^1.7.18",
"@lightninglabs/lnc-web": "^0.3.1-alpha",
"@noble/ciphers": "^0.5.1",
Expand Down
39 changes: 34 additions & 5 deletions src/app/components/TransactionsTable/TransactionModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
PopiconsArrowUpSolid,
PopiconsChevronBottomLine,
PopiconsChevronTopLine,
PopiconsXSolid,
} from "@popicons/react";
import dayjs from "dayjs";
import { useEffect, useState } from "react";
Expand Down Expand Up @@ -56,17 +57,43 @@ export default function TransactionModal({
<div>
<div className="flex items-center justify-center">
{getTransactionType(transaction) == "outgoing" ? (
<div className="flex justify-center items-center bg-orange-100 dark:bg-orange-950 rounded-full p-3">
<PopiconsArrowUpSolid className="w-10 h-10 text-orange-400 dark:text-amber-600 stroke-[1px] stroke-orange-400 dark:stroke-amber-600" />
</div>
transaction.state === "pending" ? (
<div className="flex justify-center items-center bg-orange-100 dark:bg-orange-950 rounded-full p-3 animate-pulse">
pavanjoshi914 marked this conversation as resolved.
Show resolved Hide resolved
<PopiconsArrowUpSolid className="w-10 h-10 rotate-45 text-orange-400 dark:text-amber-600 stroke-[1px] stroke-orange-400 dark:stroke-amber-600" />
</div>
) : transaction.state === "failed" ? (
<div className="flex justify-center items-center bg-red-100 dark:bg-rose-950 rounded-full p-3">
<PopiconsXSolid className="w-10 h-10 text-red-400 dark:text-rose-600 stroke-[1px] stroke-red-400 dark:stroke-red-600" />
</div>
) : (
<div className="flex justify-center items-center bg-orange-100 dark:bg-orange-950 rounded-full p-3">
<PopiconsArrowUpSolid className="w-10 h-10 text-orange-400 dark:text-amber-600 stroke-[1px] stroke-orange-400 dark:stroke-amber-600" />
</div>
)
) : (
<div className="flex justify-center items-center bg-green-100 dark:bg-emerald-950 rounded-full p-3">
<PopiconsArrowDownSolid className="w-10 h-10 text-green-500 dark:text-emerald-500 stroke-[1px] stroke-green-400 dark:stroke-emerald-500" />
</div>
)}
</div>
<h2 className="mt-4 text-md text-gray-900 font-bold dark:text-white text-center">
{transaction.type == "received" ? t("received") : t("sent")}

<h2
className={classNames(
"mt-4 text-md text-gray-900 font-bold dark:text-white text-center",
transaction.state == "pending" && "animate-pulse text-gray-400"
)}
>
{transaction.type == "received"
? t("received")
: t(
transaction.state === "settled"
? "sent"
: transaction.state === "pending"
? "sending"
: transaction.state === "failed"
? "failed"
: "sent"
)}
</h2>
</div>
<div className="flex items-center text-center justify-center dark:text-white">
Expand All @@ -76,6 +103,8 @@ export default function TransactionModal({
"text-3xl font-medium",
transaction.type == "received"
? "text-green-600 dark:text-emerald-500"
: transaction.state == "failed"
? "text-red-400 dark:text-rose-600"
: "text-orange-600 dark:text-amber-600"
)}
>
Expand Down
43 changes: 37 additions & 6 deletions src/app/components/TransactionsTable/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import Loading from "@components/Loading";
import { PopiconsArrowDownSolid, PopiconsArrowUpSolid } from "@popicons/react";
import {
PopiconsArrowDownSolid,
PopiconsArrowUpSolid,
PopiconsXSolid,
} from "@popicons/react";

import { useState } from "react";
import { useTranslation } from "react-i18next";
Expand Down Expand Up @@ -60,9 +64,19 @@ export default function TransactionsTable({
<div className="flex gap-3">
<div className="flex items-center">
{type == "outgoing" ? (
<div className="flex justify-center items-center bg-orange-100 dark:bg-orange-950 rounded-full w-8 h-8">
<PopiconsArrowUpSolid className="w-5 h-5 text-orange-400 dark:text-amber-600 stroke-[1px] stroke-orange-400 dark:stroke-amber-600" />
</div>
tx.state === "pending" ? (
<div className="flex justify-center items-center bg-orange-100 dark:bg-orange-950 rounded-full w-8 h-8 animate-pulse">
<PopiconsArrowUpSolid className="w-5 h-5 rotate-45 text-orange-400 dark:text-amber-600 stroke-[1px] stroke-orange-400 dark:stroke-amber-600" />
</div>
) : tx.state === "failed" ? (
<div className="flex justify-center items-center bg-red-100 dark:bg-rose-950 rounded-full w-8 h-8">
<PopiconsXSolid className="w-5 h-5 text-red-400 dark:text-rose-600 stroke-[1px] stroke-red-400 dark:stroke-red-600" />
</div>
) : (
<div className="flex justify-center items-center bg-orange-100 dark:bg-orange-950 rounded-full w-8 h-8">
<PopiconsArrowUpSolid className="w-5 h-5 text-orange-400 dark:text-amber-600 stroke-[1px] stroke-orange-400 dark:stroke-amber-600" />
</div>
)
) : (
<div className="flex justify-center items-center bg-green-100 dark:bg-emerald-950 rounded-full w-8 h-8">
<PopiconsArrowDownSolid className="w-5 h-5 text-green-500 dark:text-emerald-500 stroke-[1px] stroke-green-400 dark:stroke-emerald-500" />
Expand All @@ -71,10 +85,25 @@ export default function TransactionsTable({
</div>
<div className="overflow-hidden mr-3">
<div className="text-sm font-medium text-black truncate dark:text-white">
<p className="truncate">
<p
className={classNames(
"truncate",
tx.state == "pending" && "animate-pulse"
)}
>
{tx.title ||
tx.boostagram?.message ||
(type == "incoming" ? t("received") : t("sent"))}
(type == "incoming"
? t("received")
: t(
tx.state === "settled"
? "sent"
: tx.state === "pending"
? "sending"
: tx.state === "failed"
? "failed"
: "sent"
))}
</p>
</div>
<p className="text-xs text-gray-400 dark:text-neutral-500">
Expand All @@ -88,6 +117,8 @@ export default function TransactionsTable({
"text-sm",
type == "incoming"
? "text-green-600 dark:text-emerald-500"
: tx.state == "failed"
? "text-red-600 dark:text-rose-500"
: "text-orange-600 dark:text-amber-600"
)}
>
Expand Down
8 changes: 5 additions & 3 deletions src/app/hooks/useTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ export const useTransactions = () => {
const getTransactionsResponse = await api.getTransactions({
limit,
});

const transactions = getTransactionsResponse.transactions.map(
(transaction) => ({
...transaction,
title: transaction.memo,
date: dayjs(transaction.settleDate).fromNow(),
timestamp: transaction.settleDate,
date: dayjs(
transaction.settleDate || transaction.creationDate
).fromNow(),
timestamp:
pavanjoshi914 marked this conversation as resolved.
Show resolved Hide resolved
transaction.settleDate || transaction.creationDate || Date.now(),
})
);

Expand Down
2 changes: 1 addition & 1 deletion src/common/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function mergeTransactions(
payments: ConnectorTransaction[]
): ConnectorTransaction[] {
const mergedTransactions = [...invoices, ...payments].sort((a, b) => {
return b.settleDate - a.settleDate;
return (b.settleDate ?? 0) - (a.settleDate ?? 0);
});

return mergedTransactions;
Expand Down
29 changes: 16 additions & 13 deletions src/extension/background-script/actions/ln/getTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ const getTransactions = async (message: MessageGetTransactions) => {
const connector = await state.getState().getConnector();
try {
const result = await connector.getTransactions();

let transactions: ConnectorTransaction[] = result.data.transactions
.filter((transaction) => transaction.settled)
.map((transaction) => {
const boostagram = utils.getBoostagramFromInvoiceCustomRecords(
transaction.custom_records
);
return {
...transaction,
boostagram,
paymentHash: transaction.payment_hash,
};
});
const isNWC = connector.connectorType == "nwc";
pavanjoshi914 marked this conversation as resolved.
Show resolved Hide resolved
// we show pending and failed transactions for nwc. pass on all transactions for nwc connector to frontend
let transactions: ConnectorTransaction[] = result.data.transactions;
if (!isNWC) {
transactions = transactions.filter((transaction) => transaction.settled);
}
transactions = transactions.map((transaction) => {
const boostagram = utils.getBoostagramFromInvoiceCustomRecords(
transaction.custom_records
);
return {
...transaction,
boostagram,
paymentHash: transaction.payment_hash,
};
});

if (limit) {
transactions = transactions.slice(0, limit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ export interface ConnectorTransaction {
/**
* Settle date in UNIX milliseconds
*/
settleDate: number;
settleDate: number | null;
creationDate?: number;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which connectors do not have a creation date? can't this be taken from the bolt11 invoice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all other connectors only pass settled transactions and we don't even need creationDate for them except for nwc. so i kept it optional and passed this field only for nwc connector. maybe we can be consistent accross and pass it for all now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for commando there is no creation date passed via api. https://docs.corelightning.org/reference/lightning-listinvoices

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can be taken from the bolt11 invoice, can't it?

totalAmount: number;
displayAmount?: [number, ACCOUNT_CURRENCIES];
type: "received" | "sent";
state?: "settled" | "pending" | "failed";
}

export interface MakeInvoiceArgs {
Expand Down Expand Up @@ -150,6 +152,7 @@ export default interface Connector {
getOAuthToken?(): OAuthToken | undefined;
getSwapInfo?(): Promise<SwapInfoResponse>;
createSwap?(params: CreateSwapParams): Promise<CreateSwapResponse>;
connectorType?: string;
}

export function flattenRequestMethods(methods: string[]) {
Expand Down
2 changes: 1 addition & 1 deletion src/extension/background-script/connectors/lawallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export default class LaWallet implements Connector {
return {
data: {
transactions: parsedTransactions.sort(
(a, b) => b.settleDate - a.settleDate
(a, b) => (b.settleDate ?? 0) - (a.settleDate ?? 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this connector should be affected like this in this PR? if lawallet only has settled transactions, there is now extra code that does not even apply.

Can't you sort the transactions before mapping them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that was just to make typescript happy we have common type definitions for all the connectors. now settleDate can be either number | null (cause of nwc pending and failed transactions). any better approach here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you sort the transactions before mapping them?

),
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/extension/background-script/connectors/lndhub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export default class LndHub implements Connector {
})
)
.sort((a, b) => {
return b.settleDate - a.settleDate;
return (b.settleDate ?? 0) - (a.settleDate ?? 0);
});

return invoices;
Expand Down
11 changes: 8 additions & 3 deletions src/extension/background-script/connectors/nwc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,25 @@ class NWCConnector implements Connector {

async getTransactions(): Promise<GetTransactionsResponse> {
const listTransactionsResponse = await this.nwc.listTransactions({
unpaid: false,
limit: 50, // restricted by relay max event payload size
unpaid_outgoing: true,
});

const transactions: ConnectorTransaction[] =
listTransactionsResponse.transactions.map(
(transaction, index): ConnectorTransaction => ({
id: `${index}`,
memo: transaction.description,
preimage: transaction.preimage,
payment_hash: transaction.payment_hash,
settled: true,
settled: transaction.state == "settled",
settleDate: transaction.settled_at * 1000,
creationDate: transaction.created_at * 1000,
totalAmount: Math.floor(transaction.amount / 1000),
type: transaction.type == "incoming" ? "received" : "sent",
custom_records: this.tlvToCustomRecords(
transaction.metadata?.["tlv_records"] as TLVRecord[] | undefined
),
state: transaction.state,
})
);
return {
Expand Down Expand Up @@ -221,6 +222,10 @@ class NWCConnector implements Connector {
throw new Error("Method not implemented.");
}

get connectorType(): string {
return "nwc";
}

private customRecordsToTlv(
customRecords: Record<string, string>
): TlvRecord[] {
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,8 @@
"payment_hash": "Payment Hash",
"received": "Received",
"sent": "Sent",
"sending": "Sending",
"failed": "Failed",
"date_time": "Date & Time",
"boostagram": {
"sender": "Sender",
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@ export type Transaction = {
type?: "sent" | "received";
value?: string;
publisherLink?: string; // either the invoice URL if on PublisherSingleView, or the internal link to Publisher
state?: "settled" | "pending" | "failed";
};

export interface DbPayment {
Expand Down Expand Up @@ -958,7 +959,8 @@ export interface Invoice {
memo?: string;
type: "received" | "sent";
settled: boolean;
settleDate: number;
settleDate: number | null;
creationDate?: number;
totalAmount: number;
totalAmountFiat?: string;
displayAmount?: [number, ACCOUNT_CURRENCIES];
Expand Down
Loading
Loading