Throw error if sell conditions don't hold
This commit is contained in:
parent
ed5f69db7a
commit
3eb0bb1711
|
@ -159,13 +159,16 @@ function calculateCpmmShareValue(
|
|||
|
||||
export function calculateCpmmSale(
|
||||
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(
|
||||
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<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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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<CPMM, Binary>,
|
||||
{ shares, outcome } as Bet
|
||||
shares,
|
||||
outcome as 'YES' | 'NO'
|
||||
)
|
||||
: getDpmProbabilityAfterSale(contract.totalShares, outcome, shares)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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 (
|
||||
|
|
Loading…
Reference in New Issue
Block a user