manifold/web/lib/firebase/transactions.ts

58 lines
1.3 KiB
TypeScript
Raw Normal View History

import { collection, doc, query, setDoc } from 'firebase/firestore'
import { db } from './init'
import { getValues, listenForValues } from './utils'
export type Transaction = {
id: string
createdTime: number
fromId: string
fromName: string
fromUsername: string
fromAvatarUrl?: string
toId: string
toName: string
toUsername: string
toAvatarUrl?: string
amount: number
category: 'BUY_LEADERBOARD_SLOT' | 'LEADERBOARD_TAX'
// Human-readable description
description?: string
// Structured metadata for different kinds of transactions
data?: SlotData | TaxData
}
2022-04-01 05:16:22 +00:00
export type SlotData = {
slot: number
2022-04-01 05:16:22 +00:00
newValue: number
message: string
}
2022-04-01 05:16:22 +00:00
export type TaxData = {
slot: number
}
export async function listAllTransactions() {
const col = collection(db, 'transactions')
const transactions = await getValues<Transaction>(col)
transactions.sort((t1, t2) => t1.createdTime - t2.createdTime)
return transactions
}
export function listenForTransactions(setTxns: (txns: Transaction[]) => void) {
const col = collection(db, 'transactions')
const queryAll = query(col)
return listenForValues<Transaction>(queryAll, setTxns)
}
export async function writeTransaction(transaction: Transaction) {
const col = collection(db, 'transactions')
const newRef = doc(col)
transaction.id = newRef.id
await setDoc(newRef, transaction)
}