Revert "create market: use transaction"

This reverts commit e1f24f24a9.
This commit is contained in:
mantikoros 2022-10-06 10:54:42 -05:00
parent 26f04fb04a
commit b8d65acc3f

View File

@ -15,7 +15,7 @@ import {
import { slugify } from '../../common/util/slugify'
import { randomString } from '../../common/util/random'
import { isProd } from './utils'
import { chargeUser, getContract, isProd } from './utils'
import { APIError, AuthedUser, newEndpoint, validate, zTimestamp } from './api'
import { FIXED_ANTE, FREE_MARKETS_PER_USER_MAX } from '../../common/economy'
@ -36,7 +36,7 @@ import { getPseudoProbability } from '../../common/pseudo-numeric'
import { JSONContent } from '@tiptap/core'
import { uniq, zip } from 'lodash'
import { Bet } from '../../common/bet'
import { FieldValue, Transaction } from 'firebase-admin/firestore'
import { FieldValue } from 'firebase-admin/firestore'
const descScehma: z.ZodType<JSONContent> = z.lazy(() =>
z.intersection(
@ -107,7 +107,6 @@ export async function createMarketHelper(body: any, auth: AuthedUser) {
visibility = 'public',
} = validate(bodySchema, body)
return await firestore.runTransaction(async (trans) => {
let min, max, initialProb, isLogScale, answers
if (outcomeType === 'PSEUDO_NUMERIC' || outcomeType === 'NUMERIC') {
@ -116,8 +115,7 @@ export async function createMarketHelper(body: any, auth: AuthedUser) {
if (max - min <= 0.01 || initialValue <= min || initialValue >= max)
throw new APIError(400, 'Invalid range.')
initialProb =
getPseudoProbability(initialValue, min, max, isLogScale) * 100
initialProb = getPseudoProbability(initialValue, min, max, isLogScale) * 100
if (initialProb < 1 || initialProb > 99)
if (outcomeType === 'PSEUDO_NUMERIC')
@ -136,7 +134,7 @@ export async function createMarketHelper(body: any, auth: AuthedUser) {
;({ answers } = validate(multipleChoiceSchema, body))
}
const userDoc = await trans.get(firestore.collection('users').doc(auth.uid))
const userDoc = await firestore.collection('users').doc(auth.uid).get()
if (!userDoc.exists) {
throw new APIError(400, 'No user exists with the authenticated user ID.')
}
@ -152,15 +150,15 @@ export async function createMarketHelper(body: any, auth: AuthedUser) {
let group: Group | null = null
if (groupId) {
const groupDocRef = firestore.collection('groups').doc(groupId)
const groupDoc = await trans.get(groupDocRef)
const groupDoc = await groupDocRef.get()
if (!groupDoc.exists) {
throw new APIError(400, 'No group exists with the given group ID.')
}
group = groupDoc.data() as Group
const groupMembersSnap = await trans.get(
firestore.collection(`groups/${groupId}/groupMembers`)
)
const groupMembersSnap = await firestore
.collection(`groups/${groupId}/groupMembers`)
.get()
const groupMemberDocs = groupMembersSnap.docs.map(
(doc) => doc.data() as { userId: string; createdTime: number }
)
@ -175,7 +173,7 @@ export async function createMarketHelper(body: any, auth: AuthedUser) {
)
}
}
const slug = await getSlug(trans, question)
const slug = await getSlug(question)
const contractRef = firestore.collection('contracts').doc()
console.log(
@ -226,36 +224,28 @@ export async function createMarketHelper(body: any, auth: AuthedUser) {
: DEV_HOUSE_LIQUIDITY_PROVIDER_ID
: user.id
if (ante) {
const delta = FieldValue.increment(-ante)
const providerDoc = firestore.collection('users').doc(providerId)
await trans.update(providerDoc, { balance: delta, totalDeposits: delta })
}
if (deservesFreeMarket) {
await trans.update(firestore.collection('users').doc(user.id), {
freeMarketsCreated: FieldValue.increment(1),
})
}
if (ante) await chargeUser(providerId, ante, true)
if (deservesFreeMarket)
await firestore
.collection('users')
.doc(user.id)
.update({ freeMarketsCreated: FieldValue.increment(1) })
await contractRef.create(contract)
if (group != null) {
const groupContractsSnap = await trans.get(
firestore.collection(`groups/${groupId}/groupContracts`)
)
const groupContractsSnap = await firestore
.collection(`groups/${groupId}/groupContracts`)
.get()
const groupContracts = groupContractsSnap.docs.map(
(doc) => doc.data() as { contractId: string; createdTime: number }
)
if (!groupContracts.map((c) => c.contractId).includes(contractRef.id)) {
await createGroupLinks(trans, group, [contractRef.id], auth.uid)
await createGroupLinks(group, [contractRef.id], auth.uid)
const groupContractRef = firestore
.collection(`groups/${groupId}/groupContracts`)
.doc(contract.id)
await trans.set(groupContractRef, {
await groupContractRef.set({
contractId: contract.id,
createdTime: Date.now(),
})
@ -274,7 +264,7 @@ export async function createMarketHelper(body: any, auth: AuthedUser) {
ante
)
await trans.set(liquidityDoc, lp)
await liquidityDoc.set(lp)
} else if (outcomeType === 'MULTIPLE_CHOICE') {
const betCol = firestore.collection(`contracts/${contract.id}/bets`)
const betDocs = (answers ?? []).map(() => betCol.doc())
@ -292,23 +282,21 @@ export async function createMarketHelper(body: any, auth: AuthedUser) {
)
await Promise.all(
zip(bets, betDocs).map(([bet, doc]) =>
doc ? trans.create(doc, bet as Bet) : undefined
)
zip(bets, betDocs).map(([bet, doc]) => doc?.create(bet as Bet))
)
await Promise.all(
zip(answerObjects, answerDocs).map(([answer, doc]) =>
doc ? trans.create(doc, answer as Answer) : undefined
doc?.create(answer as Answer)
)
)
await trans.update(contractRef, { answers: answerObjects })
await contractRef.update({ answers: answerObjects })
} else if (outcomeType === 'FREE_RESPONSE') {
const noneAnswerDoc = firestore
.collection(`contracts/${contract.id}/answers`)
.doc('0')
const noneAnswer = getNoneAnswer(contract.id, user)
await trans.set(noneAnswerDoc, noneAnswer)
await noneAnswerDoc.set(noneAnswer)
const anteBetDoc = firestore
.collection(`contracts/${contract.id}/bets`)
@ -319,7 +307,7 @@ export async function createMarketHelper(body: any, auth: AuthedUser) {
contract as FreeResponseContract,
anteBetDoc.id
)
await trans.set(anteBetDoc, anteBet)
await anteBetDoc.set(anteBet)
} else if (outcomeType === 'NUMERIC') {
const anteBetDoc = firestore
.collection(`contracts/${contract.id}/bets`)
@ -332,17 +320,16 @@ export async function createMarketHelper(body: any, auth: AuthedUser) {
anteBetDoc.id
)
await trans.set(anteBetDoc, anteBet)
await anteBetDoc.set(anteBet)
}
return contract
})
}
const getSlug = async (trans: Transaction, question: string) => {
const getSlug = async (question: string) => {
const proposedSlug = slugify(question)
const preexistingContract = await getContractFromSlug(trans, proposedSlug)
const preexistingContract = await getContractFromSlug(proposedSlug)
return preexistingContract
? proposedSlug + '-' + randomString()
@ -351,31 +338,35 @@ const getSlug = async (trans: Transaction, question: string) => {
const firestore = admin.firestore()
async function getContractFromSlug(trans: Transaction, slug: string) {
const snap = await trans.get(
firestore.collection('contracts').where('slug', '==', slug)
)
export async function getContractFromSlug(slug: string) {
const snap = await firestore
.collection('contracts')
.where('slug', '==', slug)
.get()
return snap.empty ? undefined : (snap.docs[0].data() as Contract)
}
async function createGroupLinks(
trans: Transaction,
group: Group,
contractIds: string[],
userId: string
) {
for (const contractId of contractIds) {
const contractRef = firestore.collection('contracts').doc(contractId)
const contract = (await trans.get(contractRef)).data() as Contract
const contract = await getContract(contractId)
if (!contract?.groupSlugs?.includes(group.slug)) {
await trans.update(contractRef, {
await firestore
.collection('contracts')
.doc(contractId)
.update({
groupSlugs: uniq([group.slug, ...(contract?.groupSlugs ?? [])]),
})
}
if (!contract?.groupLinks?.map((gl) => gl.groupId).includes(group.id)) {
await trans.update(contractRef, {
await firestore
.collection('contracts')
.doc(contractId)
.update({
groupLinks: [
{
groupId: group.id,