Skip to content

Commit

Permalink
fix: add payment hash, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
reneaaron committed Oct 30, 2023
1 parent 88a93ee commit 32e17a0
Show file tree
Hide file tree
Showing 15 changed files with 185 additions and 139 deletions.
1 change: 1 addition & 0 deletions src/app/hooks/useTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const useTransactions = () => {
isSettled: true,
limit,
});

const transactions = getTransactionsResponse.transactions.map(
(transaction) => ({
...transaction,
Expand Down
7 changes: 4 additions & 3 deletions src/app/screens/Home/DefaultView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ export type Props = {
};

const DefaultView: FC<Props> = (props) => {
const itemsLimit = 8;

const { t } = useTranslation("translation", { keyPrefix: "home" });
const { t: tCommon } = useTranslation("common");

const navigate = useNavigate();

const { account, balancesDecorated, accountLoading } = useAccount();
const { account, accountLoading } = useAccount();

const lightningAddress = account?.lightningAddress || "";

Expand All @@ -47,11 +49,10 @@ const DefaultView: FC<Props> = (props) => {
useTransactions();

const isLoading = accountLoading || isLoadingTransactions;
const itemsLimit = 8;

useEffect(() => {
loadTransactions(itemsLimit);
}, [balancesDecorated?.accountBalance, loadTransactions, itemsLimit]);
}, [loadTransactions, itemsLimit]);

// check if currentURL is blocked
useEffect(() => {
Expand Down
6 changes: 5 additions & 1 deletion src/extension/background-script/actions/ln/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ const transactions = async (message: MessageTransactions) => {
const boostagram = utils.getBoostagramFromInvoiceCustomRecords(
transaction.custom_records
);
return { ...transaction, boostagram };
return {
...transaction,
boostagram,
paymentHash: transaction.payment_hash,
};
});

if (limit) {
Expand Down
16 changes: 9 additions & 7 deletions src/extension/background-script/connectors/alby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import state from "../state";
import Connector, {
CheckPaymentArgs,
CheckPaymentResponse,
ConnectorInvoice,
ConnectorTransaction,
ConnectPeerResponse,
GetBalanceResponse,
GetInfoResponse,
Expand Down Expand Up @@ -94,15 +94,16 @@ export default class Alby implements Connector {
client.incomingInvoices({})
)) as Invoice[];

const invoices: ConnectorInvoice[] = incomingInvoices.map(
(invoice, index): ConnectorInvoice => ({
const invoices: ConnectorTransaction[] = incomingInvoices.map(
(invoice, index): ConnectorTransaction => ({
custom_records: invoice.custom_records,
id: `${invoice.payment_request}-${index}`,
payment_hash: invoice.payment_hash,
memo: invoice.comment || invoice.memo,
preimage: "", // alby wallet api doesn't support preimage (yet)
settled: invoice.settled,
settleDate: new Date(invoice.settled_at).getTime(),
totalAmount: `${invoice.amount}`,
totalAmount: invoice.amount,
type: "received",
})
);
Expand All @@ -118,15 +119,16 @@ export default class Alby implements Connector {
client.invoices({})
)) as Invoice[];

const transactions: ConnectorInvoice[] = invoicesResponse.map(
(invoice, index): ConnectorInvoice => ({
const transactions: ConnectorTransaction[] = invoicesResponse.map(
(invoice, index): ConnectorTransaction => ({
custom_records: invoice.custom_records,
id: `${invoice.payment_request}-${index}`,
memo: invoice.comment || invoice.memo,
preimage: "", // alby wallet api doesn't support preimage (yet)
payment_hash: invoice.payment_hash,
settled: invoice.settled,
settleDate: new Date(invoice.settled_at).getTime(),
totalAmount: `${invoice.amount}`,
totalAmount: invoice.amount,
type: invoice.type == "incoming" ? "received" : "sent",
})
);
Expand Down
4 changes: 2 additions & 2 deletions src/extension/background-script/connectors/citadel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class CitadelConnector implements Connector {
// not yet implemented
async getInvoices(): Promise<GetInvoicesResponse> {
console.error(
`Not yet supported with the currently used account: ${this.constructor.name}`
`getInvoices() is not yet supported with the currently used account: ${this.constructor.name}`
);
throw new Error(
`${this.constructor.name}: "getInvoices" is not yet supported. Contact us if you need it.`
Expand All @@ -89,7 +89,7 @@ class CitadelConnector implements Connector {

async getTransactions(): Promise<GetTransactionsResponse> {
console.error(
`Not yet supported with the currently used account: ${this.constructor.name}`
`getTransactions() is not yet supported with the currently used account: ${this.constructor.name}`
);
return { data: { transactions: [] } };
}
Expand Down
46 changes: 26 additions & 20 deletions src/extension/background-script/connectors/commando.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Account } from "~/types";
import Connector, {
CheckPaymentArgs,
CheckPaymentResponse,
ConnectorInvoice,
ConnectorTransaction,
ConnectPeerArgs,
ConnectPeerResponse,
flattenRequestMethods,
Expand Down Expand Up @@ -227,14 +227,15 @@ export default class Commando implements Connector {
data: {
invoices: parsed.invoices
.map(
(invoice, index): ConnectorInvoice => ({
(invoice, index): ConnectorTransaction => ({
id: invoice.label,
memo: invoice.description,
settled: invoice.status === "paid",
preimage: invoice.payment_preimage,
payment_hash: invoice.payment_hash,
settleDate: invoice.paid_at * 1000,
type: "received",
totalAmount: (invoice.msatoshi / 1000).toString(),
totalAmount: Math.floor(invoice.msatoshi / 1000),
})
)
.filter((invoice) => invoice.settled)
Expand All @@ -248,7 +249,24 @@ export default class Commando implements Connector {

async getTransactions(): Promise<GetTransactionsResponse> {
const incomingInvoicesResponse = await this.getInvoices();
const outgoingInvoicesResponse = await this.ln
const outgoingInvoicesResponse = await this.getPayments();

const transactions: ConnectorTransaction[] = [
...incomingInvoicesResponse.data.invoices,
...outgoingInvoicesResponse.data.payments,
].sort((a, b) => {
return b.settleDate - a.settleDate;
});

return {
data: {
transactions,
},
};
}

private async getPayments() {
return await this.ln
.commando({
method: "listsendpays",
params: {},
Expand All @@ -260,33 +278,21 @@ export default class Commando implements Connector {
data: {
payments: parsed.payments
.map(
(payment, index): ConnectorInvoice => ({
(payment, index): ConnectorTransaction => ({
id: `${payment.id}`,
memo: payment.description || "Sent",
memo: payment.description ?? "",
settled: payment.status === "complete",
preimage: payment.payment_preimage,
payment_hash: payment.payment_hash,
settleDate: payment.created_at * 1000,
type: "sent",
totalAmount: (payment.amount_sent_msat / 1000).toString(),
totalAmount: payment.amount_sent_msat / 1000,
})
)
.filter((payment) => payment.settled),
},
};
});

const transactions: ConnectorInvoice[] = [
...incomingInvoicesResponse.data.invoices,
...outgoingInvoicesResponse.data.payments,
].sort((a, b) => {
return b.settleDate - a.settleDate;
});

return {
data: {
transactions,
},
};
}

async getInfo(): Promise<GetInfoResponse> {
Expand Down
18 changes: 13 additions & 5 deletions src/extension/background-script/connectors/connector.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ interface Route {
total_fees: number;
}

export interface ConnectorInvoice {
export interface ConnectorTransaction {
custom_records?: {
"696969"?: string;
"7629169"?: string;
"5482373484"?: string;
} & Record<string, string>;
id: string;
memo: string;
memo?: string;
preimage: string;
payment_hash?: string;
settled: boolean;
settleDate: number;
totalAmount: string;
totalAmount: number;
type: "received" | "sent";
}

Expand Down Expand Up @@ -57,15 +58,22 @@ export type GetBalanceResponse = {

export type GetInvoicesResponse = {
data: {
invoices: ConnectorInvoice[];
invoices: ConnectorTransaction[];
};
};

export type GetTransactionsResponse = {
data: {
transactions: ConnectorInvoice[];
transactions: ConnectorTransaction[];
};
};

export type GetPaymentsResponse = {
data: {
payments: ConnectorTransaction[];
};
};

export type SendPaymentResponse = {
data: {
preimage: string;
Expand Down
9 changes: 5 additions & 4 deletions src/extension/background-script/connectors/eclair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Account } from "~/types";
import Connector, {
CheckPaymentArgs,
CheckPaymentResponse,
ConnectorInvoice,
ConnectorTransaction,
ConnectPeerResponse,
GetBalanceResponse,
GetInfoResponse,
Expand Down Expand Up @@ -76,7 +76,7 @@ class Eclair implements Connector {

async getInvoices(): Promise<GetInvoicesResponse> {
const response = await this.request("/listinvoices");
const invoices: ConnectorInvoice[] = response
const invoices: ConnectorTransaction[] = response
.map(
(invoice: {
paymentHash: string;
Expand All @@ -85,14 +85,15 @@ class Eclair implements Connector {
amount: number;
}) => ({
id: invoice.paymentHash,
payment_hash: invoice.paymentHash,
memo: invoice.description,
settled: true,
settleDate: invoice.timestamp * 1000,
totalAmount: invoice.amount / 1000,
type: "received",
})
)
.sort((a: ConnectorInvoice, b: ConnectorInvoice) => {
.sort((a: ConnectorTransaction, b: ConnectorTransaction) => {
return b.settleDate - a.settleDate;
});
return {
Expand All @@ -104,7 +105,7 @@ class Eclair implements Connector {

async getTransactions(): Promise<GetTransactionsResponse> {
console.error(
`Not yet supported with the currently used account: ${this.constructor.name}`
`getTransactions() is not yet supported with the currently used account: ${this.constructor.name}`
);
return { data: { transactions: [] } };
}
Expand Down
4 changes: 2 additions & 2 deletions src/extension/background-script/connectors/galoy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ class Galoy implements Connector {
// not yet implemented
async getInvoices(): Promise<GetInvoicesResponse> {
console.error(
`Not yet supported with the currently used account: ${this.constructor.name}`
`getInvoices() is not yet supported with the currently used account: ${this.constructor.name}`
);
return { data: { invoices: [] } };
}

async getTransactions(): Promise<GetTransactionsResponse> {
console.error(
`Not yet supported with the currently used account: ${this.constructor.name}`
`getTransactions() is not yet supported with the currently used account: ${this.constructor.name}`
);
return { data: { transactions: [] } };
}
Expand Down
11 changes: 6 additions & 5 deletions src/extension/background-script/connectors/kollider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Account } from "~/types";
import Connector, {
CheckPaymentArgs,
CheckPaymentResponse,
ConnectorInvoice,
ConnectorTransaction,
ConnectPeerResponse,
GetBalanceResponse,
GetInfoResponse,
Expand Down Expand Up @@ -109,17 +109,18 @@ export default class Kollider implements Connector {
}[]
>("GET", "/getuserinvoices", undefined);

const invoices: ConnectorInvoice[] = data
const invoices: ConnectorTransaction[] = data
.filter((i) => i.incoming)
.filter((i) => i.account_id === this.currentAccountId)
.map(
(invoice, index): ConnectorInvoice => ({
(invoice, index): ConnectorTransaction => ({
id: `${invoice.payment_hash}-${index}`,
payment_hash: invoice.payment_hash,
memo: invoice.reference,
preimage: "", // kollider doesn't support preimage (yet)
settled: invoice.settled,
settleDate: invoice.settled_date,
totalAmount: `${invoice.value}`,
totalAmount: invoice.value,
type: "received",
})
)
Expand All @@ -136,7 +137,7 @@ export default class Kollider implements Connector {

async getTransactions(): Promise<GetTransactionsResponse> {
console.error(
`Not yet supported with the currently used account: ${this.constructor.name}`
`getTransactions() is not yet supported with the currently used account: ${this.constructor.name}`
);
return { data: { transactions: [] } };
}
Expand Down
Loading

0 comments on commit 32e17a0

Please sign in to comment.