2021-12-11 00:06:17 +00:00
|
|
|
import * as functions from 'firebase-functions'
|
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
|
|
|
|
import { Contract } from './types/contract'
|
|
|
|
import { User } from './types/user'
|
|
|
|
import { Bet } from './types/bet'
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if (outcome !== 'YES' && outcome !== 'NO')
|
|
|
|
return { status: 'error', message: 'Invalid outcome' }
|
|
|
|
|
|
|
|
// run as transaction to prevent race conditions
|
2021-12-12 21:32:06 +00:00
|
|
|
return await firestore.runTransaction(async (transaction) => {
|
2021-12-11 03:45:05 +00:00
|
|
|
const userDoc = firestore.doc(`users/${userId}`)
|
|
|
|
const userSnap = await transaction.get(userDoc)
|
2021-12-12 21:32:06 +00:00
|
|
|
if (!userSnap.exists)
|
|
|
|
return { status: 'error', message: 'User not found' }
|
2021-12-11 03:45:05 +00:00
|
|
|
const user = userSnap.data() as User
|
|
|
|
|
2021-12-13 17:58:47 +00:00
|
|
|
if (user.balance < amount)
|
2021-12-11 03:45:05 +00:00
|
|
|
return { status: 'error', message: 'Insufficient balance' }
|
|
|
|
|
|
|
|
const contractDoc = firestore.doc(`contracts/${contractId}`)
|
|
|
|
const contractSnap = await transaction.get(contractDoc)
|
2021-12-12 21:32:06 +00:00
|
|
|
if (!contractSnap.exists)
|
|
|
|
return { status: 'error', message: 'Invalid contract' }
|
2021-12-11 03:45:05 +00:00
|
|
|
const contract = contractSnap.data() as Contract
|
|
|
|
|
2021-12-12 21:32:06 +00:00
|
|
|
const newBetDoc = firestore
|
|
|
|
.collection(`contracts/${contractId}/bets`)
|
|
|
|
.doc()
|
2021-12-11 03:45:05 +00:00
|
|
|
|
2022-01-02 01:03:18 +00:00
|
|
|
const { newBet, newPool, newTotalShares, newTotalBets, newBalance } =
|
|
|
|
getNewBetInfo(user, outcome, amount, contract, newBetDoc.id)
|
2021-12-11 03:45:05 +00:00
|
|
|
|
|
|
|
transaction.create(newBetDoc, newBet)
|
2021-12-17 22:15:09 +00:00
|
|
|
transaction.update(contractDoc, {
|
|
|
|
pool: newPool,
|
2021-12-24 21:06:01 +00:00
|
|
|
totalShares: newTotalShares,
|
2022-01-02 01:03:18 +00:00
|
|
|
totalBets: newTotalBets,
|
2021-12-17 22:15:09 +00:00
|
|
|
})
|
2021-12-13 17:58:47 +00:00
|
|
|
transaction.update(userDoc, { balance: newBalance })
|
2021-12-11 03:45:05 +00:00
|
|
|
|
|
|
|
return { status: 'success' }
|
|
|
|
})
|
2021-12-12 21:32:06 +00:00
|
|
|
}
|
|
|
|
)
|
2021-12-11 00:06:17 +00:00
|
|
|
|
|
|
|
const firestore = admin.firestore()
|
|
|
|
|
2021-12-12 21:32:06 +00:00
|
|
|
const getNewBetInfo = (
|
|
|
|
user: User,
|
|
|
|
outcome: 'YES' | 'NO',
|
|
|
|
amount: number,
|
|
|
|
contract: Contract,
|
|
|
|
newBetId: string
|
|
|
|
) => {
|
2021-12-17 22:15:09 +00:00
|
|
|
const { YES: yesPool, NO: noPool } = contract.pool
|
2021-12-11 00:06:17 +00:00
|
|
|
|
2021-12-17 22:15:09 +00:00
|
|
|
const newPool =
|
2021-12-15 22:44:22 +00:00
|
|
|
outcome === 'YES'
|
2021-12-17 22:15:09 +00:00
|
|
|
? { YES: yesPool + amount, NO: noPool }
|
|
|
|
: { YES: yesPool, NO: noPool + amount }
|
2021-12-12 21:32:06 +00:00
|
|
|
|
2021-12-24 21:06:01 +00:00
|
|
|
const shares =
|
2021-12-12 21:32:06 +00:00
|
|
|
outcome === 'YES'
|
2021-12-24 21:06:01 +00:00
|
|
|
? amount + (amount * noPool ** 2) / (yesPool ** 2 + amount * yesPool)
|
|
|
|
: amount + (amount * yesPool ** 2) / (noPool ** 2 + amount * noPool)
|
2021-12-12 21:32:06 +00:00
|
|
|
|
2021-12-24 21:06:01 +00:00
|
|
|
const { YES: yesShares, NO: noShares } = contract.totalShares
|
2021-12-15 22:44:22 +00:00
|
|
|
|
2021-12-24 21:06:01 +00:00
|
|
|
const newTotalShares =
|
2021-12-12 21:32:06 +00:00
|
|
|
outcome === 'YES'
|
2021-12-24 21:06:01 +00:00
|
|
|
? { YES: yesShares + shares, NO: noShares }
|
|
|
|
: { YES: yesShares, NO: noShares + shares }
|
2021-12-15 22:44:22 +00:00
|
|
|
|
2022-01-02 01:03:18 +00:00
|
|
|
const { YES: yesBets, NO: noBets } = contract.totalBets
|
|
|
|
|
|
|
|
const newTotalBets =
|
|
|
|
outcome === 'YES'
|
|
|
|
? { YES: yesBets + amount, NO: noBets }
|
|
|
|
: { YES: yesBets, NO: noBets + amount }
|
|
|
|
|
2021-12-17 22:15:09 +00:00
|
|
|
const probBefore = yesPool ** 2 / (yesPool ** 2 + noPool ** 2)
|
|
|
|
const probAfter = newPool.YES ** 2 / (newPool.YES ** 2 + newPool.NO ** 2)
|
2021-12-11 00:06:17 +00:00
|
|
|
|
|
|
|
const newBet: Bet = {
|
|
|
|
id: newBetId,
|
|
|
|
userId: user.id,
|
|
|
|
contractId: contract.id,
|
|
|
|
amount,
|
2021-12-24 21:06:01 +00:00
|
|
|
shares,
|
2021-12-11 00:06:17 +00:00
|
|
|
outcome,
|
2021-12-12 21:32:06 +00:00
|
|
|
probBefore,
|
|
|
|
probAfter,
|
|
|
|
createdTime: Date.now(),
|
2021-12-11 00:06:17 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 17:58:47 +00:00
|
|
|
const newBalance = user.balance - amount
|
2021-12-11 00:06:17 +00:00
|
|
|
|
2022-01-02 01:03:18 +00:00
|
|
|
return { newBet, newPool, newTotalShares, newTotalBets, newBalance }
|
2021-12-12 21:32:06 +00:00
|
|
|
}
|