Download services/wallet/src/memoryStore.ts from AnthropicallyHuggingFaces/ProjectOS: direct link, hf CLI and curl.
- Browser
- Download file 1.08 kB
-
https://huggingface.co/spaces/AnthropicallyHuggingFaces/ProjectOS/resolve/main/services/wallet/src/memoryStore.ts
- Command line
-
hf download hf://spaces/AnthropicallyHuggingFaces/ProjectOS/services/wallet/src/memoryStore.ts
-
curl -L -o memoryStore.ts https://huggingface.co/spaces/AnthropicallyHuggingFaces/ProjectOS/resolve/main/services/wallet/src/memoryStore.ts
1.08 kB
| import { randomUUID } from "node:crypto"; | |
| import type { Account, AccountKind, LedgerStore, Transaction } from "./types.js"; | |
| export class MemoryStore implements LedgerStore { | |
| accounts: Account[] = []; | |
| txs: Transaction[] = []; | |
| async getOrCreateAccount(userId: string, kind: AccountKind, currency: string) { | |
| let a = this.accounts.find((x) => x.user_id === userId && x.kind === kind && x.currency === currency); | |
| if (!a) { a = { id: randomUUID(), user_id: userId, kind, currency }; this.accounts.push(a); } | |
| return a; | |
| } | |
| async findTransactionByKey(userId: string, key: string) { | |
| return this.txs.find((t) => t.user_id === userId && t.idempotency_key === key) ?? null; | |
| } | |
| async insertTransaction(tx: Transaction) { this.txs.push(structuredClone(tx)); } | |
| async balance(accountId: string) { | |
| return this.txs.flatMap((t) => t.postings).filter((p) => p.account_id === accountId).reduce((s, p) => s + p.amount, 0); | |
| } | |
| async listTransactions(userId: string, limit = 50) { | |
| return this.txs.filter((t) => t.user_id === userId).slice(-limit).reverse(); | |
| } | |
| } | |