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) updateMakers(makers, betDoc.id, contractDoc, trans)
} }
trans.update(userDoc, { balance: FieldValue.increment(-newBet.amount) }) if (newBet.amount !== 0) {
log('Updated user balance.') trans.update(userDoc, { balance: FieldValue.increment(-newBet.amount) })
trans.update( log('Updated user balance.')
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 } 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.') log('Main transaction finished.')
await redeemShares(auth.uid, contractId)
const userIds = [ if (result.newBet.amount !== 0) {
auth.uid, const userIds = [
...(result.makers ?? []).map((maker) => maker.bet.userId), auth.uid,
] ...(result.makers ?? []).map((maker) => maker.bet.userId),
await Promise.all(userIds.map((userId) => redeemShares(userId, contractId))) ]
log('Share redemption transaction finished.') await Promise.all(userIds.map((userId) => redeemShares(userId, contractId)))
log('Share redemption transaction finished.')
}
return { betId: result.betId } return { betId: result.betId }
}) })