2022-01-10 21:07:57 +00:00
|
|
|
import dayjs from 'dayjs'
|
2021-12-10 04:54:40 +00:00
|
|
|
import {
|
|
|
|
getFirestore,
|
|
|
|
doc,
|
|
|
|
setDoc,
|
|
|
|
deleteDoc,
|
|
|
|
where,
|
|
|
|
collection,
|
|
|
|
query,
|
|
|
|
getDocs,
|
2021-12-10 07:08:28 +00:00
|
|
|
orderBy,
|
2021-12-14 01:07:28 +00:00
|
|
|
getDoc,
|
2022-01-07 19:27:59 +00:00
|
|
|
updateDoc,
|
2022-01-09 20:26:51 +00:00
|
|
|
limit,
|
2022-06-09 01:08:06 +00:00
|
|
|
startAfter,
|
2021-12-10 04:54:40 +00:00
|
|
|
} from 'firebase/firestore'
|
2022-05-24 23:57:34 +00:00
|
|
|
import { range, sortBy, sum } from 'lodash'
|
2021-12-17 22:15:09 +00:00
|
|
|
|
2022-01-10 21:07:57 +00:00
|
|
|
import { app } from './init'
|
2022-01-30 05:05:32 +00:00
|
|
|
import { getValues, listenForValue, listenForValues } from './utils'
|
2022-06-17 07:20:43 +00:00
|
|
|
import { BinaryContract, Contract } from 'common/contract'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { getDpmProbability } from 'common/calculate-dpm'
|
|
|
|
import { createRNG, shuffle } from 'common/util/random'
|
|
|
|
import { getCpmmProbability } from 'common/calculate-cpmm'
|
|
|
|
import { formatMoney, formatPercent } from 'common/util/format'
|
|
|
|
import { DAY_MS } from 'common/util/time'
|
|
|
|
import { MAX_FEED_CONTRACTS } from 'common/recommended-contracts'
|
|
|
|
import { Bet } from 'common/bet'
|
|
|
|
import { Comment } from 'common/comment'
|
|
|
|
import { ENV_CONFIG } from 'common/envs/constants'
|
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-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-01-12 19:01:04 +00:00
|
|
|
export function contractMetrics(contract: Contract) {
|
2022-03-23 05:27:22 +00:00
|
|
|
const { createdTime, resolutionTime, isResolved } = contract
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2021-12-14 10:23:32 +00:00
|
|
|
const createdDate = dayjs(createdTime).format('MMM D')
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2021-12-15 07:41:50 +00:00
|
|
|
const resolvedDate = isResolved
|
|
|
|
? dayjs(resolutionTime).format('MMM D')
|
|
|
|
: undefined
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-05-07 14:10:25 +00:00
|
|
|
const volumeLabel = `${formatMoney(contract.volume)} bet`
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2022-03-23 05:27:22 +00:00
|
|
|
return { volumeLabel, createdDate, resolvedDate }
|
2022-02-17 23:00:19 +00:00
|
|
|
}
|
|
|
|
|
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 getBinaryProb(contract: BinaryContract) {
|
|
|
|
const { pool, resolutionProbability, mechanism } = contract
|
2022-02-17 23:00:19 +00:00
|
|
|
|
2022-04-15 04:17:55 +00:00
|
|
|
return (
|
|
|
|
resolutionProbability ??
|
|
|
|
(mechanism === 'cpmm-1'
|
2022-06-01 02:42:35 +00:00
|
|
|
? getCpmmProbability(pool, contract.p)
|
|
|
|
: getDpmProbability(contract.totalShares))
|
2022-04-15 04:17:55 +00:00
|
|
|
)
|
2022-03-19 02:22:12 +00:00
|
|
|
}
|
2022-02-17 23:00:19 +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
|
|
|
const db = getFirestore(app)
|
2022-01-21 23:21:46 +00:00
|
|
|
export const contractCollection = collection(db, 'contracts')
|
2022-05-12 15:07:10 +00:00
|
|
|
export const contractDocRef = (contractId: string) =>
|
|
|
|
doc(db, 'contracts', contractId)
|
2021-12-10 04:54:40 +00:00
|
|
|
|
|
|
|
// Push contract to Firestore
|
|
|
|
export async function setContract(contract: Contract) {
|
|
|
|
const docRef = doc(db, 'contracts', contract.id)
|
|
|
|
await setDoc(docRef, contract)
|
|
|
|
}
|
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>
|
|
|
|
) {
|
|
|
|
const docRef = doc(db, 'contracts', contractId)
|
|
|
|
await updateDoc(docRef, update)
|
|
|
|
}
|
|
|
|
|
2021-12-17 23:16:42 +00:00
|
|
|
export async function getContractFromId(contractId: string) {
|
2021-12-14 01:07:28 +00:00
|
|
|
const docRef = doc(db, 'contracts', contractId)
|
|
|
|
const result = await getDoc(docRef)
|
|
|
|
|
2021-12-14 01:09:16 +00:00
|
|
|
return result.exists() ? (result.data() as Contract) : undefined
|
2021-12-14 01:07:28 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 23:16:42 +00:00
|
|
|
export async function getContractFromSlug(slug: string) {
|
|
|
|
const q = query(contractCollection, where('slug', '==', slug))
|
|
|
|
const snapshot = await getDocs(q)
|
|
|
|
|
|
|
|
return snapshot.empty ? undefined : (snapshot.docs[0].data() as Contract)
|
|
|
|
}
|
|
|
|
|
2021-12-10 04:54:40 +00:00
|
|
|
export async function deleteContract(contractId: string) {
|
|
|
|
const docRef = doc(db, 'contracts', contractId)
|
|
|
|
await deleteDoc(docRef)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function listContracts(creatorId: string): Promise<Contract[]> {
|
2021-12-10 07:08:28 +00:00
|
|
|
const q = query(
|
|
|
|
contractCollection,
|
|
|
|
where('creatorId', '==', creatorId),
|
|
|
|
orderBy('createdTime', 'desc')
|
|
|
|
)
|
2021-12-10 04:54:40 +00:00
|
|
|
const snapshot = await getDocs(q)
|
2021-12-14 01:09:16 +00:00
|
|
|
return snapshot.docs.map((doc) => doc.data() as Contract)
|
|
|
|
}
|
|
|
|
|
2022-04-29 05:39:39 +00:00
|
|
|
export async function listTaggedContractsCaseInsensitive(
|
|
|
|
tag: string
|
|
|
|
): Promise<Contract[]> {
|
|
|
|
const q = query(
|
|
|
|
contractCollection,
|
|
|
|
where('lowercaseTags', 'array-contains', tag.toLowerCase()),
|
|
|
|
orderBy('createdTime', 'desc')
|
|
|
|
)
|
|
|
|
const snapshot = await getDocs(q)
|
|
|
|
return snapshot.docs.map((doc) => doc.data() as Contract)
|
|
|
|
}
|
|
|
|
|
2022-06-09 01:08:06 +00:00
|
|
|
export async function listAllContracts(
|
|
|
|
n: number,
|
|
|
|
before?: string
|
|
|
|
): Promise<Contract[]> {
|
|
|
|
let q = query(contractCollection, orderBy('createdTime', 'desc'), limit(n))
|
|
|
|
if (before != null) {
|
|
|
|
const snap = await getDoc(doc(db, 'contracts', before))
|
|
|
|
q = query(q, startAfter(snap))
|
|
|
|
}
|
2021-12-14 01:09:16 +00:00
|
|
|
const snapshot = await getDocs(q)
|
|
|
|
return snapshot.docs.map((doc) => doc.data() as Contract)
|
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
|
|
|
|
) {
|
2021-12-30 18:35:29 +00:00
|
|
|
const q = query(contractCollection, 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(
|
|
|
|
contractCollection,
|
|
|
|
where('creatorId', '==', creatorId),
|
|
|
|
orderBy('createdTime', 'desc')
|
|
|
|
)
|
|
|
|
return listenForValues<Contract>(q, setContracts)
|
|
|
|
}
|
|
|
|
|
2022-02-27 21:37:04 +00:00
|
|
|
const activeContractsQuery = query(
|
2022-02-25 08:11:10 +00:00
|
|
|
contractCollection,
|
|
|
|
where('isResolved', '==', false),
|
|
|
|
where('visibility', '==', 'public'),
|
2022-04-09 23:10:58 +00:00
|
|
|
where('volume7Days', '>', 0)
|
2022-02-25 08:11:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
export function getActiveContracts() {
|
2022-02-27 21:37:04 +00:00
|
|
|
return getValues<Contract>(activeContractsQuery)
|
2022-02-25 08:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function listenForActiveContracts(
|
|
|
|
setContracts: (contracts: Contract[]) => void
|
|
|
|
) {
|
2022-02-27 21:37:04 +00:00
|
|
|
return listenForValues<Contract>(activeContractsQuery, setContracts)
|
|
|
|
}
|
|
|
|
|
|
|
|
const inactiveContractsQuery = query(
|
|
|
|
contractCollection,
|
|
|
|
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(
|
|
|
|
contractCollection,
|
|
|
|
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
|
|
|
) {
|
|
|
|
const contractRef = doc(contractCollection, 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-01-17 22:54:00 +00:00
|
|
|
function chooseRandomSubset(contracts: Contract[], count: number) {
|
|
|
|
const fiveMinutes = 5 * 60 * 1000
|
|
|
|
const seed = Math.round(Date.now() / fiveMinutes).toString()
|
|
|
|
shuffle(contracts, createRNG(seed))
|
|
|
|
return contracts.slice(0, count)
|
|
|
|
}
|
|
|
|
|
2022-01-09 20:26:51 +00:00
|
|
|
const hotContractsQuery = query(
|
|
|
|
contractCollection,
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getHotContracts() {
|
|
|
|
const contracts = await getValues<Contract>(hotContractsQuery)
|
2022-05-22 08:36:05 +00:00
|
|
|
return sortBy(
|
2022-01-27 00:21:14 +00:00
|
|
|
chooseRandomSubset(contracts, 10),
|
2022-01-17 22:54:00 +00:00
|
|
|
(contract) => -1 * contract.volume24Hours
|
2022-01-15 06:43:15 +00:00
|
|
|
)
|
2022-01-05 06:32:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 23:44:01 +00:00
|
|
|
export async function getContractsBySlugs(slugs: string[]) {
|
|
|
|
const q = query(contractCollection, where('slug', 'in', slugs))
|
|
|
|
const snapshot = await getDocs(q)
|
|
|
|
const contracts = snapshot.docs.map((doc) => doc.data() as Contract)
|
2022-05-22 08:36:05 +00:00
|
|
|
return sortBy(contracts, (contract) => -1 * contract.volume24Hours)
|
2022-05-07 23:44:01 +00:00
|
|
|
}
|
|
|
|
|
2022-05-01 16:36:54 +00:00
|
|
|
const topWeeklyQuery = query(
|
|
|
|
contractCollection,
|
|
|
|
where('isResolved', '==', false),
|
|
|
|
orderBy('volume7Days', 'desc'),
|
|
|
|
limit(MAX_FEED_CONTRACTS)
|
|
|
|
)
|
|
|
|
export async function getTopWeeklyContracts() {
|
|
|
|
return await getValues<Contract>(topWeeklyQuery)
|
|
|
|
}
|
|
|
|
|
2022-01-17 22:54:00 +00:00
|
|
|
const closingSoonQuery = query(
|
|
|
|
contractCollection,
|
|
|
|
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() {
|
|
|
|
const contracts = await getValues<Contract>(closingSoonQuery)
|
2022-05-22 08:36:05 +00:00
|
|
|
return sortBy(
|
2022-01-17 22:54:00 +00:00
|
|
|
chooseRandomSubset(contracts, 2),
|
|
|
|
(contract) => contract.closeTime
|
|
|
|
)
|
2022-01-05 06:32:52 +00:00
|
|
|
}
|
2022-03-03 01:52:41 +00:00
|
|
|
|
|
|
|
const getContractsQuery = (startTime: number, endTime: number) =>
|
|
|
|
query(
|
|
|
|
collection(db, 'contracts'),
|
|
|
|
where('createdTime', '>=', startTime),
|
|
|
|
where('createdTime', '<', endTime),
|
|
|
|
orderBy('createdTime', 'asc')
|
|
|
|
)
|
|
|
|
|
|
|
|
const DAY_IN_MS = 24 * 60 * 60 * 1000
|
|
|
|
|
|
|
|
export async function getDailyContracts(
|
|
|
|
startTime: number,
|
|
|
|
numberOfDays: number
|
|
|
|
) {
|
|
|
|
const query = getContractsQuery(
|
|
|
|
startTime,
|
|
|
|
startTime + DAY_IN_MS * numberOfDays
|
|
|
|
)
|
|
|
|
const contracts = await getValues<Contract>(query)
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const contractsByDay = range(0, numberOfDays).map(() => [] as Contract[])
|
2022-03-03 01:52:41 +00:00
|
|
|
for (const contract of contracts) {
|
|
|
|
const dayIndex = Math.floor((contract.createdTime - startTime) / DAY_IN_MS)
|
|
|
|
contractsByDay[dayIndex].push(contract)
|
|
|
|
}
|
|
|
|
|
|
|
|
return contractsByDay
|
|
|
|
}
|
2022-05-01 16:36:54 +00:00
|
|
|
|
|
|
|
export async function getRecentBetsAndComments(contract: Contract) {
|
|
|
|
const contractDoc = doc(db, 'contracts', contract.id)
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|