return liquidity pool after resolution according to true pool weight
This commit is contained in:
parent
ccd0e42734
commit
f6d4409899
|
@ -2,6 +2,8 @@ import * as _ from 'lodash'
|
||||||
|
|
||||||
import { Binary, CPMM, FullContract } from './contract'
|
import { Binary, CPMM, FullContract } from './contract'
|
||||||
import { CREATOR_FEE, Fees, LIQUIDITY_FEE, noFees, PLATFORM_FEE } from './fees'
|
import { CREATOR_FEE, Fees, LIQUIDITY_FEE, noFees, PLATFORM_FEE } from './fees'
|
||||||
|
import { LiquidityProvision } from './liquidity-provision'
|
||||||
|
import { addObjects } from './util/object'
|
||||||
|
|
||||||
export function getCpmmProbability(
|
export function getCpmmProbability(
|
||||||
pool: { [outcome: string]: number },
|
pool: { [outcome: string]: number },
|
||||||
|
@ -258,6 +260,36 @@ export function addCpmmLiquidity(
|
||||||
return { newPool, liquidity, newP }
|
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(
|
// export function removeCpmmLiquidity(
|
||||||
// contract: FullContract<CPMM, Binary>,
|
// contract: FullContract<CPMM, Binary>,
|
||||||
// liquidity: number
|
// liquidity: number
|
||||||
|
|
|
@ -2,6 +2,7 @@ import * as _ from 'lodash'
|
||||||
|
|
||||||
import { Bet } from './bet'
|
import { Bet } from './bet'
|
||||||
import { getProbability } from './calculate'
|
import { getProbability } from './calculate'
|
||||||
|
import { getCpmmLiquidityPoolWeights } from './calculate-cpmm'
|
||||||
import { Binary, CPMM, FixedPayouts, FullContract } from './contract'
|
import { Binary, CPMM, FixedPayouts, FullContract } from './contract'
|
||||||
import { LiquidityProvision } from './liquidity-provision'
|
import { LiquidityProvision } from './liquidity-provision'
|
||||||
|
|
||||||
|
@ -60,14 +61,14 @@ export const getLiquidityPoolPayouts = (
|
||||||
outcome: string,
|
outcome: string,
|
||||||
liquidities: LiquidityProvision[]
|
liquidities: LiquidityProvision[]
|
||||||
) => {
|
) => {
|
||||||
const providedLiquidity = _.sumBy(liquidities, (lp) => lp.liquidity)
|
|
||||||
|
|
||||||
const { pool } = contract
|
const { pool } = contract
|
||||||
const finalPool = pool[outcome]
|
const finalPool = pool[outcome]
|
||||||
|
|
||||||
return liquidities.map((lp) => ({
|
const weights = getCpmmLiquidityPoolWeights(contract, liquidities)
|
||||||
userId: lp.userId,
|
|
||||||
payout: (lp.liquidity / providedLiquidity) * finalPool,
|
return Object.entries(weights).map(([providerId, weight]) => ({
|
||||||
|
userId: providerId,
|
||||||
|
payout: weight * finalPool,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,13 +112,13 @@ export const getLiquidityPoolProbPayouts = (
|
||||||
p: number,
|
p: number,
|
||||||
liquidities: LiquidityProvision[]
|
liquidities: LiquidityProvision[]
|
||||||
) => {
|
) => {
|
||||||
const providedLiquidity = _.sumBy(liquidities, (lp) => lp.liquidity)
|
|
||||||
|
|
||||||
const { pool } = contract
|
const { pool } = contract
|
||||||
const finalPool = p * pool.YES + (1 - p) * pool.NO
|
const finalPool = p * pool.YES + (1 - p) * pool.NO
|
||||||
|
|
||||||
return liquidities.map((lp) => ({
|
const weights = getCpmmLiquidityPoolWeights(contract, liquidities)
|
||||||
userId: lp.userId,
|
|
||||||
payout: (lp.liquidity / providedLiquidity) * finalPool,
|
return Object.entries(weights).map(([providerId, weight]) => ({
|
||||||
|
userId: providerId,
|
||||||
|
payout: weight * finalPool,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user