manifold/web/lib/firebase/contracts.ts

180 lines
4.7 KiB
TypeScript
Raw Normal View History

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,
getDoc,
2022-01-07 19:27:59 +00:00
updateDoc,
limit,
2021-12-10 04:54:40 +00:00
} from 'firebase/firestore'
2022-01-17 22:54:00 +00:00
import _ from 'lodash'
2021-12-17 22:15:09 +00:00
import { app } from './init'
import { getValues, listenForValue, listenForValues } from './utils'
import { Contract } from '../../../common/contract'
import { getProbability } from '../../../common/calculate'
import { createRNG, shuffle } from '../../../common/util/random'
export type { Contract }
2021-12-10 04:54:40 +00:00
export function contractPath(contract: Contract) {
2021-12-18 07:27:29 +00:00
return `/${contract.creatorUsername}/${contract.slug}`
}
export function contractMetrics(contract: Contract) {
const {
pool,
phantomShares,
totalShares,
createdTime,
resolutionTime,
isResolved,
resolutionProbability,
} = contract
const truePool = pool.YES + pool.NO
const prob = resolutionProbability ?? getProbability(totalShares)
const probPercent = Math.round(prob * 100) + '%'
const startProb = getProbability(phantomShares)
const createdDate = dayjs(createdTime).format('MMM D')
2021-12-15 07:41:50 +00:00
const resolvedDate = isResolved
? dayjs(resolutionTime).format('MMM D')
: undefined
return { truePool, probPercent, startProb, createdDate, resolvedDate }
}
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)
export const contractCollection = collection(db, 'contracts')
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) {
const docRef = doc(db, 'contracts', contractId)
const result = await getDoc(docRef)
return result.exists() ? (result.data() as Contract) : undefined
}
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)
return snapshot.docs.map((doc) => doc.data() as Contract)
}
export async function listAllContracts(): Promise<Contract[]> {
const q = query(contractCollection, orderBy('createdTime', 'desc'))
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
export function listenForContracts(
setContracts: (contracts: Contract[]) => void
) {
const q = query(contractCollection, orderBy('createdTime', 'desc'))
return listenForValues<Contract>(q, 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)
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)
}
const hotContractsQuery = query(
contractCollection,
where('isResolved', '==', false),
where('visibility', '==', 'public'),
orderBy('volume24Hours', 'desc'),
limit(16)
)
export function listenForHotContracts(
setHotContracts: (contracts: Contract[]) => void
) {
2022-01-17 22:54:00 +00:00
return listenForValues<Contract>(hotContractsQuery, (contracts) => {
const hotContracts = _.sortBy(
chooseRandomSubset(contracts, 4),
(contract) => contract.volume24Hours
)
setHotContracts(hotContracts)
})
}
export async function getHotContracts() {
const contracts = await getValues<Contract>(hotContractsQuery)
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-05 06:32:52 +00:00
}
2022-01-17 22:54:00 +00:00
const closingSoonQuery = query(
contractCollection,
where('isResolved', '==', false),
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)
return _.sortBy(
chooseRandomSubset(contracts, 2),
(contract) => contract.closeTime
)
2022-01-05 06:32:52 +00:00
}