2021-12-10 04:54:40 +00:00
|
|
|
import { app } from './init'
|
|
|
|
import {
|
|
|
|
getFirestore,
|
|
|
|
doc,
|
|
|
|
setDoc,
|
|
|
|
deleteDoc,
|
|
|
|
where,
|
|
|
|
collection,
|
|
|
|
query,
|
|
|
|
getDocs,
|
2021-12-10 05:01:44 +00:00
|
|
|
onSnapshot,
|
2021-12-10 07:08:28 +00:00
|
|
|
orderBy,
|
2021-12-14 01:07:28 +00:00
|
|
|
getDoc,
|
2021-12-14 01:09:16 +00:00
|
|
|
limit,
|
2021-12-10 04:54:40 +00:00
|
|
|
} from 'firebase/firestore'
|
2021-12-14 10:23:32 +00:00
|
|
|
import dayjs from 'dayjs'
|
2021-12-09 22:05:55 +00:00
|
|
|
|
|
|
|
export type Contract = {
|
2021-12-17 23:16:42 +00:00
|
|
|
id: string
|
|
|
|
slug: string // auto-generated; must be unique
|
|
|
|
|
2021-12-10 04:54:40 +00:00
|
|
|
creatorId: string
|
2021-12-10 06:21:12 +00:00
|
|
|
creatorName: string
|
2021-12-18 07:27:29 +00:00
|
|
|
creatorUsername: string
|
2021-12-10 04:54:40 +00:00
|
|
|
|
2021-12-09 22:05:55 +00:00
|
|
|
question: string
|
2021-12-10 04:54:40 +00:00
|
|
|
description: string // More info about what the contract is about
|
|
|
|
outcomeType: 'BINARY' // | 'MULTI' | 'interval' | 'date'
|
|
|
|
// outcomes: ['YES', 'NO']
|
2021-12-17 22:15:09 +00:00
|
|
|
|
|
|
|
startPool: { YES: number; NO: number }
|
|
|
|
pool: { YES: number; NO: number }
|
2021-12-24 21:06:01 +00:00
|
|
|
totalShares: { YES: number; NO: number }
|
2022-01-02 01:03:18 +00:00
|
|
|
totalBets: { YES: number; NO: number }
|
2021-12-10 04:54:40 +00:00
|
|
|
|
|
|
|
createdTime: number // Milliseconds since epoch
|
|
|
|
lastUpdatedTime: number // If the question or description was changed
|
|
|
|
closeTime?: number // When no more trading is allowed
|
|
|
|
|
2021-12-14 06:12:25 +00:00
|
|
|
isResolved: boolean
|
2021-12-17 22:15:09 +00:00
|
|
|
resolutionTime?: number // When the contract creator resolved the market
|
2021-12-10 04:54:40 +00:00
|
|
|
resolution?: 'YES' | 'NO' | 'CANCEL' // Chosen by creator; must be one of outcomes
|
|
|
|
}
|
|
|
|
|
2021-12-16 00:52:19 +00:00
|
|
|
export function path(contract: Contract) {
|
|
|
|
// For now, derive username from creatorName
|
2021-12-18 07:27:29 +00:00
|
|
|
return `/${contract.creatorUsername}/${contract.slug}`
|
2021-12-16 00:52:19 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 10:23:32 +00:00
|
|
|
export function compute(contract: Contract) {
|
2021-12-17 22:15:09 +00:00
|
|
|
const { pool, startPool, createdTime, resolutionTime, isResolved } = contract
|
2021-12-24 21:06:01 +00:00
|
|
|
const truePool = pool.YES + pool.NO - startPool.YES - startPool.NO
|
2021-12-17 22:15:09 +00:00
|
|
|
const prob = pool.YES ** 2 / (pool.YES ** 2 + pool.NO ** 2)
|
2021-12-14 10:23:32 +00:00
|
|
|
const probPercent = Math.round(prob * 100) + '%'
|
2021-12-21 06:29:32 +00:00
|
|
|
const startProb =
|
|
|
|
startPool.YES ** 2 / (startPool.YES ** 2 + startPool.NO ** 2)
|
2021-12-14 10:23:32 +00:00
|
|
|
const createdDate = dayjs(createdTime).format('MMM D')
|
2021-12-15 07:41:50 +00:00
|
|
|
const resolvedDate = isResolved
|
|
|
|
? dayjs(resolutionTime).format('MMM D')
|
|
|
|
: undefined
|
2021-12-24 21:06:01 +00:00
|
|
|
return { truePool, probPercent, startProb, createdDate, resolvedDate }
|
2021-12-14 10:23:32 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 04:54:40 +00:00
|
|
|
const db = getFirestore(app)
|
2021-12-10 05:01:44 +00:00
|
|
|
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
|
|
|
|
2021-12-17 23:16:42 +00:00
|
|
|
export async function pushNewContract(contract: Omit<Contract, 'id'>) {
|
|
|
|
const newContractRef = doc(contractCollection)
|
|
|
|
const fullContract: Contract = { ...contract, id: newContractRef.id }
|
|
|
|
|
|
|
|
await setDoc(newContractRef, fullContract)
|
|
|
|
return fullContract
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function listAllContracts(): Promise<Contract[]> {
|
2021-12-30 18:35:29 +00:00
|
|
|
const q = query(contractCollection, orderBy('createdTime', 'desc'))
|
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'))
|
2021-12-17 04:44:48 +00:00
|
|
|
return onSnapshot(q, (snap) => {
|
|
|
|
setContracts(snap.docs.map((doc) => doc.data() as Contract))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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 onSnapshot(contractRef, (contractSnap) => {
|
2021-12-15 07:41:50 +00:00
|
|
|
setContract((contractSnap.data() ?? null) as Contract | null)
|
2021-12-10 05:01:44 +00:00
|
|
|
})
|
|
|
|
}
|