2021-12-14 05:40:38 +00:00
|
|
|
import {
|
|
|
|
collection,
|
|
|
|
collectionGroup,
|
|
|
|
query,
|
|
|
|
where,
|
2022-02-02 06:40:46 +00:00
|
|
|
orderBy,
|
2022-07-07 22:36:02 +00:00
|
|
|
QueryConstraint,
|
|
|
|
limit,
|
|
|
|
startAfter,
|
|
|
|
doc,
|
|
|
|
getDocs,
|
|
|
|
getDoc,
|
|
|
|
DocumentSnapshot,
|
2022-08-28 23:03:00 +00:00
|
|
|
Query,
|
2021-12-14 05:40:38 +00:00
|
|
|
} from 'firebase/firestore'
|
2022-06-26 21:42:42 +00:00
|
|
|
import { uniq } from 'lodash'
|
2021-12-24 21:06:01 +00:00
|
|
|
|
2022-01-10 21:07:57 +00:00
|
|
|
import { db } from './init'
|
2022-07-10 18:05:44 +00:00
|
|
|
import { Bet, LimitBet } from 'common/bet'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { Contract } from 'common/contract'
|
2022-02-04 00:13:04 +00:00
|
|
|
import { getValues, listenForValues } from './utils'
|
2022-06-08 04:42:42 +00:00
|
|
|
import { getContractFromId } from './contracts'
|
|
|
|
import { filterDefined } from 'common/util/array'
|
2022-01-10 21:07:57 +00:00
|
|
|
export type { Bet }
|
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')
|
|
|
|
}
|
|
|
|
|
2022-01-15 00:16:25 +00:00
|
|
|
export async function listAllBets(contractId: string) {
|
2022-08-31 03:28:30 +00:00
|
|
|
return await getValues<Bet>(
|
|
|
|
query(getBetsCollection(contractId), orderBy('createdTime', 'desc'))
|
|
|
|
)
|
2022-01-15 00:16:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 06:40:46 +00:00
|
|
|
const DAY_IN_MS = 24 * 60 * 60 * 1000
|
|
|
|
|
|
|
|
// Define "recent" as "<24 hours ago" for now
|
|
|
|
const recentBetsQuery = query(
|
|
|
|
collectionGroup(db, 'bets'),
|
|
|
|
where('createdTime', '>', Date.now() - DAY_IN_MS),
|
|
|
|
orderBy('createdTime', 'desc')
|
|
|
|
)
|
|
|
|
|
|
|
|
export async function getRecentBets() {
|
|
|
|
return getValues<Bet>(recentBetsQuery)
|
|
|
|
}
|
|
|
|
|
2022-02-21 03:06:10 +00:00
|
|
|
export function listenForRecentBets(setBets: (bets: Bet[]) => void) {
|
|
|
|
return listenForValues<Bet>(recentBetsQuery, setBets)
|
|
|
|
}
|
|
|
|
|
2022-02-03 05:58:27 +00:00
|
|
|
export async function getRecentContractBets(contractId: string) {
|
|
|
|
const q = query(
|
|
|
|
getBetsCollection(contractId),
|
|
|
|
where('createdTime', '>', Date.now() - DAY_IN_MS),
|
|
|
|
orderBy('createdTime', 'desc')
|
|
|
|
)
|
|
|
|
|
|
|
|
return getValues<Bet>(q)
|
|
|
|
}
|
|
|
|
|
2021-12-12 22:14:52 +00:00
|
|
|
export function listenForBets(
|
|
|
|
contractId: string,
|
|
|
|
setBets: (bets: Bet[]) => void
|
|
|
|
) {
|
2022-08-31 03:28:30 +00:00
|
|
|
return listenForValues<Bet>(
|
|
|
|
query(getBetsCollection(contractId), orderBy('createdTime', 'desc')),
|
|
|
|
setBets
|
|
|
|
)
|
2021-12-10 17:14:05 +00:00
|
|
|
}
|
2021-12-14 05:40:38 +00:00
|
|
|
|
2022-09-02 02:38:09 +00:00
|
|
|
export async function getUserBets(userId: string) {
|
|
|
|
return getValues<Bet>(getUserBetsQuery(userId))
|
|
|
|
}
|
|
|
|
|
2022-10-07 16:53:09 +00:00
|
|
|
export const MAX_USER_BETS_LOADED = 10000
|
2022-09-02 02:38:09 +00:00
|
|
|
export function getUserBetsQuery(userId: string) {
|
|
|
|
return query(
|
|
|
|
collectionGroup(db, 'bets'),
|
|
|
|
where('userId', '==', userId),
|
2022-10-07 16:53:09 +00:00
|
|
|
orderBy('createdTime', 'desc'),
|
|
|
|
limit(MAX_USER_BETS_LOADED)
|
2022-09-02 02:38:09 +00:00
|
|
|
) as Query<Bet>
|
2022-05-18 15:52:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 22:36:02 +00:00
|
|
|
export async function getBets(options: {
|
|
|
|
userId?: string
|
|
|
|
contractId?: string
|
|
|
|
before?: string
|
|
|
|
limit: number
|
|
|
|
}) {
|
|
|
|
const { userId, contractId, before } = options
|
|
|
|
|
|
|
|
const queryParts: QueryConstraint[] = [
|
|
|
|
orderBy('createdTime', 'desc'),
|
|
|
|
limit(options.limit),
|
|
|
|
]
|
|
|
|
if (userId) {
|
|
|
|
queryParts.push(where('userId', '==', userId))
|
|
|
|
}
|
|
|
|
if (before) {
|
|
|
|
let beforeSnap: DocumentSnapshot
|
|
|
|
if (contractId) {
|
|
|
|
beforeSnap = await getDoc(
|
|
|
|
doc(db, 'contracts', contractId, 'bets', before)
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
beforeSnap = (
|
|
|
|
await getDocs(
|
|
|
|
query(collectionGroup(db, 'bets'), where('id', '==', before))
|
|
|
|
)
|
|
|
|
).docs[0]
|
|
|
|
}
|
|
|
|
queryParts.push(startAfter(beforeSnap))
|
|
|
|
}
|
|
|
|
|
|
|
|
const querySource = contractId
|
|
|
|
? collection(db, 'contracts', contractId, 'bets')
|
|
|
|
: collectionGroup(db, 'bets')
|
|
|
|
return await getValues<Bet>(query(querySource, ...queryParts))
|
|
|
|
}
|
|
|
|
|
2022-06-08 04:42:42 +00:00
|
|
|
export async function getContractsOfUserBets(userId: string) {
|
2022-09-02 02:38:09 +00:00
|
|
|
const bets = await getUserBets(userId)
|
|
|
|
const contractIds = uniq(
|
|
|
|
bets.filter((b) => !b.isAnte).map((bet) => bet.contractId)
|
|
|
|
)
|
2022-06-08 04:42:42 +00:00
|
|
|
const contracts = await Promise.all(
|
|
|
|
contractIds.map((contractId) => getContractFromId(contractId))
|
|
|
|
)
|
|
|
|
return filterDefined(contracts)
|
|
|
|
}
|
|
|
|
|
2022-03-02 03:31:48 +00:00
|
|
|
export function listenForUserContractBets(
|
|
|
|
userId: string,
|
|
|
|
contractId: string,
|
|
|
|
setBets: (bets: Bet[]) => void
|
|
|
|
) {
|
|
|
|
const betsQuery = query(
|
|
|
|
collection(db, 'contracts', contractId, 'bets'),
|
2022-08-31 03:28:30 +00:00
|
|
|
where('userId', '==', userId),
|
|
|
|
orderBy('createdTime', 'desc')
|
2022-03-02 03:31:48 +00:00
|
|
|
)
|
2022-08-31 03:28:30 +00:00
|
|
|
return listenForValues<Bet>(betsQuery, setBets)
|
2022-03-02 03:31:48 +00:00
|
|
|
}
|
|
|
|
|
2022-07-10 18:05:44 +00:00
|
|
|
export function listenForUnfilledBets(
|
|
|
|
contractId: string,
|
|
|
|
setBets: (bets: LimitBet[]) => void
|
|
|
|
) {
|
|
|
|
const betsQuery = query(
|
|
|
|
collection(db, 'contracts', contractId, 'bets'),
|
|
|
|
where('isFilled', '==', false),
|
2022-08-31 03:28:30 +00:00
|
|
|
where('isCancelled', '==', false),
|
|
|
|
orderBy('createdTime', 'desc')
|
2022-07-10 18:05:44 +00:00
|
|
|
)
|
2022-08-31 03:28:30 +00:00
|
|
|
return listenForValues<LimitBet>(betsQuery, setBets)
|
2022-07-10 18:05:44 +00:00
|
|
|
}
|
|
|
|
|
2022-01-22 17:15:04 +00:00
|
|
|
export function withoutAnteBets(contract: Contract, bets?: Bet[]) {
|
2022-01-13 17:01:20 +00:00
|
|
|
const { createdTime } = contract
|
|
|
|
|
|
|
|
if (
|
2022-01-22 17:15:04 +00:00
|
|
|
bets &&
|
2022-01-13 17:01:20 +00:00
|
|
|
bets.length >= 2 &&
|
|
|
|
bets[0].createdTime === createdTime &&
|
|
|
|
bets[1].createdTime === createdTime
|
|
|
|
) {
|
|
|
|
return bets.slice(2)
|
|
|
|
}
|
|
|
|
|
2022-02-04 00:13:04 +00:00
|
|
|
return bets?.filter((bet) => !bet.isAnte) ?? []
|
2022-01-13 17:01:20 +00:00
|
|
|
}
|