If a limit bet doesn't match any orders, don't update the contract, don't redeem shares. Perf win!

This commit is contained in:
James Grugett 2022-07-14 18:01:35 -05:00
parent 6e1aa4b0f4
commit a9018d77c7

View File

@ -128,34 +128,38 @@ export const placebet = newEndpoint({}, async (req, auth) => {
updateMakers(makers, betDoc.id, contractDoc, trans)
}
trans.update(userDoc, { balance: FieldValue.increment(-newBet.amount) })
log('Updated user balance.')
trans.update(
contractDoc,
removeUndefinedProps({
pool: newPool,
p: newP,
totalShares: newTotalShares,
totalBets: newTotalBets,
totalLiquidity: newTotalLiquidity,
collectedFees: addObjects(newBet.fees, collectedFees),
volume: volume + newBet.amount,
})
)
log('Updated contract properties.')
if (newBet.amount !== 0) {
trans.update(userDoc, { balance: FieldValue.increment(-newBet.amount) })
log('Updated user balance.')
return { betId: betDoc.id, makers }
trans.update(
contractDoc,
removeUndefinedProps({
pool: newPool,
p: newP,
totalShares: newTotalShares,
totalBets: newTotalBets,
totalLiquidity: newTotalLiquidity,
collectedFees: addObjects(newBet.fees, collectedFees),
volume: volume + newBet.amount,
})
)
log('Updated contract properties.')
}
return { betId: betDoc.id, makers, newBet }
})
log('Main transaction finished.')
await redeemShares(auth.uid, contractId)
const userIds = [
auth.uid,
...(result.makers ?? []).map((maker) => maker.bet.userId),
]
await Promise.all(userIds.map((userId) => redeemShares(userId, contractId)))
log('Share redemption transaction finished.')
if (result.newBet.amount !== 0) {
const userIds = [
auth.uid,
...(result.makers ?? []).map((maker) => maker.bet.userId),
]
await Promise.all(userIds.map((userId) => redeemShares(userId, contractId)))
log('Share redemption transaction finished.')
}
return { betId: result.betId }
})