return creator liquidity after resolution
This commit is contained in:
parent
c1ca1471a1
commit
e7e686d579
|
@ -1,4 +1,4 @@
|
||||||
import { sum, groupBy, mapValues, sumBy, partition } from 'lodash'
|
import { sum, groupBy, mapValues, sumBy } from 'lodash'
|
||||||
|
|
||||||
import { CPMMContract } from './contract'
|
import { CPMMContract } from './contract'
|
||||||
import { CREATOR_FEE, Fees, LIQUIDITY_FEE, PLATFORM_FEE } from './fees'
|
import { CREATOR_FEE, Fees, LIQUIDITY_FEE, PLATFORM_FEE } from './fees'
|
||||||
|
@ -268,18 +268,20 @@ const calculateLiquidityDelta = (p: number) => (l: LiquidityProvision) => {
|
||||||
|
|
||||||
export function getCpmmLiquidityPoolWeights(
|
export function getCpmmLiquidityPoolWeights(
|
||||||
contract: CPMMContract,
|
contract: CPMMContract,
|
||||||
liquidities: LiquidityProvision[]
|
liquidities: LiquidityProvision[],
|
||||||
|
excludeAntes: boolean
|
||||||
) {
|
) {
|
||||||
const [antes, nonAntes] = partition(liquidities, (l) => !!l.isAnte)
|
|
||||||
|
|
||||||
const calcLiqudity = calculateLiquidityDelta(contract.p)
|
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,
|
weight: s / shareSum,
|
||||||
providerId: nonAntes[i].userId,
|
providerId: liquidities[i].userId,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
const userWeights = groupBy(weights, (w) => w.providerId)
|
const userWeights = groupBy(weights, (w) => w.providerId)
|
||||||
|
@ -292,9 +294,14 @@ export function getCpmmLiquidityPoolWeights(
|
||||||
export function getUserLiquidityShares(
|
export function getUserLiquidityShares(
|
||||||
userId: string,
|
userId: string,
|
||||||
contract: CPMMContract,
|
contract: CPMMContract,
|
||||||
liquidities: LiquidityProvision[]
|
liquidities: LiquidityProvision[],
|
||||||
|
excludeAntes: boolean
|
||||||
) {
|
) {
|
||||||
const weights = getCpmmLiquidityPoolWeights(contract, liquidities)
|
const weights = getCpmmLiquidityPoolWeights(
|
||||||
|
contract,
|
||||||
|
liquidities,
|
||||||
|
excludeAntes
|
||||||
|
)
|
||||||
const userWeight = weights[userId] ?? 0
|
const userWeight = weights[userId] ?? 0
|
||||||
|
|
||||||
return mapValues(contract.pool, (shares) => userWeight * shares)
|
return mapValues(contract.pool, (shares) => userWeight * shares)
|
||||||
|
|
|
@ -72,7 +72,7 @@ export const getLiquidityPoolPayouts = (
|
||||||
const { pool } = contract
|
const { pool } = contract
|
||||||
const finalPool = pool[outcome]
|
const finalPool = pool[outcome]
|
||||||
|
|
||||||
const weights = getCpmmLiquidityPoolWeights(contract, liquidities)
|
const weights = getCpmmLiquidityPoolWeights(contract, liquidities, false)
|
||||||
|
|
||||||
return Object.entries(weights).map(([providerId, weight]) => ({
|
return Object.entries(weights).map(([providerId, weight]) => ({
|
||||||
userId: providerId,
|
userId: providerId,
|
||||||
|
@ -123,7 +123,7 @@ export const getLiquidityPoolProbPayouts = (
|
||||||
const { pool } = contract
|
const { pool } = contract
|
||||||
const finalPool = p * pool.YES + (1 - p) * pool.NO
|
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]) => ({
|
return Object.entries(weights).map(([providerId, weight]) => ({
|
||||||
userId: providerId,
|
userId: providerId,
|
||||||
|
|
|
@ -42,7 +42,12 @@ export const withdrawliquidity = newEndpoint({}, async (req, auth) => {
|
||||||
(doc) => doc.data() as LiquidityProvision
|
(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
|
// zero all added amounts for now
|
||||||
// can add support for partial withdrawals in the future
|
// can add support for partial withdrawals in the future
|
||||||
|
|
|
@ -21,6 +21,11 @@ export const useLiquidity = (contractId: string) => {
|
||||||
export const useUserLiquidity = (contract: CPMMContract, userId: string) => {
|
export const useUserLiquidity = (contract: CPMMContract, userId: string) => {
|
||||||
const liquidities = useLiquidity(contract.id)
|
const liquidities = useLiquidity(contract.id)
|
||||||
|
|
||||||
const userShares = getUserLiquidityShares(userId, contract, liquidities ?? [])
|
const userShares = getUserLiquidityShares(
|
||||||
|
userId,
|
||||||
|
contract,
|
||||||
|
liquidities ?? [],
|
||||||
|
true
|
||||||
|
)
|
||||||
return userShares
|
return userShares
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user