2022-03-15 22:27:51 +00:00
|
|
|
import { PHANTOM_ANTE } from './antes'
|
|
|
|
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-15 22:27:51 +00:00
|
|
|
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,
|
2022-05-09 20:09:07 +00:00
|
|
|
extraTags: string[],
|
|
|
|
manaLimitPerUser: number
|
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-15 22:27:51 +00:00
|
|
|
? getBinaryCpmmProps(initialProb, ante) // getBinaryDpmProps(initialProb, ante)
|
2022-02-17 23:00:19 +00:00
|
|
|
: getFreeAnswerProps(ante)
|
|
|
|
|
2022-03-23 04:49:15 +00:00
|
|
|
const volume = outcomeType === 'BINARY' ? 0 : ante
|
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const contract: 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(),
|
2022-02-17 23:00:19 +00:00
|
|
|
closeTime,
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-03-23 04:49:15 +00:00
|
|
|
volume,
|
2022-01-10 22:49:04 +00:00
|
|
|
volume24Hours: 0,
|
|
|
|
volume7Days: 0,
|
2022-03-15 22:27:51 +00:00
|
|
|
|
|
|
|
collectedFees: {
|
|
|
|
creatorFee: 0,
|
|
|
|
liquidityFee: 0,
|
|
|
|
platformFee: 0,
|
|
|
|
},
|
2022-05-09 20:09:07 +00:00
|
|
|
manaLimitPerUser,
|
2022-02-17 23:00:19 +00:00
|
|
|
})
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
return contract as Contract
|
2022-02-17 23:00:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 22:27:51 +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-15 22:27:51 +00:00
|
|
|
calcDpmInitialPool(initialProb, ante, PHANTOM_ANTE)
|
2022-02-17 23:00:19 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
const system: DPM & Binary = {
|
|
|
|
mechanism: 'dpm-2',
|
|
|
|
outcomeType: 'BINARY',
|
|
|
|
initialProbability: initialProb / 100,
|
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-15 22:27:51 +00:00
|
|
|
|
|
|
|
return system
|
|
|
|
}
|
|
|
|
|
|
|
|
const getBinaryCpmmProps = (initialProb: number, ante: number) => {
|
|
|
|
const pool = { YES: ante, NO: ante }
|
|
|
|
const p = initialProb / 100
|
|
|
|
|
|
|
|
const system: CPMM & Binary = {
|
|
|
|
mechanism: 'cpmm-1',
|
|
|
|
outcomeType: 'BINARY',
|
|
|
|
totalLiquidity: ante,
|
|
|
|
initialProbability: p,
|
|
|
|
p,
|
|
|
|
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-15 22:27:51 +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-15 22:27:51 +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
|
|
|
}
|