return liquidity pool after resolution according to true pool weight

This commit is contained in:
mantikoros 2022-04-30 12:24:24 -04:00
parent ccd0e42734
commit f6d4409899
2 changed files with 43 additions and 10 deletions

View File

@ -2,6 +2,8 @@ import * as _ from 'lodash'
import { Binary, CPMM, FullContract } from './contract'
import { CREATOR_FEE, Fees, LIQUIDITY_FEE, noFees, PLATFORM_FEE } from './fees'
import { LiquidityProvision } from './liquidity-provision'
import { addObjects } from './util/object'
export function getCpmmProbability(
pool: { [outcome: string]: number },
@ -258,6 +260,36 @@ export function addCpmmLiquidity(
return { newPool, liquidity, newP }
}
export function getCpmmLiquidityPoolWeights(
contract: FullContract<CPMM, Binary>,
liquidities: LiquidityProvision[]
) {
const { p } = contract
const liquidityShares = liquidities.map((l) => {
const oldLiquidity = getCpmmLiquidity(l.pool, p)
const newPool = addObjects(l.pool, { YES: l.amount, NO: l.amount })
const newLiquidity = getCpmmLiquidity(newPool, p)
const liquidity = newLiquidity - oldLiquidity
return liquidity
})
const shareSum = _.sum(liquidityShares)
const weights = liquidityShares.map((s, i) => ({
weight: s / shareSum,
providerId: liquidities[i].userId,
}))
const userWeights = _.groupBy(weights, (w) => w.providerId)
const totalUserWeights = _.mapValues(userWeights, (userWeight) =>
_.sumBy(userWeight, (w) => w.weight)
)
return totalUserWeights
}
// export function removeCpmmLiquidity(
// contract: FullContract<CPMM, Binary>,
// liquidity: number

View File

@ -2,6 +2,7 @@ import * as _ from 'lodash'
import { Bet } from './bet'
import { getProbability } from './calculate'
import { getCpmmLiquidityPoolWeights } from './calculate-cpmm'
import { Binary, CPMM, FixedPayouts, FullContract } from './contract'
import { LiquidityProvision } from './liquidity-provision'
@ -60,14 +61,14 @@ export const getLiquidityPoolPayouts = (
outcome: string,
liquidities: LiquidityProvision[]
) => {
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,
const weights = getCpmmLiquidityPoolWeights(contract, liquidities)
return Object.entries(weights).map(([providerId, weight]) => ({
userId: providerId,
payout: weight * finalPool,
}))
}
@ -111,13 +112,13 @@ export const getLiquidityPoolProbPayouts = (
p: number,
liquidities: LiquidityProvision[]
) => {
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,
const weights = getCpmmLiquidityPoolWeights(contract, liquidities)
return Object.entries(weights).map(([providerId, weight]) => ({
userId: providerId,
payout: weight * finalPool,
}))
}