From b05c4da0f20f1c826452372923fcca454ec7b5c5 Mon Sep 17 00:00:00 2001 From: mantikoros Date: Mon, 7 Mar 2022 17:49:55 -0600 Subject: [PATCH] liquidity fee --- common/calculate-cpmm.ts | 46 ++++++++++++++++++++++++++++++++++------ common/calculate.ts | 8 +++++-- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/common/calculate-cpmm.ts b/common/calculate-cpmm.ts index 1c4f38f7..87c03143 100644 --- a/common/calculate-cpmm.ts +++ b/common/calculate-cpmm.ts @@ -20,7 +20,8 @@ export function getCpmmOutcomeProbabilityAfterBet( return outcome === 'NO' ? 1 - p : p } -export function calculateCpmmShares( +// before liquidity fee +function calculateCpmmShares( pool: { [outcome: string]: number }, @@ -28,21 +29,46 @@ export function calculateCpmmShares( betChoice: string ) { const { YES: y, NO: n } = pool - const k = y * n - const numerator = bet ** 2 + bet * (y + n) - k + y * n + const numerator = bet ** 2 + bet * (y + n) const denominator = betChoice === 'YES' ? bet + n : bet + y const shares = numerator / denominator return shares } +export const CPMM_LIQUIDITY_FEE = 0 // 0.02 + +export function getCpmmLiquidityFee( + contract: FullContract, + bet: number, + outcome: string +) { + const p = getCpmmProbability(contract.pool) + const betP = outcome === 'YES' ? 1 - p : p + const fee = CPMM_LIQUIDITY_FEE * betP * bet + const remainingBet = bet - fee + return { fee, remainingBet } +} + +export function calculateCpmmSharesAfterFee( + contract: FullContract, + bet: number, + outcome: string +) { + const { pool } = contract + const { remainingBet } = getCpmmLiquidityFee(contract, bet, outcome) + + return calculateCpmmShares(pool, remainingBet, outcome) +} + export function calculateCpmmPurchase( contract: FullContract, bet: number, outcome: string ) { const { pool } = contract + const { remainingBet } = getCpmmLiquidityFee(contract, bet, outcome) - const shares = calculateCpmmShares(pool, bet, outcome) + const shares = calculateCpmmShares(pool, remainingBet, outcome) const { YES: y, NO: n } = pool const [newY, newN] = @@ -75,15 +101,21 @@ export function calculateCpmmSale( ) { const { shares, outcome } = bet - const saleValue = calculateCpmmShareValue(contract, shares, outcome) + const rawSaleValue = calculateCpmmShareValue(contract, shares, outcome) + + const { fee, remainingBet: saleValue } = getCpmmLiquidityFee( + contract, + rawSaleValue, + outcome === 'YES' ? 'NO' : 'YES' + ) const { pool } = contract const { YES: y, NO: n } = pool const [newY, newN] = outcome === 'YES' - ? [y + shares - saleValue, n - saleValue] - : [y - saleValue, n + shares - saleValue] + ? [y + shares - saleValue + fee, n - saleValue + fee] + : [y - saleValue + fee, n + shares - saleValue + fee] const newPool = { YES: newY, NO: newN } diff --git a/common/calculate.ts b/common/calculate.ts index cf5174f0..21583065 100644 --- a/common/calculate.ts +++ b/common/calculate.ts @@ -1,10 +1,10 @@ import { Bet } from './bet' import { - calculateCpmmShares, calculateCpmmShareValue, getCpmmProbability, getCpmmOutcomeProbabilityAfterBet, getCpmmProbabilityAfterSale, + calculateCpmmSharesAfterFee, } from './calculate-cpmm' import { calculateDpmPayout, @@ -62,7 +62,11 @@ export function calculateShares( betChoice: string ) { return contract.mechanism === 'cpmm-1' - ? calculateCpmmShares(contract.pool, bet, betChoice) + ? calculateCpmmSharesAfterFee( + contract as FullContract, + bet, + betChoice + ) : calculateDpmShares(contract.totalShares, bet, betChoice) }