2021-12-11 00:06:17 +00:00
|
|
|
import * as functions from 'firebase-functions'
|
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
|
2022-05-10 20:59:38 +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-10 20:59:38 +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-10 20:59:38 +00:00
|
|
|
import { Fees } from 'common/fees'
|
|
|
|
import { hasUserHitManaLimit } from 'common/calculate'
|
2021-12-11 00:06:17 +00:00
|
|
|
|
2021-12-12 21:32:06 +00:00
|
|
|
export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
|
|
|
|
async (
|
|
|
|
data: {
|
|
|
|
amount: number
|
|
|
|
outcome: string
|
|
|
|
contractId: string
|
|
|
|
},
|
|
|
|
context
|
|
|
|
) => {
|
2021-12-11 03:45:05 +00:00
|
|
|
const userId = context?.auth?.uid
|
2021-12-12 21:32:06 +00:00
|
|
|
if (!userId) return { status: 'error', message: 'Not authorized' }
|
2021-12-11 03:45:05 +00:00
|
|
|
|
|
|
|
const { amount, outcome, contractId } = data
|
|
|
|
|
2022-01-08 17:51:31 +00:00
|
|
|
if (amount <= 0 || isNaN(amount) || !isFinite(amount))
|
|
|
|
return { status: 'error', message: 'Invalid amount' }
|
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
if (outcome !== 'YES' && outcome !== 'NO' && isNaN(+outcome))
|
2021-12-11 03:45:05 +00:00
|
|
|
return { status: 'error', message: 'Invalid outcome' }
|
|
|
|
|
|
|
|
// run as transaction to prevent race conditions
|
2022-03-15 22:27:51 +00:00
|
|
|
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)
|
2022-02-17 23:00:19 +00:00
|
|
|
return { status: 'error', message: 'Invalid contract' }
|
2022-03-15 22:27:51 +00:00
|
|
|
const contract = contractSnap.data() as Contract
|
|
|
|
|
2022-03-23 04:49:15 +00:00
|
|
|
const { closeTime, outcomeType, mechanism, collectedFees, volume } =
|
|
|
|
contract
|
2022-03-15 22:27:51 +00:00
|
|
|
if (closeTime && Date.now() > closeTime)
|
|
|
|
return { status: 'error', message: 'Trading is closed' }
|
2021-12-11 03:45:05 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
const yourBetsSnap = await transaction.get(
|
|
|
|
contractDoc.collection('bets').where('userId', '==', userId)
|
|
|
|
)
|
|
|
|
const yourBets = yourBetsSnap.docs.map((doc) => doc.data() as Bet)
|
|
|
|
|
2022-04-13 17:52:12 +00:00
|
|
|
const loanAmount = 0 // getLoanAmount(yourBets, amount)
|
|
|
|
if (user.balance < amount)
|
2022-03-15 22:27:51 +00:00
|
|
|
return { status: 'error', message: 'Insufficient balance' }
|
|
|
|
|
|
|
|
if (outcomeType === 'FREE_RESPONSE') {
|
|
|
|
const answerSnap = await transaction.get(
|
|
|
|
contractDoc.collection('answers').doc(outcome)
|
|
|
|
)
|
|
|
|
if (!answerSnap.exists)
|
|
|
|
return { status: 'error', message: 'Invalid contract' }
|
2022-05-09 20:09:07 +00:00
|
|
|
|
|
|
|
const { status, message } = hasUserHitManaLimit(
|
|
|
|
contract,
|
|
|
|
yourBets,
|
|
|
|
amount
|
|
|
|
)
|
|
|
|
if (status === 'error') return { status, message: message }
|
2022-03-15 22:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const newBetDoc = firestore
|
|
|
|
.collection(`contracts/${contractId}/bets`)
|
|
|
|
.doc()
|
|
|
|
|
|
|
|
const {
|
|
|
|
newBet,
|
|
|
|
newPool,
|
|
|
|
newTotalShares,
|
|
|
|
newTotalBets,
|
|
|
|
newBalance,
|
|
|
|
newTotalLiquidity,
|
|
|
|
fees,
|
|
|
|
newP,
|
|
|
|
} =
|
|
|
|
outcomeType === 'BINARY'
|
|
|
|
? mechanism === 'dpm-2'
|
|
|
|
? getNewBinaryDpmBetInfo(
|
|
|
|
user,
|
|
|
|
outcome as 'YES' | 'NO',
|
|
|
|
amount,
|
|
|
|
contract,
|
|
|
|
loanAmount,
|
|
|
|
newBetDoc.id
|
|
|
|
)
|
|
|
|
: (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)) {
|
|
|
|
return {
|
|
|
|
status: 'error',
|
|
|
|
message: '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 ?? {}),
|
2022-03-23 05:09:47 +00:00
|
|
|
volume: volume + Math.abs(amount),
|
2022-03-15 22:27:51 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
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 })
|
|
|
|
|
|
|
|
return { status: 'success', betId: newBetDoc.id }
|
|
|
|
})
|
|
|
|
.then(async (result) => {
|
|
|
|
await redeemShares(userId, contractId)
|
|
|
|
return result
|
|
|
|
})
|
2021-12-12 21:32:06 +00:00
|
|
|
}
|
|
|
|
)
|
2021-12-11 00:06:17 +00:00
|
|
|
|
|
|
|
const firestore = admin.firestore()
|