return creator liquidity after resolution

This commit is contained in:
mantikoros 2022-07-09 13:53:50 -04:00
parent c1ca1471a1
commit e7e686d579
4 changed files with 31 additions and 14 deletions

View File

@ -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)

View File

@ -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,

View File

@ -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

View File

@ -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
}