ditto/packages/cashu/views.ts
P. Reis 83c96c88b7 feat: support pagination in GET transactions
remove getTransactions function and replace it with renderTransaction function (all tests updated)
2025-03-24 23:02:04 -03:00

44 lines
1.1 KiB
TypeScript

import { type NostrEvent, type NostrSigner, NSchema as n } from '@nostrify/nostrify';
import type { SetRequired } from 'type-fest';
import { z } from 'zod';
type Transaction = {
amount: number;
created_at: number;
direction: 'in' | 'out';
};
/** Renders one history of transaction. */
async function renderTransaction(
event: NostrEvent,
viewerPubkey: string,
signer: SetRequired<NostrSigner, 'nip44'>,
): Promise<Transaction | undefined> {
if (event.kind !== 7376) return;
const { data: contentTags, success } = n.json().pipe(z.coerce.string().array().min(2).array()).safeParse(
await signer.nip44.decrypt(viewerPubkey, event.content),
);
if (!success) {
return;
}
const direction = contentTags.find(([name]) => name === 'direction')?.[1];
if (direction !== 'out' && direction !== 'in') {
return;
}
const amount = parseInt(contentTags.find(([name]) => name === 'amount')?.[1] ?? '', 10);
if (isNaN(amount)) {
return;
}
return {
created_at: event.created_at,
direction,
amount,
};
}
export { renderTransaction, type Transaction };