Switch to new dpm mechanism (#26)

* initial commit

* antes

* rename path, compute to contractPath, contractMetrics

* merge

* Include antes as bets; more calculations

* fees on estimated winnings

* mkt payout calculation

* contract: remove startPool, add phantomShares

* Merge branch 'main' into new-dpm

* dpm migration script

* my service account
This commit is contained in:
mantikoros 2022-01-12 13:01:04 -06:00 committed by GitHub
parent d38f1300c3
commit 9d5490cf9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 379 additions and 193 deletions

View File

@ -1,19 +1,58 @@
import { Bet } from './bet'
import { getProbability } from './calculate'
import { Contract } from './contract'
import { User } from './user'
export const PHANTOM_ANTE = 200 export const PHANTOM_ANTE = 200
export const calcStartPool = (initialProbInt: number, ante?: number) => { export const calcStartPool = (initialProbInt: number, ante = 0) => {
const p = initialProbInt / 100.0 const p = initialProbInt / 100.0
const totalAnte = PHANTOM_ANTE + (ante || 0) const totalAnte = PHANTOM_ANTE + ante
const poolYes = const sharesYes = Math.sqrt(p * totalAnte ** 2)
p === 0.5 const sharesNo = Math.sqrt(totalAnte ** 2 - sharesYes ** 2)
? p * totalAnte
: -(totalAnte * (-p + Math.sqrt((-1 + p) * -p))) / (-1 + 2 * p)
const poolNo = totalAnte - poolYes const poolYes = p * ante
const poolNo = (1 - p) * ante
const f = PHANTOM_ANTE / totalAnte const phantomYes = Math.sqrt(p) * PHANTOM_ANTE
const startYes = f * poolYes const phantomNo = Math.sqrt(1 - p) * PHANTOM_ANTE
const startNo = f * poolNo
return { startYes, startNo, poolYes, poolNo } return { sharesYes, sharesNo, poolYes, poolNo, phantomYes, phantomNo }
}
export function getAnteBets(
creator: User,
contract: Contract,
yesAnteId: string,
noAnteId: string
) {
const p = getProbability(contract.totalShares)
const ante = contract.totalBets.YES + contract.totalBets.NO
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,
createdTime: Date.now(),
}
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,
createdTime: Date.now(),
}
return { yesBet, noBet }
} }

View File

