manifold/web/lib/firebase/bets.ts

33 lines
788 B
TypeScript
Raw Normal View History

2021-12-12 22:14:52 +00:00
import { collection, onSnapshot } from 'firebase/firestore'
2021-12-10 17:14:05 +00:00
import { db } from './init'
export type Bet = {
id: string
userId: string
contractId: string
2021-12-12 22:14:52 +00:00
amount: number // Amount of bet
2021-12-10 17:14:05 +00:00
outcome: 'YES' | 'NO' // Chosen outcome
dpmWeight: number // Dynamic Parimutuel weight
2021-12-12 22:14:52 +00:00
probBefore: number
probAverage: number
probAfter: number
createdTime: number
2021-12-10 17:14:05 +00:00
}
2021-12-12 22:14:52 +00:00
function getBetsCollection(contractId: string) {
return collection(db, 'contracts', contractId, 'bets')
}
export function listenForBets(
contractId: string,
setBets: (bets: Bet[]) => void
) {
return onSnapshot(getBetsCollection(contractId), (snap) => {
const bets = snap.docs.map((doc) => doc.data() as Bet)
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
setBets(bets)
})
2021-12-10 17:14:05 +00:00
}