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 {
|
|
|
|
Binary,
|
|
|
|
CPMM,
|
|
|
|
DPM,
|
|
|
|
FreeResponse,
|
|
|
|
FullContract,
|
|
|
|
Numeric,
|
|
|
|
} 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-01-12 19:01:04 +00:00
|
|
|
|
2022-04-29 19:53:13 +00:00
|
|
|
export const FIXED_ANTE = 100
|
2022-04-09 18:51:22 +00:00
|
|
|
|
|
|
|
// deprecated
|
2022-02-13 01:19:17 +00:00
|
|
|
export const PHANTOM_ANTE = 0.001
|
2022-04-04 15:47:29 +00:00
|
|
|
export const MINIMUM_ANTE = 50
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-04-29 19:58:01 +00:00
|
|
|
export const HOUSE_LIQUIDITY_PROVIDER_ID = 'IPTOzEqrpkWmEzh6hwvAyY9PqFb2' // @ManifoldMarkets' id
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
export function getCpmmInitialLiquidity(
|
2022-04-29 19:58:01 +00:00
|
|
|
providerId: string,
|
2022-03-15 22:27:51 +00:00
|
|
|
contract: FullContract<CPMM, Binary>,
|
|
|
|
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-03-15 22:27:51 +00:00
|
|
|
contract: FullContract<DPM, Binary>,
|
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-03-15 22:27:51 +00:00
|
|
|
contract: FullContract<DPM, FreeResponse>,
|
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
|
|
|
|
|
|
|
export function getNumericAnte(
|
|
|
|
creator: User,
|
|
|
|
contract: FullContract<DPM, Numeric>,
|
|
|
|
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,
|
|
|
|
userId: creator.id,
|
|
|
|
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
|
|
|
|
}
|