Skip to content

Commit

Permalink
Remove runTxnInBetQueue from create-market
Browse files Browse the repository at this point in the history
  • Loading branch information
IanPhilips committed Nov 5, 2024
1 parent 737b6e2 commit 4aa90da
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 56 deletions.
72 changes: 18 additions & 54 deletions backend/api/src/create-market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
},
}
Expand Down Expand Up @@ -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 ', {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
28 changes: 26 additions & 2 deletions backend/shared/src/txn/run-txn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,35 @@ import { buildArray } from 'common/util/array'

export type TxnData = Omit<Txn, 'id' | 'createdTime'>

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')
}
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 4aa90da

Please sign in to comment.