From 3eb0bb1711e72ee012e85b576e5fb8c0247015f7 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Tue, 29 Mar 2022 21:30:04 -0500 Subject: [PATCH] Throw error if sell conditions don't hold --- common/calculate-cpmm.ts | 28 +++++++++++++++++++++++----- common/calculate.ts | 5 +++-- common/sell-bet.ts | 7 ++++--- web/components/amount-input.tsx | 9 +++++---- web/components/bet-panel.tsx | 9 +++++---- 5 files changed, 40 insertions(+), 18 deletions(-) diff --git a/common/calculate-cpmm.ts b/common/calculate-cpmm.ts index 44e195a2..7ecd87d7 100644 --- a/common/calculate-cpmm.ts +++ b/common/calculate-cpmm.ts @@ -159,13 +159,16 @@ function calculateCpmmShareValue( export function calculateCpmmSale( contract: FullContract, - bet: { shares: number; outcome: string } + shares: number, + outcome: string ) { - const { shares, outcome } = bet + if (shares < 0) { + throw new Error('Cannot sell non-positive shares') + } const rawSaleValue = calculateCpmmShareValue( contract, - Math.abs(shares), + shares, outcome as 'YES' | 'NO' ) @@ -185,6 +188,20 @@ export function calculateCpmmSale( ? [y + shares - saleValue + fee, n - saleValue + fee] : [y - saleValue + fee, n + shares - saleValue + fee] + if (newY < 0 || newN < 0) { + console.log('calculateCpmmSale', { + newY, + newN, + y, + n, + shares, + saleValue, + fee, + outcome, + }) + throw new Error('Cannot sell more than in pool') + } + const postBetPool = { YES: newY, NO: newN } const { newPool, newP } = addCpmmLiquidity(postBetPool, contract.p, fee) @@ -194,9 +211,10 @@ export function calculateCpmmSale( export function getCpmmProbabilityAfterSale( contract: FullContract, - bet: Bet + shares: number, + outcome: 'YES' | 'NO' ) { - const { newPool } = calculateCpmmSale(contract, bet) + const { newPool } = calculateCpmmSale(contract, shares, outcome) return getCpmmProbability(newPool, contract.p) } diff --git a/common/calculate.ts b/common/calculate.ts index 3bf57c2b..f3adf46f 100644 --- a/common/calculate.ts +++ b/common/calculate.ts @@ -72,7 +72,7 @@ export function calculateShares( export function calculateSaleAmount(contract: Contract, bet: Bet) { return contract.mechanism === 'cpmm-1' && contract.outcomeType === 'BINARY' - ? calculateCpmmSale(contract, bet).saleValue + ? calculateCpmmSale(contract, Math.abs(bet.shares), bet.outcome).saleValue : calculateDpmSaleAmount(contract, bet) } @@ -90,7 +90,8 @@ export function getProbabilityAfterSale( return contract.mechanism === 'cpmm-1' ? getCpmmProbabilityAfterSale( contract as FullContract, - { shares, outcome } as Bet + shares, + outcome as 'YES' | 'NO' ) : getDpmProbabilityAfterSale(contract.totalShares, outcome, shares) } diff --git a/common/sell-bet.ts b/common/sell-bet.ts index 750cfb39..1bdc1fd6 100644 --- a/common/sell-bet.ts +++ b/common/sell-bet.ts @@ -92,10 +92,11 @@ export const getCpmmSellBetInfo = ( ) => { const { pool, p } = contract - const { saleValue, newPool, newP, fees } = calculateCpmmSale(contract, { + const { saleValue, newPool, newP, fees } = calculateCpmmSale( + contract, shares, - outcome, - }) + outcome + ) const probBefore = getCpmmProbability(pool, p) const probAfter = getCpmmProbability(newPool, p) diff --git a/web/components/amount-input.tsx b/web/components/amount-input.tsx index e77802bc..f0a64a6b 100644 --- a/web/components/amount-input.tsx +++ b/web/components/amount-input.tsx @@ -212,10 +212,11 @@ export function SellAmountInput(props: { const prevLoanAmount = _.sumBy(openUserBets, (bet) => bet.loanAmount ?? 0) const sharesSold = Math.min(amount ?? 0, yesShares || noShares) - const { saleValue } = calculateCpmmSale(contract, { - shares: sharesSold, - outcome: sellOutcome as 'YES' | 'NO', - }) + const { saleValue } = calculateCpmmSale( + contract, + sharesSold, + sellOutcome as 'YES' | 'NO' + ) const loanRepaid = Math.min(prevLoanAmount, saleValue) diff --git a/web/components/bet-panel.tsx b/web/components/bet-panel.tsx index 8175a3ed..fc96fb53 100644 --- a/web/components/bet-panel.tsx +++ b/web/components/bet-panel.tsx @@ -441,10 +441,11 @@ function SellPanel(props: { } const initialProb = getProbability(contract) - const { newPool } = calculateCpmmSale(contract, { - shares: Math.min(amount ?? 0, shares), - outcome: sharesOutcome, - }) + const { newPool } = calculateCpmmSale( + contract, + Math.min(amount ?? 0, shares), + sharesOutcome + ) const resultProb = getCpmmProbability(newPool, contract.p) return (