Resolve multi market
This commit is contained in:
parent
0a178a7519
commit
fc954b6b4b
|
@ -179,29 +179,24 @@ export function calculatePayoutAfterCorrectBet(contract: Contract, bet: Bet) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function calculateMktPayout(contract: Contract, bet: Bet) {
|
function calculateMktPayout(contract: Contract, bet: Bet) {
|
||||||
const { resolutionProbability, totalShares, pool, phantomShares } = contract
|
if (contract.outcomeType === 'BINARY')
|
||||||
|
return calculateBinaryMktPayout(contract, bet)
|
||||||
|
|
||||||
|
const { totalShares, pool } = contract as any as Contract<'MULTI'>
|
||||||
|
|
||||||
const totalPool = _.sum(Object.values(pool))
|
const totalPool = _.sum(Object.values(pool))
|
||||||
const squareSum = _.sumBy(Object.values(totalShares), (shares) => shares ** 2)
|
const sharesSquareSum = _.sumBy(
|
||||||
|
Object.values(totalShares),
|
||||||
|
(shares) => shares ** 2
|
||||||
|
)
|
||||||
|
|
||||||
let weightedShareTotal = _.sumBy(Object.keys(totalShares), (outcome) => {
|
const weightedShareTotal = _.sumBy(Object.keys(totalShares), (outcome) => {
|
||||||
const shareTotals = totalShares as { [outcome: string]: number }
|
// Avoid O(n^2) by reusing sharesSquareSum for prob.
|
||||||
|
const shares = totalShares[outcome]
|
||||||
// Avoid O(n^2) by reusing squareSum for prob.
|
const prob = shares ** 2 / sharesSquareSum
|
||||||
const prob = shareTotals[outcome] ** 2 / squareSum
|
|
||||||
const shares =
|
|
||||||
shareTotals[outcome] -
|
|
||||||
(phantomShares ? phantomShares[outcome as 'YES' | 'NO'] : 0)
|
|
||||||
return prob * shares
|
return prob * shares
|
||||||
})
|
})
|
||||||
|
|
||||||
// Compute binary case if resolutionProbability provided.
|
|
||||||
if (resolutionProbability !== undefined) {
|
|
||||||
weightedShareTotal =
|
|
||||||
resolutionProbability * (totalShares.YES - phantomShares.YES) +
|
|
||||||
(1 - resolutionProbability) * (totalShares.NO - phantomShares.NO)
|
|
||||||
}
|
|
||||||
|
|
||||||
const { outcome, amount, shares } = bet
|
const { outcome, amount, shares } = bet
|
||||||
|
|
||||||
const betP = getOutcomeProbability(totalShares, outcome)
|
const betP = getOutcomeProbability(totalShares, outcome)
|
||||||
|
@ -210,6 +205,26 @@ function calculateMktPayout(contract: Contract, bet: Bet) {
|
||||||
return deductFees(amount, winnings)
|
return deductFees(amount, winnings)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function calculateBinaryMktPayout(contract: Contract, bet: Bet) {
|
||||||
|
const p =
|
||||||
|
contract.resolutionProbability !== undefined
|
||||||
|
? contract.resolutionProbability
|
||||||
|
: getProbability(contract.totalShares)
|
||||||
|
|
||||||
|
const pool = contract.pool.YES + contract.pool.NO
|
||||||
|
|
||||||
|
const weightedShareTotal =
|
||||||
|
p * (contract.totalShares.YES - contract.phantomShares.YES) +
|
||||||
|
(1 - p) * (contract.totalShares.NO - contract.phantomShares.NO)
|
||||||
|
|
||||||
|
const { outcome, amount, shares } = bet
|
||||||
|
|
||||||
|
const betP = outcome === 'YES' ? p : 1 - p
|
||||||
|
const winnings = ((betP * shares) / weightedShareTotal) * pool
|
||||||
|
|
||||||
|
return deductFees(amount, winnings)
|
||||||
|
}
|
||||||
|
|
||||||
export function resolvedPayout(contract: Contract, bet: Bet) {
|
export function resolvedPayout(contract: Contract, bet: Bet) {
|
||||||
if (contract.resolution)
|
if (contract.resolution)
|
||||||
return calculatePayout(contract, bet, contract.resolution)
|
return calculatePayout(contract, bet, contract.resolution)
|
||||||
|
|
|
@ -7,7 +7,7 @@ import { CREATOR_FEE, FEES } from './fees'
|
||||||
|
|
||||||
export const getCancelPayouts = (contract: Contract, bets: Bet[]) => {
|
export const getCancelPayouts = (contract: Contract, bets: Bet[]) => {
|
||||||
const { pool } = contract
|
const { pool } = contract
|
||||||
const poolTotal = pool.YES + pool.NO
|
const poolTotal = _.sum(Object.values(pool))
|
||||||
console.log('resolved N/A, pool M$', poolTotal)
|
console.log('resolved N/A, pool M$', poolTotal)
|
||||||
|
|
||||||
const betSum = _.sumBy(bets, (b) => b.amount)
|
const betSum = _.sumBy(bets, (b) => b.amount)
|
||||||
|
@ -19,18 +19,17 @@ export const getCancelPayouts = (contract: Contract, bets: Bet[]) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getStandardPayouts = (
|
export const getStandardPayouts = (
|
||||||
outcome: 'YES' | 'NO',
|
outcome: string,
|
||||||
contract: Contract,
|
contract: Contract,
|
||||||
bets: Bet[]
|
bets: Bet[]
|
||||||
) => {
|
) => {
|
||||||
const [yesBets, noBets] = _.partition(bets, (bet) => bet.outcome === 'YES')
|
const winningBets = bets.filter((bet) => bet.outcome === outcome)
|
||||||
const winningBets = outcome === 'YES' ? yesBets : noBets
|
|
||||||
|
|
||||||
const pool = contract.pool.YES + contract.pool.NO
|
const poolTotal = _.sum(Object.values(contract.pool))
|
||||||
const totalShares = _.sumBy(winningBets, (b) => b.shares)
|
const totalShares = _.sumBy(winningBets, (b) => b.shares)
|
||||||
|
|
||||||
const payouts = winningBets.map(({ userId, amount, shares }) => {
|
const payouts = winningBets.map(({ userId, amount, shares }) => {
|
||||||
const winnings = (shares / totalShares) * pool
|
const winnings = (shares / totalShares) * poolTotal
|
||||||
const profit = winnings - amount
|
const profit = winnings - amount
|
||||||
|
|
||||||
// profit can be negative if using phantom shares
|
// profit can be negative if using phantom shares
|
||||||
|
@ -45,7 +44,7 @@ export const getStandardPayouts = (
|
||||||
'resolved',
|
'resolved',
|
||||||
outcome,
|
outcome,
|
||||||
'pool',
|
'pool',
|
||||||
pool,
|
poolTotal,
|
||||||
'profits',
|
'profits',
|
||||||
profits,
|
profits,
|
||||||
'creator fee',
|
'creator fee',
|
||||||
|
@ -114,5 +113,8 @@ export const getPayouts = (
|
||||||
return getMktPayouts(contract, bets, resolutionProbability)
|
return getMktPayouts(contract, bets, resolutionProbability)
|
||||||
case 'CANCEL':
|
case 'CANCEL':
|
||||||
return getCancelPayouts(contract, bets)
|
return getCancelPayouts(contract, bets)
|
||||||
|
default:
|
||||||
|
// Multi outcome.
|
||||||
|
return getStandardPayouts(outcome, contract, bets)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,12 +15,23 @@ type market_resolved_template = {
|
||||||
url: string
|
url: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const toDisplayResolution = (outcome: string, prob: number) => {
|
||||||
|
const display = {
|
||||||
|
YES: 'YES',
|
||||||
|
NO: 'NO',
|
||||||
|
CANCEL: 'N/A',
|
||||||
|
MKT: formatPercent(prob),
|
||||||
|
}[outcome]
|
||||||
|
|
||||||
|
return display === undefined ? `#${outcome}` : display
|
||||||
|
}
|
||||||
|
|
||||||
export const sendMarketResolutionEmail = async (
|
export const sendMarketResolutionEmail = async (
|
||||||
userId: string,
|
userId: string,
|
||||||
payout: number,
|
payout: number,
|
||||||
creator: User,
|
creator: User,
|
||||||
contract: Contract,
|
contract: Contract,
|
||||||
resolution: 'YES' | 'NO' | 'CANCEL' | 'MKT',
|
resolution: 'YES' | 'NO' | 'CANCEL' | 'MKT' | string,
|
||||||
resolutionProbability?: number
|
resolutionProbability?: number
|
||||||
) => {
|
) => {
|
||||||
const privateUser = await getPrivateUser(userId)
|
const privateUser = await getPrivateUser(userId)
|
||||||
|
@ -36,13 +47,7 @@ export const sendMarketResolutionEmail = async (
|
||||||
|
|
||||||
const prob = resolutionProbability ?? getProbability(contract.totalShares)
|
const prob = resolutionProbability ?? getProbability(contract.totalShares)
|
||||||
|
|
||||||
const toDisplayResolution = {
|
const outcome = toDisplayResolution(resolution, prob)
|
||||||
YES: 'YES',
|
|
||||||
NO: 'NO',
|
|
||||||
CANCEL: 'N/A',
|
|
||||||
MKT: formatPercent(prob),
|
|
||||||
}
|
|
||||||
const outcome = toDisplayResolution[resolution]
|
|
||||||
|
|
||||||
const subject = `Resolved ${outcome}: ${contract.question}`
|
const subject = `Resolved ${outcome}: ${contract.question}`
|
||||||
|
|
||||||
|
|
|
@ -25,10 +25,25 @@ export const resolveMarket = functions
|
||||||
|
|
||||||
const { outcome, contractId, probabilityInt } = data
|
const { outcome, contractId, probabilityInt } = data
|
||||||
|
|
||||||
if (!['YES', 'NO', 'MKT', 'CANCEL'].includes(outcome))
|
const contractDoc = firestore.doc(`contracts/${contractId}`)
|
||||||
return { status: 'error', message: 'Invalid outcome' }
|
const contractSnap = await contractDoc.get()
|
||||||
|
if (!contractSnap.exists)
|
||||||
|
return { status: 'error', message: 'Invalid contract' }
|
||||||
|
const contract = contractSnap.data() as Contract
|
||||||
|
const { creatorId, outcomeType } = contract
|
||||||
|
|
||||||
|
if (outcomeType === 'BINARY') {
|
||||||
|
if (!['YES', 'NO', 'MKT', 'CANCEL'].includes(outcome))
|
||||||
|
return { status: 'error', message: 'Invalid outcome' }
|
||||||
|
} else if (outcomeType === 'MULTI') {
|
||||||
|
if (isNaN(+outcome))
|
||||||
|
return { status: 'error', message: 'Invalid outcome' }
|
||||||
|
} else {
|
||||||
|
return { status: 'error', message: 'Invalid contract outcomeType' }
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
outcomeType === 'BINARY' &&
|
||||||
probabilityInt !== undefined &&
|
probabilityInt !== undefined &&
|
||||||
(probabilityInt < 0 ||
|
(probabilityInt < 0 ||
|
||||||
probabilityInt > 100 ||
|
probabilityInt > 100 ||
|
||||||
|
@ -36,19 +51,13 @@ export const resolveMarket = functions
|
||||||
)
|
)
|
||||||
return { status: 'error', message: 'Invalid probability' }
|
return { status: 'error', message: 'Invalid probability' }
|
||||||
|
|
||||||
const contractDoc = firestore.doc(`contracts/${contractId}`)
|
if (creatorId !== userId)
|
||||||
const contractSnap = await contractDoc.get()
|
|
||||||
if (!contractSnap.exists)
|
|
||||||
return { status: 'error', message: 'Invalid contract' }
|
|
||||||
const contract = contractSnap.data() as Contract
|
|
||||||
|
|
||||||
if (contract.creatorId !== userId)
|
|
||||||
return { status: 'error', message: 'User not creator of contract' }
|
return { status: 'error', message: 'User not creator of contract' }
|
||||||
|
|
||||||
if (contract.resolution)
|
if (contract.resolution)
|
||||||
return { status: 'error', message: 'Contract already resolved' }
|
return { status: 'error', message: 'Contract already resolved' }
|
||||||
|
|
||||||
const creator = await getUser(contract.creatorId)
|
const creator = await getUser(creatorId)
|
||||||
if (!creator) return { status: 'error', message: 'Creator not found' }
|
if (!creator) return { status: 'error', message: 'Creator not found' }
|
||||||
|
|
||||||
const resolutionProbability =
|
const resolutionProbability =
|
||||||
|
@ -112,7 +121,7 @@ const sendResolutionEmails = async (
|
||||||
userPayouts: { [userId: string]: number },
|
userPayouts: { [userId: string]: number },
|
||||||
creator: User,
|
creator: User,
|
||||||
contract: Contract,
|
contract: Contract,
|
||||||
outcome: 'YES' | 'NO' | 'CANCEL' | 'MKT',
|
outcome: 'YES' | 'NO' | 'CANCEL' | 'MKT' | string,
|
||||||
resolutionProbability?: number
|
resolutionProbability?: number
|
||||||
) => {
|
) => {
|
||||||
const nonWinners = _.difference(
|
const nonWinners = _.difference(
|
||||||
|
|
|
@ -8,7 +8,7 @@ import { Answer } from '../../common/answer'
|
||||||
import { Contract } from '../../common/contract'
|
import { Contract } from '../../common/contract'
|
||||||
import { AmountInput } from './amount-input'
|
import { AmountInput } from './amount-input'
|
||||||
import { Col } from './layout/col'
|
import { Col } from './layout/col'
|
||||||
import { createAnswer, placeBet } from '../lib/firebase/api-call'
|
import { createAnswer, placeBet, resolveMarket } from '../lib/firebase/api-call'
|
||||||
import { Row } from './layout/row'
|
import { Row } from './layout/row'
|
||||||
import { Avatar } from './avatar'
|
import { Avatar } from './avatar'
|
||||||
import { SiteLink } from './site-link'
|
import { SiteLink } from './site-link'
|
||||||
|
@ -391,6 +391,27 @@ function AnswerResolvePanel(props: {
|
||||||
clearAnswerChoice,
|
clearAnswerChoice,
|
||||||
} = props
|
} = props
|
||||||
|
|
||||||
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||||
|
const [error, setError] = useState<string | undefined>(undefined)
|
||||||
|
|
||||||
|
const onResolve = async () => {
|
||||||
|
if (answer === undefined) return
|
||||||
|
|
||||||
|
setIsSubmitting(true)
|
||||||
|
|
||||||
|
const result = await resolveMarket({
|
||||||
|
outcome: answer,
|
||||||
|
contractId: contract.id,
|
||||||
|
}).then((r) => r.data as any)
|
||||||
|
|
||||||
|
console.log('resolved', `#${answer}`, 'result:', result)
|
||||||
|
|
||||||
|
if (result?.status !== 'success') {
|
||||||
|
setError(result?.error || 'Error resolving market')
|
||||||
|
}
|
||||||
|
setIsSubmitting(false)
|
||||||
|
}
|
||||||
|
|
||||||
const resolutionButtonClass =
|
const resolutionButtonClass =
|
||||||
resolveOption === 'CANCEL'
|
resolveOption === 'CANCEL'
|
||||||
? 'bg-yellow-400 hover:bg-yellow-500'
|
? 'bg-yellow-400 hover:bg-yellow-500'
|
||||||
|
@ -426,13 +447,15 @@ function AnswerResolvePanel(props: {
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
<ResolveConfirmationButton
|
<ResolveConfirmationButton
|
||||||
onResolve={() => {}}
|
onResolve={onResolve}
|
||||||
isSubmitting={false}
|
isSubmitting={isSubmitting}
|
||||||
openModelButtonClass={resolutionButtonClass}
|
openModelButtonClass={resolutionButtonClass}
|
||||||
submitButtonClass={resolutionButtonClass}
|
submitButtonClass={resolutionButtonClass}
|
||||||
/>
|
/>
|
||||||
</Row>
|
</Row>
|
||||||
</Col>
|
</Col>
|
||||||
|
|
||||||
|
{!!error && <div className="text-red-500">{error}</div>}
|
||||||
</Col>
|
</Col>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ export function ResolutionPanel(props: {
|
||||||
}) {
|
}) {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// warm up cloud function
|
// warm up cloud function
|
||||||
resolveMarket({}).catch()
|
resolveMarket({} as any).catch()
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
const { contract, className } = props
|
const { contract, className } = props
|
||||||
|
@ -35,6 +35,8 @@ export function ResolutionPanel(props: {
|
||||||
const [error, setError] = useState<string | undefined>(undefined)
|
const [error, setError] = useState<string | undefined>(undefined)
|
||||||
|
|
||||||
const resolve = async () => {
|
const resolve = async () => {
|
||||||
|
if (!outcome) return
|
||||||
|
|
||||||
setIsSubmitting(true)
|
setIsSubmitting(true)
|
||||||
|
|
||||||
const result = await resolveMarket({
|
const result = await resolveMarket({
|
||||||
|
|
|
@ -28,7 +28,14 @@ export const createAnswer = cloudFunction<
|
||||||
}
|
}
|
||||||
>('createAnswer')
|
>('createAnswer')
|
||||||
|
|
||||||
export const resolveMarket = cloudFunction('resolveMarket')
|
export const resolveMarket = cloudFunction<
|
||||||
|
{
|
||||||
|
outcome: string
|
||||||
|
contractId: string
|
||||||
|
probabilityInt?: number
|
||||||
|
},
|
||||||
|
{ status: 'error' | 'success'; message?: string }
|
||||||
|
>('resolveMarket')
|
||||||
|
|
||||||
export const sellBet = cloudFunction('sellBet')
|
export const sellBet = cloudFunction('sellBet')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user