manifold/common/new-bet.ts

55 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-01-10 23:52:03 +00:00
import { Bet } from './bet'
import { calculateShares, getProbability } from './calculate'
2022-01-10 23:52:03 +00:00
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 = calculateShares(contract.totalShares, amount, outcome)
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
const probBefore = getProbability(contract.totalShares)
const probAfter = getProbability(newTotalShares)
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 }
}