2022-01-10 23:52:03 +00:00
|
|
|
import { Bet } from './bet'
|
2022-02-17 23:00:19 +00:00
|
|
|
import {
|
|
|
|
calculateShares,
|
|
|
|
getProbability,
|
|
|
|
getOutcomeProbability,
|
|
|
|
} 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
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
export const getNewBinaryBetInfo = (
|
2022-01-10 22:49:04 +00:00
|
|
|
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
|
|
|
|
2022-01-12 19:01: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
|
|
|
|
2022-01-12 19:01: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 }
|
|
|
|
}
|
2022-02-17 23:00:19 +00:00
|
|
|
|
|
|
|
export const getNewMultiBetInfo = (
|
|
|
|
user: User,
|
|
|
|
outcome: string,
|
|
|
|
amount: number,
|
|
|
|
contract: Contract,
|
|
|
|
newBetId: string
|
|
|
|
) => {
|
|
|
|
const { pool, totalShares, totalBets } = contract
|
|
|
|
|
|
|
|
const prevOutcomePool = pool[outcome] ?? 0
|
|
|
|
const newPool = { ...pool, [outcome]: prevOutcomePool + amount }
|
|
|
|
|
|
|
|
const shares = calculateShares(contract.totalShares, amount, outcome)
|
|
|
|
|
|
|
|
const prevShares = totalShares[outcome] ?? 0
|
|
|
|
const newTotalShares = { ...totalShares, [outcome]: prevShares + shares }
|
|
|
|
|
|
|
|
const prevTotalBets = totalBets[outcome] ?? 0
|
|
|
|
const newTotalBets = { ...totalBets, [outcome]: prevTotalBets + amount }
|
|
|
|
|
|
|
|
const probBefore = getOutcomeProbability(totalShares, outcome)
|
|
|
|
const probAfter = getOutcomeProbability(newTotalShares, outcome)
|
|
|
|
|
|
|
|
const newBet: Bet = {
|
|
|
|
id: newBetId,
|
|
|
|
userId: user.id,
|
|
|
|
contractId: contract.id,
|
|
|
|
amount,
|
|
|
|
shares,
|
|
|
|
outcome,
|
|
|
|
probBefore,
|
|
|
|
probAfter,
|
|
|
|
createdTime: Date.now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
const newBalance = user.balance - amount
|
|
|
|
|
|
|
|
return { newBet, newPool, newTotalShares, newTotalBets, newBalance }
|
|
|
|
}
|