2022-03-04 23:31:04 +00:00
|
|
|
import * as _ from 'lodash'
|
|
|
|
|
|
|
|
import { Bet } from './bet'
|
|
|
|
import { getProbability } from './calculate'
|
|
|
|
import { Binary, CPMM, FixedPayouts, FullContract } from './contract'
|
2022-03-07 17:29:58 +00:00
|
|
|
import { LiquidityProvision } from './liquidity-provision'
|
2022-03-04 23:31:04 +00:00
|
|
|
|
2022-03-07 17:29:58 +00:00
|
|
|
export const getFixedCancelPayouts = (
|
|
|
|
bets: Bet[],
|
|
|
|
liquidities: LiquidityProvision[]
|
|
|
|
) => {
|
|
|
|
const liquidityPayouts = liquidities.map((lp) => ({
|
|
|
|
userId: lp.userId,
|
|
|
|
payout: lp.amount,
|
2022-03-04 23:31:04 +00:00
|
|
|
}))
|
2022-03-07 17:29:58 +00:00
|
|
|
|
|
|
|
return bets
|
|
|
|
.filter((b) => !b.isAnte && !b.isLiquidityProvision)
|
|
|
|
.map((bet) => ({
|
|
|
|
userId: bet.userId,
|
|
|
|
payout: bet.amount,
|
|
|
|
}))
|
|
|
|
.concat(liquidityPayouts)
|
2022-03-04 23:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getStandardFixedPayouts = (
|
|
|
|
outcome: string,
|
|
|
|
contract: FullContract<FixedPayouts, Binary>,
|
2022-03-07 17:29:58 +00:00
|
|
|
bets: Bet[],
|
|
|
|
liquidities: LiquidityProvision[]
|
2022-03-04 23:31:04 +00:00
|
|
|
) => {
|
|
|
|
const winningBets = bets.filter((bet) => bet.outcome === outcome)
|
|
|
|
|
2022-03-10 20:55:23 +00:00
|
|
|
const payouts = winningBets.map(({ userId, shares }) => ({
|
|
|
|
userId,
|
|
|
|
payout: shares,
|
|
|
|
}))
|
2022-03-04 23:31:04 +00:00
|
|
|
|
2022-03-10 20:55:23 +00:00
|
|
|
const creatorPayout = contract.collectedFees.creatorFee
|
2022-03-04 23:31:04 +00:00
|
|
|
|
|
|
|
console.log(
|
|
|
|
'resolved',
|
|
|
|
outcome,
|
|
|
|
'pool',
|
2022-03-10 20:55:23 +00:00
|
|
|
contract.pool[outcome],
|
|
|
|
'payouts',
|
|
|
|
_.sum(payouts),
|
2022-03-04 23:31:04 +00:00
|
|
|
'creator fee',
|
|
|
|
creatorPayout
|
|
|
|
)
|
|
|
|
|
|
|
|
return payouts
|
|
|
|
.map(({ userId, payout }) => ({ userId, payout }))
|
|
|
|
.concat([{ userId: contract.creatorId, payout: creatorPayout }]) // add creator fee
|
2022-03-07 17:29:58 +00:00
|
|
|
.concat(getLiquidityPoolPayouts(contract, outcome, liquidities))
|
2022-03-04 23:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getLiquidityPoolPayouts = (
|
|
|
|
contract: FullContract<CPMM, Binary>,
|
2022-03-07 17:29:58 +00:00
|
|
|
outcome: string,
|
|
|
|
liquidities: LiquidityProvision[]
|
2022-03-04 23:31:04 +00:00
|
|
|
) => {
|
2022-03-07 17:29:58 +00:00
|
|
|
const providedLiquidity = _.sumBy(liquidities, (lp) => lp.liquidity)
|
|
|
|
|
|
|
|
const { pool } = contract
|
|
|
|
const finalPool = pool[outcome]
|
|
|
|
|
|
|
|
return liquidities.map((lp) => ({
|
|
|
|
userId: lp.userId,
|
|
|
|
payout: (lp.liquidity / providedLiquidity) * finalPool,
|
|
|
|
}))
|
2022-03-04 23:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getMktFixedPayouts = (
|
|
|
|
contract: FullContract<FixedPayouts, Binary>,
|
|
|
|
bets: Bet[],
|
2022-03-07 17:29:58 +00:00
|
|
|
liquidities: LiquidityProvision[],
|
2022-03-04 23:31:04 +00:00
|
|
|
resolutionProbability?: number
|
|
|
|
) => {
|
|
|
|
const p =
|
|
|
|
resolutionProbability === undefined
|
|
|
|
? getProbability(contract)
|
|
|
|
: resolutionProbability
|
|
|
|
|
2022-03-10 20:55:23 +00:00
|
|
|
const payouts = bets.map(({ userId, outcome, shares }) => {
|
2022-03-04 23:31:04 +00:00
|
|
|
const betP = outcome === 'YES' ? p : 1 - p
|
2022-03-10 20:55:23 +00:00
|
|
|
return { userId, payout: betP * shares }
|
2022-03-04 23:31:04 +00:00
|
|
|
})
|
|
|
|
|
2022-03-10 20:55:23 +00:00
|
|
|
const creatorPayout = contract.collectedFees.creatorFee
|
2022-03-04 23:31:04 +00:00
|
|
|
|
|
|
|
console.log(
|
2022-03-10 20:55:23 +00:00
|
|
|
'resolved PROB',
|
2022-03-04 23:31:04 +00:00
|
|
|
p,
|
|
|
|
'pool',
|
2022-03-10 20:55:23 +00:00
|
|
|
p * contract.pool.YES + (1 - p) * contract.pool.NO,
|
|
|
|
'payouts',
|
|
|
|
_.sum(payouts),
|
2022-03-04 23:31:04 +00:00
|
|
|
'creator fee',
|
|
|
|
creatorPayout
|
|
|
|
)
|
|
|
|
|
|
|
|
return payouts
|
|
|
|
.map(({ userId, payout }) => ({ userId, payout }))
|
|
|
|
.concat([{ userId: contract.creatorId, payout: creatorPayout }]) // add creator fee
|
2022-03-07 17:29:58 +00:00
|
|
|
.concat(getLiquidityPoolProbPayouts(contract, p, liquidities))
|
2022-03-04 23:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getLiquidityPoolProbPayouts = (
|
|
|
|
contract: FullContract<CPMM, Binary>,
|
2022-03-07 17:29:58 +00:00
|
|
|
p: number,
|
|
|
|
liquidities: LiquidityProvision[]
|
2022-03-04 23:31:04 +00:00
|
|
|
) => {
|
2022-03-07 17:29:58 +00:00
|
|
|
const providedLiquidity = _.sumBy(liquidities, (lp) => lp.liquidity)
|
|
|
|
|
|
|
|
const { pool } = contract
|
|
|
|
const finalPool = p * pool.YES + (1 - p) * pool.NO
|
|
|
|
|
|
|
|
return liquidities.map((lp) => ({
|
|
|
|
userId: lp.userId,
|
|
|
|
payout: (lp.liquidity / providedLiquidity) * finalPool,
|
|
|
|
}))
|
2022-03-04 23:31:04 +00:00
|
|
|
}
|