Calculate probBefore, probAfter, and probAverage on placeBet cloud function
This commit is contained in:
parent
07d8680217
commit
e34f1dbcc9
7
functions/.prettierrc
Normal file
7
functions/.prettierrc
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"tabWidth": 2,
|
||||||
|
"useTabs": false,
|
||||||
|
"semi": false,
|
||||||
|
"trailingComma": "es5",
|
||||||
|
"singleQuote": true
|
||||||
|
}
|
|
@ -5,16 +5,17 @@ import { Contract } from './types/contract'
|
||||||
import { User } from './types/user'
|
import { User } from './types/user'
|
||||||
import { Bet } from './types/bet'
|
import { Bet } from './types/bet'
|
||||||
|
|
||||||
export const placeBet = functions
|
export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
|
||||||
.runWith({ minInstances: 1 })
|
async (
|
||||||
.https.onCall(async (data: {
|
data: {
|
||||||
amount: number
|
amount: number
|
||||||
outcome: string
|
outcome: string
|
||||||
contractId: string
|
contractId: string
|
||||||
}, context) => {
|
},
|
||||||
|
context
|
||||||
|
) => {
|
||||||
const userId = context?.auth?.uid
|
const userId = context?.auth?.uid
|
||||||
if (!userId)
|
if (!userId) return { status: 'error', message: 'Not authorized' }
|
||||||
return { status: 'error', message: 'Not authorized' }
|
|
||||||
|
|
||||||
const { amount, outcome, contractId } = data
|
const { amount, outcome, contractId } = data
|
||||||
|
|
||||||
|
@ -22,10 +23,11 @@ export const placeBet = functions
|
||||||
return { status: 'error', message: 'Invalid outcome' }
|
return { status: 'error', message: 'Invalid outcome' }
|
||||||
|
|
||||||
// run as transaction to prevent race conditions
|
// run as transaction to prevent race conditions
|
||||||
return await firestore.runTransaction(async transaction => {
|
return await firestore.runTransaction(async (transaction) => {
|
||||||
const userDoc = firestore.doc(`users/${userId}`)
|
const userDoc = firestore.doc(`users/${userId}`)
|
||||||
const userSnap = await transaction.get(userDoc)
|
const userSnap = await transaction.get(userDoc)
|
||||||
if (!userSnap.exists) return { status: 'error', message: 'User not found' }
|
if (!userSnap.exists)
|
||||||
|
return { status: 'error', message: 'User not found' }
|
||||||
const user = userSnap.data() as User
|
const user = userSnap.data() as User
|
||||||
|
|
||||||
if (user.balanceUsd < amount)
|
if (user.balanceUsd < amount)
|
||||||
|
@ -33,12 +35,21 @@ export const placeBet = functions
|
||||||
|
|
||||||
const contractDoc = firestore.doc(`contracts/${contractId}`)
|
const contractDoc = firestore.doc(`contracts/${contractId}`)
|
||||||
const contractSnap = await transaction.get(contractDoc)
|
const contractSnap = await transaction.get(contractDoc)
|
||||||
if (!contractSnap.exists) return { status: 'error', message: 'Invalid contract' }
|
if (!contractSnap.exists)
|
||||||
|
return { status: 'error', message: 'Invalid contract' }
|
||||||
const contract = contractSnap.data() as Contract
|
const contract = contractSnap.data() as Contract
|
||||||
|
|
||||||
const newBetDoc = firestore.collection(`contracts/${contractId}/bets`).doc()
|
const newBetDoc = firestore
|
||||||
|
.collection(`contracts/${contractId}/bets`)
|
||||||
|
.doc()
|
||||||
|
|
||||||
const { newBet, newPot, newBalance } = getNewBetInfo(user, outcome, amount, contract, newBetDoc.id)
|
const { newBet, newPot, newBalance } = getNewBetInfo(
|
||||||
|
user,
|
||||||
|
outcome,
|
||||||
|
amount,
|
||||||
|
contract,
|
||||||
|
newBetDoc.id
|
||||||
|
)
|
||||||
|
|
||||||
transaction.create(newBetDoc, newBet)
|
transaction.create(newBetDoc, newBet)
|
||||||
transaction.update(contractDoc, { pot: newPot })
|
transaction.update(contractDoc, { pot: newPot })
|
||||||
|
@ -46,16 +57,39 @@ export const placeBet = functions
|
||||||
|
|
||||||
return { status: 'success' }
|
return { status: 'success' }
|
||||||
})
|
})
|
||||||
})
|
}
|
||||||
|
)
|
||||||
|
|
||||||
const firestore = admin.firestore()
|
const firestore = admin.firestore()
|
||||||
|
|
||||||
const getNewBetInfo = (user: User, outcome: 'YES' | 'NO', amount: number, contract: Contract, newBetId: string) => {
|
const getNewBetInfo = (
|
||||||
|
user: User,
|
||||||
|
outcome: 'YES' | 'NO',
|
||||||
|
amount: number,
|
||||||
|
contract: Contract,
|
||||||
|
newBetId: string
|
||||||
|
) => {
|
||||||
const { YES: yesPot, NO: noPot } = contract.pot
|
const { YES: yesPot, NO: noPot } = contract.pot
|
||||||
|
|
||||||
const dpmWeight = outcome === 'YES'
|
const probBefore = yesPot ** 2 / (yesPot ** 2 + noPot ** 2)
|
||||||
? amount * Math.pow(noPot, 2) / (Math.pow(yesPot, 2) + amount * yesPot)
|
|
||||||
: amount * Math.pow(yesPot, 2) / (Math.pow(noPot, 2) + amount * noPot)
|
const probAverage =
|
||||||
|
(amount +
|
||||||
|
noPot * Math.atan(yesPot / noPot) -
|
||||||
|
noPot * Math.atan((amount + yesPot) / noPot)) /
|
||||||
|
amount
|
||||||
|
|
||||||
|
const dpmWeight =
|
||||||
|
outcome === 'YES'
|
||||||
|
? (amount * noPot ** 2) / (yesPot ** 2 + amount * yesPot)
|
||||||
|
: (amount * yesPot ** 2) / (noPot ** 2 + amount * noPot)
|
||||||
|
|
||||||
|
const newPot =
|
||||||
|
outcome === 'YES'
|
||||||
|
? { YES: yesPot + amount, NO: noPot }
|
||||||
|
: { YES: yesPot, NO: noPot + amount }
|
||||||
|
|
||||||
|
const probAfter = newPot.YES ** 2 / (newPot.YES ** 2 + newPot.NO ** 2)
|
||||||
|
|
||||||
const newBet: Bet = {
|
const newBet: Bet = {
|
||||||
id: newBetId,
|
id: newBetId,
|
||||||
|
@ -64,14 +98,13 @@ const getNewBetInfo = (user: User, outcome: 'YES' | 'NO', amount: number, contra
|
||||||
amount,
|
amount,
|
||||||
dpmWeight,
|
dpmWeight,
|
||||||
outcome,
|
outcome,
|
||||||
createdTime: Date.now()
|
probBefore,
|
||||||
|
probAverage,
|
||||||
|
probAfter,
|
||||||
|
createdTime: Date.now(),
|
||||||
}
|
}
|
||||||
|
|
||||||
const newPot = outcome === 'YES'
|
|
||||||
? { YES: yesPot + amount, NO: noPot }
|
|
||||||
: { YES: yesPot, NO: noPot + amount }
|
|
||||||
|
|
||||||
const newBalance = user.balanceUsd - amount
|
const newBalance = user.balanceUsd - amount
|
||||||
|
|
||||||
return { newBet, newPot, newBalance }
|
return { newBet, newPot, newBalance }
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,10 +2,11 @@ export type Bet = {
|
||||||
id: string
|
id: string
|
||||||
userId: string
|
userId: string
|
||||||
contractId: string
|
contractId: string
|
||||||
|
amount: number // Amount of bet
|
||||||
amount: number // Amount of USD bid
|
|
||||||
outcome: 'YES' | 'NO' // Chosen outcome
|
outcome: 'YES' | 'NO' // Chosen outcome
|
||||||
|
|
||||||
createdTime: number
|
createdTime: number
|
||||||
|
probBefore: number
|
||||||
|
probAverage: number
|
||||||
|
probAfter: number
|
||||||
dpmWeight: number // Dynamic Parimutuel weight
|
dpmWeight: number // Dynamic Parimutuel weight
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user