liquidity fee
This commit is contained in:
parent
dd9f4ba81c
commit
b05c4da0f2
|
@ -20,7 +20,8 @@ export function getCpmmOutcomeProbabilityAfterBet(
|
||||||
return outcome === 'NO' ? 1 - p : p
|
return outcome === 'NO' ? 1 - p : p
|
||||||
}
|
}
|
||||||
|
|
||||||
export function calculateCpmmShares(
|
// before liquidity fee
|
||||||
|
function calculateCpmmShares(
|
||||||
pool: {
|
pool: {
|
||||||
[outcome: string]: number
|
[outcome: string]: number
|
||||||
},
|
},
|
||||||
|
@ -28,21 +29,46 @@ export function calculateCpmmShares(
|
||||||
betChoice: string
|
betChoice: string
|
||||||
) {
|
) {
|
||||||
const { YES: y, NO: n } = pool
|
const { YES: y, NO: n } = pool
|
||||||
const k = y * n
|
const numerator = bet ** 2 + bet * (y + n)
|
||||||
const numerator = bet ** 2 + bet * (y + n) - k + y * n
|
|
||||||
const denominator = betChoice === 'YES' ? bet + n : bet + y
|
const denominator = betChoice === 'YES' ? bet + n : bet + y
|
||||||
const shares = numerator / denominator
|
const shares = numerator / denominator
|
||||||
return shares
|
return shares
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const CPMM_LIQUIDITY_FEE = 0 // 0.02
|
||||||
|
|
||||||
|
export function getCpmmLiquidityFee(
|
||||||
|
contract: FullContract<CPMM, Binary>,
|
||||||
|
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<CPMM, Binary>,
|
||||||
|
bet: number,
|
||||||
|
outcome: string
|
||||||
|
) {
|
||||||
|
const { pool } = contract
|
||||||
|
const { remainingBet } = getCpmmLiquidityFee(contract, bet, outcome)
|
||||||
|
|
||||||
|
return calculateCpmmShares(pool, remainingBet, outcome)
|
||||||
|
}
|
||||||
|
|
||||||
export function calculateCpmmPurchase(
|
export function calculateCpmmPurchase(
|
||||||
contract: FullContract<CPMM, Binary>,
|
contract: FullContract<CPMM, Binary>,
|
||||||
bet: number,
|
bet: number,
|
||||||
outcome: string
|
outcome: string
|
||||||
) {
|
) {
|
||||||
const { pool } = contract
|
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 { YES: y, NO: n } = pool
|
||||||
|
|
||||||
const [newY, newN] =
|
const [newY, newN] =
|
||||||
|
@ -75,15 +101,21 @@ export function calculateCpmmSale(
|
||||||
) {
|
) {
|
||||||
const { shares, outcome } = bet
|
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 { pool } = contract
|
||||||
const { YES: y, NO: n } = pool
|
const { YES: y, NO: n } = pool
|
||||||
|
|
||||||
const [newY, newN] =
|
const [newY, newN] =
|
||||||
outcome === 'YES'
|
outcome === 'YES'
|
||||||
? [y + shares - saleValue, n - saleValue]
|
? [y + shares - saleValue + fee, n - saleValue + fee]
|
||||||
: [y - saleValue, n + shares - saleValue]
|
: [y - saleValue + fee, n + shares - saleValue + fee]
|
||||||
|
|
||||||
const newPool = { YES: newY, NO: newN }
|
const newPool = { YES: newY, NO: newN }
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
import { Bet } from './bet'
|
import { Bet } from './bet'
|
||||||
import {
|
import {
|
||||||
calculateCpmmShares,
|
|
||||||
calculateCpmmShareValue,
|
calculateCpmmShareValue,
|
||||||
getCpmmProbability,
|
getCpmmProbability,
|
||||||
getCpmmOutcomeProbabilityAfterBet,
|
getCpmmOutcomeProbabilityAfterBet,
|
||||||
getCpmmProbabilityAfterSale,
|
getCpmmProbabilityAfterSale,
|
||||||
|
calculateCpmmSharesAfterFee,
|
||||||
} from './calculate-cpmm'
|
} from './calculate-cpmm'
|
||||||
import {
|
import {
|
||||||
calculateDpmPayout,
|
calculateDpmPayout,
|
||||||
|
@ -62,7 +62,11 @@ export function calculateShares(
|
||||||
betChoice: string
|
betChoice: string
|
||||||
) {
|
) {
|
||||||
return contract.mechanism === 'cpmm-1'
|
return contract.mechanism === 'cpmm-1'
|
||||||
? calculateCpmmShares(contract.pool, bet, betChoice)
|
? calculateCpmmSharesAfterFee(
|
||||||
|
contract as FullContract<CPMM, Binary>,
|
||||||
|
bet,
|
||||||
|
betChoice
|
||||||
|
)
|
||||||
: calculateDpmShares(contract.totalShares, bet, betChoice)
|
: calculateDpmShares(contract.totalShares, bet, betChoice)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user