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-03-15 22:27:51 +00:00
|
|
|
import { getCpmmSellBetInfo, getSellBetInfo } from '../../common/sell-bet'
|
|
|
|
import { addObjects, removeUndefinedProps } from '../../common/util/object'
|
|
|
|
import { Fees } from '../../common/fees'
|
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-03-15 22:27:51 +00:00
|
|
|
const { closeTime, mechanism, collectedFees } = contract
|
2022-01-10 16:32:20 +00:00
|
|
|
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,
|
2022-03-15 22:27:51 +00:00
|
|
|
fees,
|
|
|
|
} =
|
|
|
|
mechanism === 'dpm-2'
|
|
|
|
? getSellBetInfo(user, bet, contract, newBetDoc.id)
|
|
|
|
: (getCpmmSellBetInfo(
|
|
|
|
user,
|
|
|
|
bet,
|
|
|
|
contract as any,
|
|
|
|
newBetDoc.id
|
|
|
|
) as any)
|
|
|
|
|
2022-03-16 03:05:08 +00:00
|
|
|
if (!isFinite(newBalance)) {
|
|
|
|
throw new Error('Invalid user balance for ' + user.username)
|
|
|
|
}
|
2022-03-15 22:27:51 +00:00
|
|
|
transaction.update(userDoc, { balance: newBalance })
|
2021-12-24 21:06:01 +00:00
|
|
|
|
|
|
|
transaction.update(betDoc, { isSold: true })
|
|
|
|
transaction.create(newBetDoc, newBet)
|
2022-03-15 22:27:51 +00:00
|
|
|
transaction.update(
|
|
|
|
contractDoc,
|
|
|
|
removeUndefinedProps({
|
|
|
|
pool: newPool,
|
|
|
|
totalShares: newTotalShares,
|
|
|
|
totalBets: newTotalBets,
|
|
|
|
collectedFees: addObjects<Fees>(fees ?? {}, collectedFees ?? {}),
|
|
|
|
})
|
|
|
|
)
|
2021-12-24 21:06:01 +00:00
|
|
|
|
|
|
|
return { status: 'success' }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const firestore = admin.firestore()
|