manifold/common/new-bet.ts

57 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-01-10 23:52:03 +00:00
import { Bet } from './bet'
import { Contract } from './contract'
import { User } from './user'
2022-01-10 22:49:04 +00:00
export const getNewBetInfo = (
user: User,
2022-01-10 23:52:03 +00:00
outcome: 'YES' | 'NO',
2022-01-10 22:49:04 +00:00
amount: number,
contract: Contract,
newBetId: string
) => {
2022-01-10 23:52:03 +00:00
const { YES: yesPool, NO: noPool } = contract.pool
2022-01-10 22:49:04 +00:00
const newPool =
2022-01-10 23:52:03 +00:00
outcome === 'YES'
2022-01-10 22:49:04 +00:00
? { YES: yesPool + amount, NO: noPool }
2022-01-10 23:52:03 +00:00
: { YES: yesPool, NO: noPool + amount }
2022-01-10 22:49:04 +00:00
const shares =
2022-01-10 23:52:03 +00:00
outcome === 'YES'
2022-01-10 22:49:04 +00:00
? amount + (amount * noPool ** 2) / (yesPool ** 2 + amount * yesPool)
2022-01-10 23:52:03 +00:00
: amount + (amount * yesPool ** 2) / (noPool ** 2 + amount * noPool)
2022-01-10 22:49:04 +00:00
2022-01-10 23:52:03 +00:00
const { YES: yesShares, NO: noShares } = contract.totalShares
2022-01-10 22:49:04 +00:00
const newTotalShares =
2022-01-10 23:52:03 +00:00
outcome === 'YES'
2022-01-10 22:49:04 +00:00
? { YES: yesShares + shares, NO: noShares }
2022-01-10 23:52:03 +00:00
: { YES: yesShares, NO: noShares + shares }
2022-01-10 22:49:04 +00:00
2022-01-10 23:52:03 +00:00
const { YES: yesBets, NO: noBets } = contract.totalBets
2022-01-10 22:49:04 +00:00
const newTotalBets =
2022-01-10 23:52:03 +00:00
outcome === 'YES'
2022-01-10 22:49:04 +00:00
? { YES: yesBets + amount, NO: noBets }
2022-01-10 23:52:03 +00:00
: { YES: yesBets, NO: noBets + amount }
2022-01-10 22:49:04 +00:00
2022-01-10 23:52:03 +00:00
const probBefore = yesPool ** 2 / (yesPool ** 2 + noPool ** 2)
const probAfter = newPool.YES ** 2 / (newPool.YES ** 2 + newPool.NO ** 2)
2022-01-10 22:49:04 +00:00
const newBet: Bet = {
id: newBetId,
userId: user.id,
contractId: contract.id,
amount,
shares,
outcome,
probBefore,
probAfter,
createdTime: Date.now(),
2022-01-10 23:52:03 +00:00
}
2022-01-10 22:49:04 +00:00
2022-01-10 23:52:03 +00:00
const newBalance = user.balance - amount
2022-01-10 22:49:04 +00:00
2022-01-10 23:52:03 +00:00
return { newBet, newPool, newTotalShares, newTotalBets, newBalance }
}