2021-12-10 04:54:40 +00:00
|
|
|
import {
|
|
|
|
collection,
|
2022-07-22 22:28:53 +00:00
|
|
|
deleteDoc,
|
|
|
|
doc,
|
2021-12-14 01:07:28 +00:00
|
|
|
getDoc,
|
2022-07-22 22:28:53 +00:00
|
|
|
getDocs,
|
2022-01-09 20:26:51 +00:00
|
|
|
limit,
|
2022-07-22 22:28:53 +00:00
|
|
|
orderBy,
|
2022-08-28 23:03:00 +00:00
|
|
|
Query,
|
2022-07-22 22:28:53 +00:00
|
|
|
query,
|
|
|
|
setDoc,
|
2022-06-09 01:08:06 +00:00
|
|
|
startAfter,
|
2022-07-22 22:28:53 +00:00
|
|
|
updateDoc,
|
|
|
|
where,
|
2021-12-10 04:54:40 +00:00
|
|
|
} from 'firebase/firestore'
|
2022-08-30 05:22:11 +00:00
|
|
|
import { partition, sortBy, sum, uniqBy } from 'lodash'
|
2021-12-17 22:15:09 +00:00
|
|
|
|
2022-06-29 19:21:40 +00:00
|
|
|
import { coll, getValues, listenForValue, listenForValues } from './utils'
|
2022-09-22 21:57:48 +00:00
|
|
|
import { BinaryContract, Contract } from 'common/contract'
|
2022-09-16 21:12:24 +00:00
|
|
|
import { chooseRandomSubset } from 'common/util/random'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { formatMoney, formatPercent } from 'common/util/format'
|
|
|
|
import { DAY_MS } from 'common/util/time'
|
|
|
|
import { Bet } from 'common/bet'
|
|
|
|
import { Comment } from 'common/comment'
|
|
|
|
import { ENV_CONFIG } from 'common/envs/constants'
|
2022-08-19 17:43:57 +00:00
|
|
|
import { getBinaryProb } from 'common/contract-details'
|
2022-09-16 21:12:24 +00:00
|
|
|
import { Sort } from 'web/components/contract-search'
|
2022-06-29 19:21:40 +00:00
|
|
|
|
|
|
|
export const contracts = coll<Contract>('contracts')
|
|
|
|
|
2022-06-17 07:20:43 +00:00
|
|
|
export type { Contract }
|
2021-12-10 04:54:40 +00:00
|
|
|
|
2022-01-12 19:01:04 +00:00
|
|
|
export function contractPath(contract: Contract) {
|
2021-12-18 07:27:29 +00:00
|
|
|
return `/${contract.creatorUsername}/${contract.slug}`
|
2021-12-16 00:52:19 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 21:27:02 +00:00
|
|
|
export function contractPathWithoutContract(
|
|
|
|
creatorUsername: string,
|
|
|
|
slug: string
|
|
|
|
) {
|
|
|
|
return `/${creatorUsername}/${slug}`
|
|
|
|
}
|
|
|
|
|
2022-04-11 21:13:26 +00:00
|
|
|
export function homeContractPath(contract: Contract) {
|
|
|
|
return `/home?c=${contract.slug}`
|
|
|
|
}
|
|
|
|
|
2022-05-02 15:23:51 +00:00
|
|
|
export function contractUrl(contract: Contract) {
|
|
|
|
return `https://${ENV_CONFIG.domain}${contractPath(contract)}`
|
|
|
|
}
|
|
|
|
|
2022-05-24 23:57:34 +00:00
|
|
|
export function contractPool(contract: Contract) {
|
|
|
|
return contract.mechanism === 'cpmm-1'
|
2022-06-14 17:06:22 +00:00
|
|
|
? formatMoney(contract.totalLiquidity)
|
2022-05-24 23:57:34 +00:00
|
|
|
: contract.mechanism === 'dpm-2'
|
2022-06-14 17:06:22 +00:00
|
|
|
? formatMoney(sum(Object.values(contract.pool)))
|
|
|
|
: 'Empty pool'
|
2022-05-24 23:57:34 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 02:42:35 +00:00
|
|
|
export function getBinaryProbPercent(contract: BinaryContract) {
|
2022-03-19 02:22:12 +00:00
|
|
|
return formatPercent(getBinaryProb(contract))
|
2021-12-14 10:23:32 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 20:08:03 +00:00
|
|
|
export function tradingAllowed(contract: Contract) {
|
|
|
|
return (
|
|
|
|
!contract.isResolved &&
|
|
|
|
(!contract.closeTime || contract.closeTime > Date.now())
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-10 04:54:40 +00:00
|
|
|
// Push contract to Firestore
|
|
|
|
export async function setContract(contract: Contract) {
|
2022-06-29 19:21:40 +00:00
|
|
|
await setDoc(doc(contracts, contract.id), contract)
|
2021-12-10 04:54:40 +00:00
|
|
|
}
|
2021-12-09 22:05:55 +00:00
|
|
|
|
2022-01-07 19:27:59 +00:00
|
|
|
export async function updateContract(
|
|
|
|
contractId: string,
|
|
|
|
update: Partial<Contract>
|
|
|
|
) {
|
2022-06-29 19:21:40 +00:00
|
|
|
await updateDoc(doc(contracts, contractId), update)
|
2022-01-07 19:27:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 23:16:42 +00:00
|
|
|
export async function getContractFromId(contractId: string) {
|
2022-06-29 19:21:40 +00:00
|
|
|
const result = await getDoc(doc(contracts, contractId))
|
|
|
|
return result.exists() ? result.data() : undefined
|
2021-12-14 01:07:28 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 23:16:42 +00:00
|
|
|
export async function getContractFromSlug(slug: string) {
|
2022-06-29 19:21:40 +00:00
|
|
|
const q = query(contracts, where('slug', '==', slug))
|
2021-12-17 23:16:42 +00:00
|
|
|
const snapshot = await getDocs(q)
|
2022-06-29 19:21:40 +00:00
|
|
|
return snapshot.empty ? undefined : snapshot.docs[0].data()
|
2021-12-17 23:16:42 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 04:54:40 +00:00
|
|
|
export async function deleteContract(contractId: string) {
|
2022-06-29 19:21:40 +00:00
|
|
|
await deleteDoc(doc(contracts, contractId))
|
2021-12-10 04:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function listContracts(creatorId: string): Promise<Contract[]> {
|
2021-12-10 07:08:28 +00:00
|
|
|
const q = query(
|
2022-06-29 19:21:40 +00:00
|
|
|
contracts,
|
2021-12-10 07:08:28 +00:00
|
|
|
where('creatorId', '==', creatorId),
|
|
|
|
orderBy('createdTime', 'desc')
|
|
|
|
)
|
2021-12-10 04:54:40 +00:00
|
|
|
const snapshot = await getDocs(q)
|
2022-06-29 19:21:40 +00:00
|
|
|
return snapshot.docs.map((doc) => doc.data())
|
2021-12-14 01:09:16 +00:00
|
|
|
}
|
|
|
|
|
2022-09-06 13:17:21 +00:00
|
|
|
export const tournamentContractsByGroupSlugQuery = (slug: string) =>
|
2022-09-03 16:55:10 +00:00
|
|
|
query(
|
|
|
|
contracts,
|
|
|
|
where('groupSlugs', 'array-contains', slug),
|
|
|
|
where('isResolved', '==', false),
|
|
|
|
orderBy('popularityScore', 'desc')
|
|
|
|
)
|
|
|
|
|
2022-07-14 15:09:12 +00:00
|
|
|
export async function listContractsByGroupSlug(
|
|
|
|
slug: string
|
|
|
|
): Promise<Contract[]> {
|
2022-09-06 13:17:21 +00:00
|
|
|
const q = query(contracts, where('groupSlugs', 'array-contains', slug))
|
|
|
|
const snapshot = await getDocs(q)
|
2022-07-14 15:09:12 +00:00
|
|
|
return snapshot.docs.map((doc) => doc.data())
|
|
|
|
}
|
|
|
|
|
2022-04-29 05:39:39 +00:00
|
|
|
export async function listTaggedContractsCaseInsensitive(
|
|
|
|
tag: string
|
|
|
|
): Promise<Contract[]> {
|
|
|
|
const q = query(
|
2022-06-29 19:21:40 +00:00
|
|
|
contracts,
|
2022-04-29 05:39:39 +00:00
|
|
|
where('lowercaseTags', 'array-contains', tag.toLowerCase()),
|
|
|
|
orderBy('createdTime', 'desc')
|
|
|
|
)
|
|
|
|
const snapshot = await getDocs(q)
|
2022-06-29 19:21:40 +00:00
|
|
|
return snapshot.docs.map((doc) => doc.data())
|
2022-04-29 05:39:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 01:08:06 +00:00
|
|
|
export async function listAllContracts(
|
|
|
|
n: number,
|
2022-08-22 19:23:43 +00:00
|
|
|
before?: string,
|
|
|
|
sortDescBy = 'createdTime'
|
2022-06-09 01:08:06 +00:00
|
|
|
): Promise<Contract[]> {
|
2022-08-22 19:23:43 +00:00
|
|
|
let q = query(contracts, orderBy(sortDescBy, 'desc'), limit(n))
|
2022-06-09 01:08:06 +00:00
|
|
|
if (before != null) {
|
2022-06-29 19:21:40 +00:00
|
|
|
const snap = await getDoc(doc(contracts, before))
|
2022-06-09 01:08:06 +00:00
|
|
|
q = query(q, startAfter(snap))
|
|
|
|
}
|
2021-12-14 01:09:16 +00:00
|
|
|
const snapshot = await getDocs(q)
|
2022-06-29 19:21:40 +00:00
|
|
|
return snapshot.docs.map((doc) => doc.data())
|
2021-12-10 04:54:40 +00:00
|
|
|
}
|
2021-12-09 22:05:55 +00:00
|
|
|
|
2021-12-17 04:44:48 +00:00
|
|
|
export function listenForContracts(
|
|
|
|
setContracts: (contracts: Contract[]) => void
|
|
|
|
) {
|
2022-06-29 19:21:40 +00:00
|
|
|
const q = query(contracts, orderBy('createdTime', 'desc'))
|
2022-02-04 00:13:04 +00:00
|
|
|
return listenForValues<Contract>(q, setContracts)
|
2021-12-17 04:44:48 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
export function listenForUserContracts(
|
|
|
|
creatorId: string,
|
|
|
|
setContracts: (contracts: Contract[]) => void
|
|
|
|
) {
|
|
|
|
const q = query(
|
2022-06-29 19:21:40 +00:00
|
|
|
contracts,
|
2022-06-22 16:35:50 +00:00
|
|
|
where('creatorId', '==', creatorId),
|
|
|
|
orderBy('createdTime', 'desc')
|
|
|
|
)
|
|
|
|
return listenForValues<Contract>(q, setContracts)
|
|
|
|
}
|
|
|
|
|
2022-09-02 02:38:09 +00:00
|
|
|
export function getUserBetContracts(userId: string) {
|
|
|
|
return getValues<Contract>(getUserBetContractsQuery(userId))
|
|
|
|
}
|
|
|
|
|
2022-08-28 23:03:00 +00:00
|
|
|
export function getUserBetContractsQuery(userId: string) {
|
|
|
|
return query(
|
|
|
|
contracts,
|
|
|
|
where('uniqueBettorIds', 'array-contains', userId)
|
|
|
|
) as Query<Contract>
|
|
|
|
}
|
|
|
|
|
2022-02-27 21:37:04 +00:00
|
|
|
const inactiveContractsQuery = query(
|
2022-06-29 19:21:40 +00:00
|
|
|
contracts,
|
2022-02-27 21:37:04 +00:00
|
|
|
where('isResolved', '==', false),
|
|
|
|
where('closeTime', '>', Date.now()),
|
|
|
|
where('visibility', '==', 'public'),
|
|
|
|
where('volume24Hours', '==', 0)
|
|
|
|
)
|
|
|
|
|
|
|
|
export function getInactiveContracts() {
|
|
|
|
return getValues<Contract>(inactiveContractsQuery)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function listenForInactiveContracts(
|
|
|
|
setContracts: (contracts: Contract[]) => void
|
|
|
|
) {
|
|
|
|
return listenForValues<Contract>(inactiveContractsQuery, setContracts)
|
2022-02-25 08:11:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 21:59:34 +00:00
|
|
|
const newContractsQuery = query(
|
2022-06-29 19:21:40 +00:00
|
|
|
contracts,
|
2022-04-20 21:59:34 +00:00
|
|
|
where('isResolved', '==', false),
|
|
|
|
where('volume7Days', '==', 0),
|
|
|
|
where('createdTime', '>', Date.now() - 7 * DAY_MS)
|
|
|
|
)
|
|
|
|
|
|
|
|
export function listenForNewContracts(
|
|
|
|
setContracts: (contracts: Contract[]) => void
|
|
|
|
) {
|
|
|
|
return listenForValues<Contract>(newContractsQuery, setContracts)
|
|
|
|
}
|
|
|
|
|
2021-12-10 05:01:44 +00:00
|
|
|
export function listenForContract(
|
|
|
|
contractId: string,
|
2021-12-15 07:41:50 +00:00
|
|
|
setContract: (contract: Contract | null) => void
|
2021-12-10 05:01:44 +00:00
|
|
|
) {
|
2022-06-29 19:21:40 +00:00
|
|
|
const contractRef = doc(contracts, contractId)
|
2022-01-30 05:05:32 +00:00
|
|
|
return listenForValue<Contract>(contractRef, setContract)
|
2021-12-10 05:01:44 +00:00
|
|
|
}
|
2022-01-05 06:32:52 +00:00
|
|
|
|
2022-08-24 16:49:53 +00:00
|
|
|
export function listenForContractFollows(
|
|
|
|
contractId: string,
|
|
|
|
setFollowIds: (followIds: string[]) => void
|
|
|
|
) {
|
|
|
|
const follows = collection(contracts, contractId, 'follows')
|
|
|
|
return listenForValues<{ id: string }>(follows, (docs) =>
|
|
|
|
setFollowIds(docs.map(({ id }) => id))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function followContract(contractId: string, userId: string) {
|
|
|
|
const followDoc = doc(collection(contracts, contractId, 'follows'), userId)
|
|
|
|
return await setDoc(followDoc, {
|
|
|
|
id: userId,
|
|
|
|
createdTime: Date.now(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function unFollowContract(contractId: string, userId: string) {
|
|
|
|
const followDoc = doc(collection(contracts, contractId, 'follows'), userId)
|
|
|
|
await deleteDoc(followDoc)
|
|
|
|
}
|
|
|
|
|
2022-01-09 20:26:51 +00:00
|
|
|
const hotContractsQuery = query(
|
2022-06-29 19:21:40 +00:00
|
|
|
contracts,
|
2022-01-09 20:26:51 +00:00
|
|
|
where('isResolved', '==', false),
|
2022-01-13 01:53:50 +00:00
|
|
|
where('visibility', '==', 'public'),
|
2022-01-09 20:26:51 +00:00
|
|
|
orderBy('volume24Hours', 'desc'),
|
2022-01-15 06:43:15 +00:00
|
|
|
limit(16)
|
2022-01-09 20:26:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
export function listenForHotContracts(
|
|
|
|
setHotContracts: (contracts: Contract[]) => void
|
|
|
|
) {
|
2022-01-17 22:54:00 +00:00
|
|
|
return listenForValues<Contract>(hotContractsQuery, (contracts) => {
|
2022-05-22 08:36:05 +00:00
|
|
|
const hotContracts = sortBy(
|
2022-01-17 22:54:00 +00:00
|
|
|
chooseRandomSubset(contracts, 4),
|
|
|
|
(contract) => contract.volume24Hours
|
|
|
|
)
|
|
|
|
setHotContracts(hotContracts)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-16 21:12:24 +00:00
|
|
|
export const trendingContractsQuery = query(
|
2022-08-16 01:34:45 +00:00
|
|
|
contracts,
|
|
|
|
where('isResolved', '==', false),
|
|
|
|
where('visibility', '==', 'public'),
|
2022-09-16 21:12:24 +00:00
|
|
|
orderBy('popularityScore', 'desc')
|
2022-08-16 01:34:45 +00:00
|
|
|
)
|
|
|
|
|
2022-09-16 21:12:24 +00:00
|
|
|
export async function getTrendingContracts(maxContracts = 10) {
|
|
|
|
return await getValues<Contract>(
|
|
|
|
query(trendingContractsQuery, limit(maxContracts))
|
|
|
|
)
|
2022-01-05 06:32:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 23:44:01 +00:00
|
|
|
export async function getContractsBySlugs(slugs: string[]) {
|
2022-06-29 19:21:40 +00:00
|
|
|
const q = query(contracts, where('slug', 'in', slugs))
|
2022-05-07 23:44:01 +00:00
|
|
|
const snapshot = await getDocs(q)
|
2022-06-29 19:21:40 +00:00
|
|
|
const data = snapshot.docs.map((doc) => doc.data())
|
|
|
|
return sortBy(data, (contract) => -1 * contract.volume24Hours)
|
2022-05-07 23:44:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 22:54:00 +00:00
|
|
|
const closingSoonQuery = query(
|
2022-06-29 19:21:40 +00:00
|
|
|
contracts,
|
2022-01-17 22:54:00 +00:00
|
|
|
where('isResolved', '==', false),
|
2022-01-18 19:17:56 +00:00
|
|
|
where('visibility', '==', 'public'),
|
2022-01-17 22:54:00 +00:00
|
|
|
where('closeTime', '>', Date.now()),
|
|
|
|
orderBy('closeTime', 'asc'),
|
|
|
|
limit(6)
|
|
|
|
)
|
|
|
|
|
|
|
|
export async function getClosingSoonContracts() {
|
2022-06-29 19:21:40 +00:00
|
|
|
const data = await getValues<Contract>(closingSoonQuery)
|
|
|
|
return sortBy(chooseRandomSubset(data, 2), (contract) => contract.closeTime)
|
2022-01-05 06:32:52 +00:00
|
|
|
}
|
2022-03-03 01:52:41 +00:00
|
|
|
|
2022-08-30 05:22:11 +00:00
|
|
|
export const getTopCreatorContracts = async (
|
2022-08-24 17:59:21 +00:00
|
|
|
creatorId: string,
|
2022-08-30 05:22:11 +00:00
|
|
|
count: number
|
2022-08-24 17:59:21 +00:00
|
|
|
) => {
|
|
|
|
const creatorContractsQuery = query(
|
|
|
|
contracts,
|
|
|
|
where('isResolved', '==', false),
|
|
|
|
where('creatorId', '==', creatorId),
|
|
|
|
orderBy('popularityScore', 'desc'),
|
2022-08-30 05:22:11 +00:00
|
|
|
limit(count)
|
2022-08-24 17:59:21 +00:00
|
|
|
)
|
2022-08-30 05:22:11 +00:00
|
|
|
return await getValues<Contract>(creatorContractsQuery)
|
2022-08-24 17:59:21 +00:00
|
|
|
}
|
|
|
|
|
2022-08-30 05:22:11 +00:00
|
|
|
export const getTopGroupContracts = async (
|
2022-08-28 03:26:37 +00:00
|
|
|
groupSlug: string,
|
2022-08-30 05:22:11 +00:00
|
|
|
count: number
|
2022-08-28 03:26:37 +00:00
|
|
|
) => {
|
|
|
|
const creatorContractsQuery = query(
|
|
|
|
contracts,
|
|
|
|
where('groupSlugs', 'array-contains', groupSlug),
|
|
|
|
where('isResolved', '==', false),
|
|
|
|
orderBy('popularityScore', 'desc'),
|
2022-08-30 05:22:11 +00:00
|
|
|
limit(count)
|
2022-08-28 03:26:37 +00:00
|
|
|
)
|
2022-08-30 05:22:11 +00:00
|
|
|
return await getValues<Contract>(creatorContractsQuery)
|
2022-08-28 03:26:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 21:12:24 +00:00
|
|
|
const sortToField = {
|
|
|
|
newest: 'createdTime',
|
|
|
|
score: 'popularityScore',
|
|
|
|
'most-traded': 'volume',
|
|
|
|
'24-hour-vol': 'volume24Hours',
|
|
|
|
'prob-change-day': 'probChanges.day',
|
|
|
|
'last-updated': 'lastUpdated',
|
|
|
|
liquidity: 'totalLiquidity',
|
|
|
|
'close-date': 'closeTime',
|
|
|
|
'resolve-date': 'resolutionTime',
|
|
|
|
'prob-descending': 'prob',
|
|
|
|
'prob-ascending': 'prob',
|
|
|
|
} as const
|
|
|
|
|
|
|
|
const sortToDirection = {
|
|
|
|
newest: 'desc',
|
|
|
|
score: 'desc',
|
|
|
|
'most-traded': 'desc',
|
|
|
|
'24-hour-vol': 'desc',
|
|
|
|
'prob-change-day': 'desc',
|
|
|
|
'last-updated': 'desc',
|
|
|
|
liquidity: 'desc',
|
|
|
|
'close-date': 'asc',
|
|
|
|
'resolve-date': 'desc',
|
|
|
|
'prob-ascending': 'asc',
|
|
|
|
'prob-descending': 'desc',
|
|
|
|
} as const
|
|
|
|
|
|
|
|
export const getContractsQuery = (
|
|
|
|
sort: Sort,
|
|
|
|
maxItems: number,
|
|
|
|
filters: { groupSlug?: string } = {},
|
|
|
|
visibility?: 'public'
|
|
|
|
) => {
|
|
|
|
const { groupSlug } = filters
|
|
|
|
return query(
|
|
|
|
contracts,
|
|
|
|
where('isResolved', '==', false),
|
|
|
|
...(visibility ? [where('visibility', '==', visibility)] : []),
|
|
|
|
...(groupSlug ? [where('groupSlugs', 'array-contains', groupSlug)] : []),
|
|
|
|
orderBy(sortToField[sort], sortToDirection[sort]),
|
|
|
|
limit(maxItems)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-28 03:26:37 +00:00
|
|
|
export const getRecommendedContracts = async (
|
|
|
|
contract: Contract,
|
2022-08-30 05:22:11 +00:00
|
|
|
excludeBettorId: string,
|
2022-08-28 03:26:37 +00:00
|
|
|
count: number
|
|
|
|
) => {
|
|
|
|
const { creatorId, groupSlugs, id } = contract
|
|
|
|
|
|
|
|
const [userContracts, groupContracts] = await Promise.all([
|
2022-08-30 05:22:11 +00:00
|
|
|
getTopCreatorContracts(creatorId, count * 2),
|
2022-08-28 03:26:37 +00:00
|
|
|
groupSlugs && groupSlugs[0]
|
2022-08-30 05:22:11 +00:00
|
|
|
? getTopGroupContracts(groupSlugs[0], count * 2)
|
2022-08-28 03:26:37 +00:00
|
|
|
: [],
|
|
|
|
])
|
|
|
|
|
|
|
|
const combined = uniqBy([...userContracts, ...groupContracts], (c) => c.id)
|
|
|
|
|
2022-08-30 05:22:11 +00:00
|
|
|
const open = combined
|
|
|
|
.filter((c) => c.closeTime && c.closeTime > Date.now())
|
|
|
|
.filter((c) => c.id !== id)
|
|
|
|
|
|
|
|
const [betOnContracts, nonBetOnContracts] = partition(
|
|
|
|
open,
|
|
|
|
(c) => c.uniqueBettorIds && c.uniqueBettorIds.includes(excludeBettorId)
|
|
|
|
)
|
|
|
|
const chosen = chooseRandomSubset(nonBetOnContracts, count)
|
|
|
|
if (chosen.length < count)
|
|
|
|
chosen.push(...chooseRandomSubset(betOnContracts, count - chosen.length))
|
|
|
|
|
|
|
|
return chosen
|
2022-08-28 03:26:37 +00:00
|
|
|
}
|
|
|
|
|
2022-05-01 16:36:54 +00:00
|
|
|
export async function getRecentBetsAndComments(contract: Contract) {
|
2022-06-29 19:21:40 +00:00
|
|
|
const contractDoc = doc(contracts, contract.id)
|
2022-05-01 16:36:54 +00:00
|
|
|
|
|
|
|
const [recentBets, recentComments] = await Promise.all([
|
|
|
|
getValues<Bet>(
|
|
|
|
query(
|
|
|
|
collection(contractDoc, 'bets'),
|
|
|
|
where('createdTime', '>', Date.now() - DAY_MS),
|
|
|
|
orderBy('createdTime', 'desc'),
|
|
|
|
limit(1)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
|
|
|
|
getValues<Comment>(
|
|
|
|
query(
|
|
|
|
collection(contractDoc, 'comments'),
|
|
|
|
where('createdTime', '>', Date.now() - 3 * DAY_MS),
|
|
|
|
orderBy('createdTime', 'desc'),
|
|
|
|
limit(3)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
])
|
|
|
|
|
|
|
|
return {
|
|
|
|
contract,
|
|
|
|
recentBets,
|
|
|
|
recentComments,
|
|
|
|
}
|
|
|
|
}
|