2021-12-24 21:06:01 +00:00
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
import * as functions from 'firebase-functions'
|
|
|
|
|
2022-01-10 21:07:57 +00:00
|
|
|
import { Contract } from '../../common/contract'
|
|
|
|
import { User } from '../../common/user'
|
|
|
|
import { Bet } from '../../common/bet'
|
2022-01-10 22:49:04 +00:00
|
|
|
import { getSellBetInfo } from '../../common/sell-bet'
|
2021-12-24 21:06:01 +00:00
|
|
|
|
|
|
|
export const sellBet = functions.runWith({ minInstances: 1 }).https.onCall(
|
|
|
|
async (
|
|
|
|
data: {
|
|
|
|
contractId: string
|
|
|
|
betId: string
|
|
|
|
},
|
|
|
|
context
|
|
|
|
) => {
|
|
|
|
const userId = context?.auth?.uid
|
|
|
|
if (!userId) return { status: 'error', message: 'Not authorized' }
|
|
|
|
|
|
|
|
const { contractId, betId } = data
|
|
|
|
|
|
|
|
// run as transaction to prevent race conditions
|
|
|
|
return await firestore.runTransaction(async (transaction) => {
|
|
|
|
const userDoc = firestore.doc(`users/${userId}`)
|
|
|
|
const userSnap = await transaction.get(userDoc)
|
|
|
|
if (!userSnap.exists)
|
|
|
|
return { status: 'error', message: 'User not found' }
|
|
|
|
const user = userSnap.data() as User
|
|
|
|
|
|
|
|
const contractDoc = firestore.doc(`contracts/${contractId}`)
|
|
|
|
const contractSnap = await transaction.get(contractDoc)
|
|
|
|
if (!contractSnap.exists)
|
|
|
|
return { status: 'error', message: 'Invalid contract' }
|
|
|
|
const contract = contractSnap.data() as Contract
|
|
|
|
|
2022-01-10 16:32:20 +00:00
|
|
|
const { closeTime } = contract
|
|
|
|
if (closeTime && Date.now() > closeTime)
|
|
|
|
return { status: 'error', message: 'Trading is closed' }
|
|
|
|
|
2021-12-24 21:06:01 +00:00
|
|
|
const betDoc = firestore.doc(`contracts/${contractId}/bets/${betId}`)
|
|
|
|
const betSnap = await transaction.get(betDoc)
|
|
|
|
if (!betSnap.exists) return { status: 'error', message: 'Invalid bet' }
|
|
|
|
const bet = betSnap.data() as Bet
|
|
|
|
|
|
|
|
if (bet.isSold) return { status: 'error', message: 'Bet already sold' }
|
|
|
|
|
|
|
|
const newBetDoc = firestore
|
|
|
|
.collection(`contracts/${contractId}/bets`)
|
|
|
|
.doc()
|
|
|
|
|
2022-01-02 01:03:18 +00:00
|
|
|
const {
|
|
|
|
newBet,
|
|
|
|
newPool,
|
|
|
|
newTotalShares,
|
|
|
|
newTotalBets,
|
|
|
|
newBalance,
|
|
|
|
creatorFee,
|
|
|
|
} = getSellBetInfo(user, bet, contract, newBetDoc.id)
|
2021-12-24 21:06:01 +00:00
|
|
|
|
|
|
|
const creatorDoc = firestore.doc(`users/${contract.creatorId}`)
|
|
|
|
const creatorSnap = await transaction.get(creatorDoc)
|
|
|
|
if (creatorSnap.exists) {
|
|
|
|
const creator = creatorSnap.data() as User
|
|
|
|
const creatorNewBalance = creator.balance + creatorFee
|
|
|
|
transaction.update(creatorDoc, { balance: creatorNewBalance })
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction.update(betDoc, { isSold: true })
|
|
|
|
transaction.create(newBetDoc, newBet)
|
|
|
|
transaction.update(contractDoc, {
|
|
|
|
pool: newPool,
|
|
|
|
totalShares: newTotalShares,
|
2022-01-02 01:03:18 +00:00
|
|
|
totalBets: newTotalBets,
|
2021-12-24 21:06:01 +00:00
|
|
|
})
|
|
|
|
transaction.update(userDoc, { balance: newBalance })
|
|
|
|
|
|
|
|
return { status: 'success' }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const firestore = admin.firestore()
|