Skip to content

Commit

Permalink
Receiver wallet history
Browse files Browse the repository at this point in the history
  • Loading branch information
quietbits committed Oct 23, 2023
1 parent cb5d48e commit 2145129
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 87 deletions.
32 changes: 0 additions & 32 deletions src/api/getStellarAccountPayments.ts

This file was deleted.

81 changes: 81 additions & 0 deletions src/apiQueries/useStellarAccountPayments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { useQuery } from "@tanstack/react-query";
import { getStellarTransaction } from "api/getStellarTransaction";
import { HORIZON_URL } from "constants/settings";
import { fetchStellarApi } from "helpers/fetchStellarApi";
import { shortenAccountKey } from "helpers/shortenAccountKey";
import {
ApiStellarOperationRecord,
ApiStellarPaymentType,
AppError,
ReceiverWalletPayment,
} from "types";

const ACCEPTED_TYPE: ApiStellarPaymentType[] = [
"payment",
"path_payment_strict_send",
"path_payment_strict_receive",
];

export const useStellarAccountPayments = (
stellarAddress: string | undefined,
) => {
const query = useQuery<ReceiverWalletPayment[], AppError>({
queryKey: ["stellar", "accounts", "payments", stellarAddress],
queryFn: async () => {
if (!stellarAddress) {
return [];
}

// TODO: make params dynamic
const response = await fetchStellarApi(
`${HORIZON_URL}/accounts/${stellarAddress}/operations?limit=20&order=desc`,
undefined,
{
notFoundMessage: `${shortenAccountKey(
stellarAddress,
)} address was not found.`,
},
);

const { records } = response._embedded;

const paymentRecords = records.filter((r: ApiStellarOperationRecord) =>
ACCEPTED_TYPE.includes(r.type),
);

const payments = [];

for await (const record of paymentRecords) {
const payment = await formatWalletPayment(record, stellarAddress);
payments.push(payment);
}

return payments;
},
enabled: Boolean(stellarAddress),
});

return query;
};

const formatWalletPayment = async (
payment: ApiStellarOperationRecord,
walletAddress: string,
): Promise<ReceiverWalletPayment> => {
const isSend = payment.from === walletAddress;

// Getting transaction details to get the memo
const transaction = await getStellarTransaction(payment.transaction_hash);

return {
id: payment.id.toString(),
amount: payment.amount,
paymentAddress: isSend ? payment.to : payment.from,
createdAt: payment.created_at,
assetCode: payment.asset_code,
assetIssuer: payment.asset_issuer,
operationKind: isSend ? "send" : "receive",
transactionHash: payment.transaction_hash,
memo: transaction.memo || "",
};
};
2 changes: 1 addition & 1 deletion src/components/ReceiverWalletBalance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const ReceiverWalletBalance = ({
const balances =
data?.balances.filter((b) => b.asset_issuer && b.asset_code) || [];

if (isLoading || isFetching) {
if (stellarAddress && (isLoading || isFetching)) {
return <Loader />;
}

Expand Down
60 changes: 6 additions & 54 deletions src/components/ReceiverWalletHistory.tsx
Original file line number Diff line number Diff line change
@@ -1,77 +1,29 @@
import { Card, Link, Profile, Notification } from "@stellar/design-system";
import { useQuery } from "@tanstack/react-query";

import { getStellarAccountPayments } from "api/getStellarAccountPayments";
import { getStellarTransaction } from "api/getStellarTransaction";
import { STELLAR_EXPERT_URL } from "constants/settings";
import { useStellarAccountPayments } from "apiQueries/useStellarAccountPayments";
import { formatDateTime } from "helpers/formatIntlDateTime";

import { Table } from "components/Table";
import { AssetAmount } from "components/AssetAmount";

import { ApiStellarOperationRecord, ReceiverWalletPayment } from "types";

interface ReceiverWalletHistoryProps {
stellarAddress: string | undefined;
}

const formatWalletPayment = async (
payment: ApiStellarOperationRecord,
walletAddress: string,
): Promise<ReceiverWalletPayment> => {
const isSend = payment.from === walletAddress;

// Getting transaction details to get the memo
const transaction = await getStellarTransaction(payment.transaction_hash);

return {
id: payment.id.toString(),
amount: payment.amount,
paymentAddress: isSend ? payment.to : payment.from,
createdAt: payment.created_at,
assetCode: payment.asset_code,
assetIssuer: payment.asset_issuer,
operationKind: isSend ? "send" : "receive",
transactionHash: payment.transaction_hash,
memo: transaction.memo || "",
};
};

export const ReceiverWalletHistory = ({
stellarAddress,
}: ReceiverWalletHistoryProps) => {
const getPayments = async () => {
if (!stellarAddress) {
return [];
}

// We don't want to show XLM (native) payments
const response = (await getStellarAccountPayments(stellarAddress)).filter(
(r) => r.asset_issuer && r.asset_code,
);
const payments = [];

for await (const record of response) {
const payment = await formatWalletPayment(record, stellarAddress);
payments.push(payment);
}

return payments;
};

const { isLoading, isError, data, error } = useQuery({
queryKey: ["stellar", "accounts", stellarAddress],
queryFn: getPayments,
});
const { isLoading, isFetching, data, error } =
useStellarAccountPayments(stellarAddress);

if (isLoading) {
if (stellarAddress && (isLoading || isFetching)) {
return <div className="Note">Loading…</div>;
}

if (isError) {
if (error) {
return (
<Notification variant="error" title="Error">
{error as string}
{error.message}
</Notification>
);
}
Expand Down

0 comments on commit 2145129

Please sign in to comment.