Some cleanup

This commit is contained in:
James Grugett 2022-07-03 18:17:40 -04:00
parent 3d4f993998
commit 71401c4482
2 changed files with 20 additions and 53 deletions

View File

@ -1,4 +1,4 @@
import { sumBy } from 'lodash'
import { sortBy, sumBy } from 'lodash'
import { Bet, LimitBet, MAX_LOAN_PER_CONTRACT, NumericBet } from './bet'
import {
@ -152,11 +152,18 @@ const computeFill = (
export const getBinaryCpmmBetInfo = (
outcome: 'YES' | 'NO',
betAmount: number,
contract: CPMMBinaryContract,
contract: CPMMBinaryContract | PseudoNumericContract,
limitProb: number | undefined,
unfilledBets: LimitBet[] // Sorted by limitProb, createdTime
unfilledBets: LimitBet[]
) => {
console.log({ outcome, betAmount, limitProb, unfilledBets })
const sortedBets = sortBy(
unfilledBets,
(bet) => (outcome === 'YES' ? bet.limitProb : -bet.limitProb),
(bet) => bet.createdTime
)
console.log({ outcome, betAmount, limitProb, sortedBets })
const takers: {
matchedBetId: string | null
amount: number
@ -170,19 +177,21 @@ export const getBinaryCpmmBetInfo = (
let i = 0
while (true) {
const matchedBet: LimitBet | undefined = unfilledBets[i]
const matchedBet: LimitBet | undefined = sortedBets[i]
const fill = computeFill(amount, outcome, limitProb, cpmmState, matchedBet)
if (!fill) break
const { maker, taker } = fill
const { taker, maker } = fill
amount -= taker.amount
if (maker.matchedBetId === null) {
// Matched against pool.
cpmmState = maker.state
totalFees = addObjects(totalFees, maker.fees)
takers.push(taker)
} else {
// Matched against bet.
takers.push(taker)
makers.push(maker)
i++
@ -228,40 +237,6 @@ export const getBinaryCpmmBetInfo = (
}
}
export const getNewBinaryCpmmBetInfo = (
outcome: 'YES' | 'NO',
amount: number,
contract: CPMMBinaryContract | PseudoNumericContract,
loanAmount: number
) => {
const { shares, newPool, newP, fees } = calculateCpmmPurchase(
contract,
amount,
outcome
)
const { pool, p, totalLiquidity } = contract
const probBefore = getCpmmProbability(pool, p)
const probAfter = getCpmmProbability(newPool, newP)
const newBet: CandidateBet<Bet> = {
contractId: contract.id,
amount,
shares,
outcome,
fees,
loanAmount,
probBefore,
probAfter,
createdTime: Date.now(),
}
const { liquidityFee } = fees
const newTotalLiquidity = (totalLiquidity ?? 0) + liquidityFee
return { newBet, newPool, newP, newTotalLiquidity }
}
export const getNewBinaryDpmBetInfo = (
outcome: 'YES' | 'NO',
amount: number,

View File

@ -7,7 +7,6 @@ import { User } from '../../common/user'
import {
BetInfo,
getBinaryCpmmBetInfo,
getNewBinaryDpmBetInfo,
getNewMultiBetInfo,
getNumericBetsInfo,
} from '../../common/new-bet'
@ -16,7 +15,8 @@ import { redeemShares } from './redeem-shares'
import { log } from './utils'
import { LimitBet } from 'common/bet'
import { Query } from 'firebase-admin/firestore'
import { sortBy, sumBy } from 'lodash'
import { sumBy } from 'lodash'
import { floatingEqual } from 'common/util/math'
const bodySchema = z.object({
contractId: z.string(),
@ -79,11 +79,8 @@ export const placebet = newEndpoint({}, async (req, auth) => {
> => {
if (
(outcomeType == 'BINARY' || outcomeType === 'PSEUDO_NUMERIC') &&
mechanism == 'dpm-2'
mechanism == 'cpmm-1'
) {
const { outcome } = validate(binarySchema, req.body)
return getNewBinaryDpmBetInfo(outcome, amount, contract, loanAmount)
} else if (outcomeType == 'BINARY' && mechanism == 'cpmm-1') {
const { outcome, limitProb } = validate(binarySchema, req.body)
const boundedLimitProb = limitProb ?? (outcome === 'YES' ? 1 : 0)
const unfilledBetsQuery = contractDoc
@ -98,11 +95,7 @@ export const placebet = newEndpoint({}, async (req, auth) => {
) as Query<LimitBet>
const unfilledBetsSnap = await trans.get(unfilledBetsQuery)
const unfilledBets = sortBy(
unfilledBetsSnap.docs.map((doc) => doc.data()),
(bet) => (outcome === 'YES' ? bet.limitProb : -bet.limitProb),
(bet) => bet.createdTime
)
const unfilledBets = unfilledBetsSnap.docs.map((doc) => doc.data())
return getBinaryCpmmBetInfo(
outcome,
@ -146,8 +139,7 @@ export const placebet = newEndpoint({}, async (req, auth) => {
const newFill = { amount, shares, matchedBetId: betDoc.id }
const fills = [...bet.fills, newFill]
const totalShares = sumBy(fills, 'shares')
const isFilled =
Math.abs(sumBy(fills, 'amount') - bet.amount) < 0.0000001
const isFilled = floatingEqual(sumBy(fills, 'amount'), bet.amount)
log('Updated a matched limit bet.')
trans.update(contractDoc.collection('bets').doc(bet.id), {