placeBet: always run 1 instance
This commit is contained in:
parent
8867d841cb
commit
62ea1bab15
|
@ -5,46 +5,48 @@ import { Contract } from './types/contract'
|
||||||
import { User } from './types/user'
|
import { User } from './types/user'
|
||||||
import { Bet } from './types/bet'
|
import { Bet } from './types/bet'
|
||||||
|
|
||||||
export const placeBet = functions.https.onCall(async (data: {
|
export const placeBet = functions
|
||||||
amount: number
|
.runWith({ minInstances: 1 })
|
||||||
outcome: string
|
.https.onCall(async (data: {
|
||||||
contractId: string
|
amount: number
|
||||||
}, context) => {
|
outcome: string
|
||||||
const userId = context?.auth?.uid
|
contractId: string
|
||||||
if (!userId)
|
}, context) => {
|
||||||
return { status: 'error', message: 'Not authorized' }
|
const userId = context?.auth?.uid
|
||||||
|
if (!userId)
|
||||||
|
return { status: 'error', message: 'Not authorized' }
|
||||||
|
|
||||||
const { amount, outcome, contractId } = data
|
const { amount, outcome, contractId } = data
|
||||||
|
|
||||||
if (outcome !== 'YES' && outcome !== 'NO')
|
if (outcome !== 'YES' && outcome !== 'NO')
|
||||||
return { status: 'error', message: 'Invalid outcome' }
|
return { status: 'error', message: 'Invalid outcome' }
|
||||||
|
|
||||||
// run as transaction to prevent race conditions
|
// run as transaction to prevent race conditions
|
||||||
return await firestore.runTransaction(async transaction => {
|
return await firestore.runTransaction(async transaction => {
|
||||||
const userDoc = firestore.doc(`users/${userId}`)
|
const userDoc = firestore.doc(`users/${userId}`)
|
||||||
const userSnap = await transaction.get(userDoc)
|
const userSnap = await transaction.get(userDoc)
|
||||||
if (!userSnap.exists) return { status: 'error', message: 'User not found' }
|
if (!userSnap.exists) return { status: 'error', message: 'User not found' }
|
||||||
const user = userSnap.data() as User
|
const user = userSnap.data() as User
|
||||||
|
|
||||||
if (user.balanceUsd < amount)
|
if (user.balanceUsd < amount)
|
||||||
return { status: 'error', message: 'Insufficient balance' }
|
return { status: 'error', message: 'Insufficient balance' }
|
||||||
|
|
||||||
const contractDoc = firestore.doc(`contracts/${contractId}`)
|
const contractDoc = firestore.doc(`contracts/${contractId}`)
|
||||||
const contractSnap = await transaction.get(contractDoc)
|
const contractSnap = await transaction.get(contractDoc)
|
||||||
if (!contractSnap.exists) return { status: 'error', message: 'Invalid contract' }
|
if (!contractSnap.exists) return { status: 'error', message: 'Invalid contract' }
|
||||||
const contract = contractSnap.data() as Contract
|
const contract = contractSnap.data() as Contract
|
||||||
|
|
||||||
const newBetDoc = firestore.collection(`contracts/${contractId}/bets`).doc()
|
const newBetDoc = firestore.collection(`contracts/${contractId}/bets`).doc()
|
||||||
|
|
||||||
const { newBet, newPot, newBalance } = getNewBetInfo(user, outcome, amount, contract, newBetDoc.id)
|
const { newBet, newPot, newBalance } = getNewBetInfo(user, outcome, amount, contract, newBetDoc.id)
|
||||||
|
|
||||||
transaction.create(newBetDoc, newBet)
|
transaction.create(newBetDoc, newBet)
|
||||||
transaction.update(contractDoc, { pot: newPot })
|
transaction.update(contractDoc, { pot: newPot })
|
||||||
transaction.update(userDoc, { balanceUsd: newBalance })
|
transaction.update(userDoc, { balanceUsd: newBalance })
|
||||||
|
|
||||||
return { status: 'success' }
|
return { status: 'success' }
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
const firestore = admin.firestore()
|
const firestore = admin.firestore()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user