2022-05-22 08:36:05 +00:00
|
|
|
import { sum } from 'lodash'
|
2022-03-15 22:27:51 +00:00
|
|
|
|
|
|
|
import { Bet } from './bet'
|
|
|
|
import { getProbability } from './calculate'
|
2022-04-30 16:24:24 +00:00
|
|
|
import { getCpmmLiquidityPoolWeights } from './calculate-cpmm'
|
2022-03-15 22:27:51 +00:00
|
|
|
import { Binary, CPMM, FixedPayouts, FullContract } from './contract'
|
2022-05-09 21:04:40 +00:00
|
|
|
import { noFees } from './fees'
|
2022-03-15 22:27:51 +00:00
|
|
|
import { LiquidityProvision } from './liquidity-provision'
|
|
|
|
|
|
|
|
export const getFixedCancelPayouts = (
|
|
|
|
bets: Bet[],
|
|
|
|
liquidities: LiquidityProvision[]
|
|
|
|
) => {
|
|
|
|
const liquidityPayouts = liquidities.map((lp) => ({
|
|
|
|
userId: lp.userId,
|
|
|
|
payout: lp.amount,
|
|
|
|
}))
|
|
|
|
|
2022-05-09 21:04:40 +00:00
|
|
|
const payouts = bets
|
2022-03-15 22:27:51 +00:00
|
|
|
.filter((b) => !b.isAnte && !b.isLiquidityProvision)
|
|
|
|
.map((bet) => ({
|
|
|
|
userId: bet.userId,
|
|
|
|
payout: bet.amount,
|
|
|
|
}))
|
2022-05-09 21:04:40 +00:00
|
|
|
|
|
|
|
const creatorPayout = 0
|
|
|
|
|
|
|
|
return { payouts, creatorPayout, liquidityPayouts, collectedFees: noFees }
|
2022-03-15 22:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getStandardFixedPayouts = (
|
|
|
|
outcome: string,
|
|
|
|
contract: FullContract<FixedPayouts, Binary>,
|
|
|
|
bets: Bet[],
|
|
|
|
liquidities: LiquidityProvision[]
|
|
|
|
) => {
|
|
|
|
const winningBets = bets.filter((bet) => bet.outcome === outcome)
|
|
|
|
|
|
|
|
const payouts = winningBets.map(({ userId, shares }) => ({
|
|
|
|
userId,
|
|
|
|
payout: shares,
|
|
|
|
}))
|
|
|
|
|
2022-05-09 21:04:40 +00:00
|
|
|
const { collectedFees } = contract
|
|
|
|
const creatorPayout = collectedFees.creatorFee
|
2022-03-15 22:27:51 +00:00
|
|
|
|
|
|
|
console.log(
|
|
|
|
'resolved',
|
|
|
|
outcome,
|
|
|
|
'pool',
|
|
|
|
contract.pool[outcome],
|
|
|
|
'payouts',
|
2022-05-22 08:36:05 +00:00
|
|
|
sum(payouts),
|
2022-03-15 22:27:51 +00:00
|
|
|
'creator fee',
|
|
|
|
creatorPayout
|
|
|
|
)
|
|
|
|
|
2022-05-09 21:04:40 +00:00
|
|
|
const liquidityPayouts = getLiquidityPoolPayouts(
|
|
|
|
contract,
|
|
|
|
outcome,
|
|
|
|
liquidities
|
|
|
|
)
|
|
|
|
|
|
|
|
return { payouts, creatorPayout, liquidityPayouts, collectedFees }
|
2022-03-15 22:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getLiquidityPoolPayouts = (
|
|
|
|
contract: FullContract<CPMM, Binary>,
|
|
|
|
outcome: string,
|
|
|
|
liquidities: LiquidityProvision[]
|
|
|
|
) => {
|
|
|
|
const { pool } = contract
|
|
|
|
const finalPool = pool[outcome]
|
|
|
|
|
2022-04-30 16:24:24 +00:00
|
|
|
const weights = getCpmmLiquidityPoolWeights(contract, liquidities)
|
|
|
|
|
|
|
|
return Object.entries(weights).map(([providerId, weight]) => ({
|
|
|
|
userId: providerId,
|
|
|
|
payout: weight * finalPool,
|
2022-03-15 22:27:51 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getMktFixedPayouts = (
|
|
|
|
contract: FullContract<FixedPayouts, Binary>,
|
|
|
|
bets: Bet[],
|
|
|
|
liquidities: LiquidityProvision[],
|
|
|
|
resolutionProbability?: number
|
|
|
|
) => {
|
|
|
|
const p =
|
|
|
|
resolutionProbability === undefined
|
|
|
|
? getProbability(contract)
|
|
|
|
: resolutionProbability
|
|
|
|
|
|
|
|
const payouts = bets.map(({ userId, outcome, shares }) => {
|
|
|
|
const betP = outcome === 'YES' ? p : 1 - p
|
|
|
|
return { userId, payout: betP * shares }
|
|
|
|
})
|
|
|
|
|
2022-05-09 21:04:40 +00:00
|
|
|
const { collectedFees } = contract
|
|
|
|
const creatorPayout = collectedFees.creatorFee
|
2022-03-15 22:27:51 +00:00
|
|
|
|
|
|
|
console.log(
|
|
|
|
'resolved PROB',
|
|
|
|
p,
|
|
|
|
'pool',
|
|
|
|
p * contract.pool.YES + (1 - p) * contract.pool.NO,
|
|
|
|
'payouts',
|
2022-05-22 08:36:05 +00:00
|
|
|
sum(payouts),
|
2022-03-15 22:27:51 +00:00
|
|
|
'creator fee',
|
|
|
|
creatorPayout
|
|
|
|
)
|
|
|
|
|
2022-05-09 21:04:40 +00:00
|
|
|
const liquidityPayouts = getLiquidityPoolProbPayouts(contract, p, liquidities)
|
|
|
|
|
|
|
|
return { payouts, creatorPayout, liquidityPayouts, collectedFees }
|
2022-03-15 22:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getLiquidityPoolProbPayouts = (
|
|
|
|
contract: FullContract<CPMM, Binary>,
|
|
|
|
p: number,
|
|
|
|
liquidities: LiquidityProvision[]
|
|
|
|
) => {
|
|
|
|
const { pool } = contract
|
|
|
|
const finalPool = p * pool.YES + (1 - p) * pool.NO
|
|
|
|
|
2022-04-30 16:24:24 +00:00
|
|
|
const weights = getCpmmLiquidityPoolWeights(contract, liquidities)
|
|
|
|
|
|
|
|
return Object.entries(weights).map(([providerId, weight]) => ({
|
|
|
|
userId: providerId,
|
|
|
|
payout: weight * finalPool,
|
2022-03-15 22:27:51 +00:00
|
|
|
}))
|
|
|
|
}
|