2022-05-22 08:36:05 +00:00
|
|
|
import { range } from 'lodash'
|
2022-03-15 22:27:51 +00:00
|
|
|
import {
|
|
|
|
Binary,
|
|
|
|
Contract,
|
|
|
|
CPMM,
|
|
|
|
DPM,
|
|
|
|
FreeResponse,
|
2022-05-19 17:42:03 +00:00
|
|
|
Numeric,
|
2022-03-15 22:27:51 +00:00
|
|
|
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-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[],
|
2022-05-19 17:42:03 +00:00
|
|
|
|
|
|
|
// used for numeric markets
|
|
|
|
bucketCount: number,
|
|
|
|
min: number,
|
2022-06-14 20:01:32 +00:00
|
|
|
max: number,
|
|
|
|
autoResolutionTime?: 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-05-19 17:42:03 +00:00
|
|
|
: outcomeType === 'NUMERIC'
|
|
|
|
? getNumericProps(ante, bucketCount, min, max)
|
2022-02-17 23:00:19 +00:00
|
|
|
: getFreeAnswerProps(ante)
|
|
|
|
|
|
|
|
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-06-14 20:01:32 +00:00
|
|
|
autoResolutionTime,
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-05-13 13:43:12 +00:00
|
|
|
volume: 0,
|
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-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-05-26 00:12:36 +00:00
|
|
|
/*
|
|
|
|
import { PHANTOM_ANTE } from './antes'
|
|
|
|
import { calcDpmInitialPool } from './calculate-dpm'
|
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
|
|
|
|
}
|
2022-05-26 00:12:36 +00:00
|
|
|
*/
|
2022-03-15 22:27:51 +00:00
|
|
|
|
|
|
|
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-05-19 17:42:03 +00:00
|
|
|
const getNumericProps = (
|
|
|
|
ante: number,
|
|
|
|
bucketCount: number,
|
|
|
|
min: number,
|
|
|
|
max: number
|
|
|
|
) => {
|
2022-05-22 08:36:05 +00:00
|
|
|
const buckets = range(0, bucketCount).map((i) => i.toString())
|
2022-05-19 17:42:03 +00:00
|
|
|
|
|
|
|
const betAnte = ante / bucketCount
|
|
|
|
const pool = Object.fromEntries(buckets.map((answer) => [answer, betAnte]))
|
|
|
|
const totalBets = pool
|
|
|
|
|
|
|
|
const betShares = Math.sqrt(ante ** 2 / bucketCount)
|
|
|
|
const totalShares = Object.fromEntries(
|
|
|
|
buckets.map((answer) => [answer, betShares])
|
|
|
|
)
|
|
|
|
|
|
|
|
const system: DPM & Numeric = {
|
|
|
|
mechanism: 'dpm-2',
|
|
|
|
outcomeType: 'NUMERIC',
|
|
|
|
pool,
|
|
|
|
totalBets,
|
|
|
|
totalShares,
|
|
|
|
bucketCount,
|
|
|
|
min,
|
|
|
|
max,
|
|
|
|
}
|
|
|
|
|
|
|
|
return system
|
|
|
|
}
|