2021-12-11 00:06:17 +00:00
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
|
2022-05-17 04:43:40 +00:00
|
|
|
import { APIError, newEndpoint, parseCredentials, lookupUser } from './api'
|
2022-05-15 17:39:42 +00:00
|
|
|
import { Contract } from '../../common/contract'
|
|
|
|
import { User } from '../../common/user'
|
2022-03-02 03:31:48 +00:00
|
|
|
import {
|
2022-03-15 22:27:51 +00:00
|
|
|
getNewBinaryCpmmBetInfo,
|
|
|
|
getNewBinaryDpmBetInfo,
|
2022-03-02 03:31:48 +00:00
|
|
|
getNewMultiBetInfo,
|
2022-05-15 17:39:42 +00:00
|
|
|
} from '../../common/new-bet'
|
|
|
|
import { addObjects, removeUndefinedProps } from '../../common/util/object'
|
|
|
|
import { Bet } from '../../common/bet'
|
2022-03-15 22:27:51 +00:00
|
|
|
import { redeemShares } from './redeem-shares'
|
2022-05-15 17:39:42 +00:00
|
|
|
import { Fees } from '../../common/fees'
|
|
|
|
import { hasUserHitManaLimit } from '../../common/calculate'
|
2021-12-11 00:06:17 +00:00
|
|
|
|
2022-05-17 04:43:40 +00:00
|
|
|
export const placeBet = newEndpoint(['POST'], async (req, _res) => {
|
|
|
|
const [bettor, _privateUser] = await lookupUser(await parseCredentials(req))
|
|
|
|
const { amount, outcome, contractId } = req.body.data || {}
|
|
|
|
|
|
|
|
if (amount <= 0 || isNaN(amount) || !isFinite(amount))
|
|
|
|
throw new APIError(400, 'Invalid amount')
|
|
|
|
|
|
|
|
if (outcome !== 'YES' && outcome !== 'NO' && isNaN(+outcome))
|
|
|
|
throw new APIError(400, 'Invalid outcome')
|
|
|
|
|
|
|
|
// run as transaction to prevent race conditions
|
|
|
|
return await firestore
|
|
|
|
.runTransaction(async (transaction) => {
|
|
|
|
const userDoc = firestore.doc(`users/${bettor.id}`)
|
|
|
|
const userSnap = await transaction.get(userDoc)
|
|
|
|
if (!userSnap.exists) throw new APIError(400, 'User not found')
|
|
|
|
const user = userSnap.data() as User
|
|
|
|
|
|
|
|
const contractDoc = firestore.doc(`contracts/${contractId}`)
|
|
|
|
const contractSnap = await transaction.get(contractDoc)
|
|
|
|
if (!contractSnap.exists) throw new APIError(400, 'Invalid contract')
|
|
|
|
const contract = contractSnap.data() as Contract
|
|
|
|
|
|
|
|
const { closeTime, outcomeType, mechanism, collectedFees, volume } =
|
|
|
|
contract
|
|
|
|
if (closeTime && Date.now() > closeTime)
|
|
|
|
throw new APIError(400, 'Trading is closed')
|
|
|
|
|
|
|
|
const yourBetsSnap = await transaction.get(
|
|
|
|
contractDoc.collection('bets').where('userId', '==', bettor.id)
|
|
|
|
)
|
|
|
|
const yourBets = yourBetsSnap.docs.map((doc) => doc.data() as Bet)
|
|
|
|
|
|
|
|
const loanAmount = 0 // getLoanAmount(yourBets, amount)
|
|
|
|
if (user.balance < amount) throw new APIError(400, 'Insufficient balance')
|
|
|
|
|
|
|
|
if (outcomeType === 'FREE_RESPONSE') {
|
|
|
|
const answerSnap = await transaction.get(
|
|
|
|
contractDoc.collection('answers').doc(outcome)
|
2022-03-15 22:27:51 +00:00
|
|
|
)
|
2022-05-17 04:43:40 +00:00
|
|
|
if (!answerSnap.exists) throw new APIError(400, 'Invalid contract')
|
|
|
|
|
|
|
|
const { status, message } = hasUserHitManaLimit(
|
|
|
|
contract,
|
|
|
|
yourBets,
|
|
|
|
amount
|
|
|
|
)
|
|
|
|
if (status === 'error') throw new APIError(400, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
const newBetDoc = firestore
|
|
|
|
.collection(`contracts/${contractId}/bets`)
|
|
|
|
.doc()
|
|
|
|
|
|
|
|
const {
|
|
|
|
newBet,
|
|
|
|
newPool,
|
|
|
|
newTotalShares,
|
|
|
|
newTotalBets,
|
|
|
|
newBalance,
|
|
|
|
newTotalLiquidity,
|
|
|
|
fees,
|
|
|
|
newP,
|
|
|
|
} =
|
|
|
|
outcomeType === 'BINARY'
|
|
|
|
? mechanism === 'dpm-2'
|
|
|
|
? getNewBinaryDpmBetInfo(
|
2022-03-15 22:27:51 +00:00
|
|
|
user,
|
2022-05-17 04:43:40 +00:00
|
|
|
outcome as 'YES' | 'NO',
|
2022-03-15 22:27:51 +00:00
|
|
|
amount,
|
2022-05-17 04:43:40 +00:00
|
|
|
contract,
|
2022-03-15 22:27:51 +00:00
|
|
|
loanAmount,
|
|
|
|
newBetDoc.id
|
|
|
|
)
|
2022-05-17 04:43:40 +00:00
|
|
|
: (getNewBinaryCpmmBetInfo(
|
|
|
|
user,
|
|
|
|
outcome as 'YES' | 'NO',
|
|
|
|
amount,
|
|
|
|
contract,
|
|
|
|
loanAmount,
|
|
|
|
newBetDoc.id
|
|
|
|
) as any)
|
|
|
|
: getNewMultiBetInfo(
|
|
|
|
user,
|
|
|
|
outcome,
|
|
|
|
amount,
|
|
|
|
contract as any,
|
|
|
|
loanAmount,
|
|
|
|
newBetDoc.id
|
|
|
|
)
|
|
|
|
|
|
|
|
if (newP !== undefined && !isFinite(newP)) {
|
|
|
|
throw new APIError(400, 'Trade rejected due to overflow error.')
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction.create(newBetDoc, newBet)
|
|
|
|
|
|
|
|
transaction.update(
|
|
|
|
contractDoc,
|
|
|
|
removeUndefinedProps({
|
|
|
|
pool: newPool,
|
|
|
|
p: newP,
|
|
|
|
totalShares: newTotalShares,
|
|
|
|
totalBets: newTotalBets,
|
|
|
|
totalLiquidity: newTotalLiquidity,
|
|
|
|
collectedFees: addObjects<Fees>(fees ?? {}, collectedFees ?? {}),
|
|
|
|
volume: volume + Math.abs(amount),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!isFinite(newBalance)) {
|
|
|
|
throw new APIError(500, 'Invalid user balance for ' + user.username)
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction.update(userDoc, { balance: newBalance })
|
|
|
|
|
|
|
|
return { betId: newBetDoc.id }
|
|
|
|
})
|
|
|
|
.then(async (result) => {
|
|
|
|
await redeemShares(bettor.id, contractId)
|
|
|
|
return result
|
|
|
|
})
|
|
|
|
})
|
2021-12-11 00:06:17 +00:00
|
|
|
|
|
|
|
const firestore = admin.firestore()
|