different approach to resolve MKT

This commit is contained in:
mantikoros 2022-01-01 23:50:48 -06:00
parent 19c4e2d820
commit 9d9e0f2a4d
2 changed files with 81 additions and 5 deletions

View File

@ -155,7 +155,6 @@ const getStandardPayouts = (
const shareDifferenceSum = _.sumBy(winningBets, (b) => b.shares - b.amount)
const winningsPool = truePool - betSum
const fees = PLATFORM_FEE + CREATOR_FEE
const winnerPayouts = winningBets.map((bet) => ({
userId: bet.userId,
@ -173,11 +172,53 @@ const getStandardPayouts = (
const getMktPayouts = (truePool: number, contract: Contract, bets: Bet[]) => {
const p =
contract.pool.YES ** 2 / (contract.pool.YES ** 2 + contract.pool.NO ** 2)
console.log('Resolved MKT at p=', p)
console.log('Resolved MKT at p=', p, 'pool: $M', truePool)
const [yesBets, noBets] = _.partition(bets, (bet) => bet.outcome === 'YES')
const weightedBetTotal =
p * _.sumBy(yesBets, (b) => b.amount) +
(1 - p) * _.sumBy(noBets, (b) => b.amount)
if (weightedBetTotal >= truePool) {
return bets.map((bet) => ({
userId: bet.userId,
payout:
(((bet.outcome === 'YES' ? p : 1 - p) * bet.amount) /
weightedBetTotal) *
truePool,
}))
}
const winningsPool = truePool - weightedBetTotal
const weightedShareTotal =
p * _.sumBy(yesBets, (b) => b.shares - b.amount) +
(1 - p) * _.sumBy(noBets, (b) => b.shares - b.amount)
const yesPayouts = yesBets.map((bet) => ({
userId: bet.userId,
payout:
(1 - fees) *
(p * bet.amount +
((p * (bet.shares - bet.amount)) / weightedShareTotal) * winningsPool),
}))
const noPayouts = noBets.map((bet) => ({
userId: bet.userId,
payout:
(1 - fees) *
((1 - p) * bet.amount +
(((1 - p) * (bet.shares - bet.amount)) / weightedShareTotal) *
winningsPool),
}))
const creatorPayout = CREATOR_FEE * truePool
return [
...getStandardPayouts('YES', p * truePool, contract, bets),
...getStandardPayouts('NO', (1 - p) * truePool, contract, bets),
...yesPayouts,
...noPayouts,
{ userId: contract.creatorId, payout: creatorPayout },
]
}
@ -192,3 +233,5 @@ const payUser = ([userId, payout]: [string, number]) => {
transaction.update(userDoc, { balance: newUserBalance })
})
}
const fees = PLATFORM_FEE + CREATOR_FEE

View File

@ -41,10 +41,15 @@ export function calculatePayout(
) {
const { amount, outcome: betOutcome, shares } = bet
if (outcome === 'CANCEL' || outcome === 'MKT') return amount
if (outcome === 'CANCEL') return amount
if (outcome === 'MKT') return calculateMktPayout(contract, bet)
if (betOutcome !== outcome) return 0
const { totalShares, totalBets } = contract
if (totalShares[outcome] === 0) return 0
const startPool = contract.startPool.YES + contract.startPool.NO
const truePool = contract.pool.YES + contract.pool.NO - startPool
@ -57,6 +62,34 @@ export function calculatePayout(
return (1 - fees) * (amount + ((shares - amount) / total) * winningsPool)
}
function calculateMktPayout(contract: Contract, bet: Bet) {
const p =
contract.pool.YES ** 2 / (contract.pool.YES ** 2 + contract.pool.NO ** 2)
const weightedTotal =
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 - startPool
const betP = bet.outcome === 'YES' ? p : 1 - p
if (weightedTotal >= truePool) {
return ((betP * bet.amount) / weightedTotal) * truePool
}
const winningsPool = truePool - weightedTotal
const weightedShareTotal =
p * (contract.totalShares.YES - contract.totalBets.YES) +
(1 - p) * (contract.totalShares.NO - contract.totalBets.NO)
return (
(1 - fees) *
(betP * bet.amount +
((betP * (bet.shares - bet.amount)) / weightedShareTotal) * winningsPool)
)
}
export function resolvedPayout(contract: Contract, bet: Bet) {
if (contract.resolution)
return calculatePayout(contract, bet, contract.resolution)