@ -2,37 +2,96 @@ import { Bet } from './bet'
import { Contract } from './contract' import { Contract } from './contract'
import { FEES } from './fees' import { FEES } from './fees'
export const blah = () => 999 export function getProbability(totalShares: { YES: number; NO: number }) {
const { YES: y, NO: n } = totalShares
export const getProbability = (pool: { YES: number; NO: number }) => { return y ** 2 / (y ** 2 + n ** 2)
const [yesPool, noPool] = [pool.YES, pool.NO]
const numerator = Math.pow(yesPool, 2)
const denominator = Math.pow(yesPool, 2) + Math.pow(noPool, 2)
return numerator / denominator
} }
export function getProbabilityAfterBet( export function getProbabilityAfterBet(
pool: { YES: number; NO: number }, totalShares: { YES: number; NO: number },
outcome: 'YES' | 'NO', outcome: 'YES' | 'NO',
bet: number bet: number
) { ) {
const [YES, NO] = [ const shares = calculateShares(totalShares, bet, outcome)
pool.YES + (outcome === 'YES' ? bet : 0),
pool.NO + (outcome === 'NO' ? bet : 0), const [YES, NO] =
] outcome === 'YES'
? [totalShares.YES + shares, totalShares.NO]
: [totalShares.YES, totalShares.NO + shares]
return getProbability({ YES, NO }) return getProbability({ YES, NO })
} }
export function calculateShares( export function calculateShares(
pool: { YES: number; NO: number }, totalShares: { YES: number; NO: number },
bet: number, bet: number,
betChoice: 'YES' | 'NO' betChoice: 'YES' | 'NO'
) { ) {
const [yesPool, noPool] = [pool.YES, pool.NO] const [yesShares, noShares] = [totalShares.YES, totalShares.NO]
const c = 2 * bet * Math.sqrt(yesShares ** 2 + noShares ** 2)
return betChoice === 'YES' return betChoice === 'YES'
? bet + (bet * noPool ** 2) / (yesPool ** 2 + bet * yesPool) ? Math.sqrt(bet ** 2 + yesShares ** 2 + c) - yesShares
: bet + (bet * yesPool ** 2) / (noPool ** 2 + bet * noPool) : Math.sqrt(bet ** 2 + noShares ** 2 + c) - noShares
}
export function calculateEstimatedWinnings(
totalShares: { YES: number; NO: number },
shares: number,
betChoice: 'YES' | 'NO'
) {
const ind = betChoice === 'YES' ? 1 : 0
const yesShares = totalShares.YES + ind * shares
const noShares = totalShares.NO + (1 - ind) * shares
const estPool = Math.sqrt(yesShares ** 2 + noShares ** 2)
const total = ind * yesShares + (1 - ind) * noShares
return ((1 - FEES) * (shares * estPool)) / total
}
export function calculateRawShareValue(
totalShares: { YES: number; NO: number },
shares: number,
betChoice: 'YES' | 'NO'
) {
const [yesShares, noShares] = [totalShares.YES, totalShares.NO]
const currentValue = Math.sqrt(yesShares ** 2 + noShares ** 2)
const postSaleValue =
betChoice === 'YES'
? Math.sqrt(Math.max(0, yesShares - shares) ** 2 + noShares ** 2)
: Math.sqrt(yesShares ** 2 + Math.max(0, noShares - shares) ** 2)
return currentValue - postSaleValue
}
export function calculateMoneyRatio(contract: Contract) {
const { totalShares, pool } = contract
const [yesShares, noShares] = [totalShares.YES, totalShares.NO]
const actual = pool.YES + pool.NO
const expected = Math.sqrt(yesShares ** 2 + noShares ** 2)
return actual / expected
}
export function calculateShareValue(contract: Contract, bet: Bet) {
const shareValue = calculateRawShareValue(
contract.totalShares,
bet.shares,
bet.outcome
)
const f = calculateMoneyRatio(contract)
const myPool = contract.pool[bet.outcome]
const adjShareValue = Math.min(Math.min(1, f) * shareValue, myPool)
return adjShareValue
}
export function calculateSaleAmount(contract: Contract, bet: Bet) {
return (1 - FEES) * calculateShareValue(contract, bet)
} }
export function calculatePayout( export function calculatePayout(
@ -40,56 +99,74 @@ export function calculatePayout(
bet: Bet, bet: Bet,
outcome: 'YES' | 'NO' | 'CANCEL' | 'MKT' outcome: 'YES' | 'NO' | 'CANCEL' | 'MKT'
) { ) {
const { amount, outcome: betOutcome, shares } = bet if (outcome === 'CANCEL') return calculateCancelPayout(contract, bet)
if (outcome === 'CANCEL') return amount
if (outcome === 'MKT') return calculateMktPayout(contract, bet) if (outcome === 'MKT') return calculateMktPayout(contract, bet)
return calculateStandardPayout(contract, bet, outcome)
}
export function calculateCancelPayout(contract: Contract, bet: Bet) {
const totalBets = contract.totalBets.YES + contract.totalBets.NO
const pool = contract.pool.YES + contract.pool.NO
return (bet.amount / totalBets) * pool
}
export function calculateStandardPayout(
contract: Contract,
bet: Bet,
outcome: 'YES' | 'NO'
) {
const { amount, outcome: betOutcome, shares } = bet
if (betOutcome !== outcome) return 0 if (betOutcome !== outcome) return 0
const { totalShares, totalBets } = contract const { totalShares, totalBets, phantomShares } = contract
if (totalShares[outcome] === 0) return 0 if (totalShares[outcome] === 0) return 0
const startPool = contract.startPool.YES + contract.startPool.NO const truePool = contract.pool.YES + contract.pool.NO
const truePool = contract.pool.YES + contract.pool.NO - startPool
if (totalBets[outcome] >= truePool) if (totalBets[outcome] >= truePool)
return (amount / totalBets[outcome]) * truePool return (amount / totalBets[outcome]) * truePool
const total = totalShares[outcome] - totalBets[outcome] const total =
totalShares[outcome] - phantomShares[outcome] - totalBets[outcome]
const winningsPool = truePool - totalBets[outcome] const winningsPool = truePool - totalBets[outcome]
return (1 - FEES) * (amount + ((shares - amount) / total) * winningsPool) return (1 - FEES) * (amount + ((shares - amount) / total) * winningsPool)
} }
export function calculatePayoutAfterCorrectBet(contract: Contract, bet: Bet) { export function calculatePayoutAfterCorrectBet(contract: Contract, bet: Bet) {
const { amount, outcome, shares } = bet const { totalShares, pool, totalBets } = contract
const { totalShares, totalBets } = contract
const startPool = contract.startPool.YES + contract.startPool.NO const ind = bet.outcome === 'YES' ? 1 : 0
const truePool = amount + contract.pool.YES + contract.pool.NO - startPool const { shares, amount } = bet
const totalBetsOutcome = totalBets[outcome] + amount const newContract = {
const totalSharesOutcome = totalShares[outcome] + shares ...contract,
totalShares: {
YES: totalShares.YES + ind * shares,
NO: totalShares.NO + (1 - ind) * shares,
},
pool: {
YES: pool.YES + ind * amount,
NO: pool.NO + (1 - ind) * amount,
},
totalBets: {
YES: totalBets.YES + ind * amount,
NO: totalBets.NO + (1 - ind) * amount,
},
}
if (totalBetsOutcome >= truePool) return calculateStandardPayout(newContract, bet, bet.outcome)
return (amount / totalBetsOutcome) * truePool
const total = totalSharesOutcome - totalBetsOutcome
const winningsPool = truePool - totalBetsOutcome
return (1 - FEES) * (amount + ((shares - amount) / total) * winningsPool)
} }
function calculateMktPayout(contract: Contract, bet: Bet) { function calculateMktPayout(contract: Contract, bet: Bet) {
const p = const p = getProbability(contract.totalShares)
contract.pool.YES ** 2 / (contract.pool.YES ** 2 + contract.pool.NO ** 2)
const weightedTotal = const weightedTotal =
p * contract.totalBets.YES + (1 - p) * contract.totalBets.NO p * contract.totalBets.YES + (1 - p) * contract.totalBets.NO
const startPool = contract.startPool.YES + contract.startPool.NO const truePool = contract.pool.YES + contract.pool.NO
const truePool = contract.pool.YES + contract.pool.NO - startPool
const betP = bet.outcome === 'YES' ? p : 1 - p const betP = bet.outcome === 'YES' ? p : 1 - p
@ -100,8 +177,14 @@ function calculateMktPayout(contract: Contract, bet: Bet) {
const winningsPool = truePool - weightedTotal const winningsPool = truePool - weightedTotal
const weightedShareTotal = const weightedShareTotal =
p * (contract.totalShares.YES - contract.totalBets.YES) + p *
(1 - p) * (contract.totalShares.NO - contract.totalBets.NO) (contract.totalShares.YES -
contract.phantomShares.YES -
contract.totalBets.YES) +
(1 - p) *
(contract.totalShares.NO -
contract.phantomShares.NO -
contract.totalBets.NO)
return ( return (
(1 - FEES) * (1 - FEES) *
@ -116,6 +199,7 @@ export function resolvedPayout(contract: Contract, bet: Bet) {
throw new Error('Contract was not resolved') throw new Error('Contract was not resolved')
} }
// deprecated use MKT payout
export function currentValue(contract: Contract, bet: Bet) { export function currentValue(contract: Contract, bet: Bet) {
const prob = getProbability(contract.pool) const prob = getProbability(contract.pool)
const yesPayout = calculatePayout(contract, bet, 'YES') const yesPayout = calculatePayout(contract, bet, 'YES')
@ -123,44 +207,3 @@ export function currentValue(contract: Contract, bet: Bet) {
return prob * yesPayout + (1 - prob) * noPayout return prob * yesPayout + (1 - prob) * noPayout
} }
export function calculateSaleAmount(contract: Contract, bet: Bet) {
const { shares, outcome } = bet
const { YES: yesPool, NO: noPool } = contract.pool
const { YES: yesStart, NO: noStart } = contract.startPool
const { YES: yesShares, NO: noShares } = contract.totalShares
const [y, n, s] = [yesPool, noPool, shares]
const shareValue =
outcome === 'YES'
? // https://www.wolframalpha.com/input/?i=b+%2B+%28b+n%5E2%29%2F%28y+%28-b+%2B+y%29%29+%3D+c+solve+b
(n ** 2 +
s * y +
y ** 2 -
Math.sqrt(
n ** 4 + (s - y) ** 2 * y ** 2 + 2 * n ** 2 * y * (s + y)
)) /
(2 * y)
: (y ** 2 +
s * n +
n ** 2 -
Math.sqrt(
y ** 4 + (s - n) ** 2 * n ** 2 + 2 * y ** 2 * n * (s + n)
)) /
(2 * n)
const startPool = yesStart + noStart
const pool = yesPool + noPool - startPool
const probBefore = yesPool ** 2 / (yesPool ** 2 + noPool ** 2)
const f = pool / (probBefore * yesShares + (1 - probBefore) * noShares)
const myPool = outcome === 'YES' ? yesPool - yesStart : noPool - noStart
const adjShareValue = Math.min(Math.min(1, f) * shareValue, myPool)
const saleAmount = (1 - FEES) * adjShareValue
return saleAmount
}

View File

@ -11,7 +11,8 @@ export type Contract = {
outcomeType: 'BINARY' // | 'MULTI' | 'interval' | 'date' outcomeType: 'BINARY' // | 'MULTI' | 'interval' | 'date'
// outcomes: ['YES', 'NO'] // outcomes: ['YES', 'NO']
startPool: { YES: number; NO: number } mechanism: 'dpm-2'
phantomShares: { YES: number; NO: number }
pool: { YES: number; NO: number } pool: { YES: number; NO: number }
totalShares: { YES: number; NO: number } totalShares: { YES: number; NO: number }
totalBets: { YES: number; NO: number } totalBets: { YES: number; NO: number }

View File

@ -1,4 +1,5 @@
import { Bet } from './bet' import { Bet } from './bet'
import { calculateShares, getProbability } from './calculate'
import { Contract } from './contract' import { Contract } from './contract'
import { User } from './user' import { User } from './user'
@ -16,10 +17,7 @@ export const getNewBetInfo = (
? { YES: yesPool + amount, NO: noPool } ? { YES: yesPool + amount, NO: noPool }
: { YES: yesPool, NO: noPool + amount } : { YES: yesPool, NO: noPool + amount }
const shares = const shares = calculateShares(contract.totalShares, amount, outcome)
outcome === 'YES'
? amount + (amount * noPool ** 2) / (yesPool ** 2 + amount * yesPool)
: amount + (amount * yesPool ** 2) / (noPool ** 2 + amount * noPool)
const { YES: yesShares, NO: noShares } = contract.totalShares const { YES: yesShares, NO: noShares } = contract.totalShares
@ -35,8 +33,8 @@ export const getNewBetInfo = (
? { YES: yesBets + amount, NO: noBets } ? { YES: yesBets + amount, NO: noBets }
: { YES: yesBets, NO: noBets + amount } : { YES: yesBets, NO: noBets + amount }
const probBefore = yesPool ** 2 / (yesPool ** 2 + noPool ** 2) const probBefore = getProbability(contract.totalShares)
const probAfter = newPool.YES ** 2 / (newPool.YES ** 2 + newPool.NO ** 2) const probAfter = getProbability(newTotalShares)
const newBet: Bet = { const newBet: Bet = {
id: newBetId, id: newBetId,

View File

@ -1,4 +1,5 @@
import { calcStartPool } from './antes' import { calcStartPool } from './antes'
import { Contract } from './contract' import { Contract } from './contract'
import { User } from './user' import { User } from './user'
@ -12,10 +13,8 @@ export function getNewContract(
ante?: number, ante?: number,
closeTime?: number closeTime?: number
) { ) {
const { startYes, startNo, poolYes, poolNo } = calcStartPool( const { sharesYes, sharesNo, poolYes, poolNo, phantomYes, phantomNo } =
initialProb, calcStartPool(initialProb, ante)
ante
)
const contract: Contract = { const contract: Contract = {
id, id,
@ -29,10 +28,11 @@ export function getNewContract(
question: question.trim(), question: question.trim(),
description: description.trim(), description: description.trim(),
startPool: { YES: startYes, NO: startNo }, mechanism: 'dpm-2',
phantomShares: { YES: phantomYes, NO: phantomNo },
pool: { YES: poolYes, NO: poolNo }, pool: { YES: poolYes, NO: poolNo },
totalShares: { YES: 0, NO: 0 }, totalShares: { YES: sharesYes, NO: sharesNo },
totalBets: { YES: 0, NO: 0 }, totalBets: { YES: poolYes, NO: poolNo },
isResolved: false, isResolved: false,
createdTime: Date.now(), createdTime: Date.now(),

View File

@ -1,4 +1,5 @@
import { Bet } from './bet' import { Bet } from './bet'
import { getProbability } from './calculate'
import { Contract } from './contract' import { Contract } from './contract'
import { CREATOR_FEE, FEES } from './fees' import { CREATOR_FEE, FEES } from './fees'
@ -58,8 +59,7 @@ export const getMktPayouts = (
contract: Contract, contract: Contract,
bets: Bet[] bets: Bet[]
) => { ) => {
const p = const p = getProbability(contract.totalShares)
contract.pool.YES ** 2 / (contract.pool.YES ** 2 + contract.pool.NO ** 2)
console.log('Resolved MKT at p=', p, 'pool: $M', truePool) console.log('Resolved MKT at p=', p, 'pool: $M', truePool)
const [yesBets, noBets] = partition(bets, (bet) => bet.outcome === 'YES') const [yesBets, noBets] = partition(bets, (bet) => bet.outcome === 'YES')

View File

@ -1,6 +1,7 @@
import { Bet } from './bet' import { Bet } from './bet'
import { calculateShareValue, getProbability } from './calculate'
import { Contract } from './contract' import { Contract } from './contract'
import { CREATOR_FEE, PLATFORM_FEE } from './fees' import { CREATOR_FEE, FEES } from './fees'
import { User } from './user' import { User } from './user'
export const getSellBetInfo = ( export const getSellBetInfo = (
@ -12,40 +13,10 @@ export const getSellBetInfo = (
const { id: betId, amount, shares, outcome } = bet const { id: betId, amount, shares, outcome } = bet
const { YES: yesPool, NO: noPool } = contract.pool const { YES: yesPool, NO: noPool } = contract.pool
const { YES: yesStart, NO: noStart } = contract.startPool
const { YES: yesShares, NO: noShares } = contract.totalShares const { YES: yesShares, NO: noShares } = contract.totalShares
const { YES: yesBets, NO: noBets } = contract.totalBets const { YES: yesBets, NO: noBets } = contract.totalBets
const [y, n, s] = [yesPool, noPool, shares] const adjShareValue = calculateShareValue(contract, bet)
const shareValue =
outcome === 'YES'
? // https://www.wolframalpha.com/input/?i=b+%2B+%28b+n%5E2%29%2F%28y+%28-b+%2B+y%29%29+%3D+c+solve+b
(n ** 2 +
s * y +
y ** 2 -
Math.sqrt(
n ** 4 + (s - y) ** 2 * y ** 2 + 2 * n ** 2 * y * (s + y)
)) /
(2 * y)
: (y ** 2 +
s * n +
n ** 2 -
Math.sqrt(
y ** 4 + (s - n) ** 2 * n ** 2 + 2 * y ** 2 * n * (s + n)
)) /
(2 * n)
const startPool = yesStart + noStart
const pool = yesPool + noPool - startPool
const probBefore = yesPool ** 2 / (yesPool ** 2 + noPool ** 2)
const f = pool / (probBefore * yesShares + (1 - probBefore) * noShares)
const myPool = outcome === 'YES' ? yesPool - yesStart : noPool - noStart
const adjShareValue = Math.min(Math.min(1, f) * shareValue, myPool)
const newPool = const newPool =
outcome === 'YES' outcome === 'YES'
@ -62,10 +33,11 @@ export const getSellBetInfo = (
? { YES: yesBets - amount, NO: noBets } ? { YES: yesBets - amount, NO: noBets }
: { YES: yesBets, NO: noBets - amount } : { YES: yesBets, NO: noBets - amount }
const probAfter = newPool.YES ** 2 / (newPool.YES ** 2 + newPool.NO ** 2) const probBefore = getProbability(contract.totalShares)
const probAfter = getProbability(newTotalShares)
const creatorFee = CREATOR_FEE * adjShareValue const creatorFee = CREATOR_FEE * adjShareValue
const saleAmount = (1 - CREATOR_FEE - PLATFORM_FEE) * adjShareValue const saleAmount = (1 - FEES) * adjShareValue
console.log( console.log(
'SELL M$', 'SELL M$',
@ -73,8 +45,6 @@ export const getSellBetInfo = (
outcome, outcome,
'for M$', 'for M$',
saleAmount, saleAmount,
'M$/share:',
f,
'creator fee: M$', 'creator fee: M$',
creatorFee creatorFee
) )

View File

@ -6,6 +6,7 @@ import { Contract } from '../../common/contract'
import { slugify } from '../../common/util/slugify' import { slugify } from '../../common/util/slugify'
import { randomString } from '../../common/util/random-string' import { randomString } from '../../common/util/random-string'
import { getNewContract } from '../../common/new-contract' import { getNewContract } from '../../common/new-contract'
import { getAnteBets } from '../../common/antes'
export const createContract = functions export const createContract = functions
.runWith({ minInstances: 1 }) .runWith({ minInstances: 1 })
@ -64,6 +65,26 @@ export const createContract = functions
if (ante) await chargeUser(creator.id, ante) if (ante) await chargeUser(creator.id, ante)
await contractRef.create(contract) await contractRef.create(contract)
if (ante) {
const yesBetDoc = firestore
.collection(`contracts/${contract.id}/bets`)
.doc()
const noBetDoc = firestore
.collection(`contracts/${contract.id}/bets`)
.doc()
const { yesBet, noBet } = getAnteBets(
creator,
contract,
yesBetDoc.id,
noBetDoc.id
)
await yesBetDoc.set(yesBet)
await noBetDoc.set(noBet)
}
return { status: 'success', contract } return { status: 'success', contract }
} }
) )

View File

@ -33,7 +33,7 @@ const getContractInfo = ({
description, description,
slug, slug,
pool, pool,
startPool, totalShares,
volume7Days, volume7Days,
volume24Hours, volume24Hours,
isResolved, isResolved,
@ -47,8 +47,8 @@ const getContractInfo = ({
question, question,
description, description,
url: `https://manifold.markets/${creatorUsername}/${slug}`, url: `https://manifold.markets/${creatorUsername}/${slug}`,
pool: pool.YES + pool.NO - startPool.YES - startPool.NO, pool: pool.YES + pool.NO,
probability: getProbability(pool), probability: getProbability(totalShares),
volume7Days, volume7Days,
volume24Hours, volume24Hours,
isResolved, isResolved,

View File

@ -61,8 +61,7 @@ export const resolveMarket = functions
const bets = betsSnap.docs.map((doc) => doc.data() as Bet) const bets = betsSnap.docs.map((doc) => doc.data() as Bet)
const openBets = bets.filter((b) => !b.isSold && !b.sale) const openBets = bets.filter((b) => !b.isSold && !b.sale)
const startPool = contract.startPool.YES + contract.startPool.NO const truePool = contract.pool.YES + contract.pool.NO
const truePool = contract.pool.YES + contract.pool.NO - startPool
const payouts = const payouts =
outcome === 'CANCEL' outcome === 'CANCEL'

View File

@ -0,0 +1,71 @@
import * as admin from 'firebase-admin'
import { Contract } from '../../../common/contract'
type DocRef = admin.firestore.DocumentReference
// Generate your own private key, and set the path below:
// https://console.firebase.google.com/u/0/project/mantic-markets/settings/serviceaccounts/adminsdk
// const serviceAccount = require('../../../../../../Downloads/dev-mantic-markets-firebase-adminsdk-sir5m-b2d27f8970.json')
const serviceAccount = require('../../../../../../Downloads/mantic-markets-firebase-adminsdk-1ep46-351a65eca3.json')
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
})
const firestore = admin.firestore()
async function recalculateContract(contractRef: DocRef, contract: Contract) {
const startPool = (contract as any).startPool as
| undefined
| { YES: number; NO: number }
if (contract.phantomShares || !startPool) return
const phantomAnte = startPool.YES + startPool.NO
const p = startPool.YES ** 2 / (startPool.YES ** 2 + startPool.NO ** 2)
const phantomShares = {
YES: Math.sqrt(p) * phantomAnte,
NO: Math.sqrt(1 - p) * phantomAnte,
}
const pool = {
YES: contract.pool.YES - startPool.YES,
NO: contract.pool.NO - startPool.NO,
}
const totalShares = {
YES: contract.totalShares.YES + phantomShares.YES,
NO: contract.totalShares.NO + phantomShares.NO,
}
const update: Partial<Contract> = {
mechanism: 'dpm-2',
phantomShares,
pool,
totalShares,
}
await contractRef.update(update)
console.log('updated', contract.slug, 'with', update)
console.log()
}
async function recalculateContractTotals() {
console.log('Migrating ante calculations to DPM-2')
const snapshot = await firestore.collection('contracts').get()
const contracts = snapshot.docs.map((doc) => doc.data() as Contract)
console.log('Loaded', contracts.length, 'contracts')
for (const contract of contracts) {
const contractRef = firestore.doc(`contracts/${contract.id}`)
await recalculateContract(contractRef, contract)
}
}
if (require.main === module)
recalculateContractTotals().then(() => process.exit())

View File

@ -18,6 +18,7 @@ import {
calculateShares, calculateShares,
getProbabilityAfterBet, getProbabilityAfterBet,
calculatePayoutAfterCorrectBet, calculatePayoutAfterCorrectBet,
calculateEstimatedWinnings,
} from '../../common/calculate' } from '../../common/calculate'
import { firebaseLogin } from '../lib/firebase/users' import { firebaseLogin } from '../lib/firebase/users'
import { OutcomeLabel } from './outcome-label' import { OutcomeLabel } from './outcome-label'
@ -84,18 +85,30 @@ export function BetPanel(props: { contract: Contract; className?: string }) {
const betDisabled = isSubmitting || !betAmount || error const betDisabled = isSubmitting || !betAmount || error
const initialProb = getProbability(contract.pool) const initialProb = getProbability(contract.totalShares)
const resultProb = getProbabilityAfterBet( const resultProb = getProbabilityAfterBet(
contract.pool, contract.totalShares,
betChoice, betChoice,
betAmount ?? 0 betAmount ?? 0
) )
const shares = calculateShares(contract.pool, betAmount ?? 0, betChoice)
const estimatedWinnings = Math.floor(shares) const shares = calculateShares(
contract.totalShares,
betAmount ?? 0,
betChoice
)
const estimatedWinnings = calculateEstimatedWinnings(
contract.totalShares,
shares,
betChoice
)
const estimatedReturn = betAmount const estimatedReturn = betAmount
? (estimatedWinnings - betAmount) / betAmount ? (estimatedWinnings - betAmount) / betAmount
: 0 : 0
const estimatedReturnPercent = (estimatedReturn * 100).toFixed() + '%' const estimatedReturnPercent = (estimatedReturn * 100).toFixed() + '%'
return ( return (

View File

@ -14,7 +14,11 @@ import {
} from '../lib/util/format' } from '../lib/util/format'
import { Col } from './layout/col' import { Col } from './layout/col'
import { Spacer } from './layout/spacer' import { Spacer } from './layout/spacer'
import { Contract, getContractFromId, path } from '../lib/firebase/contracts' import {
Contract,
getContractFromId,
contractPath,
} from '../lib/firebase/contracts'
import { Row } from './layout/row' import { Row } from './layout/row'
import { UserLink } from './user-page' import { UserLink } from './user-page'
import { import {
@ -130,7 +134,7 @@ function MyContractBets(props: { contract: Contract; bets: Bet[] }) {
<Row className="flex-wrap gap-4"> <Row className="flex-wrap gap-4">
<Col className="flex-[2] gap-1"> <Col className="flex-[2] gap-1">
<Row className="mr-6"> <Row className="mr-6">
<Link href={path(contract)}> <Link href={contractPath(contract)}>
<a <a
className="font-medium text-indigo-700 hover:underline hover:decoration-indigo-400 hover:decoration-2" className="font-medium text-indigo-700 hover:underline hover:decoration-indigo-400 hover:decoration-2"
onClick={(e) => e.stopPropagation()} onClick={(e) => e.stopPropagation()}

View File

@ -4,7 +4,11 @@ import { Row } from '../components/layout/row'
import { formatMoney } from '../lib/util/format' import { formatMoney } from '../lib/util/format'
import { UserLink } from './user-page' import { UserLink } from './user-page'
import { Linkify } from './linkify' import { Linkify } from './linkify'
import { Contract, compute, path } from '../lib/firebase/contracts' import {
Contract,
contractMetrics,
contractPath,
} from '../lib/firebase/contracts'
import { Col } from './layout/col' import { Col } from './layout/col'
import { parseTags } from '../lib/util/parse' import { parseTags } from '../lib/util/parse'
import dayjs from 'dayjs' import dayjs from 'dayjs'
@ -16,10 +20,10 @@ export function ContractCard(props: {
}) { }) {
const { contract, showHotVolume } = props const { contract, showHotVolume } = props
const { question, resolution } = contract const { question, resolution } = contract
const { probPercent } = compute(contract) const { probPercent } = contractMetrics(contract)
return ( return (
<Link href={path(contract)}> <Link href={contractPath(contract)}>
<a> <a>
<li className="col-span-1 bg-white hover:bg-gray-100 shadow-md rounded-lg divide-y divide-gray-200"> <li className="col-span-1 bg-white hover:bg-gray-100 shadow-md rounded-lg divide-y divide-gray-200">
<div className="card"> <div className="card">
@ -99,7 +103,7 @@ export function AbbrContractDetails(props: {
}) { }) {
const { contract, showHotVolume } = props const { contract, showHotVolume } = props
const { volume24Hours } = contract const { volume24Hours } = contract
const { truePool } = compute(contract) const { truePool } = contractMetrics(contract)
return ( return (
<Col className={clsx('text-sm text-gray-500 gap-2')}> <Col className={clsx('text-sm text-gray-500 gap-2')}>
@ -124,7 +128,7 @@ export function AbbrContractDetails(props: {
export function ContractDetails(props: { contract: Contract }) { export function ContractDetails(props: { contract: Contract }) {
const { contract } = props const { contract } = props
const { question, description, closeTime } = contract const { question, description, closeTime } = contract
const { truePool, createdDate, resolvedDate } = compute(contract) const { truePool, createdDate, resolvedDate } = contractMetrics(contract)
const tags = parseTags(`${question} ${description}`).map((tag) => `#${tag}`) const tags = parseTags(`${question} ${description}`).map((tag) => `#${tag}`)
@ -171,7 +175,7 @@ export function ContractDetails(props: { contract: Contract }) {
// String version of the above, to send to the OpenGraph image generator // String version of the above, to send to the OpenGraph image generator
export function contractTextDetails(contract: Contract) { export function contractTextDetails(contract: Contract) {
const { question, description, closeTime } = contract const { question, description, closeTime } = contract
const { truePool, createdDate, resolvedDate } = compute(contract) const { truePool, createdDate, resolvedDate } = contractMetrics(contract)
const tags = parseTags(`${question} ${description}`).map((tag) => `#${tag}`) const tags = parseTags(`${question} ${description}`).map((tag) => `#${tag}`)

View File

@ -17,9 +17,9 @@ import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime' import relativeTime from 'dayjs/plugin/relativeTime'
import { OutcomeLabel } from './outcome-label' import { OutcomeLabel } from './outcome-label'
import { import {
compute, contractMetrics,
Contract, Contract,
path, contractPath,
updateContract, updateContract,
} from '../lib/firebase/contracts' } from '../lib/firebase/contracts'
import { useUser } from '../hooks/use-user' import { useUser } from '../hooks/use-user'
@ -205,7 +205,7 @@ export function ContractDescription(props: {
function FeedQuestion(props: { contract: Contract }) { function FeedQuestion(props: { contract: Contract }) {
const { contract } = props const { contract } = props
const { probPercent } = compute(contract) const { probPercent } = contractMetrics(contract)
return ( return (
<> <>
@ -223,7 +223,7 @@ function FeedQuestion(props: { contract: Contract }) {
</div> </div>
<Col className="items-start sm:flex-row justify-between gap-2 sm:gap-4 mb-4 mr-2"> <Col className="items-start sm:flex-row justify-between gap-2 sm:gap-4 mb-4 mr-2">
<SiteLink <SiteLink
href={path(contract)} href={contractPath(contract)}
className="text-lg sm:text-xl text-indigo-700" className="text-lg sm:text-xl text-indigo-700"
> >
{contract.question} {contract.question}

View File

@ -1,8 +1,8 @@
import { import {
compute, contractMetrics,
Contract, Contract,
deleteContract, deleteContract,
path, contractPath,
} from '../lib/firebase/contracts' } from '../lib/firebase/contracts'
import { Col } from './layout/col' import { Col } from './layout/col'
import { Spacer } from './layout/spacer' import { Spacer } from './layout/spacer'
@ -22,7 +22,7 @@ export const ContractOverview = (props: {
}) => { }) => {
const { contract, className } = props const { contract, className } = props
const { resolution, creatorId, creatorName } = contract const { resolution, creatorId, creatorName } = contract
const { probPercent, truePool } = compute(contract) const { probPercent, truePool } = contractMetrics(contract)
const user = useUser() const user = useUser()
const isCreator = user?.id === creatorId const isCreator = user?.id === creatorId
@ -35,7 +35,7 @@ export const ContractOverview = (props: {
? `Resolved ${resolution}!` ? `Resolved ${resolution}!`
: `Resolved ${resolution} by ${creatorName}:` : `Resolved ${resolution} by ${creatorName}:`
: `Currently ${probPercent} chance, place your bets here:` : `Currently ${probPercent} chance, place your bets here:`
const url = `https://manifold.markets${path(contract)}` const url = `https://manifold.markets${contractPath(contract)}`
const tweetText = `${tweetQuestion}\n\n${tweetDescription}\n\n${url}` const tweetText = `${tweetQuestion}\n\n${tweetDescription}\n\n${url}`
return ( return (

View File

@ -1,19 +1,19 @@
import { DatumValue } from '@nivo/core' import { DatumValue } from '@nivo/core'
import { ResponsiveLine } from '@nivo/line' import { ResponsiveLine } from '@nivo/line'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { getProbability } from '../../common/calculate'
import { useBets } from '../hooks/use-bets' import { useBets } from '../hooks/use-bets'
import { useWindowSize } from '../hooks/use-window-size' import { useWindowSize } from '../hooks/use-window-size'
import { Contract } from '../lib/firebase/contracts' import { Contract } from '../lib/firebase/contracts'
export function ContractProbGraph(props: { contract: Contract }) { export function ContractProbGraph(props: { contract: Contract }) {
const { contract } = props const { contract } = props
const { id, startPool, resolutionTime } = contract const { id, phantomShares, resolutionTime } = contract
let bets = useBets(id) let bets = useBets(id)
if (bets === 'loading') bets = [] if (bets === 'loading') bets = []
const startProb = const startProb = getProbability(phantomShares)
startPool.YES ** 2 / (startPool.YES ** 2 + startPool.NO ** 2)
const times = [ const times = [
contract.createdTime, contract.createdTime,

View File

@ -3,7 +3,11 @@ import Link from 'next/link'
import clsx from 'clsx' import clsx from 'clsx'
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import { compute, Contract, listContracts } from '../lib/firebase/contracts' import {
contractMetrics,
Contract,
listContracts,
} from '../lib/firebase/contracts'
import { User } from '../lib/firebase/users' import { User } from '../lib/firebase/users'
import { Col } from './layout/col' import { Col } from './layout/col'
import { SiteLink } from './site-link' import { SiteLink } from './site-link'
@ -189,7 +193,9 @@ export function SearchableGrid(props: {
if (sort === 'newest' || sort === 'resolved' || sort === 'all') { if (sort === 'newest' || sort === 'resolved' || sort === 'all') {
matches.sort((a, b) => b.createdTime - a.createdTime) matches.sort((a, b) => b.createdTime - a.createdTime)
} else if (sort === 'most-traded') { } else if (sort === 'most-traded') {
matches.sort((a, b) => compute(b).truePool - compute(a).truePool) matches.sort(
(a, b) => contractMetrics(b).truePool - contractMetrics(a).truePool
)
} else if (sort === 'creator' || sort === 'tag') { } else if (sort === 'creator' || sort === 'tag') {
matches.sort((a, b) => b.volume7Days - a.volume7Days) matches.sort((a, b) => b.volume7Days - a.volume7Days)
} }

View File

@ -59,6 +59,7 @@ function getNavigationOptions(user: User, options: { mobile: boolean }) {
name: 'Discord', name: 'Discord',
href: 'https://discord.gg/eHQBNBqXuh', href: 'https://discord.gg/eHQBNBqXuh',
}, },
...(mobile ? [{ name: 'About', href: '/about' }] : []),
{ {
name: 'About', name: 'About',
href: '/about', href: '/about',

View File

@ -18,24 +18,36 @@ import {
import { app } from './init' import { app } from './init'
import { getValues, listenForValues } from './utils' import { getValues, listenForValues } from './utils'
import { Contract } from '../../../common/contract' import { Contract } from '../../../common/contract'
import { getProbability } from '../../../common/calculate'
export type { Contract } export type { Contract }
export function path(contract: Contract) { export function contractPath(contract: Contract) {
// For now, derive username from creatorName // For now, derive username from creatorName
return `/${contract.creatorUsername}/${contract.slug}` return `/${contract.creatorUsername}/${contract.slug}`
} }
export function compute(contract: Contract) { export function contractMetrics(contract: Contract) {
const { pool, startPool, createdTime, resolutionTime, isResolved } = contract const {
const truePool = pool.YES + pool.NO - startPool.YES - startPool.NO pool,
const prob = pool.YES ** 2 / (pool.YES ** 2 + pool.NO ** 2) phantomShares,
totalShares,
createdTime,
resolutionTime,
isResolved,
} = contract
const truePool = pool.YES + pool.NO
const prob = getProbability(totalShares)
const probPercent = Math.round(prob * 100) + '%' const probPercent = Math.round(prob * 100) + '%'
const startProb =
startPool.YES ** 2 / (startPool.YES ** 2 + startPool.NO ** 2) const startProb = getProbability(phantomShares)
const createdDate = dayjs(createdTime).format('MMM D') const createdDate = dayjs(createdTime).format('MMM D')
const resolvedDate = isResolved const resolvedDate = isResolved
? dayjs(resolutionTime).format('MMM D') ? dayjs(resolutionTime).format('MMM D')
: undefined : undefined
return { truePool, probPercent, startProb, createdDate, resolvedDate } return { truePool, probPercent, startProb, createdDate, resolvedDate }
} }

View File

@ -2,8 +2,8 @@ import { getFirestore } from '@firebase/firestore'
import { initializeApp } from 'firebase/app' import { initializeApp } from 'firebase/app'
// TODO: Reenable this when we have a way to set the Firebase db in dev // TODO: Reenable this when we have a way to set the Firebase db in dev
// export const isProd = process.env.NODE_ENV === 'production' export const isProd = process.env.NODE_ENV === 'production'
export const isProd = true // export const isProd = true
const firebaseConfig = isProd const firebaseConfig = isProd
? { ? {

View File

@ -12,7 +12,7 @@ import { Title } from '../../components/title'
import { Spacer } from '../../components/layout/spacer' import { Spacer } from '../../components/layout/spacer'
import { User } from '../../lib/firebase/users' import { User } from '../../lib/firebase/users'
import { import {
compute, contractMetrics,
Contract, Contract,
getContractFromSlug, getContractFromSlug,
} from '../../lib/firebase/contracts' } from '../../lib/firebase/contracts'
@ -58,7 +58,7 @@ export default function ContractPage(props: {
!isResolved && (!contract.closeTime || contract.closeTime > Date.now()) !isResolved && (!contract.closeTime || contract.closeTime > Date.now())
const allowResolve = !isResolved && isCreator && !!user const allowResolve = !isResolved && isCreator && !!user
const { probPercent } = compute(contract) const { probPercent } = contractMetrics(contract)
const description = resolution const description = resolution
? `Resolved ${resolution}. ${contract.description}` ? `Resolved ${resolution}. ${contract.description}`

View File

@ -7,7 +7,7 @@ import Textarea from 'react-expanding-textarea'
import { Spacer } from '../components/layout/spacer' import { Spacer } from '../components/layout/spacer'
import { Title } from '../components/title' import { Title } from '../components/title'
import { useUser } from '../hooks/use-user' import { useUser } from '../hooks/use-user'
import { Contract, path } from '../lib/firebase/contracts' import { Contract, contractPath } from '../lib/firebase/contracts'
import { Page } from '../components/page' import { Page } from '../components/page'
import { AdvancedPanel } from '../components/advanced-panel' import { AdvancedPanel } from '../components/advanced-panel'
import { createContract } from '../lib/firebase/api-call' import { createContract } from '../lib/firebase/api-call'
@ -72,7 +72,7 @@ export default function NewContract() {
return return
} }
await router.push(path(result.contract as Contract)) await router.push(contractPath(result.contract as Contract))
} }
// const descriptionPlaceholder = `e.g. This market will resolve to “Yes” if, by June 2, 2021, 11:59:59 PM ET, Paxlovid (also known under PF-07321332)...` // const descriptionPlaceholder = `e.g. This market will resolve to “Yes” if, by June 2, 2021, 11:59:59 PM ET, Paxlovid (also known under PF-07321332)...`

View File

@ -10,7 +10,11 @@ import { Page } from '../components/page'
import { Title } from '../components/title' import { Title } from '../components/title'
import { useUser } from '../hooks/use-user' import { useUser } from '../hooks/use-user'
import { createContract } from '../lib/firebase/api-call' import { createContract } from '../lib/firebase/api-call'
import { compute, Contract, path } from '../lib/firebase/contracts' import {
contractMetrics,
Contract,
contractPath,
} from '../lib/firebase/contracts'
type Prediction = { type Prediction = {
question: string question: string
@ -20,12 +24,12 @@ type Prediction = {
} }
function toPrediction(contract: Contract): Prediction { function toPrediction(contract: Contract): Prediction {
const { startProb } = compute(contract) const { startProb } = contractMetrics(contract)
return { return {
question: contract.question, question: contract.question,
description: contract.description, description: contract.description,
initialProb: startProb * 100, initialProb: startProb * 100,
createdUrl: path(contract), createdUrl: contractPath(contract),
} }
} }