sellBet: handle case where bettor equals market creator

This commit is contained in:
mantikoros 2022-02-06 17:18:22 -06:00
parent 25e498ab3c
commit 4f0b94150c

View File

@ -57,14 +57,21 @@ export const sellBet = functions.runWith({ minInstances: 1 }).https.onCall(
creatorFee, creatorFee,
} = getSellBetInfo(user, bet, contract, newBetDoc.id) } = getSellBetInfo(user, bet, contract, newBetDoc.id)
if (contract.creatorId === userId) {
transaction.update(userDoc, { balance: newBalance + creatorFee })
} else {
const creatorDoc = firestore.doc(`users/${contract.creatorId}`) const creatorDoc = firestore.doc(`users/${contract.creatorId}`)
const creatorSnap = await transaction.get(creatorDoc) const creatorSnap = await transaction.get(creatorDoc)
if (creatorSnap.exists) { if (creatorSnap.exists) {
const creator = creatorSnap.data() as User const creator = creatorSnap.data() as User
const creatorNewBalance = creator.balance + creatorFee const creatorNewBalance = creator.balance + creatorFee
transaction.update(creatorDoc, { balance: creatorNewBalance }) transaction.update(creatorDoc, { balance: creatorNewBalance })
} }
transaction.update(userDoc, { balance: newBalance })
}
transaction.update(betDoc, { isSold: true }) transaction.update(betDoc, { isSold: true })
transaction.create(newBetDoc, newBet) transaction.create(newBetDoc, newBet)
transaction.update(contractDoc, { transaction.update(contractDoc, {
@ -72,7 +79,6 @@ export const sellBet = functions.runWith({ minInstances: 1 }).https.onCall(
totalShares: newTotalShares, totalShares: newTotalShares,
totalBets: newTotalBets, totalBets: newTotalBets,
}) })
transaction.update(userDoc, { balance: newBalance })
return { status: 'success' } return { status: 'success' }
}) })