From f6d440989920d7212b4b8c900a061b256b7764d8 Mon Sep 17 00:00:00 2001 From: mantikoros Date: Sat, 30 Apr 2022 12:24:24 -0400 Subject: [PATCH] return liquidity pool after resolution according to true pool weight --- common/calculate-cpmm.ts | 32 ++++++++++++++++++++++++++++++++ common/payouts-fixed.ts | 21 +++++++++++---------- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/common/calculate-cpmm.ts b/common/calculate-cpmm.ts index 88caf0d0..d36d2f21 100644 --- a/common/calculate-cpmm.ts +++ b/common/calculate-cpmm.ts @@ -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, + 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, // liquidity: number diff --git a/common/payouts-fixed.ts b/common/payouts-fixed.ts index 3965c352..d06a8411 100644 --- a/common/payouts-fixed.ts +++ b/common/payouts-fixed.ts @@ -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, })) }