Range bet executes both bets immediately.

This commit is contained in:
James Grugett 2022-07-18 19:06:19 -05:00
parent 16c11adc61
commit 30b95d75d9
2 changed files with 79 additions and 13 deletions

View File

@ -265,6 +265,53 @@ export const getBinaryBetStats = (
return { currentPayout, currentReturn, totalFees } return { currentPayout, currentReturn, totalFees }
} }
export const getBinaryRangeBetInfo = (
betAmount: number,
contract: CPMMBinaryContract | PseudoNumericContract,
lowLimitProb: number,
highLimitProb: number,
unfilledBets: LimitBet[]
) => {
const shares = Math.min(
betAmount / lowLimitProb,
betAmount / (1 - highLimitProb)
)
const yesAmount = shares * lowLimitProb
const noAmount = shares * (1 - highLimitProb)
const yesResult = getBinaryCpmmBetInfo(
'YES',
yesAmount,
contract,
lowLimitProb,
unfilledBets
)
const noResult = getBinaryCpmmBetInfo(
'NO',
noAmount,
{
...contract,
pool: yesResult.newPool,
p: yesResult.newP,
totalLiquidity: yesResult.newTotalLiquidity,
},
highLimitProb,
unfilledBets
)
const { newP, newPool, newTotalLiquidity } = noResult
const makers = [...yesResult.makers, ...noResult.makers]
return {
yesBet: yesResult.newBet,
noBet: noResult.newBet,
newP,
newPool,
newTotalLiquidity,
makers,
}
}
export const getNewBinaryDpmBetInfo = ( export const getNewBinaryDpmBetInfo = (
outcome: 'YES' | 'NO', outcome: 'YES' | 'NO',
amount: number, amount: number,

View File

@ -14,6 +14,7 @@ import { User } from '../../common/user'
import { import {
BetInfo, BetInfo,
getBinaryCpmmBetInfo, getBinaryCpmmBetInfo,
getBinaryRangeBetInfo,
getNewMultiBetInfo, getNewMultiBetInfo,
getNumericBetsInfo, getNumericBetsInfo,
} from '../../common/new-bet' } from '../../common/new-bet'
@ -28,10 +29,16 @@ const bodySchema = z.object({
amount: z.number().gte(1), amount: z.number().gte(1),
}) })
const binarySchema = z.object({ const binarySchema = z.union([
z.object({
outcome: z.enum(['YES', 'NO']), outcome: z.enum(['YES', 'NO']),
limitProb: z.number().gte(0.001).lte(0.999).optional(), limitProb: z.number().gte(0.001).lte(0.999).optional(),
}) }),
z.object({
lowLimitProb: z.number().gte(0.001).lte(0.999),
highLimitProb: z.number().gte(0.001).lte(0.999),
}),
])
const freeResponseSchema = z.object({ const freeResponseSchema = z.object({
outcome: z.string(), outcome: z.string(),
@ -82,12 +89,13 @@ export const placebet = newEndpoint({}, async (req, auth) => {
(outcomeType == 'BINARY' || outcomeType === 'PSEUDO_NUMERIC') && (outcomeType == 'BINARY' || outcomeType === 'PSEUDO_NUMERIC') &&
mechanism == 'cpmm-1' mechanism == 'cpmm-1'
) { ) {
const { outcome, limitProb } = validate(binarySchema, req.body)
const unfilledBetsSnap = await trans.get( const unfilledBetsSnap = await trans.get(
getUnfilledBetsQuery(contractDoc) getUnfilledBetsQuery(contractDoc)
) )
const unfilledBets = unfilledBetsSnap.docs.map((doc) => doc.data()) const unfilledBets = unfilledBetsSnap.docs.map((doc) => doc.data())
const data = validate(binarySchema, req.body)
if ('outcome' in data) {
const { outcome, limitProb } = data
return getBinaryCpmmBetInfo( return getBinaryCpmmBetInfo(
outcome, outcome,
@ -96,6 +104,17 @@ export const placebet = newEndpoint({}, async (req, auth) => {
limitProb, limitProb,
unfilledBets unfilledBets
) )
} else {
const { lowLimitProb, highLimitProb } = data
const result = getBinaryRangeBetInfo(
amount,
contract,
lowLimitProb,
highLimitProb,
unfilledBets
)
return { newBet: result.yesBet, ...result }
}
} else if (outcomeType == 'FREE_RESPONSE' && mechanism == 'dpm-2') { } else if (outcomeType == 'FREE_RESPONSE' && mechanism == 'dpm-2') {
const { outcome } = validate(freeResponseSchema, req.body) const { outcome } = validate(freeResponseSchema, req.body)
const answerDoc = contractDoc.collection('answers').doc(outcome) const answerDoc = contractDoc.collection('answers').doc(outcome)