simplify payouts

This commit is contained in:
mantikoros 2022-02-12 18:49:57 -06:00
parent cac289ae95
commit 51d5ff532c
2 changed files with 39 additions and 64 deletions

View File

@ -140,7 +140,8 @@ export function calculateStandardPayout(
const total = totalShares[outcome] - phantomShares[outcome] const total = totalShares[outcome] - phantomShares[outcome]
const winnings = (shares / total) * pool const winnings = (shares / total) * pool
return deductFees(amount, winnings) // profit can be negative if using phantom shares
return amount + (1 - FEES) * Math.max(0, winnings - amount)
} }
export function calculatePayoutAfterCorrectBet(contract: Contract, bet: Bet) { export function calculatePayoutAfterCorrectBet(contract: Contract, bet: Bet) {

View File

@ -29,37 +29,17 @@ export const getStandardPayouts = (
const pool = contract.pool.YES + contract.pool.NO const pool = contract.pool.YES + contract.pool.NO
const totalShares = _.sumBy(winningBets, (b) => b.shares) const totalShares = _.sumBy(winningBets, (b) => b.shares)
const winnerPayouts = winningBets.map((bet) => { const payouts = winningBets.map(({ userId, amount, shares }) => {
const winnings = (bet.shares / totalShares) * pool const winnings = (shares / totalShares) * pool
const profit = winnings - amount
return { // profit can be negative if using phantom shares
userId: bet.userId, const payout = amount + (1 - FEES) * Math.max(0, profit)
amount: bet.amount, return { userId, profit, payout }
profit: winnings - bet.amount,
}
}) })
const [profitable, unprofitable] = _.partition( const profits = _.sumBy(payouts, (po) => Math.max(0, po.profit))
winnerPayouts, const creatorPayout = CREATOR_FEE * profits
(wp) => wp.profit >= 0
)
const totalProfits = _.sumBy(profitable, (wp) => wp.profit)
const totalFees = FEES * totalProfits
const deficit = _.sumBy(unprofitable, (wp) => -wp.profit)
const adjTotalFees = deficit >= totalFees ? 0 : totalFees - deficit
const creatorPayout = Math.min(adjTotalFees, CREATOR_FEE * totalProfits)
const w = deficit > totalFees ? totalFees / deficit : 1
const adjUnprofitable = unprofitable.map((wp) => {
const adjustment = w * -wp.profit
return {
...wp,
profit: wp.profit + adjustment,
}
})
console.log( console.log(
'resolved', 'resolved',
@ -67,18 +47,13 @@ export const getStandardPayouts = (
'pool', 'pool',
pool, pool,
'profits', 'profits',
totalProfits, profits,
'deficit',
deficit,
'creator fee', 'creator fee',
creatorPayout creatorPayout
) )
return _.union(profitable, adjUnprofitable) return payouts
.map(({ userId, amount, profit }) => ({ .map(({ userId, payout }) => ({ userId, payout }))
userId,
payout: amount + (1 - FEES) * profit,
}))
.concat([{ userId: contract.creatorId, payout: creatorPayout }]) // add creator fee .concat([{ userId: contract.creatorId, payout: creatorPayout }]) // add creator fee
} }
@ -92,38 +67,37 @@ export const getMktPayouts = (
? getProbability(contract.totalShares) ? getProbability(contract.totalShares)
: resolutionProbability : resolutionProbability
const weightedShareTotal = _.sumBy(bets, (b) =>
b.outcome === 'YES' ? p * b.shares : (1 - p) * b.shares
)
const pool = contract.pool.YES + contract.pool.NO const pool = contract.pool.YES + contract.pool.NO
console.log('Resolved MKT at p=', p, 'pool: $M', pool)
const [yesBets, noBets] = _.partition(bets, (bet) => bet.outcome === 'YES') const payouts = bets.map(({ userId, outcome, amount, shares }) => {
const betP = outcome === 'YES' ? p : 1 - p
const winnings = ((betP * shares) / weightedShareTotal) * pool
const profit = winnings - amount
const payout = deductFees(amount, winnings)
return { userId, profit, payout }
})
const weightedShareTotal = const profits = _.sumBy(payouts, (po) => Math.max(0, po.profit))
p * _.sumBy(yesBets, (b) => b.shares) + const creatorPayout = CREATOR_FEE * profits
(1 - p) * _.sumBy(noBets, (b) => b.shares)
const yesPayouts = yesBets.map((bet) => ({ console.log(
userId: bet.userId, 'resolved MKT',
payout: deductFees( p,
bet.amount, 'pool',
((p * bet.shares) / weightedShareTotal) * pool pool,
), 'profits',
})) profits,
'creator fee',
creatorPayout
)
const noPayouts = noBets.map((bet) => ({ return payouts
userId: bet.userId, .map(({ userId, payout }) => ({ userId, payout }))
payout: deductFees( .concat([{ userId: contract.creatorId, payout: creatorPayout }]) // add creator fee
bet.amount,
(((1 - p) * bet.shares) / weightedShareTotal) * pool
),
}))
const creatorPayout = CREATOR_FEE * pool
return [
...yesPayouts,
...noPayouts,
{ userId: contract.creatorId, payout: creatorPayout },
]
} }
export const getPayouts = ( export const getPayouts = (