From 4aa90da13b1fc9d5e0d67fa2c6561fdba6b88abe Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Mon, 4 Nov 2024 23:17:11 -0800 Subject: [PATCH] Remove runTxnInBetQueue from create-market --- backend/api/src/create-market.ts | 72 ++++++++----------------------- backend/shared/src/txn/run-txn.ts | 28 +++++++++++- 2 files changed, 44 insertions(+), 56 deletions(-) diff --git a/backend/api/src/create-market.ts b/backend/api/src/create-market.ts index aac17661b8..4d6a32d82d 100644 --- a/backend/api/src/create-market.ts +++ b/backend/api/src/create-market.ts @@ -44,7 +44,7 @@ import { } from 'shared/supabase/init' import { insertLiquidity } from 'shared/supabase/liquidity' import { anythingToRichText } from 'shared/tiptap' -import { runTxnInBetQueue, runTxnFromBank } from 'shared/txn/run-txn' +import { runTxnOutsideBetQueue } from 'shared/txn/run-txn' import { addGroupToContract, canUserAddGroupToMarket, @@ -60,15 +60,17 @@ import { z } from 'zod' type Body = ValidatedAPIParams<'market'> export const createMarket: APIHandler<'market'> = async (body, auth) => { - const market = await createMarketHelper(body, auth) - const pg = createSupabaseDirectClient() + const { contract: market, user } = await createMarketHelper(body, auth) // Should have the embedding ready for the related contracts cache - const embedding = await generateContractEmbeddings(market, pg).catch((e) => - log.error(`Failed to generate embeddings, returning ${market.id} `, e) - ) return { result: toLiteMarket(market), continue: async () => { + const pg = createSupabaseDirectClient() + const embedding = await generateContractEmbeddings(market, pg).catch( + (e) => + log.error(`Failed to generate embeddings, returning ${market.id} `, e) + ) + broadcastNewContract(market, user) await onCreateMarket(market, embedding) }, } @@ -194,12 +196,14 @@ export async function createMarketHelper(body: Body, auth: AuthedUser) { [contract.id, JSON.stringify(contract), contract.token] ) - await runCreateMarketTxn({ - contractId: contract.id, - userId, - amountSuppliedByUser: ante, - amountSuppliedByHouse: 0, - transaction: tx, + await runTxnOutsideBetQueue(tx, { + fromId: userId, + fromType: 'USER', + toId: contract.id, + toType: 'CONTRACT', + amount: ante, + token: 'M$', + category: 'CREATE_CONTRACT_ANTE', }) log('created contract ', { @@ -229,50 +233,10 @@ export async function createMarketHelper(body: Body, auth: AuthedUser) { totalMarketCost ) - broadcastNewContract(contract, user) - return contract + return { contract, user } }) } -const runCreateMarketTxn = async (args: { - contractId: string - userId: string - amountSuppliedByUser: number - amountSuppliedByHouse: number - transaction: SupabaseTransaction -}) => { - const { - contractId, - userId, - amountSuppliedByUser, - amountSuppliedByHouse, - transaction, - } = args - - if (amountSuppliedByUser > 0) { - await runTxnInBetQueue(transaction, { - fromId: userId, - fromType: 'USER', - toId: contractId, - toType: 'CONTRACT', - amount: amountSuppliedByUser, - token: 'M$', - category: 'CREATE_CONTRACT_ANTE', - }) - } - - if (amountSuppliedByHouse > 0) { - await runTxnFromBank(transaction, { - amount: amountSuppliedByHouse, - category: 'CREATE_CONTRACT_ANTE', - toId: contractId, - toType: 'CONTRACT', - fromType: 'BANK', - token: 'M$', - }) - } -} - async function getDuplicateSubmissionUrl( idempotencyKey: string | undefined, pg: SupabaseDirectClient @@ -589,7 +553,7 @@ export async function generateAntes( (contract.mechanism === 'cpmm-1' || contract.mechanism === 'cpmm-multi-1') ) { return await pg.txIf(async (tx) => { - await runTxnInBetQueue(tx, { + await runTxnOutsideBetQueue(tx, { fromId: providerId, amount: drizzledAmount, toId: contract.id, diff --git a/backend/shared/src/txn/run-txn.ts b/backend/shared/src/txn/run-txn.ts index 3ff38c8c7d..633128ed82 100644 --- a/backend/shared/src/txn/run-txn.ts +++ b/backend/shared/src/txn/run-txn.ts @@ -12,17 +12,35 @@ import { buildArray } from 'common/util/array' export type TxnData = Omit +export async function runTxnOutsideBetQueue( + pgTransaction: SupabaseTransaction, + data: TxnData, + affectsProfit = false +) { + return await runTxnInternal(pgTransaction, data, affectsProfit, false) +} + export async function runTxnInBetQueue( pgTransaction: SupabaseTransaction, data: TxnData, affectsProfit = false +) { + return await runTxnInternal(pgTransaction, data, affectsProfit, true) +} + +async function runTxnInternal( + pgTransaction: SupabaseTransaction, + data: TxnData, + affectsProfit = false, + useQueue = true ) { const { amount, fromType, fromId, toId, toType, token } = data const deps = buildArray( (fromType === 'USER' || fromType === 'CONTRACT') && fromId, (toType === 'USER' || toType === 'CONTRACT') && toId ) - return await betsQueue.enqueueFn(async () => { + + const runTxn = async () => { if (!isFinite(amount)) { throw new APIError(400, 'Invalid amount') } @@ -136,7 +154,13 @@ export async function runTxnInBetQueue( } return await insertTxn(pgTransaction, data) - }, deps) + } + + if (useQueue) { + return await betsQueue.enqueueFn(runTxn, deps) + } else { + return await runTxn() + } } export async function runTxnFromBank(