2022-03-07 17:29:58 +00:00
|
|
|
import { PHANTOM_ANTE } from './antes'
|
2022-03-02 21:12:27 +00:00
|
|
|
import {
|
|
|
|
Binary,
|
|
|
|
Contract,
|
|
|
|
CPMM,
|
|
|
|
DPM,
|
|
|
|
FreeResponse,
|
|
|
|
outcomeType,
|
|
|
|
} from './contract'
|
2022-01-10 23:52:03 +00:00
|
|
|
import { User } from './user'
|
2022-01-21 23:21:46 +00:00
|
|
|
import { parseTags } from './util/parse'
|
2022-02-17 23:00:19 +00:00
|
|
|
import { removeUndefinedProps } from './util/object'
|
2022-03-07 17:29:58 +00:00
|
|
|
import { calcCpmmInitialPool } from './calculate-cpmm'
|
|
|
|
import { calcDpmInitialPool } from './calculate-dpm'
|
2022-01-10 22:49:04 +00:00
|
|
|
|
|
|
|
export function getNewContract(
|
|
|
|
id: string,
|
|
|
|
slug: string,
|
|
|
|
creator: User,
|
|
|
|
question: string,
|
2022-02-17 23:00:19 +00:00
|
|
|
outcomeType: outcomeType,
|
2022-01-10 22:49:04 +00:00
|
|
|
description: string,
|
|
|
|
initialProb: number,
|
2022-01-24 22:33:02 +00:00
|
|
|
ante: number,
|
2022-01-27 18:45:35 +00:00
|
|
|
closeTime: number,
|
|
|
|
extraTags: string[]
|
2022-01-10 22:49:04 +00:00
|
|
|
) {
|
2022-01-27 18:45:35 +00:00
|
|
|
const tags = parseTags(
|
2022-01-31 03:25:50 +00:00
|
|
|
`${question} ${description} ${extraTags.map((tag) => `#${tag}`).join(' ')}`
|
2022-01-27 18:45:35 +00:00
|
|
|
)
|
2022-01-24 22:33:02 +00:00
|
|
|
const lowercaseTags = tags.map((tag) => tag.toLowerCase())
|
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const propsByOutcomeType =
|
|
|
|
outcomeType === 'BINARY'
|
2022-03-02 21:12:27 +00:00
|
|
|
? getBinaryCpmmProps(initialProb, ante, creator.id) // getBinaryDpmProps(initialProb, ante)
|
2022-02-17 23:00:19 +00:00
|
|
|
: getFreeAnswerProps(ante)
|
|
|
|
|
2022-03-02 21:12:27 +00:00
|
|
|
const contract = removeUndefinedProps({
|
2022-01-10 22:49:04 +00:00
|
|
|
id,
|
|
|
|
slug,
|
2022-02-17 23:00:19 +00:00
|
|
|
...propsByOutcomeType,
|
2022-01-10 22:49:04 +00:00
|
|
|
|
|
|
|
creatorId: creator.id,
|
|
|
|
creatorName: creator.name,
|
|
|
|
creatorUsername: creator.username,
|
2022-01-16 06:03:59 +00:00
|
|
|
creatorAvatarUrl: creator.avatarUrl,
|
2022-01-10 22:49:04 +00:00
|
|
|
|
|
|
|
question: question.trim(),
|
|
|
|
description: description.trim(),
|
2022-01-24 22:33:02 +00:00
|
|
|
tags,
|
|
|
|
lowercaseTags,
|
2022-01-13 01:53:50 +00:00
|
|
|
visibility: 'public',
|
2022-01-10 22:49:04 +00:00
|
|
|
isResolved: false,
|
|
|
|
createdTime: Date.now(),
|
|
|
|
lastUpdatedTime: Date.now(),
|
2022-02-17 23:00:19 +00:00
|
|
|
closeTime,
|
2022-01-10 22:49:04 +00:00
|
|
|
|
|
|
|
volume24Hours: 0,
|
|
|
|
volume7Days: 0,
|
2022-02-17 23:00:19 +00:00
|
|
|
})
|
|
|
|
|
2022-03-02 21:12:27 +00:00
|
|
|
return contract as Contract
|
2022-02-17 23:00:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 21:12:27 +00:00
|
|
|
const getBinaryDpmProps = (initialProb: number, ante: number) => {
|
2022-02-17 23:00:19 +00:00
|
|
|
const { sharesYes, sharesNo, poolYes, poolNo, phantomYes, phantomNo } =
|
2022-03-07 17:29:58 +00:00
|
|
|
calcDpmInitialPool(initialProb, ante, PHANTOM_ANTE)
|
2022-02-17 23:00:19 +00:00
|
|
|
|
2022-03-02 21:12:27 +00:00
|
|
|
const system: DPM & Binary = {
|
|
|
|
mechanism: 'dpm-2',
|
|
|
|
outcomeType: 'BINARY',
|
2022-02-17 23:00:19 +00:00
|
|
|
phantomShares: { YES: phantomYes, NO: phantomNo },
|
|
|
|
pool: { YES: poolYes, NO: poolNo },
|
|
|
|
totalShares: { YES: sharesYes, NO: sharesNo },
|
|
|
|
totalBets: { YES: poolYes, NO: poolNo },
|
2022-01-10 23:52:03 +00:00
|
|
|
}
|
2022-03-02 21:12:27 +00:00
|
|
|
|
|
|
|
return system
|
|
|
|
}
|
|
|
|
|
|
|
|
const getBinaryCpmmProps = (
|
|
|
|
initialProb: number,
|
|
|
|
ante: number,
|
|
|
|
userId: string
|
|
|
|
) => {
|
2022-03-07 17:29:58 +00:00
|
|
|
const { poolYes, poolNo } = calcCpmmInitialPool(initialProb, ante)
|
2022-03-02 21:12:27 +00:00
|
|
|
const pool = { YES: poolYes, NO: poolNo }
|
|
|
|
|
|
|
|
const system: CPMM & Binary = {
|
|
|
|
mechanism: 'cpmm-1',
|
|
|
|
outcomeType: 'BINARY',
|
|
|
|
pool: pool,
|
|
|
|
}
|
|
|
|
|
|
|
|
return system
|
2022-02-17 23:00:19 +00:00
|
|
|
}
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const getFreeAnswerProps = (ante: number) => {
|
2022-03-02 21:12:27 +00:00
|
|
|
const system: DPM & FreeResponse = {
|
|
|
|
mechanism: 'dpm-2',
|
|
|
|
outcomeType: 'FREE_RESPONSE',
|
2022-02-17 23:00:19 +00:00
|
|
|
pool: { '0': ante },
|
|
|
|
totalShares: { '0': ante },
|
|
|
|
totalBets: { '0': ante },
|
|
|
|
answers: [],
|
|
|
|
}
|
2022-03-02 21:12:27 +00:00
|
|
|
|
|
|
|
return system
|
2022-02-17 23:00:19 +00:00
|
|
|
}
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const getMultiProps = (
|
|
|
|
outcomes: string[],
|
|
|
|
initialProbs: number[],
|
|
|
|
ante: number
|
|
|
|
) => {
|
|
|
|
// Not implemented.
|
2022-01-10 22:49:04 +00:00
|
|
|
}
|