Throw error if sell conditions don't hold

This commit is contained in:
James Grugett 2022-03-29 21:30:04 -05:00
parent ed5f69db7a
commit 3eb0bb1711
5 changed files with 40 additions and 18 deletions

View File

@ -159,13 +159,16 @@ function calculateCpmmShareValue(
export function calculateCpmmSale( export function calculateCpmmSale(
contract: FullContract<CPMM, Binary>, contract: FullContract<CPMM, Binary>,
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( const rawSaleValue = calculateCpmmShareValue(
contract, contract,
Math.abs(shares), shares,
outcome as 'YES' | 'NO' outcome as 'YES' | 'NO'
) )
@ -185,6 +188,20 @@ export function calculateCpmmSale(
? [y + shares - saleValue + fee, n - saleValue + fee] ? [y + shares - saleValue + fee, n - saleValue + fee]
: [y - saleValue + fee, n + shares - 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 postBetPool = { YES: newY, NO: newN }
const { newPool, newP } = addCpmmLiquidity(postBetPool, contract.p, fee) const { newPool, newP } = addCpmmLiquidity(postBetPool, contract.p, fee)
@ -194,9 +211,10 @@ export function calculateCpmmSale(
export function getCpmmProbabilityAfterSale( export function getCpmmProbabilityAfterSale(
contract: FullContract<CPMM, Binary>, contract: FullContract<CPMM, Binary>,
bet: Bet shares: number,
outcome: 'YES' | 'NO'
) { ) {
const { newPool } = calculateCpmmSale(contract, bet) const { newPool } = calculateCpmmSale(contract, shares, outcome)
return getCpmmProbability(newPool, contract.p) return getCpmmProbability(newPool, contract.p)
} }

View File

@ -72,7 +72,7 @@ export function calculateShares(
export function calculateSaleAmount(contract: Contract, bet: Bet) { export function calculateSaleAmount(contract: Contract, bet: Bet) {
return contract.mechanism === 'cpmm-1' && contract.outcomeType === 'BINARY' return contract.mechanism === 'cpmm-1' && contract.outcomeType === 'BINARY'
? calculateCpmmSale(contract, bet).saleValue ? calculateCpmmSale(contract, Math.abs(bet.shares), bet.outcome).saleValue
: calculateDpmSaleAmount(contract, bet) : calculateDpmSaleAmount(contract, bet)
} }
@ -90,7 +90,8 @@ export function getProbabilityAfterSale(
return contract.mechanism === 'cpmm-1' return contract.mechanism === 'cpmm-1'
? getCpmmProbabilityAfterSale( ? getCpmmProbabilityAfterSale(
contract as FullContract<CPMM, Binary>, contract as FullContract<CPMM, Binary>,
{ shares, outcome } as Bet shares,
outcome as 'YES' | 'NO'
) )
: getDpmProbabilityAfterSale(contract.totalShares, outcome, shares) : getDpmProbabilityAfterSale(contract.totalShares, outcome, shares)
} }

View File

@ -92,10 +92,11 @@ export const getCpmmSellBetInfo = (
) => { ) => {
const { pool, p } = contract const { pool, p } = contract
const { saleValue, newPool, newP, fees } = calculateCpmmSale(contract, { const { saleValue, newPool, newP, fees } = calculateCpmmSale(
contract,
shares, shares,
outcome, outcome
}) )
const probBefore = getCpmmProbability(pool, p) const probBefore = getCpmmProbability(pool, p)
const probAfter = getCpmmProbability(newPool, p) const probAfter = getCpmmProbability(newPool, p)

View File

@ -212,10 +212,11 @@ export function SellAmountInput(props: {
const prevLoanAmount = _.sumBy(openUserBets, (bet) => bet.loanAmount ?? 0) const prevLoanAmount = _.sumBy(openUserBets, (bet) => bet.loanAmount ?? 0)
const sharesSold = Math.min(amount ?? 0, yesShares || noShares) const sharesSold = Math.min(amount ?? 0, yesShares || noShares)
const { saleValue } = calculateCpmmSale(contract, { const { saleValue } = calculateCpmmSale(
shares: sharesSold, contract,
outcome: sellOutcome as 'YES' | 'NO', sharesSold,
}) sellOutcome as 'YES' | 'NO'
)
const loanRepaid = Math.min(prevLoanAmount, saleValue) const loanRepaid = Math.min(prevLoanAmount, saleValue)

View File

@ -441,10 +441,11 @@ function SellPanel(props: {
} }
const initialProb = getProbability(contract) const initialProb = getProbability(contract)
const { newPool } = calculateCpmmSale(contract, { const { newPool } = calculateCpmmSale(
shares: Math.min(amount ?? 0, shares), contract,
outcome: sharesOutcome, Math.min(amount ?? 0, shares),
}) sharesOutcome
)
const resultProb = getCpmmProbability(newPool, contract.p) const resultProb = getCpmmProbability(newPool, contract.p)
return ( return (