2022-05-22 08:36:05 +00:00
|
|
|
import { range } from 'lodash'
|
2022-05-19 17:42:03 +00:00
|
|
|
import { Bet, NumericBet } from './bet'
|
|
|
|
import { getDpmProbability, getValueFromBucket } from './calculate-dpm'
|
|
|
|
import {
|
2022-06-01 02:42:35 +00:00
|
|
|
CPMMBinaryContract,
|
|
|
|
DPMBinaryContract,
|
|
|
|
FreeResponseContract,
|
2022-07-28 02:40:33 +00:00
|
|
|
MultipleChoiceContract,
|
2022-06-01 02:42:35 +00:00
|
|
|
NumericContract,
|
2022-05-19 17:42:03 +00:00
|
|
|
} from './contract'
|
2022-01-12 19:01:04 +00:00
|
|
|
import { User } from './user'
|
2022-03-15 22:27:51 +00:00
|
|
|
import { LiquidityProvision } from './liquidity-provision'
|
|
|
|
import { noFees } from './fees'
|
2022-07-28 02:40:33 +00:00
|
|
|
import { Answer } from './answer'
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-04-29 19:58:01 +00:00
|
|
|
export const HOUSE_LIQUIDITY_PROVIDER_ID = 'IPTOzEqrpkWmEzh6hwvAyY9PqFb2' // @ManifoldMarkets' id
|
2022-07-06 19:30:51 +00:00
|
|
|
export const DEV_HOUSE_LIQUIDITY_PROVIDER_ID = '94YYTk1AFWfbWMpfYcvnnwI1veP2' // @ManifoldMarkets' id
|
2022-04-29 19:58:01 +00:00
|
|
|
|
2022-09-14 05:26:47 +00:00
|
|
|
export const UNIQUE_BETTOR_LIQUIDITY_AMOUNT = 20
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
export function getCpmmInitialLiquidity(
|
2022-04-29 19:58:01 +00:00
|
|
|
providerId: string,
|
2022-06-01 02:42:35 +00:00
|
|
|
contract: CPMMBinaryContract,
|
2022-03-15 22:27:51 +00:00
|
|
|
anteId: string,
|
|
|
|
amount: number
|
|
|
|
) {
|
|
|
|
const { createdTime, p } = contract
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
const lp: LiquidityProvision = {
|
|
|
|
id: anteId,
|
2022-04-29 19:58:01 +00:00
|
|
|
userId: providerId,
|
2022-03-15 22:27:51 +00:00
|
|
|
contractId: contract.id,
|
|
|
|
createdTime,
|
|
|
|
isAnte: true,
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
amount: amount,
|
|
|
|
liquidity: amount,
|
|
|
|
p: p,
|
|
|
|
pool: { YES: 0, NO: 0 },
|
|
|
|
}
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
return lp
|
2022-01-12 19:01:04 +00:00
|
|
|
}
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-01-12 19:01:04 +00:00
|
|
|
export function getAnteBets(
|
|
|
|
creator: User,
|
2022-06-01 02:42:35 +00:00
|
|
|
contract: DPMBinaryContract,
|
2022-01-12 19:01:04 +00:00
|
|
|
yesAnteId: string,
|
|
|
|
noAnteId: string
|
|
|
|
) {
|
2022-03-15 22:27:51 +00:00
|
|
|
const p = getDpmProbability(contract.totalShares)
|
2022-01-12 19:01:04 +00:00
|
|
|
const ante = contract.totalBets.YES + contract.totalBets.NO
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-01-13 16:40:08 +00:00
|
|
|
const { createdTime } = contract
|
|
|
|
|
2022-01-12 19:01:04 +00:00
|
|
|
const yesBet: Bet = {
|
|
|
|
id: yesAnteId,
|
|
|
|
userId: creator.id,
|
|
|
|
contractId: contract.id,
|
|
|
|
amount: p * ante,
|
|
|
|
shares: Math.sqrt(p) * ante,
|
|
|
|
outcome: 'YES',
|
|
|
|
probBefore: p,
|
|
|
|
probAfter: p,
|
2022-01-13 16:40:08 +00:00
|
|
|
createdTime,
|
2022-01-19 22:36:55 +00:00
|
|
|
isAnte: true,
|
2022-03-15 22:27:51 +00:00
|
|
|
fees: noFees,
|
2022-01-12 19:01:04 +00:00
|
|
|
}
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-01-12 19:01:04 +00:00
|
|
|
const noBet: Bet = {
|
|
|
|
id: noAnteId,
|
|
|
|
userId: creator.id,
|
|
|
|
contractId: contract.id,
|
|
|
|
amount: (1 - p) * ante,
|
|
|
|
shares: Math.sqrt(1 - p) * ante,
|
|
|
|
outcome: 'NO',
|
|
|
|
probBefore: p,
|
|
|
|
probAfter: p,
|
2022-01-13 16:40:08 +00:00
|
|
|
createdTime,
|
2022-01-19 22:36:55 +00:00
|
|
|
isAnte: true,
|
2022-03-15 22:27:51 +00:00
|
|
|
fees: noFees,
|
2022-01-12 19:01:04 +00:00
|
|
|
}
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-01-12 19:01:04 +00:00
|
|
|
return { yesBet, noBet }
|
2022-01-10 23:52:03 +00:00
|
|
|
}
|
2022-02-17 23:00:19 +00:00
|
|
|
|
|
|
|
export function getFreeAnswerAnte(
|
2022-05-20 02:06:50 +00:00
|
|
|
anteBettorId: string,
|
2022-06-01 02:42:35 +00:00
|
|
|
contract: FreeResponseContract,
|
2022-02-17 23:00:19 +00:00
|
|
|
anteBetId: string
|
|
|
|
) {
|
|
|
|
const { totalBets, totalShares } = contract
|
|
|
|
const amount = totalBets['0']
|
|
|
|
const shares = totalShares['0']
|
|
|
|
|
|
|
|
const { createdTime } = contract
|
|
|
|
|
|
|
|
const anteBet: Bet = {
|
|
|
|
id: anteBetId,
|
2022-05-20 02:06:50 +00:00
|
|
|
userId: anteBettorId,
|
2022-02-17 23:00:19 +00:00
|
|
|
contractId: contract.id,
|
|
|
|
amount,
|
|
|
|
shares,
|
|
|
|
outcome: '0',
|
|
|
|
probBefore: 0,
|
|
|
|
probAfter: 1,
|
|
|
|
createdTime,
|
|
|
|
isAnte: true,
|
2022-03-15 22:27:51 +00:00
|
|
|
fees: noFees,
|
2022-02-17 23:00:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return anteBet
|
|
|
|
}
|
2022-05-19 17:42:03 +00:00
|
|
|
|
2022-07-28 02:40:33 +00:00
|
|
|
export function getMultipleChoiceAntes(
|
|
|
|
creator: User,
|
|
|
|
contract: MultipleChoiceContract,
|
|
|
|
answers: string[],
|
|
|
|
betDocIds: string[]
|
|
|
|
) {
|
|
|
|
const { totalBets, totalShares } = contract
|
|
|
|
const amount = totalBets['0']
|
|
|
|
const shares = totalShares['0']
|
|
|
|
const p = 1 / answers.length
|
|
|
|
|
|
|
|
const { createdTime } = contract
|
|
|
|
|
|
|
|
const bets: Bet[] = answers.map((answer, i) => ({
|
|
|
|
id: betDocIds[i],
|
|
|
|
userId: creator.id,
|
|
|
|
contractId: contract.id,
|
|
|
|
amount,
|
|
|
|
shares,
|
|
|
|
outcome: i.toString(),
|
|
|
|
probBefore: p,
|
|
|
|
probAfter: p,
|
|
|
|
createdTime,
|
|
|
|
isAnte: true,
|
|
|
|
fees: noFees,
|
|
|
|
}))
|
|
|
|
|
|
|
|
const { username, name, avatarUrl } = creator
|
|
|
|
|
|
|
|
const answerObjects: Answer[] = answers.map((answer, i) => ({
|
|
|
|
id: i.toString(),
|
|
|
|
number: i,
|
|
|
|
contractId: contract.id,
|
|
|
|
createdTime,
|
|
|
|
userId: creator.id,
|
|
|
|
username,
|
|
|
|
name,
|
|
|
|
avatarUrl,
|
|
|
|
text: answer,
|
|
|
|
}))
|
|
|
|
|
|
|
|
return { bets, answerObjects }
|
|
|
|
}
|
|
|
|
|
2022-05-19 17:42:03 +00:00
|
|
|
export function getNumericAnte(
|
2022-05-28 18:43:57 +00:00
|
|
|
anteBettorId: string,
|
2022-06-01 02:42:35 +00:00
|
|
|
contract: NumericContract,
|
2022-05-19 17:42:03 +00:00
|
|
|
ante: number,
|
|
|
|
newBetId: string
|
|
|
|
) {
|
|
|
|
const { bucketCount, createdTime } = contract
|
|
|
|
|
|
|
|
const betAnte = ante / bucketCount
|
|
|
|
const betShares = Math.sqrt(ante ** 2 / bucketCount)
|
|
|
|
|
|
|
|
const allOutcomeShares = Object.fromEntries(
|
2022-05-22 08:36:05 +00:00
|
|
|
range(0, bucketCount).map((_, i) => [i, betShares])
|
2022-05-19 17:42:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const allBetAmounts = Object.fromEntries(
|
2022-05-22 08:36:05 +00:00
|
|
|
range(0, bucketCount).map((_, i) => [i, betAnte])
|
2022-05-19 17:42:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const anteBet: NumericBet = {
|
|
|
|
id: newBetId,
|
2022-05-28 18:43:57 +00:00
|
|
|
userId: anteBettorId,
|
2022-05-19 17:42:03 +00:00
|
|
|
contractId: contract.id,
|
|
|
|
amount: ante,
|
|
|
|
allBetAmounts,
|
|
|
|
outcome: '0',
|
|
|
|
value: getValueFromBucket('0', contract),
|
|
|
|
shares: betShares,
|
|
|
|
allOutcomeShares,
|
|
|
|
probBefore: 0,
|
|
|
|
probAfter: 1 / bucketCount,
|
|
|
|
createdTime,
|
|
|
|
isAnte: true,
|
|
|
|
fees: noFees,
|
|
|
|
}
|
|
|
|
|
|
|
|
return anteBet
|
|
|
|
}
|