2021-12-24 21:06:01 +00:00
|
|
|
import * as admin from 'firebase-admin'
|
2022-06-07 21:08:56 +00:00
|
|
|
import { z } from 'zod'
|
2021-12-24 21:06:01 +00:00
|
|
|
|
2022-06-07 21:08:56 +00:00
|
|
|
import { APIError, newEndpoint, validate } from './api'
|
2022-05-15 17:39:42 +00:00
|
|
|
import { Contract } from '../../common/contract'
|
|
|
|
import { User } from '../../common/user'
|
|
|
|
import { Bet } from '../../common/bet'
|
|
|
|
import { getSellBetInfo } from '../../common/sell-bet'
|
|
|
|
import { addObjects, removeUndefinedProps } from '../../common/util/object'
|
2021-12-24 21:06:01 +00:00
|
|
|
|
2022-06-07 21:08:56 +00:00
|
|
|
const bodySchema = z.object({
|
|
|
|
contractId: z.string(),
|
|
|
|
betId: z.string(),
|
|
|
|
})
|
2021-12-24 21:06:01 +00:00
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
export const sellbet = newEndpoint({}, async (req, auth) => {
|
2022-06-07 21:08:56 +00:00
|
|
|
const { contractId, betId } = validate(bodySchema, req.body)
|
2021-12-24 21:06:01 +00:00
|
|
|
|
2022-06-07 21:08:56 +00:00
|
|
|
// run as transaction to prevent race conditions
|
|
|
|
return await firestore.runTransaction(async (transaction) => {
|
|
|
|
const contractDoc = firestore.doc(`contracts/${contractId}`)
|
2022-06-11 00:51:55 +00:00
|
|
|
const userDoc = firestore.doc(`users/${auth.uid}`)
|
|
|
|
const betDoc = firestore.doc(`contracts/${contractId}/bets/${betId}`)
|
2022-07-02 23:24:03 +00:00
|
|
|
const [contractSnap, userSnap, betSnap] = await transaction.getAll(
|
|
|
|
contractDoc,
|
|
|
|
userDoc,
|
|
|
|
betDoc
|
|
|
|
)
|
2022-06-07 21:08:56 +00:00
|
|
|
if (!contractSnap.exists) throw new APIError(400, 'Contract not found.')
|
2022-06-11 00:51:55 +00:00
|
|
|
if (!userSnap.exists) throw new APIError(400, 'User not found.')
|
|
|
|
if (!betSnap.exists) throw new APIError(400, 'Bet not found.')
|
|
|
|
|
2022-06-07 21:08:56 +00:00
|
|
|
const contract = contractSnap.data() as Contract
|
2022-06-11 00:51:55 +00:00
|
|
|
const user = userSnap.data() as User
|
|
|
|
const bet = betSnap.data() as Bet
|
2022-03-29 19:56:56 +00:00
|
|
|
|
2022-06-07 21:08:56 +00:00
|
|
|
const { closeTime, mechanism, collectedFees, volume } = contract
|
|
|
|
if (mechanism !== 'dpm-2')
|
|
|
|
throw new APIError(400, 'You can only sell bets on DPM-2 contracts.')
|
|
|
|
if (closeTime && Date.now() > closeTime)
|
|
|
|
throw new APIError(400, 'Trading is closed.')
|
2022-03-29 19:56:56 +00:00
|
|
|
|
2022-06-11 00:51:55 +00:00
|
|
|
if (auth.uid !== bet.userId)
|
2022-06-07 21:08:56 +00:00
|
|
|
throw new APIError(400, 'The specified bet does not belong to you.')
|
|
|
|
if (bet.isSold)
|
|
|
|
throw new APIError(400, 'The specified bet is already sold.')
|
2021-12-24 21:06:01 +00:00
|
|
|
|
2022-06-07 21:08:56 +00:00
|
|
|
const { newBet, newPool, newTotalShares, newTotalBets, fees } =
|
|
|
|
getSellBetInfo(bet, contract)
|
2021-12-24 21:06:01 +00:00
|
|
|
|
2022-06-07 21:08:56 +00:00
|
|
|
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */
|
|
|
|
const saleAmount = newBet.sale!.amount
|
|
|
|
const newBalance = user.balance + saleAmount - (bet.loanAmount ?? 0)
|
|
|
|
const newBetDoc = firestore.collection(`contracts/${contractId}/bets`).doc()
|
2021-12-24 21:06:01 +00:00
|
|
|
|
2022-06-07 21:08:56 +00:00
|
|
|
transaction.update(userDoc, { balance: newBalance })
|
|
|
|
transaction.update(betDoc, { isSold: true })
|
|
|
|
transaction.create(newBetDoc, { id: betDoc.id, userId: user.id, ...newBet })
|
|
|
|
transaction.update(
|
|
|
|
contractDoc,
|
|
|
|
removeUndefinedProps({
|
|
|
|
pool: newPool,
|
|
|
|
totalShares: newTotalShares,
|
|
|
|
totalBets: newTotalBets,
|
|
|
|
collectedFees: addObjects(fees, collectedFees),
|
|
|
|
volume: volume + Math.abs(newBet.amount),
|
|
|
|
})
|
|
|
|
)
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2022-06-07 21:08:56 +00:00
|
|
|
return {}
|
|
|
|
})
|
|
|
|
})
|
2021-12-24 21:06:01 +00:00
|
|
|
|
|
|
|
const firestore = admin.firestore()
|