addCpmmLiquidity
This commit is contained in:
parent
062d4f88fd
commit
dce7639573
|
@ -12,6 +12,23 @@ export function getCpmmProbability(
|
||||||
return (p * NO) / ((1 - p) * YES + p * NO)
|
return (p * NO) / ((1 - p) * YES + p * NO)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getCpmmProbabilityAfterBetBeforeFees(
|
||||||
|
contract: FullContract<CPMM, Binary>,
|
||||||
|
outcome: string,
|
||||||
|
bet: number
|
||||||
|
) {
|
||||||
|
const { pool, p } = contract
|
||||||
|
const shares = calculateCpmmShares(pool, p, bet, outcome)
|
||||||
|
const { YES: y, NO: n } = pool
|
||||||
|
|
||||||
|
const [newY, newN] =
|
||||||
|
outcome === 'YES'
|
||||||
|
? [y - shares + bet, n + bet]
|
||||||
|
: [y + bet, n - shares + bet]
|
||||||
|
|
||||||
|
return getCpmmProbability({ YES: newY, NO: newN }, p)
|
||||||
|
}
|
||||||
|
|
||||||
export function getCpmmOutcomeProbabilityAfterBet(
|
export function getCpmmOutcomeProbabilityAfterBet(
|
||||||
contract: FullContract<CPMM, Binary>,
|
contract: FullContract<CPMM, Binary>,
|
||||||
outcome: string,
|
outcome: string,
|
||||||
|
@ -46,6 +63,7 @@ export function getCpmmLiquidityFee(
|
||||||
outcome: string
|
outcome: string
|
||||||
) {
|
) {
|
||||||
const p = getCpmmProbability(contract.pool, contract.p)
|
const p = getCpmmProbability(contract.pool, contract.p)
|
||||||
|
// const p = getCpmmProbabilityAfterBetBeforeFees(contract, outcome, bet)
|
||||||
const betP = outcome === 'YES' ? 1 - p : p
|
const betP = outcome === 'YES' ? 1 - p : p
|
||||||
|
|
||||||
const liquidityFee = LIQUIDITY_FEE * betP * bet
|
const liquidityFee = LIQUIDITY_FEE * betP * bet
|
||||||
|
@ -65,9 +83,9 @@ export function calculateCpmmSharesAfterFee(
|
||||||
outcome: string
|
outcome: string
|
||||||
) {
|
) {
|
||||||
const { pool, p } = contract
|
const { pool, p } = contract
|
||||||
// const { remainingBet } = getCpmmLiquidityFee(contract, bet, outcome)
|
const { remainingBet } = getCpmmLiquidityFee(contract, bet, outcome)
|
||||||
|
|
||||||
return calculateCpmmShares(pool, p, bet, outcome)
|
return calculateCpmmShares(pool, p, remainingBet, outcome)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function calculateCpmmPurchase(
|
export function calculateCpmmPurchase(
|
||||||
|
@ -90,10 +108,16 @@ export function calculateCpmmPurchase(
|
||||||
? [y - shares + remainingBet + fee, n + remainingBet + fee]
|
? [y - shares + remainingBet + fee, n + remainingBet + fee]
|
||||||
: [y + remainingBet + fee, n - shares + remainingBet + fee]
|
: [y + remainingBet + fee, n - shares + remainingBet + fee]
|
||||||
|
|
||||||
const newPool = { YES: newY, NO: newN }
|
const postBetPool = { YES: newY, NO: newN }
|
||||||
// console.log(getCpmmLiquidity(pool, p), getCpmmLiquidity(newPool, p))
|
|
||||||
|
|
||||||
return { shares, newPool, fees }
|
const { newPool, liquidity, newP } = addCpmmLiquidity(postBetPool, p, fee)
|
||||||
|
const prob = getCpmmProbability(postBetPool, p)
|
||||||
|
const newProb = getCpmmProbability(newPool, newP)
|
||||||
|
// console.log(shares)
|
||||||
|
// console.log(fee, liquidity, newP, newPool)
|
||||||
|
console.log(pool, postBetPool, shares)
|
||||||
|
|
||||||
|
return { shares, newPool, newP, fees }
|
||||||
}
|
}
|
||||||
|
|
||||||
export function calculateCpmmShareValue(
|
export function calculateCpmmShareValue(
|
||||||
|
@ -156,29 +180,27 @@ export function getCpmmLiquidity(
|
||||||
return YES ** (1 - p) * NO ** p
|
return YES ** (1 - p) * NO ** p
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: use new pricing equation
|
export function addCpmmLiquidity(
|
||||||
// export function addCpmmLiquidity(
|
pool: { [outcome: string]: number },
|
||||||
// contract: FullContract<CPMM, Binary>,
|
p: number,
|
||||||
// amount: number
|
amount: number
|
||||||
// ) {
|
) {
|
||||||
// const { YES, NO } = contract.pool
|
const prob = getCpmmProbability(pool, p)
|
||||||
// const p = getCpmmProbability({ YES, NO }, contract.p)
|
|
||||||
|
|
||||||
// const [newYes, newNo] =
|
//https://www.wolframalpha.com/input?i=p%28n%2Bb%29%2F%28%281-p%29%28y%2Bb%29%2Bp%28n%2Bb%29%29%3Dq%2C+solve+p
|
||||||
// p >= 0.5
|
const { YES: y, NO: n } = pool
|
||||||
// ? [amount * (1 / p - 1), amount]
|
const numerator = prob * (amount + y)
|
||||||
// : [amount, amount * (1 / (1 - p) - 1)]
|
const denominator = amount - n * (prob - 1) + prob * y
|
||||||
|
const newP = numerator / denominator
|
||||||
|
|
||||||
// const betAmount = Math.abs(newYes - newNo)
|
const newPool = { YES: y + amount, NO: n + amount }
|
||||||
// const betOutcome = p >= 0.5 ? 'YES' : 'NO'
|
|
||||||
|
|
||||||
// const poolLiquidity = getCpmmLiquidity({ YES, NO })
|
const oldLiquidity = getCpmmLiquidity(pool, newP)
|
||||||
// const newPool = { YES: YES + newYes, NO: NO + newNo }
|
const newLiquidity = getCpmmLiquidity(newPool, newP)
|
||||||
// const resultingLiquidity = getCpmmLiquidity(newPool)
|
const liquidity = newLiquidity - oldLiquidity
|
||||||
// const liquidity = resultingLiquidity - poolLiquidity
|
|
||||||
|
|
||||||
// return { newPool, liquidity, betAmount, betOutcome }
|
return { newPool, liquidity, newP }
|
||||||
// }
|
}
|
||||||
|
|
||||||
// export function removeCpmmLiquidity(
|
// export function removeCpmmLiquidity(
|
||||||
// contract: FullContract<CPMM, Binary>,
|
// contract: FullContract<CPMM, Binary>,
|
||||||
|
|
|
@ -26,7 +26,7 @@ export const getNewBinaryCpmmBetInfo = (
|
||||||
loanAmount: number,
|
loanAmount: number,
|
||||||
newBetId: string
|
newBetId: string
|
||||||
) => {
|
) => {
|
||||||
const { shares, newPool, fees } = calculateCpmmPurchase(
|
const { shares, newPool, newP, fees } = calculateCpmmPurchase(
|
||||||
contract,
|
contract,
|
||||||
amount,
|
amount,
|
||||||
outcome
|
outcome
|
||||||
|
@ -36,7 +36,7 @@ export const getNewBinaryCpmmBetInfo = (
|
||||||
|
|
||||||
const { pool, p } = contract
|
const { pool, p } = contract
|
||||||
const probBefore = getCpmmProbability(pool, p)
|
const probBefore = getCpmmProbability(pool, p)
|
||||||
const probAfter = getCpmmProbability(newPool, p)
|
const probAfter = getCpmmProbability(newPool, newP)
|
||||||
|
|
||||||
const newBet: Bet = {
|
const newBet: Bet = {
|
||||||
id: newBetId,
|
id: newBetId,
|
||||||
|
@ -52,7 +52,7 @@ export const getNewBinaryCpmmBetInfo = (
|
||||||
createdTime: Date.now(),
|
createdTime: Date.now(),
|
||||||
}
|
}
|
||||||
|
|
||||||
return { newBet, newPool, newBalance, fees }
|
return { newBet, newPool, newP, newBalance, fees }
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getNewBinaryDpmBetInfo = (
|
export const getNewBinaryDpmBetInfo = (
|
||||||
|
|
|
@ -81,6 +81,7 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
|
||||||
newTotalBets,
|
newTotalBets,
|
||||||
newBalance,
|
newBalance,
|
||||||
fees,
|
fees,
|
||||||
|
newP,
|
||||||
} =
|
} =
|
||||||
outcomeType === 'BINARY'
|
outcomeType === 'BINARY'
|
||||||
? mechanism === 'dpm-2'
|
? mechanism === 'dpm-2'
|
||||||
|
@ -115,6 +116,7 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
|
||||||
contractDoc,
|
contractDoc,
|
||||||
removeUndefinedProps({
|
removeUndefinedProps({
|
||||||
pool: newPool,
|
pool: newPool,
|
||||||
|
p: newP,
|
||||||
totalShares: newTotalShares,
|
totalShares: newTotalShares,
|
||||||
totalBets: newTotalBets,
|
totalBets: newTotalBets,
|
||||||
collectedFees: addObjects<Fees>(fees ?? {}, collectedFees ?? {}),
|
collectedFees: addObjects<Fees>(fees ?? {}, collectedFees ?? {}),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user