diff --git a/common/calculate-cpmm.ts b/common/calculate-cpmm.ts index 92d95251..8a609970 100644 --- a/common/calculate-cpmm.ts +++ b/common/calculate-cpmm.ts @@ -1,4 +1,4 @@ -import { sum, groupBy, mapValues, sumBy, partition } from 'lodash' +import { sum, groupBy, mapValues, sumBy } from 'lodash' import { CPMMContract } from './contract' import { CREATOR_FEE, Fees, LIQUIDITY_FEE, PLATFORM_FEE } from './fees' @@ -268,18 +268,20 @@ const calculateLiquidityDelta = (p: number) => (l: LiquidityProvision) => { export function getCpmmLiquidityPoolWeights( contract: CPMMContract, - liquidities: LiquidityProvision[] + liquidities: LiquidityProvision[], + excludeAntes: boolean ) { - const [antes, nonAntes] = partition(liquidities, (l) => !!l.isAnte) - const calcLiqudity = calculateLiquidityDelta(contract.p) - const liquidityShares = nonAntes.map(calcLiqudity) + const liquidityShares = liquidities.map(calcLiqudity) + const shareSum = sum(liquidityShares) - const shareSum = sum(liquidityShares) + sum(antes.map(calcLiqudity)) + const includedLiquidities = excludeAntes + ? liquidityShares.filter((_, i) => !liquidities[i].isAnte) + : liquidityShares - const weights = liquidityShares.map((s, i) => ({ + const weights = includedLiquidities.map((s, i) => ({ weight: s / shareSum, - providerId: nonAntes[i].userId, + providerId: liquidities[i].userId, })) const userWeights = groupBy(weights, (w) => w.providerId) @@ -292,9 +294,14 @@ export function getCpmmLiquidityPoolWeights( export function getUserLiquidityShares( userId: string, contract: CPMMContract, - liquidities: LiquidityProvision[] + liquidities: LiquidityProvision[], + excludeAntes: boolean ) { - const weights = getCpmmLiquidityPoolWeights(contract, liquidities) + const weights = getCpmmLiquidityPoolWeights( + contract, + liquidities, + excludeAntes + ) const userWeight = weights[userId] ?? 0 return mapValues(contract.pool, (shares) => userWeight * shares) diff --git a/common/payouts-fixed.ts b/common/payouts-fixed.ts index 4e06042b..4b8de85a 100644 --- a/common/payouts-fixed.ts +++ b/common/payouts-fixed.ts @@ -72,7 +72,7 @@ export const getLiquidityPoolPayouts = ( const { pool } = contract const finalPool = pool[outcome] - const weights = getCpmmLiquidityPoolWeights(contract, liquidities) + const weights = getCpmmLiquidityPoolWeights(contract, liquidities, false) return Object.entries(weights).map(([providerId, weight]) => ({ userId: providerId, @@ -123,7 +123,7 @@ export const getLiquidityPoolProbPayouts = ( const { pool } = contract const finalPool = p * pool.YES + (1 - p) * pool.NO - const weights = getCpmmLiquidityPoolWeights(contract, liquidities) + const weights = getCpmmLiquidityPoolWeights(contract, liquidities, false) return Object.entries(weights).map(([providerId, weight]) => ({ userId: providerId, diff --git a/functions/src/withdraw-liquidity.ts b/functions/src/withdraw-liquidity.ts index 1bdb19de..53974f7d 100644 --- a/functions/src/withdraw-liquidity.ts +++ b/functions/src/withdraw-liquidity.ts @@ -42,7 +42,12 @@ export const withdrawliquidity = newEndpoint({}, async (req, auth) => { (doc) => doc.data() as LiquidityProvision ) - const userShares = getUserLiquidityShares(auth.uid, contract, liquidities) + const userShares = getUserLiquidityShares( + auth.uid, + contract, + liquidities, + true + ) // zero all added amounts for now // can add support for partial withdrawals in the future diff --git a/web/hooks/use-liquidity.ts b/web/hooks/use-liquidity.ts index 9c610f3b..9c7c2b6f 100644 --- a/web/hooks/use-liquidity.ts +++ b/web/hooks/use-liquidity.ts @@ -21,6 +21,11 @@ export const useLiquidity = (contractId: string) => { export const useUserLiquidity = (contract: CPMMContract, userId: string) => { const liquidities = useLiquidity(contract.id) - const userShares = getUserLiquidityShares(userId, contract, liquidities ?? []) + const userShares = getUserLiquidityShares( + userId, + contract, + liquidities ?? [], + true + ) return userShares }