2021-12-14 05:40:38 +00:00
|
|
|
import {
|
|
|
|
collection,
|
|
|
|
collectionGroup,
|
|
|
|
query,
|
|
|
|
onSnapshot,
|
|
|
|
where,
|
2022-01-05 06:32:52 +00:00
|
|
|
getDocs,
|
2021-12-14 05:40:38 +00:00
|
|
|
} from 'firebase/firestore'
|
2022-01-05 06:32:52 +00:00
|
|
|
import _ from 'lodash'
|
2021-12-10 17:14:05 +00:00
|
|
|
import { db } from './init'
|
|
|
|
|
|
|
|
export type Bet = {
|
|
|
|
id: string
|
|
|
|
userId: string
|
|
|
|
contractId: string
|
2021-12-24 21:06:01 +00:00
|
|
|
|
|
|
|
amount: number // bet size; negative if SELL bet
|
|
|
|
outcome: 'YES' | 'NO'
|
|
|
|
shares: number // dynamic parimutuel pool weight; negative if SELL bet
|
|
|
|
|
2021-12-12 22:14:52 +00:00
|
|
|
probBefore: number
|
|
|
|
probAfter: number
|
2021-12-24 21:06:01 +00:00
|
|
|
|
|
|
|
sale?: {
|
|
|
|
amount: number // amount user makes from sale
|
|
|
|
betId: string // id of bet being sold
|
2022-01-04 07:21:14 +00:00
|
|
|
// TODO: add sale time?
|
2021-12-24 21:06:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isSold?: boolean // true if this BUY bet has been sold
|
|
|
|
|
2021-12-12 22:14:52 +00:00
|
|
|
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
|
|
|
}
|
2021-12-14 05:40:38 +00:00
|
|
|
|
|
|
|
export function listenForUserBets(
|
|
|
|
userId: string,
|
|
|
|
setBets: (bets: Bet[]) => void
|
|
|
|
) {
|
|
|
|
const userQuery = query(
|
|
|
|
collectionGroup(db, 'bets'),
|
|
|
|
where('userId', '==', userId)
|
|
|
|
)
|
|
|
|
|
|
|
|
return onSnapshot(userQuery, (snap) => {
|
|
|
|
const bets = snap.docs.map((doc) => doc.data() as Bet)
|
|
|
|
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
|
|
|
|
setBets(bets)
|
|
|
|
})
|
|
|
|
}
|
2022-01-05 06:32:52 +00:00
|
|
|
|
|
|
|
export function listenForRecentBets(
|
|
|
|
timePeriodMs: number,
|
|
|
|
setBets: (bets: Bet[]) => void
|
|
|
|
) {
|
|
|
|
const recentQuery = query(
|
|
|
|
collectionGroup(db, 'bets'),
|
|
|
|
where('createdTime', '>', Date.now() - timePeriodMs)
|
|
|
|
)
|
|
|
|
return onSnapshot(recentQuery, (snap) => {
|
|
|
|
const bets = snap.docs.map((doc) => doc.data() as Bet)
|
|
|
|
|
|
|
|
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
|
|
|
|
|
|
|
|
setBets(bets)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getRecentBets(timePeriodMs: number) {
|
|
|
|
const recentQuery = query(
|
|
|
|
collectionGroup(db, 'bets'),
|
|
|
|
where('createdTime', '>', Date.now() - timePeriodMs)
|
|
|
|
)
|
|
|
|
|
|
|
|
const snapshot = await getDocs(recentQuery)
|
|
|
|
const bets = snapshot.docs.map((doc) => doc.data() as Bet)
|
|
|
|
|
|
|
|
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
|
|
|
|
|
|
|
|
return bets
|
|
|
|
}
|